Skip to content

Commit 5ab0e77

Browse files
committed
feat(core): support RGB alpha notation
Functional notation: rgb[a](R G B[ / A]) CSS Colors Level 4 adds support for space-separated values in the functional notation. See https://www.w3.org/TR/css-color-4/#rgb-functions
1 parent 80d92db commit 5ab0e77

5 files changed

Lines changed: 28 additions & 2 deletions

File tree

apps/automated/src/color/color-tests.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ export var test_rgb_Color_CSS = function () {
7272
TKUnit.assertEqual(color.argb, 0xffff6464, 'Color.argb not properly parsed');
7373
};
7474

75+
export var test_rgb_Color_CSS_lvl4 = function () {
76+
var alpha = 0.5;
77+
var expected = 0x80;
78+
// <snippet module="color" title="color">
79+
// ### Creating a Color from four RGB values
80+
// ``` JavaScript
81+
// Creates the color with 255 red, 100 green, 100 blue
82+
var color = new Color(`rgb(255 100 100 / ${alpha})`);
83+
// ```
84+
// </snippet>
85+
TKUnit.assertEqual(color.a, expected, 'Color.a not properly parsed');
86+
TKUnit.assertEqual(color.r, 255, 'Color.r not properly parsed');
87+
TKUnit.assertEqual(color.g, 100, 'Color.g not properly parsed');
88+
TKUnit.assertEqual(color.b, 100, 'Color.b not properly parsed');
89+
TKUnit.assertEqual(color.hex, '#FF646480', 'Color.hex not properly parsed');
90+
TKUnit.assertEqual(color.argb, 0x80ff6464, 'Color.argb not properly parsed');
91+
};
92+
7593
export var test_rgba_Color_CSS = function () {
7694
var alpha = 0.5;
7795
var expected = 0x80;

apps/automated/src/ui/styling/style-properties-tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export function test_setting_borderColorRGB_property_from_CSS_is_applied_to_Styl
5959
test_property_from_CSS_is_applied_to_style('borderColor', 'border-color', new Color('#FF0000'), 'rgb(255, 0, 0)');
6060
}
6161

62+
export function test_setting_borderColorRGBLvl4_property_from_CSS_is_applied_to_Style() {
63+
test_property_from_CSS_is_applied_to_style('borderColor', 'border-color', new Color('#FF0000'), 'rgb(255 0 0 / 1)');
64+
}
65+
6266
export function test_setting_borderColorRGBA_property_from_CSS_is_applied_to_Style() {
6367
test_property_from_CSS_is_applied_to_style('borderColor', 'border-color', new Color('#FF0000'), 'rgba(255,0,0,1)');
6468
}

packages/core/__tests__/css/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ describe('css', () => {
4444
test(parseColor, ' #456789 ', { start: 0, end: 10, value: new Color(0xff456789) });
4545
test(parseColor, ' #45678985 ', { start: 0, end: 12, value: new Color(0x45678985) });
4646
test(parseColor, ' rgb(255, 8, 128) ', { start: 0, end: 18, value: new Color(0xffff0880) });
47+
test(parseColor, ' rgb(255 8 128 / .5) ', { start: 0, end: 24, value: new Color(0x80ff0880) });
48+
test(parseColor, ' rgb(255 8 128/0.5) ', { start: 0, end: 24, value: new Color(0x80ff0880) });
4749
test(parseColor, ' rgba(255, 8, 128, 0.5) ', { start: 0, end: 24, value: new Color(0x80ff0880) });
4850
test(parseColor, ' hsl(330.9, 100%, 51.6%) ', { start: 0, end: 25, value: new Color(0xffff0880) });
4951
test(parseColor, ' hsla(330.9, 100%, 51.6%, 0.5) ', { start: 0, end: 31, value: new Color(0x80ff0880) });

packages/core/color/color-common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,15 @@ function isHsvOrHsva(value: string): boolean {
406406
}
407407

408408
function parseColorWithAlpha(value: string): any {
409+
const separator = value.indexOf(',') > -1 ? ',' : ' ';
409410
const parts = value
410411
.replace(/(rgb|hsl|hsv)a?\(/, '')
411412
.replace(')', '')
413+
.replace(/\//, ' ')
412414
.replace(/%/g, '')
413415
.trim()
414-
.split(',');
416+
.split(separator)
417+
.filter((part) => Boolean(part.length));
415418

416419
let f = 255;
417420
let s = 255;

packages/core/css/parser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ export function parseColorKeyword(value, start: number, keyword = parseKeyword(v
135135
const parseColor = keyword && getKnownColor(keyword.value);
136136
if (parseColor != null) {
137137
const end = keyword.end;
138-
const value = parseColor;
139138
return { start, end, value: new Color(parseColor) };
140139
}
141140
return null;

0 commit comments

Comments
 (0)