Skip to content

Commit

Permalink
2D canvas color parsing and serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
annevk committed Jul 16, 2024
1 parent e3e81ac commit 7991a1c
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions html/canvas/color.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const canvas = document.body.appendChild(document.createElement("canvas"));
const context = canvas.getContext("2d");

[
{
"input": "rgb(255, 0, 255)",
"output": "#ff00ff"
},
{
"input": "rgba(255, 0, 255, 1)",
"output": "#ff00ff"
},
{
"input": "#ff00ffff",
"output": "#ff00ff"
},
{
"input": "#ff00ff00",
"output": "rgba(255, 0, 255, 0)"
},
{
"input": "rgba(255, 0, 255, 0)",
"output": "rgba(255, 0, 255, 0)"
},
{
"input": "color(srgb 1 0 1)",
"output": "#ff00ff"
},
{
"input": "color(srgb 1 0 1 / 1)",
"output": "#ff00ff"
},
{
"input": "color(srgb 1 0 1 / 0)",
"output": "rgba(255, 0, 255, 0)"
},
{
"input": "color(srgb 1 none 1)",
"output": "color(srgb 1 none 1)" // one could argue for #ff00ff here?
},
{
"input": "color(srgb none -1 2 / 3)", // alpha is clamped to [0, 1]
"output": "color(srgb none -1 2)"
},
{
"input": "color(display-p3 0.96 0.76 \t 0.79)",
"output": "color(display-p3 0.96 0.76 0.79)"
},
{
"input": "lab(29% 39 20)",
"output": "lab(29 39 20)"
},
{
"input": "transparent",
"output": "rgba(0, 0, 0, 0)"
}
].forEach(({ input, output }) => {
test(t => {
["fillStyle", "strokeStyle", "shadowColor"].forEach(colorGetterSetter => {
context[colorGetterSetter] = input;
assert_equals(context[colorGetterSetter], output, colorGetterSetter);
});
}, `'${input}' should serialize as '${output}'`);
});

0 comments on commit 7991a1c

Please sign in to comment.