Skip to content

Commit ac931d8

Browse files
committed
finish off the sRGB - LCH utilities
1 parent a6d36d5 commit ac931d8

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

css-color-4/utilities.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function contrast(RGB1, RGB2) {
2020
var L1 = sRGB_to_luminance(RGB1);
2121
var L2 = sRGB_to_luminance(RGB2);
2222

23-
if L1 > L2 return (L1 + 0.05) / (L2 + 0.05);
23+
if (L1 > L2) return (L1 + 0.05) / (L2 + 0.05);
2424
return (L2 + 0.05) / (L1 + 0.05);
2525
}
2626

@@ -32,5 +32,19 @@ function sRGB_to_LCH(RGB) {
3232
// then convert XYZ to CIE Lab
3333
// and finally, convert to CIE LCH
3434

35-
return lin_sRGB_to_XYZ(lin_sRGB(RGB));
35+
return Lab_to_LCH(XYZ_to_Lab(D65_to_D50(lin_sRGB_to_XYZ(lin_sRGB(RGB)))));
36+
}
37+
38+
functions LCH_to_sRGB(LCH) {
39+
// convert an array of CIE LCH values
40+
// to CIE Lab, and then to XYZ,
41+
// adapt from D50 to D65,
42+
// then convert XYZ to linear-light sRGB
43+
// and finally to gamma corrected sRGB
44+
// for in-gamut colors, components are in the 0.0 to 1.0 range
45+
// out of gamut colors may have negative components
46+
// or components greater than 1.0
47+
// so check for that :)
48+
49+
return gam(sRGB(XYZ_to_lin_sRGB(Lab_to_XYZ(D50_to_D65(LCH_to_Lab(LCH)))));
3650
}

css-color-5/Overview.bs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ Mixing colors: the ''color-mix'' function {#colormix}
7171

7272
<div class="example">
7373
This example produces the mixture of red and yellow,
74-
in ''lch'' colorspace (the default),
74+
in ''lch()'' colorspace (the default),
7575
with the lightness being 30% of the lightness of red
76-
and 70% of the lightness of yellow.
76+
(and thus, 70% of the lightness of yellow).
7777
The chroma and hue of red are left unchanged.
7878

7979
<pre class="lang-css">mix-color(red, yellow, lightness(30%));</pre>
@@ -84,6 +84,8 @@ Mixing colors: the ''color-mix'' function {#colormix}
8484
* mix lightness is 54.2917 * 0.3 + 97.6071 * 0.7 = 84.6125
8585
* mixed result is lch(84.6125 106.8390 40.8526)
8686
* which is a very light red (and outside the gamut of sRGB: rgb(140.4967% 51.2654% 32.6891%))
87+
88+
<!-- Maybe the first example should reslve to an in-gamut color; show out of gamut colors later? -->
8789
</div>
8890

8991
Instead of a list of color functions,

0 commit comments

Comments
 (0)