-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[blink] Add support for HWB color values.
Implement CSSHWB classes and HWB <-> RGB conversion to implement the standard in https://drafts.csswg.org/css-color-4/#the-hwb-notation# Fixed: 1288883 I2S: https://groups.google.com/a/chromium.org/g/blink-dev/c/7SKzObJ9IBI/ Change-Id: I343651980244a18af45ac28b446153a93a55721d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3404291 Reviewed-by: Aaron Krajeski <aaronhk@chromium.org> Reviewed-by: Alex Rudenko <alexrudenko@chromium.org> Reviewed-by: Anders Hartvoll Ruud <andruud@chromium.org> Reviewed-by: Stephen Chenney <schenney@chromium.org> Commit-Queue: Stephen Chenney <schenney@chromium.org> Cr-Commit-Position: refs/heads/main@{#975498}
- Loading branch information
Showing
3 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<!doctype html> | ||
<meta charset="utf-8"> | ||
<title>CSSHWB tests</title> | ||
<link rel="help" href="https://drafts.css-houdini.org/css-typed-om-1/#csshwb"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="../resources/testhelper.js"></script> | ||
<script> | ||
'use strict'; | ||
|
||
const gValidHueInputs = [ | ||
{hue: CSS.deg(180), desc: 'degrees'}, | ||
{hue: CSS.rad(1), desc: 'radians'}, | ||
{hue: CSS.grad(81), desc: 'gradians'}, | ||
]; | ||
|
||
const gInvalidHueInputs = [ | ||
{hue: 180, desc: "a number"}, | ||
{hue: CSS.px(1), desc: "css pixels"}, | ||
{hue: undefined, desc: "undefined"}, | ||
] | ||
|
||
for (const {hue, desc} of gValidHueInputs) { | ||
test(() => { | ||
const result = new CSSHWB(hue, 0.5, 0.5); | ||
assert_color_channel_approx_equals(result.h, hue); | ||
}, `Constructing a CSSHWB with ${desc} for the hue works as intended.`); | ||
|
||
test(() => { | ||
const result = new CSSHWB(CSS.deg(0), 0.5, 0.5); | ||
result.h = hue; | ||
assert_color_channel_approx_equals(result.h, hue); | ||
}, `CSSHWB.h can be updated with a ${desc}.`); | ||
} | ||
|
||
for (const {hue, desc} of gInvalidHueInputs) { | ||
test(() => { | ||
assert_throws_js(TypeError, () => new CSSHWB(hue, 0, 0)); | ||
}, `Constructing a CSSHWB with ${desc} for hue throws a type error.`); | ||
} | ||
|
||
test(() => { | ||
const result = new CSSHWB(CSS.deg(27), 0.7, CSS.percent(91)); | ||
assert_color_channel_approx_equals(result.h, CSS.deg(27)); | ||
assert_color_channel_approx_equals(result.w, CSS.percent(70)); | ||
assert_color_channel_approx_equals(result.b, CSS.percent(91)); | ||
assert_color_channel_approx_equals(result.alpha, CSS.percent(100)); | ||
}, 'CSSHWB can be constructed from three numbers and will get an alpha of 100%.'); | ||
|
||
test(() => { | ||
const result = new CSSHWB(CSS.grad(101), 0.2, 0.3, CSS.percent(0.4)); | ||
assert_color_channel_approx_equals(result.h, CSS.grad(101)); | ||
assert_color_channel_approx_equals(result.w, CSS.percent(20)); | ||
assert_color_channel_approx_equals(result.b, CSS.percent(30)); | ||
assert_color_channel_approx_equals(result.alpha, CSS.percent(0.4)); | ||
}, 'CSSHWB can be constructed from four numbers.'); | ||
|
||
test(() => { | ||
assert_throws_js(TypeError, () => new CSSHWB(CSS.deg(0), CSS.number(1), 0, 0)); | ||
assert_throws_js(TypeError, () => new CSSHWB(CSS.deg(0), 0, CSS.number(1), 0)); | ||
assert_throws_js(TypeError, () => new CSSHWB(CSS.deg(0), 0, 0, CSS.number(1))); | ||
}, `Constructing a CSSHWB with CSS.number for s, l or alpha throws a TypeError`); | ||
|
||
for (const attr of ['w', 'b', 'alpha']) { | ||
test(() => { | ||
const result = new CSSHWB(CSS.deg(0), 0, 0); | ||
assert_throws_js(TypeError, () => result[attr] = CSS.number(1)); | ||
}, `Updating a CSSHWB with CSS.number for ${attr} throws a TypeError`); | ||
|
||
test(() => { | ||
const result = new CSSHWB(CSS.deg(0), 0, 0); | ||
result[attr] = 0.5; | ||
assert_color_channel_approx_equals(result[attr], CSS.percent(50)); | ||
}, 'CSSHWB.' + attr + ' can be updated to a number.'); | ||
|
||
test(() => { | ||
const result = new CSSHWB(CSS.deg(0), 0, 0); | ||
result[attr] = CSS.percent(50); | ||
assert_color_channel_approx_equals(result[attr], CSS.percent(50)); | ||
}, 'CSSHWB.' + attr + ' can be updated with a CSS percent.'); | ||
} | ||
</script> |