Skip to content

Commit 7441127

Browse files
zhongwuzwfacebook-github-bot
authored andcommitted
Added slash of alpha support using rgb() (#50281)
Summary: Fixes #50207 Added slash alpha support like `rgb(255 122 127 / 0.2);`, currently seems we don't support it. ## Changelog: [GENERAL] [ADDED] - Added slash of alpha support using rgb() Pull Request resolved: #50281 Test Plan: `rgb(255 122 127 / 0.2)` can shows correctly. Reviewed By: cipolleschi Differential Revision: D72009174 Pulled By: NickGerleman fbshipit-source-id: f53e550cfdc8f481785a1fb134cd0fab810f8f38
1 parent a29d5a2 commit 7441127

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

packages/normalize-color/__tests__/normalizeColor-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ it('handles rgb properly', () => {
8585
expect(normalizeColor('rgb(256, 256, 256)')).toBe(0xffffffff);
8686
expect(normalizeColor('rgb(0 0 0)')).toBe(0x000000ff);
8787
expect(normalizeColor('rgb(0 0 255)')).toBe(0x0000ffff);
88+
expect(normalizeColor('rgb(0 0 0 / 0.5)')).toBe(0x00000080);
89+
expect(normalizeColor('rgb(0 0 0 / 1)')).toBe(0x000000ff);
90+
expect(normalizeColor('rgb(0, 0, 0, 0.5)')).toBe(0x00000080);
8891
});
8992

9093
it('handles rgba properly', () => {
@@ -98,6 +101,7 @@ it('handles rgba properly', () => {
98101
expect(normalizeColor('rgba(0 0 0 / 0.0)')).toBe(0x00000000);
99102
expect(normalizeColor('rgba(0 0 0 / 1)')).toBe(0x000000ff);
100103
expect(normalizeColor('rgba(100 15 69 / 0.5)')).toBe(0x640f4580);
104+
expect(normalizeColor('rgba(0, 0, 0)')).toBe(0x000000ff);
101105
});
102106

103107
it('handles hsl properly', () => {

packages/normalize-color/index.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,33 @@ function normalizeColor(color) {
3737
return colorFromKeyword;
3838
}
3939

40-
if ((match = matchers.rgb.exec(color))) {
41-
return (
42-
((parse255(match[1]) << 24) | // r
43-
(parse255(match[2]) << 16) | // g
44-
(parse255(match[3]) << 8) | // b
45-
0x000000ff) >>> // a
46-
0
47-
);
48-
}
49-
50-
if ((match = matchers.rgba.exec(color))) {
51-
// rgba(R G B / A) notation
52-
if (match[6] !== undefined) {
40+
if ((match = matchers.rgba.exec(color) || matchers.rgb.exec(color))) {
41+
// rgb(R G B / A) / rgba(R G B / A) notation
42+
if (match[9] !== undefined) {
5343
return (
54-
((parse255(match[6]) << 24) | // r
55-
(parse255(match[7]) << 16) | // g
56-
(parse255(match[8]) << 8) | // b
57-
parse1(match[9])) >>> // a
44+
((parse255(match[9]) << 24) | // r
45+
(parse255(match[10]) << 16) | // g
46+
(parse255(match[11]) << 8) | // b
47+
parse1(match[12])) >>> // a
5848
0
5949
);
6050
}
61-
62-
// rgba(R, G, B, A) notation
51+
// rgb(R, G, B, A) / rgba(R, G, B, A) notation
52+
else if (match[5] !== undefined) {
53+
return (
54+
((parse255(match[5]) << 24) | // r
55+
(parse255(match[6]) << 16) | // g
56+
(parse255(match[7]) << 8) | // b
57+
parse1(match[8])) >>> // a
58+
0
59+
);
60+
}
61+
// rgb(R, G, B) / rgba(R, G, B) notation
6362
return (
6463
((parse255(match[2]) << 24) | // r
6564
(parse255(match[3]) << 16) | // g
6665
(parse255(match[4]) << 8) | // b
67-
parse1(match[5])) >>> // a
66+
0x000000ff) >>> // a
6867
0
6968
);
7069
}
@@ -238,15 +237,16 @@ let cachedMatchers;
238237

239238
function getMatchers() {
240239
if (cachedMatchers === undefined) {
240+
const rgbRegexPattern =
241+
call(NUMBER, NUMBER, NUMBER) +
242+
'|' +
243+
commaSeparatedCall(NUMBER, NUMBER, NUMBER, NUMBER) +
244+
'|' +
245+
callWithSlashSeparator(NUMBER, NUMBER, NUMBER, NUMBER);
246+
241247
cachedMatchers = {
242-
rgb: new RegExp('rgb' + call(NUMBER, NUMBER, NUMBER)),
243-
rgba: new RegExp(
244-
'rgba(' +
245-
commaSeparatedCall(NUMBER, NUMBER, NUMBER, NUMBER) +
246-
'|' +
247-
callWithSlashSeparator(NUMBER, NUMBER, NUMBER, NUMBER) +
248-
')',
249-
),
248+
rgb: new RegExp('rgb(' + rgbRegexPattern + ')'),
249+
rgba: new RegExp('rgba(' + rgbRegexPattern + ')'),
250250
hsl: new RegExp('hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE)),
251251
hsla: new RegExp(
252252
'hsla(' +

0 commit comments

Comments
 (0)