Skip to content

Commit 7991a1c

Browse files
committed
2D canvas color parsing and serialization
For whatwg/html#8917, w3c/csswg-drafts#10550, and whatwg/html#10481.
1 parent e3e81ac commit 7991a1c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

html/canvas/color.window.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const canvas = document.body.appendChild(document.createElement("canvas"));
2+
const context = canvas.getContext("2d");
3+
4+
[
5+
{
6+
"input": "rgb(255, 0, 255)",
7+
"output": "#ff00ff"
8+
},
9+
{
10+
"input": "rgba(255, 0, 255, 1)",
11+
"output": "#ff00ff"
12+
},
13+
{
14+
"input": "#ff00ffff",
15+
"output": "#ff00ff"
16+
},
17+
{
18+
"input": "#ff00ff00",
19+
"output": "rgba(255, 0, 255, 0)"
20+
},
21+
{
22+
"input": "rgba(255, 0, 255, 0)",
23+
"output": "rgba(255, 0, 255, 0)"
24+
},
25+
{
26+
"input": "color(srgb 1 0 1)",
27+
"output": "#ff00ff"
28+
},
29+
{
30+
"input": "color(srgb 1 0 1 / 1)",
31+
"output": "#ff00ff"
32+
},
33+
{
34+
"input": "color(srgb 1 0 1 / 0)",
35+
"output": "rgba(255, 0, 255, 0)"
36+
},
37+
{
38+
"input": "color(srgb 1 none 1)",
39+
"output": "color(srgb 1 none 1)" // one could argue for #ff00ff here?
40+
},
41+
{
42+
"input": "color(srgb none -1 2 / 3)", // alpha is clamped to [0, 1]
43+
"output": "color(srgb none -1 2)"
44+
},
45+
{
46+
"input": "color(display-p3 0.96 0.76 \t 0.79)",
47+
"output": "color(display-p3 0.96 0.76 0.79)"
48+
},
49+
{
50+
"input": "lab(29% 39 20)",
51+
"output": "lab(29 39 20)"
52+
},
53+
{
54+
"input": "transparent",
55+
"output": "rgba(0, 0, 0, 0)"
56+
}
57+
].forEach(({ input, output }) => {
58+
test(t => {
59+
["fillStyle", "strokeStyle", "shadowColor"].forEach(colorGetterSetter => {
60+
context[colorGetterSetter] = input;
61+
assert_equals(context[colorGetterSetter], output, colorGetterSetter);
62+
});
63+
}, `'${input}' should serialize as '${output}'`);
64+
});

0 commit comments

Comments
 (0)