FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

rgb() and rgba() support added · NativeScript/NativeScript@440808f · GitHub

Commit 440808f

Browse files
Vladimir Enchev
committed
rgb() and rgba() support added
1 parent c3a7ed8 commit 440808f

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎apps/tests/color-tests.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,40 @@ export var test_ArgbInt_Color = function () {
7373
TKUnit.assert(color.argb === _parseArgb(100, 100, 100, 100), "Color.argb not properly parsed");
7474
}
7575

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+
76110
var _buildHex = function (a: number, r: number, g: number, b: number): string {
77111
return "#" + _componentToHex(a) + _componentToHex(r) + _componentToHex(g) + _componentToHex(b);
78112
}

‎color/color-common.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ export class Color implements definition.Color {
1818
if (arguments.length === 1) {
1919
var arg = arguments[0];
2020
if (types.isString(arg)) {
21-
if (knownColors.isKnownName(arg)) {
21+
if(isRgbOrRgba(arg)){
22+
this._argb = argbFromRgbOrRgba(arg);
23+
} else if (knownColors.isKnownName(arg)) {
2224
// The parameter is a known color name
2325
this._hex = knownColors.getKnownColor(arg);
2426
this._name = arg;
27+
this._argb = this._argbFromString(this._hex);
2528
} else {
2629
// The parameter is a "#AARRGGBB" formatted string
2730
this._hex = this._normalizeHex(arg);
31+
this._argb = this._argbFromString(this._hex);
2832
}
29-
this._argb = this._argbFromString(this._hex);
3033
} else if (types.isNumber(arg)) {
3134
// The parameter is a 32-bit unsigned integer where each 8 bits specify a color component
3235
this._argb = arg;
@@ -121,7 +124,7 @@ export class Color implements definition.Color {
121124
return true;
122125
}
123126

124-
return HEX_REGEX.test(value);
127+
return HEX_REGEX.test(value) || isRgbOrRgba(value);
125128
}
126129

127130
private _buildHex(): string {
@@ -165,3 +168,22 @@ export class Color implements definition.Color {
165168
return hexStr;
166169
}
167170
}
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+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL