Debian Codegraph @

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

raw
1 /**
2 * Helmlab MetricSpace — 13-stage perceptual color space.
3 *
4 * A data-driven analytical color space fit on COMBVD (3,813 paired human
5 * color-difference judgments aggregating BFD-P, Witt 1999, RIT-DuPont, and
6 * Leeds). Used internally by `deltaEHelmlab` and exposed as the CSS
7 * color space `helmlab-metric` for inspectability. Its coordinate system
8 * (asymmetric ab plane, L > 1 from H-K modeling, achromatic axis offset
9 * from origin) is shaped for ΔE accuracy rather than author-facing
10 * coordinates — for general CSS authoring use HelmGen (`helmgen`).
11 *
12 * Measured in this branch: ~24% lower STRESS than CIEDE2000 on COMBVD.
13 *
14 * Pipeline: XYZ → M1 → γ → M2 → hue correction → H-K → cubic L → dark L
15 * → hue-dep chroma scale → chroma power → L-dep chroma scale
16 * → HLC interaction → hue-dep lightness → rotation
17 *
18 * Reference: arXiv:2602.23010
19 * @see https://github.com/Grkmyldz148/helmlab
20 */
21 import ColorSpace from "../ColorSpace.js";
22 import {multiply_v3_m3x3, spow, clamp} from "../util.js";
23 import XYZ_D65 from "./xyz-d65.js";
24
25 /** @import { Matrix3x3 } from "../types.js" */
26
27 const {cos, sin, sqrt, atan2, exp, abs, pow, PI} = Math;
28
29 // ── Bradford CAT: Color.js D65 ↔ Helmlab D65 ─────────────────────
30 // Color.js D65 = [0.3127/0.3290, 1, (1−0.3127−0.3290)/0.3290]
31 // Helmlab D65 = [0.95047, 1.0, 1.08883] (SPD-integrated, ASTM E308)
32
33 /** @type {number[]} */
34 export const HELMLAB_D65 = [0.95047, 1.0, 1.08883];
35
36 /** Bradford CAT: Color.js D65 → Helmlab D65 (apply in fromBase before M1) */
37 /** @type {Matrix3x3} */
38 // prettier-ignore
39 const CAT_TO_HELM = [
40 [1.000042977349746, 2.0718877053183e-05, -4.361018085669474e-05],
41 [2.6946201090235744e-05, 0.9999906145080147, -1.4898828405401079e-05],
42 [-7.941753620756204e-06, 1.2875204405137254e-05, 0.9997859822609763],
43 ];
44
45 /** Bradford CAT: Helmlab D65 → Color.js D65 (apply in toBase after M1_INV) */
46 /** @type {Matrix3x3} */
47 // prettier-ignore
48 const CAT_FROM_HELM = [
49 [0.9999570254019492, -2.071874272730964e-05, 4.361733292468361e-05],
50 [-2.694517763358666e-05, 1.000009385946497, 1.490098223546482e-05],
51 [7.943459292954202e-06, -1.287824596735154e-05, 1.000214063706999],
52 ];
53
54 // ── Utility functions ──────────────────────────────────────────────
55
56 // ── Core parameters (v21, 72 params) ──────────────────────────────
57
58 /** @type {Matrix3x3} */
59 // prettier-ignore
60 const M1 = [
61 [ 0.72129864331134985189, 0.45344826541531813024, -0.19288975751942616377],
62 [ -0.78821186949557897616, 1.79524137675723594043, 0.08761724511817850503],
63 [ -0.09177005999121559676, 0.45765588659459255361, 1.29220455139176770842],
64 ];
65 /** @type {Matrix3x3} */
66 // prettier-ignore
67 const M1_INV = [
68 [ 1.06510729580885898926, -0.31500440753011210715, 0.18034923817410389302],
69 [ 0.47211077138377960383, 0.42719957659624552271, 0.04150680489380984689],
70 [ -0.09156391926309541407, -0.17367093631949789834, 0.77197903825582936399],
71 ];
72
73 const GAMMA = [0.47229813098762524, 0.5149184096354483, 0.5113233386366979];
74 const INV_GAMMA = [2.1173067060606283, 1.9420552485353544, 1.9557096741686448];
75
76 /** @type {Matrix3x3} */
77 // prettier-ignore
78 const M2 = [
79 [ -0.26355622180094095963, 0.41683228837031738312, 0.49267631416564028335],
80 [ 1.88975705087773215851, -3.12122320342057735232, 1.04216669210603840590],
81 [ 0.35851086179620561545, 1.76940281937903676202, -1.41206260676953720967],
82 ];
83 /** @type {Matrix3x3} */
84 // prettier-ignore
85 const M2_INV = [
86 [ 0.91838978228150214278, 0.52320512370886662623, 0.70658045980908557038],
87 [ 1.08990905744330257576, 0.07005324849041903723, 0.43197768747870440853],
88 [ 1.59889572926420897581, 0.22061850068770233468, 0.01250603735522095097],
89 ];
90
91 /** Matrices used by this color space, also available as `Helmlab.M` */
92 export const M = { CAT_TO_HELM, CAT_FROM_HELM, M1, M1_INV, M2, M2_INV };
93
94 // Enrichment parameters
95 const hue_cos1 = -0.02833024015436984, hue_sin1 = -0.21131429516166544;
96 const hue_cos2 = 0.2189784817615645, hue_sin2 = -0.06871898981942523;
97 const hue_cos3 = 0.005506053349515315, hue_sin3 = -0.0641329861299175;
98 const hue_cos4 = -0.053592461436994296, hue_sin4 = -0.00954137464208059;
99
100 const hk_weight = 0.2676231133101982, hk_power = 0.8934892185255707;
101 const hk_hue_mod = 0.7173169828841472;
102 const hk_sin1 = 0.6915224124600773, hk_cos2 = 0.48647127559605596, hk_sin2 = 0.9853124591201782;
103
104 const L_corr_p1 = 0.5385456675962418, L_corr_p2 = 0.12508858146241716, L_corr_p3 = 0.6768950256217603;
105 const Lh_cos1 = -0.4963251525324449, Lh_sin1 = -0.09564696283240552;
106
107 const lp_dark = -0.029053748937210654, lp_dark_hcos = 1.3346761652952872, lp_dark_hsin = -0.1698908144723919;
108
109 const cs_cos1 = -0.195370576218515, cs_sin1 = 0.5330819227283227;
110 const cs_cos2 = 0.08863325582067766, cs_sin2 = 0.9365540137751136;
111 const cs_cos3 = 0.13789738139719568, cs_sin3 = 0.061650260197979936;
112 const cs_cos4 = 0.0641970862504494, cs_sin4 = -0.027401052793571013;
113
114 const cp_cos1 = -0.09900209889026965, cp_sin1 = 0.059635520647228726;
115 const cp_cos2 = -0.013586499967803128, cp_sin2 = 0.2253393118474472;
116
117 const lc1 = -1.5239477450767043, lc2 = -1.751157310240011;
118
119 const hlc_cos1 = -0.43576378069144767, hlc_sin1 = 1.060094063845983;
120 const hlc_cos2 = 0.47931193034584496, hlc_sin2 = -0.2622579649434462;
121
122 const hl_cos1 = 0.13610794232685908, hl_sin1 = 0.1168702235362288;
123 const hl_cos2 = -0.01617739641422492, hl_sin2 = 0.038145638815030566;
124
125 // Rigid rotation φ = −28.2°
126 const PHI = -28.2 * PI / 180;
127 const ROT_COS = cos(PHI);
128 const ROT_SIN = sin(PHI);
129
130 // ── Pipeline helper functions ──────────────────────────────────────
131
132 function hueDelta (h) {
133 return hue_cos1 * cos(h) + hue_sin1 * sin(h) +
134 hue_cos2 * cos(2 * h) + hue_sin2 * sin(2 * h) +
135 hue_cos3 * cos(3 * h) + hue_sin3 * sin(3 * h) +
136 hue_cos4 * cos(4 * h) + hue_sin4 * sin(4 * h);
137 }
138
139 function hueDeltaDeriv (h) {
140 return -hue_cos1 * sin(h) + hue_sin1 * cos(h) +
141 -2 * hue_cos2 * sin(2 * h) + 2 * hue_sin2 * cos(2 * h) +
142 -3 * hue_cos3 * sin(3 * h) + 3 * hue_sin3 * cos(3 * h) +
143 -4 * hue_cos4 * sin(4 * h) + 4 * hue_sin4 * cos(4 * h);
144 }
145
146 function chromaScaleH (h) {
147 let logS = cs_cos1 * cos(h) + cs_sin1 * sin(h) +
148 cs_cos2 * cos(2 * h) + cs_sin2 * sin(2 * h) +
149 cs_cos3 * cos(3 * h) + cs_sin3 * sin(3 * h) +
150 cs_cos4 * cos(4 * h) + cs_sin4 * sin(4 * h);
151 return exp(logS);
152 }
153
154 function lChromaScale (L) {
155 let dL = L - 0.5;
156 return exp(clamp(-30, lc1 * dL + lc2 * dL * dL, 30));
157 }
158
159 function hlcScale (h, L) {
160 let hueFactor = hlc_cos1 * cos(h) + hlc_sin1 * sin(h) +
161 hlc_cos2 * cos(2 * h) + hlc_sin2 * sin(2 * h);
162 return exp(clamp(-30, (L - 0.5) * hueFactor, 30));
163 }
164
165 function hueLightnessScale (h) {
166 let logS = hl_cos1 * cos(h) + hl_sin1 * sin(h) +
167 hl_cos2 * cos(2 * h) + hl_sin2 * sin(2 * h);
168 return exp(logS);
169 }
170
171 function chromaPowerH (h) {
172 return 1 + cp_cos1 * cos(h) + cp_sin1 * sin(h) +
173 cp_cos2 * cos(2 * h) + cp_sin2 * sin(2 * h);
174 }
175
176 function lCorrectFwd (L, h) {
177 let t = L * (1 - L);
178 let result = L + L_corr_p1 * t + L_corr_p2 * t * (0.5 - L) + L_corr_p3 * t * t;
179 result += t * (Lh_cos1 * cos(h) + Lh_sin1 * sin(h));
180 return result;
181 }
182
183 function lCorrectInv (L1, h) {
184 let Lh = Lh_cos1 * cos(h) + Lh_sin1 * sin(h);
185 let L = L1;
186 for (let i = 0; i < 15; i++) {
187 let t = L * (1 - L);
188 let dt = 1 - 2 * L;
189 let f = L + (L_corr_p1 + Lh) * t + L_corr_p2 * t * (0.5 - L) +
190 L_corr_p3 * t * t - L1;
191 let dfdL = 1 + (L_corr_p1 + Lh) * dt +
192 L_corr_p2 * (dt * (0.5 - L) - t) +
193 L_corr_p3 * 2 * t * dt;
194 if (abs(dfdL) < 1e-10) {
195 dfdL = 1;
196 }
197 L -= f / dfdL;
198 }
199 return L;
200 }
201
202 function darkLFwd (L, h) {
203 let coeff = lp_dark + lp_dark_hcos * cos(h) + lp_dark_hsin * sin(h);
204 let oml = L < 1 ? 1 - L : 0; // clamp at L=1: identity for L≥1
205 let g = coeff * L * oml * oml;
206 return L * exp(clamp(-30, g, 30));
207 }
208
209 function darkLInv (Ln, h) {
210 let coeff = lp_dark + lp_dark_hcos * cos(h) + lp_dark_hsin * sin(h);
211 let L = Ln;
212 for (let i = 0; i < 12; i++) {
213 let oml = L < 1 ? 1 - L : 0;
214 let g = coeff * L * oml * oml;
215 let eg = exp(clamp(-30, g, 30));
216 let f = L * eg - Ln;
217 let gp = coeff * oml * (1 - 3 * L);
218 let fp = eg * (1 + L * gp);
219 if (abs(fp) < 1e-10) {
220 fp = 1;
221 }
222 L -= f / fp;
223 }
224 return L;
225 }
226
227 // ── Color space definition ─────────────────────────────────────────
228 // Helmlab MetricSpace is the perceptual forward space behind `deltaEHelmlab`.
229 // Its coordinate system is shaped to maximize ΔE prediction accuracy on
230 // visual-difference datasets (COMBVD: BFD-P, Witt 1999, RIT-DuPont, Leeds),
231 // not author convenience. As a result:
232 // - the ab plane is asymmetric (the metric was fit to directionally
233 // asymmetric human discrimination thresholds);
234 // - the L axis exceeds 1 for highly chromatic colors because the
235 // Helmholtz–Kohlrausch boost is modeled explicitly (high-chroma colors
236 // appear brighter than equiluminant neutrals);
237 // - the achromatic axis is not pinned to (0, 0) — the gray axis sits
238 // slightly off-origin in this coordinate system. Distance is
239 // translation-invariant, so this does not affect ΔE quality.
240 //
241 // For CSS-author-facing color authoring (palettes, gradients, gamut
242 // mapping), use HelmGen (`helmgen`) instead. This space (`helmlab-metric`)
243 // is exposed primarily so the metric forward is inspectable for
244 // debugging, color-science exploration, and as the internal forward used
245 // by `deltaEHelmlab`.
246
247 export const fromXYZ = function (xyz) {
248 // Stage 0: Chromatic adaptation (Color.js D65 → Helmlab D65)
249 let adapted = multiply_v3_m3x3(xyz, M.CAT_TO_HELM);
250
251 // Stage 1: XYZ → LMS (M1)
252 let [lms0, lms1, lms2] = multiply_v3_m3x3(adapted, M.M1);
253
254 // Stage 2: Power compression (signed)
255 let c0 = spow(lms0, GAMMA[0]);
256 let c1 = spow(lms1, GAMMA[1]);
257 let c2 = spow(lms2, GAMMA[2]);
258
259 // Stage 3: LMS_c → Lab_raw (M2)
260 let [L, a, b] = multiply_v3_m3x3([c0, c1, c2], M.M2);
261
262 // Stage 3.5: Hue correction (4-harmonic Fourier)
263 let h = atan2(b, a);
264 let C = sqrt(a * a + b * b);
265 let delta = hueDelta(h);
266 let hNew = h + delta;
267 a = C * cos(hNew);
268 b = C * sin(hNew);
269
270 // Stage 3.7: Helmholtz-Kohlrausch correction
271 let Cr = sqrt(a * a + b * b);
272 let hkBoost = hk_weight * pow(Cr, clamp(0.01, hk_power, 10));
273 let hr = atan2(b, a);
274 let factor = 1 + hk_hue_mod * cos(hr) + hk_sin1 * sin(hr) +
275 hk_cos2 * cos(2 * hr) + hk_sin2 * sin(2 * hr);
276 L += hkBoost * factor;
277
278 // Stage 4: Cubic L correction (with hue modulation)
279 h = atan2(b, a);
280 L = lCorrectFwd(L, h);
281
282 // Stage 4.5: Dark L compression
283 h = atan2(b, a);
284 L = darkLFwd(L, h);
285
286 // Stage 5: Hue-dependent chroma scaling
287 h = atan2(b, a);
288 let cs = chromaScaleH(h);
289 a *= cs;
290 b *= cs;
291
292 // Stage 5.5: Nonlinear chroma power
293 h = atan2(b, a);
294 C = sqrt(a * a + b * b);
295 let p = chromaPowerH(h);
296 let Cn = C > 0 ? pow(C, p) : 0;
297 a = Cn * cos(h);
298 b = Cn * sin(h);
299
300 // Stage 6: L-dependent chroma scaling
301 let T = lChromaScale(L);
302 a *= T;
303 b *= T;
304
305 // Stage 6.5: HLC interaction
306 h = atan2(b, a);
307 let hlcS = hlcScale(h, L);
308 a *= hlcS;
309 b *= hlcS;
310
311 // Stage 8: Hue-dependent lightness scaling
312 h = atan2(b, a);
313 L *= hueLightnessScale(h);
314
315 // Stage 11: Rigid rotation (φ = −28.2°)
316 let aRot = a * ROT_COS - b * ROT_SIN;
317 let bRot = a * ROT_SIN + b * ROT_COS;
318
319 return [L, aRot, bRot];
320 };
321
322 export const toXYZ = function (lab) {
323 let [L, a, b] = lab;
324
325 // Undo Stage 11: rotation
326 let aUn = a * ROT_COS + b * ROT_SIN;
327 let bUn = -a * ROT_SIN + b * ROT_COS;
328 a = aUn;
329 b = bUn;
330
331 // Undo Stage 8: hue-dep lightness
332 let h = atan2(b, a);
333 L /= hueLightnessScale(h);
334
335 // Undo Stage 6.5: HLC
336 h = atan2(b, a);
337 let hlcS = hlcScale(h, L);
338 a /= hlcS;
339 b /= hlcS;
340
341 // Undo Stage 6: L-dep chroma
342 let T = lChromaScale(L);
343 a /= T;
344 b /= T;
345
346 // Undo Stage 5.5: chroma power
347 h = atan2(b, a);
348 let C = sqrt(a * a + b * b);
349 let p = chromaPowerH(h);
350 let Co = C > 0 ? pow(C, 1 / p) : 0;
351 a = Co * cos(h);
352 b = Co * sin(h);
353
354 // Undo Stage 5: chroma scaling
355 h = atan2(b, a);
356 let cs = chromaScaleH(h);
357 a /= cs;
358 b /= cs;
359
360 // Undo Stage 4.5: dark L
361 h = atan2(b, a);
362 L = darkLInv(L, h);
363
364 // Undo Stage 4: cubic L
365 h = atan2(b, a);
366 L = lCorrectInv(L, h);
367
368 // Undo Stage 3.7: H-K
369 let Cr = sqrt(a * a + b * b);
370 let hkBoost = hk_weight * pow(Cr, clamp(0.01, hk_power, 10));
371 let hr = atan2(b, a);
372 let factor = 1 + hk_hue_mod * cos(hr) + hk_sin1 * sin(hr) +
373 hk_cos2 * cos(2 * hr) + hk_sin2 * sin(2 * hr);
374 L -= hkBoost * factor;
375
376 // Undo Stage 3.5: hue correction (Newton iteration)
377 let hOut = atan2(b, a);
378 C = sqrt(a * a + b * b);
379 let hRaw = hOut;
380 for (let i = 0; i < 8; i++) {
381 let f = hRaw + hueDelta(hRaw) - hOut;
382 let fp = 1 + hueDeltaDeriv(hRaw);
383 if (abs(fp) < 1e-10) {
384 fp = 1;
385 }
386 hRaw -= f / fp;
387 }
388 a = C * cos(hRaw);
389 b = C * sin(hRaw);
390
391 // Undo Stage 3: Lab → LMS_c (M2_inv)
392 let [lc0, lc1, lc2] = multiply_v3_m3x3([L, a, b], M.M2_INV);
393
394 // Undo Stage 2: power compression
395 let l0 = spow(lc0, INV_GAMMA[0]);
396 let l1 = spow(lc1, INV_GAMMA[1]);
397 let l2 = spow(lc2, INV_GAMMA[2]);
398
399 // Undo Stage 1: LMS → XYZ (M1_inv)
400 let xyz = multiply_v3_m3x3([l0, l1, l2], M.M1_INV);
401
402 // Undo Stage 0: Chromatic adaptation (Helmlab D65 → Color.js D65)
403 return multiply_v3_m3x3(xyz, M.CAT_FROM_HELM);
404 };
405
406 export default new ColorSpace({
407 id: "helmlab-metric",
408 name: "Helmlab MetricSpace",
409 cssId: "--helmlab-metric",
410 coords: {
411 l: {
412 refRange: [0, 1.6],
413 name: "Lightness",
414 },
415 a: {
416 refRange: [-1.5, 1.5],
417 },
418 b: {
419 refRange: [-1.5, 1.5],
420 },
421 },
422 white: "D65",
423 base: XYZ_D65,
424 M,
425 fromBase (xyz) {
426 return fromXYZ(xyz);
427 },
428 toBase (lab) {
429 return toXYZ(lab);
430 },
431 });