Skip to content

Commit b84ffce

Browse files
committed
add P3 to utilitties
1 parent d579ce1 commit b84ffce

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

css-color-4/LCH-examples.html

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
<script src="math.js"></script>
44
<script src="conversions.js"></script>
55
<textarea id="sRGBresults"></textarea>
6-
<!-- <textarea id="P3results"></textarea>
7-
<textarea id="Rec2020results"></textarea> -->
6+
<textarea id="P3results"></textarea>
7+
<textarea id="Rec2020results"></textarea>
88
<script>
99
// convert a sampling of
1010
// several RGB colorspaces
1111
// to Lab and LCH
1212

1313
console.log("test sRGB");
14-
sRGBresults.value += 'sRGB';
15-
// for (var r =0; r <=1; r+=0.5) {
16-
// for (var g =0; g <=1; g+=0.5) {
17-
// for (var b =0; b <=1; b+=0.5) {
18-
// sRGBresults.value += `RGB=${r},${g},${b}
19-
// `;
20-
// var lab = XYZ_to_Lab(D65_to_D50(lin_sRGB_to_XYZ(lin_sRGB([r, g, b]))));
21-
// sRGBresults.value += `Lab=${lab}
22-
// `;
23-
// sRGBresults.value += `LCH=${Lab_to_LCH(lab)}
14+
sRGBresults.value += `sRGB
2415
25-
// `;
26-
// }
27-
// }
28-
// };
16+
`;
17+
for (var r =0; r <=1; r+=0.5) {
18+
for (var g =0; g <=1; g+=0.5) {
19+
for (var b =0; b <=1; b+=0.5) {
20+
sRGBresults.value += `RGB=${r},${g},${b}
21+
`;
22+
var lab = XYZ_to_Lab(D65_to_D50(lin_sRGB_to_XYZ(lin_sRGB([r, g, b]))));
23+
sRGBresults.value += `Lab=${lab}
24+
`;
25+
sRGBresults.value += `LCH=${Lab_to_LCH(lab)}
26+
27+
`;
28+
}
29+
}
30+
};
2931

3032
console.log("image-p3");
3133

css-color-4/utilities.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ function sRGB_to_LCH(RGB) {
3535
return Lab_to_LCH(XYZ_to_Lab(D65_to_D50(lin_sRGB_to_XYZ(lin_sRGB(RGB)))));
3636
}
3737

38+
function P3_to_LCH(RGB) {
39+
// convert an array of gamma-corrected display-p3 values
40+
// in the 0.0 to 1.0 range
41+
// to linear-light display-p3, then to CIE XYZ,
42+
// then adapt from D65 to D50,
43+
// then convert XYZ to CIE Lab
44+
// and finally, convert to CIE LCH
45+
46+
return Lab_to_LCH(XYZ_to_Lab(D65_to_D50(lin_P3_to_XYZ(lin_P3(RGB)))));
47+
}
3848
function LCH_to_sRGB(LCH) {
3949
// convert an array of CIE LCH values
4050
// to CIE Lab, and then to XYZ,
@@ -47,4 +57,18 @@ function LCH_to_sRGB(LCH) {
4757
// so check for that :)
4858

4959
return gam_sRGB(XYZ_to_lin_sRGB(D50_to_D65(Lab_to_XYZ(LCH_to_Lab(LCH)))));
60+
}
61+
62+
function LCH_to_P3(LCH) {
63+
// convert an array of CIE LCH values
64+
// to CIE Lab, and then to XYZ,
65+
// adapt from D50 to D65,
66+
// then convert XYZ to linear-light display-p3
67+
// and finally to gamma corrected display-p3
68+
// for in-gamut colors, components are in the 0.0 to 1.0 range
69+
// out of gamut colors may have negative components
70+
// or components greater than 1.0
71+
// so check for that :)
72+
73+
return gam_P3(XYZ_to_lin_P3(D50_to_D65(Lab_to_XYZ(LCH_to_Lab(LCH)))));
5074
}

0 commit comments

Comments
 (0)