Skip to content

Commit 692b05e

Browse files
zhongwuzwfacebook-github-bot
authored andcommitted
CSS Added hwb(H W B / A) notation (#50750)
Summary: Fixes #50583. cc NickGerleman ## Changelog: [GENERAL] [FIXED] - CSS Added hwb(H W B / A) notation Pull Request resolved: #50750 Test Plan: Added hwb(H W B / A) notation Reviewed By: cipolleschi Differential Revision: D73126316 Pulled By: NickGerleman fbshipit-source-id: 8e3730c6de44a0b4d8a21339e1f7339cb70e2044
1 parent 95af970 commit 692b05e

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ it('handles hwb properly', () => {
140140
expect(normalizeColor('hwb(0 0% 0%)')).toBe(0xff0000ff);
141141
expect(normalizeColor('hwb(70 50% 0%)')).toBe(0xeaff80ff);
142142
expect(normalizeColor('hwb(0, 0%, 100%)')).toBe(null);
143+
expect(normalizeColor('hwb(200 30% 20% / 0.5)')).toBe(0x4da1cc80);
143144
});
144145

145146
it('handles named colors properly', () => {

packages/normalize-color/index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,24 @@ function normalizeColor(color) {
143143
}
144144

145145
if ((match = matchers.hwb.exec(color))) {
146+
if (match[5] !== undefined) {
147+
// hwb(H W B / A) notation
148+
return (
149+
(hwbToRgb(
150+
parse360(match[5]), // h
151+
parsePercentage(match[6]), // w
152+
parsePercentage(match[7]), // b
153+
) |
154+
parse1(match[8])) >>> // a
155+
0
156+
);
157+
}
158+
// hwb(H W B) notation
146159
return (
147160
(hwbToRgb(
148-
parse360(match[1]), // h
149-
parsePercentage(match[2]), // w
150-
parsePercentage(match[3]), // b
161+
parse360(match[2]), // h
162+
parsePercentage(match[3]), // w
163+
parsePercentage(match[4]), // b
151164
) |
152165
0x000000ff) >>> // a
153166
0
@@ -255,7 +268,13 @@ function getMatchers() {
255268
callWithSlashSeparator(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
256269
')',
257270
),
258-
hwb: new RegExp('hwb' + callModern(NUMBER, PERCENTAGE, PERCENTAGE)),
271+
hwb: new RegExp(
272+
'hwb(' +
273+
callModern(NUMBER, PERCENTAGE, PERCENTAGE) +
274+
'|' +
275+
callWithSlashSeparator(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
276+
')',
277+
),
259278
hex3: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
260279
hex4: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
261280
hex6: /^#([0-9a-fA-F]{6})$/,

0 commit comments

Comments
 (0)