Skip to content

Commit 30200ed

Browse files
committed
[css-color-hdr][editorial] Implement color-hdr in JS with Color.js to create examples.
Co-authored-by: @LeaVerou
1 parent ce543d0 commit 30200ed

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

css-color-hdr-1/workings/hdr-color.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
let Color = await import("https://colorjs.io/dist/color.js").then(m => m.default);
2+
3+
const clamp = (n, min, max) =>
4+
Math.min(Math.max(n, min), max)
5+
6+
function colorHdr(col1, H1, col2, H2, H) {
7+
8+
// https://drafts.csswg.org/css-color-hdr/#headroom-interpolation
9+
// col1, col2 are colors
10+
// H1 and H2 are headroom for each color (in stops, ie log scale, 0 = SDR)
11+
// H is available headroom
12+
13+
// first check the headrooms are distinct
14+
if (H1 == H2) return 0;
15+
16+
let c1xyz = col1.to("xyz-abs-d65").coords;
17+
let c2xyz = col2.to("xyz-abs-d65").coords;
18+
let w1 = clamp((H - H2) / (H1 - H2), 0, 1);
19+
let w2 = clamp((H - H1) / (H2 - H1), 0, 1);
20+
let eps = 0.001;
21+
let cxyz = Array(3);
22+
for (let i=0; i<3; i++) {
23+
cxyz[i] = Math.pow(c1xyz[i] + eps, w1) * Math.pow(c2xyz[i] + eps, w2) - eps;
24+
}
25+
return new Color("xyz-abs-d65", cxyz);
26+
}
27+
28+
let c1 = new Color("slategray");
29+
let c2 = new Color("color(rec2100-pq 0.8 0.8 0.8)");
30+
let h1 = 0;
31+
let h2 = 2;
32+
let h = 1;
33+
let result=colorHdr(c1, h1, c2, h2, h);
34+
console.log(result.coords);

0 commit comments

Comments
 (0)