Debian Codegraph @

node/colorjs.io/src/spaces/hct.js

raw
1 import ColorSpace from "../ColorSpace.js";
2 import { constrain } from "../angles.js";
3 import xyz_d65 from "./xyz-d65.js";
4 import { fromCam16, toCam16, environment } from "./cam16.js";
5 import { WHITES } from "../adapt.js";
6
7 const white = WHITES.D65;
8 const ε = 216 / 24389; // 6^3/29^3 == (24/116)^3
9 const κ = 24389 / 27; // 29^3/3^3
10
11 function toLstar (y) {
12 // Convert XYZ Y to L*
13
14 const fy = y > ε ? Math.cbrt(y) : (κ * y + 16) / 116;
15 return 116.0 * fy - 16.0;
16 }
17
18 function fromLstar (lstar) {
19 // Convert L* back to XYZ Y
20
21 return lstar > 8 ? Math.pow((lstar + 16) / 116, 3) : lstar / κ;
22 }
23
24 function fromHct (coords, env) {
25 // Use Newton's method to try and converge as quick as possible or
26 // converge as close as we can. While the requested precision is achieved
27 // most of the time, it may not always be achievable. Especially past the
28 // visible spectrum, the algorithm will likely struggle to get the same
29 // precision. If, for whatever reason, we cannot achieve the accuracy we
30 // seek in the allotted iterations, just return the closest we were able to
31 // get.
32
33 let [h, c, t] = coords;
34 let xyz = [];
35 let j = 0;
36
37 // Shortcut out for black
38 if (t === 0) {
39 return [0.0, 0.0, 0.0];
40 }
41
42 // Calculate the Y we need to target
43 let y = fromLstar(t);
44
45 // A better initial guess yields better results. Polynomials come from
46 // curve fitting the T vs J response.
47 if (t > 0) {
48 j = 0.00379058511492914 * t ** 2 + 0.608983189401032 * t + 0.9155088574762233;
49 }
50 else {
51 j = 9.514440756550361e-6 * t ** 2 + 0.08693057439788597 * t - 21.928975842194614;
52 }
53
54 // Threshold of how close is close enough, and max number of attempts.
55 // More precision and more attempts means more time spent iterating. Higher
56 // required precision gives more accuracy but also increases the chance of
57 // not hitting the goal. 2e-12 allows us to convert round trip with
58 // reasonable accuracy of six decimal places or more.
59 const threshold = 2e-12;
60 const max_attempts = 15;
61
62 let attempt = 0;
63 let last = Infinity;
64 let best = [0, 0, 0];
65
66 // Try to find a J such that the returned y matches the returned y of the L*
67 while (attempt <= max_attempts) {
68 xyz = fromCam16({ J: j, C: c, h: h }, env);
69
70 // If we are within range, return XYZ
71 // If we are closer than last time, save the values
72 const delta = Math.abs(xyz[1] - y);
73 if (delta < last) {
74 if (delta <= threshold) {
75 return xyz;
76 }
77 best = xyz;
78 last = delta;
79 }
80
81 // f(j_root) = (j ** (1 / 2)) * 0.1
82 // f(j) = ((f(j_root) * 100) ** 2) / j - 1 = 0
83 // f(j_root) = Y = y / 100
84 // f(j) = (y ** 2) / j - 1
85 // f'(j) = (2 * y) / j
86 j = j - ((xyz[1] - y) * j) / (2 * xyz[1]);
87
88 attempt += 1;
89 }
90
91 // We could not acquire the precision we desired,
92 // return our closest attempt.
93 return best;
94 }
95
96 function toHct (xyz, env) {
97 // Calculate HCT by taking the L* of CIE LCh D65 and CAM16 chroma and hue.
98
99 const t = toLstar(xyz[1]);
100 if (t === 0.0) {
101 return [0.0, 0.0, 0.0];
102 }
103 const cam16 = toCam16(xyz, viewingConditions);
104 return [constrain(cam16.h), cam16.C, t];
105 }
106
107 // Pre-calculate everything we can with the viewing conditions
108 export const viewingConditions = environment(
109 white,
110 (200 / Math.PI) * fromLstar(50.0),
111 fromLstar(50.0) * 100,
112 "average",
113 false,
114 );
115
116 // https://material.io/blog/science-of-color-design
117 // This is not a port of the material-color-utilities,
118 // but instead implements the full color space as described,
119 // combining CAM16 JCh and Lab D65. This does not clamp conversion
120 // to HCT to specific chroma bands and provides support for wider
121 // gamuts than Google currently supports and does so at a greater
122 // precision (> 8 bits back to sRGB).
123 // This implementation comes from https://github.com/facelessuser/coloraide
124 // which is licensed under MIT.
125 export default new ColorSpace({
126 id: "hct",
127 name: "HCT",
128 coords: {
129 h: {
130 refRange: [0, 360],
131 type: "angle",
132 name: "Hue",
133 },
134 c: {
135 refRange: [0, 145],
136 name: "Colorfulness",
137 },
138 t: {
139 refRange: [0, 100],
140 name: "Tone",
141 },
142 },
143
144 base: xyz_d65,
145
146 fromBase (xyz) {
147 if (this.ε === undefined) {
148 this.ε = Object.values(this.coords)[1].refRange[1] / 100000;
149 }
150 let hct = toHct(xyz, viewingConditions);
151 if (hct[1] < this.ε) {
152 hct[1] = 0.0;
153 hct[0] = null;
154 }
155 return hct;
156 },
157 toBase (hct) {
158 return fromHct(hct, viewingConditions);
159 },
160 formats: {
161 color: {
162 id: "--hct",
163 coords: ["<number> | <angle>", "<percentage> | <number>", "<percentage> | <number>"],
164 },
165 },
166 });