| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c3a7ed8 commit 440808f
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,6 +73,40 @@ export var test_ArgbInt_Color = function () { | |||
| 73 | 73 | TKUnit.assert(color.argb === _parseArgb(100, 100, 100, 100), "Color.argb not properly parsed"); | |
| 74 | 74 | } | |
| 75 | 75 | ||
| 76 | + export var test_rgb_Color_CSS = function () { | ||
| 77 | + // <snippet module="color" title="color"> | ||
| 78 | + // ### Creating a Color from four RGB values | ||
| 79 | + // ``` JavaScript | ||
| 80 | + //// Creates the color with 255 red, 100 green, 100 blue | ||
| 81 | + var color = new Color("rgb(255, 100, 100)"); | ||
| 82 | + // ``` | ||
| 83 | + // </snippet> | ||
| 84 | + TKUnit.assert(color.a === 255, "Color.a not properly parsed"); | ||
| 85 | + TKUnit.assert(color.r === 255, "Color.r not properly parsed"); | ||
| 86 | + TKUnit.assert(color.g === 100, "Color.g not properly parsed"); | ||
| 87 | + TKUnit.assert(color.b === 100, "Color.b not properly parsed"); | ||
| 88 | + TKUnit.assert(color.hex === _buildHex(255, 255, 100, 100), "Color.hex not properly parsed"); | ||
| 89 | + TKUnit.assert(color.argb === _parseArgb(255, 255, 100, 100), "Color.argb not properly parsed"); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + export var test_rgba_Color_CSS = function () { | ||
| 93 | + var alpha = 0.5; | ||
| 94 | + var expected = Math.round(alpha * 255); | ||
| 95 | + // <snippet module="color" title="color"> | ||
| 96 | + // ### Creating a Color from four RGB values | ||
| 97 | + // ``` JavaScript | ||
| 98 | + //// Creates the color with 255 red, 100 green, 100 blue and 0 alpha | ||
| 99 | + var color = new Color(`rgba(255, 100, 100, ${alpha})`); | ||
| 100 | + // ``` | ||
| 101 | + // </snippet> | ||
| 102 | + TKUnit.assert(color.a === expected, "Color.a not properly parsed"); | ||
| 103 | + TKUnit.assert(color.r === 255, "Color.r not properly parsed"); | ||
| 104 | + TKUnit.assert(color.g === 100, "Color.g not properly parsed"); | ||
| 105 | + TKUnit.assert(color.b === 100, "Color.b not properly parsed"); | ||
| 106 | + TKUnit.assert(color.hex === _buildHex(expected, 255, 100, 100), "Color.hex not properly parsed"); | ||
| 107 | + TKUnit.assert(color.argb === _parseArgb(expected, 255, 100, 100), "Color.argb not properly parsed"); | ||
| 108 | + } | ||
| 109 | + | ||
| 76 | 110 | var _buildHex = function (a: number, r: number, g: number, b: number): string { | |
| 77 | 111 | return "#" + _componentToHex(a) + _componentToHex(r) + _componentToHex(g) + _componentToHex(b); | |
| 78 | 112 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,15 +18,18 @@ export class Color implements definition.Color { | |||
| 18 | 18 | if (arguments.length === 1) { | |
| 19 | 19 | var arg = arguments[0]; | |
| 20 | 20 | if (types.isString(arg)) { | |
| 21 | - if (knownColors.isKnownName(arg)) { | ||
| 21 | + if(isRgbOrRgba(arg)){ | ||
| 22 | + this._argb = argbFromRgbOrRgba(arg); | ||
| 23 | + } else if (knownColors.isKnownName(arg)) { | ||
| 22 | 24 | // The parameter is a known color name | |
| 23 | 25 | this._hex = knownColors.getKnownColor(arg); | |
| 24 | 26 | this._name = arg; | |
| 27 | + this._argb = this._argbFromString(this._hex); | ||
| 25 | 28 | } else { | |
| 26 | 29 | // The parameter is a "#AARRGGBB" formatted string | |
| 27 | 30 | this._hex = this._normalizeHex(arg); | |
| 31 | + this._argb = this._argbFromString(this._hex); | ||
| 28 | 32 | } | |
| 29 | - this._argb = this._argbFromString(this._hex); | ||
| 30 | 33 | } else if (types.isNumber(arg)) { | |
| 31 | 34 | // The parameter is a 32-bit unsigned integer where each 8 bits specify a color component | |
| 32 | 35 | this._argb = arg; | |
@@ -121,7 +124,7 @@ export class Color implements definition.Color { | |||
| 121 | 124 | return true; | |
| 122 | 125 | } | |
| 123 | 126 | ||
| 124 | - return HEX_REGEX.test(value); | ||
| 127 | + return HEX_REGEX.test(value) || isRgbOrRgba(value); | ||
| 125 | 128 | } | |
| 126 | 129 | ||
| 127 | 130 | private _buildHex(): string { | |
@@ -165,3 +168,22 @@ export class Color implements definition.Color { | |||
| 165 | 168 | return hexStr; | |
| 166 | 169 | } | |
| 167 | 170 | } | |
| 171 | + | ||
| 172 | + function isRgbOrRgba(value: string) : boolean { | ||
| 173 | + var toLower = value.toLowerCase(); | ||
| 174 | + return (toLower.indexOf("rgb(") === 0 || toLower.indexOf("rgba(") === 0) && toLower.indexOf(")") === (toLower.length - 1); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + function argbFromRgbOrRgba(value: string) : number { | ||
| 178 | + var toLower = value.toLowerCase(); | ||
| 179 | + var parts = toLower.replace("rgba(", "").replace("rgb(", "").replace(")", "").trim().split(","); | ||
| 180 | + | ||
| 181 | + var r = parseInt(parts[0] && parts[0].trim()); | ||
| 182 | + var g = parseInt(parts[1] && parts[1].trim()); | ||
| 183 | + var b = parseInt(parts[2] && parts[2].trim()); | ||
| 184 | + | ||
| 185 | + var a = Math.round((parseFloat(parts[3] && parts[3].trim()) || 1) * 255); | ||
| 186 | + | ||
| 187 | + // Format is ARGB, so alpha takes the first 8 bits, red the next, green the next and the last 8 bits are for the blue component | ||
| 188 | + return (a << 24) | (r << 16) | (g << 8) | b; | ||
| 189 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments