Skip to content

Commit 00e3873

Browse files
committed
[css-color-hdr] Added sample code for hdr-color()
1 parent 89515be commit 00e3873

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

css-color-hdr-1/Overview.bs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,14 @@ The end result is identical, this just takes fewer steps.
15361536
highlight: js
15371537
</pre>
15381538

1539+
<h3 id="hdr-color-code">
1540+
Sample code for ''hdr-color()''
1541+
</h3>
15391542

1543+
<pre class="include-code lang-javascript">
1544+
path: colorHdrInterpolate.js
1545+
highlight: js
1546+
</pre>
15401547

15411548

15421549
<!--
@@ -1833,6 +1840,7 @@ in a user stylesheet.
18331840

18341841
<ul>
18351842
<!-- to 18 July 2025 -->
1843+
<li>Added sample code for hdr-color()</li>
18361844
<li>Explained where the eps factor comes from, and what it is for
18371845
(<a href="https://github.com/w3c/csswg-drafts/issues/11788">#11788</a>)</li>
18381846
<li>Added second color-hdr() worked example</li>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function hdrColor(col1, H1, col2, H2, H) {
2+
3+
// col1, col2 are arrays representing two colors, in Absolute XYZ
4+
// H1 and H2 are the headroom for each color (in stops, ie log scale, 0 = SDR)
5+
// H is available headroom
6+
7+
// first check the headrooms are distinct
8+
if (H1 == H2) return 0;
9+
10+
let w1 = clamp((H - H2) / (H1 - H2), 0, 1);
11+
let w2 = clamp((H - H1) / (H2 - H1), 0, 1);
12+
let eps = 0.001;
13+
let cxyz = Array(3);
14+
for (let i=0; i<3; i++) {
15+
cxyz[i] = Math.pow(c1xyz[i] + eps, w1) * Math.pow(c2xyz[i] + eps, w2) - eps;
16+
}
17+
return cxyz;
18+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let Color = await import("https://colorjs.io/dist/color.js").then(m => m.default
33
const clamp = (n, min, max) =>
44
Math.min(Math.max(n, min), max)
55

6-
function colorHdr(col1, H1, col2, H2, H) {
6+
function hdrColor (col1, H1, col2, H2, H) {
77

88
// https://drafts.csswg.org/css-color-hdr/#headroom-interpolation
99
// col1, col2 are colors
@@ -33,5 +33,5 @@ let c2 = new Color("color(rec2100-pq 0.8 0.8 0.8)");
3333
let h1 = 0;
3434
let h2 = 2;
3535
let h = 1;
36-
let result=colorHdr(c1, h1, c2, h2, h);
36+
let result=hdrColor(c1, h1, c2, h2, h);
3737
console.log(result.coords);

0 commit comments

Comments
 (0)