Debian Codegraph @

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

raw
1 /**
2 * Helmlab GenSpace — generation-optimized color space for interpolation.
3 *
4 * Pipeline (v0.11.1, depressed cubic + chroma power + L-gated enrichment):
5 * XYZ → M1 → depcubic(α=0.021) → M2 → chroma_power(0.978) →
6 * PW_L → L-gated hue enrichment → Lab
7 *
8 * Optimized for perceptually uniform gradients, palette generation,
9 * and color-mix. 59-8 vs OKLab in head-to-head benchmarks (83 metrics,
10 * 3038 gradient pairs, sRGB/P3/Rec.2020). 360/360/360 cusps.
11 *
12 * Key properties:
13 * - Depressed cubic: y³ + αy = x, finite derivative at zero
14 * - Chroma power (0.978): improves gradient CV uniformity
15 * - L-gated hue enrichment: fixes blue→white purple shift
16 * - Structurally achromatic: a=b≈0 for all grays
17 *
18 * Note on blue-region gamut fold:
19 * All M1→f→M2 spaces (OKLab, IPT, HelmGen) exhibit a tiny non-contiguous
20 * gamut region near h≈260° in sRGB. This is a mathematical consequence of
21 * mixing LMS channels before applying a power-like transfer: the inverse
22 * creates a cubic polynomial that can have two positive roots in the blue
23 * region. OKLab has 46 such holes; HelmGen has 5, each ~0.001 chroma wide
24 * (sub-pixel, invisible). See: color.js#81, csswg-drafts#7071.
25 *
26 * Reference: arXiv:2602.23010
27 * @see https://github.com/Grkmyldz148/helmlab
28 */
29 import ColorSpace from "../ColorSpace.js";
30 import {multiply_v3_m3x3} from "../util.js";
31 import XYZ_D65 from "./xyz-d65.js";
32
33 /** @import { Matrix3x3 } from "../types.js" */
34
35 // ── Depressed cubic parameter ──────────────────────────────────────
36 const ALPHA = 0.021;
37 const S = Math.sqrt(ALPHA / 3);
38 const S3 = S * S * S;
39
40 // ── Chroma power ───────────────────────────────────────────────────
41 const CP = 0.978;
42
43 // ── L-gated hue enrichment parameters ──────────────────────────────
44 const ENR_AMP = 0.058;
45 const ENR_CENTER = 264.5 * Math.PI / 180; // radians
46 const ENR_SIGMA = 0.7;
47 const ENR_LLO = 0.37;
48 const ENR_LHI = 1.0;
49
50 // ── Core matrices (v0.11.1) ────────────────────────────────────────
51
52 /**
53 * Matrices used by this color space, also available as `HelmGen.M`
54 * @type {Record<string, Matrix3x3>}
55 */
56 // prettier-ignore
57 export const M = {
58 // M1 = M1_orig @ CAT_TO_HELM (Bradford CAT baked in: Color.js D65 → Helmlab D65 → LMS)
59 M1: [
60 [ 8.1548321559412884e-01, 3.6033406153856506e-01, -1.2434135574228214e-01],
61 [ 3.3010083527450780e-02, 9.2928650570661686e-01, 3.6121927165754429e-02],
62 [ 4.8188273564568611e-02, 2.6428415753384238e-01, 6.3349717841955344e-01],
63 ],
64 M1_INV: [
65 [ 1.2325947975032656e+00, -5.5575902392875232e-01, 2.7362015347715829e-01],
66 [-4.0801327874216024e-02, 1.1122288637386530e+00, -7.1427535864437813e-02],
67 [-7.6738259283377658e-02, -4.2172784577108585e-01, 1.5875238558143416e+00],
68 ],
69 M2: [
70 [ 0.21193779684470104, 0.7992121834263127, -0.00410075161564345],
71 [ 2.4672018828033475, -2.9877348024830788, 0.520532919679731],
72 [-0.11390787868068575, 1.3932982808117473, -1.279390402131062],
73 ],
74 M2_INV: [
75 [ 0.9930001151336143, 0.32599327253052285, 0.12945085631713921],
76 [ 0.9930001151336139, -0.08708353111074627, -0.03861361743004929],
77 [ 0.9930001151336136, -0.12386097008215022, -0.8351991365871061],
78 ],
79 };
80
81 // ── Piecewise-linear L correction (21 breakpoints, v0.11.1) ───────
82 // prettier-ignore
83 const PW_L_IN = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0];
84 // prettier-ignore
85 const PW_L_OUT = [0, 0.009494013522189627, 0.02564569838030986, 0.055259661658689105, 0.10574901531227408, 0.16055853320726027, 0.21405964892993756, 0.26786230508811226, 0.3220435246104499, 0.3739052098520243, 0.43020997780918835, 0.4835465162128873, 0.5399824670411353, 0.5956710081330342, 0.6542161666450478, 0.7115380216519989, 0.7702762412711669, 0.8293313467712837, 0.889406386197059, 0.9462829573474728, 1.0];
86 const PW_N = PW_L_IN.length;
87
88 // ── Depressed cubic: y³ + αy = x ──────────────────────────────────
89
90 function depcubicFwd (x) {
91 let t = x / (2 * S3);
92 let y = 2 * S * Math.sinh(Math.asinh(t) / 3);
93 // Halley refinement
94 let f = y * y * y + ALPHA * y - x;
95 let fp = 3 * y * y + ALPHA;
96 let fpp = 6 * y;
97 let denom = 2 * fp * fp - f * fpp;
98 if (Math.abs(denom) > 1e-30) {
99 y -= 2 * f * fp / denom;
100 }
101 return y;
102 }
103
104 function depcubicInv (y) {
105 return y * y * y + ALPHA * y;
106 }
107
108 // ── L-gated hue enrichment ─────────────────────────────────────────
109
110 function enrichGate (L) {
111 let t = Math.max(0, Math.min(1, (L - ENR_LLO) / (ENR_LHI - ENR_LLO)));
112 return Math.sin(Math.PI * t) ** 2;
113 }
114
115 function enrichFwd (L, a, b) {
116 let C = Math.sqrt(a * a + b * b);
117 if (C < 1e-12) {
118 return [a, b];
119 }
120 let gate = enrichGate(L);
121 if (gate < 1e-12) {
122 return [a, b];
123 }
124 let h = Math.atan2(b, a);
125 let dh = h - ENR_CENTER;
126 dh = dh - Math.round(dh / (2 * Math.PI)) * 2 * Math.PI;
127 let gauss = Math.exp(-0.5 * (dh / ENR_SIGMA) ** 2);
128 let hNew = h + ENR_AMP * gate * gauss;
129 return [C * Math.cos(hNew), C * Math.sin(hNew)];
130 }
131
132 function enrichInv (L, a, b) {
133 let C = Math.sqrt(a * a + b * b);
134 if (C < 1e-12) {
135 return [a, b];
136 }
137 let gate = enrichGate(L);
138 if (gate < 1e-12) {
139 return [a, b];
140 }
141 let hTarget = Math.atan2(b, a);
142 let sig2 = ENR_SIGMA * ENR_SIGMA;
143 let ag = ENR_AMP * gate;
144 let h = hTarget;
145 for (let i = 0; i < 8; i++) {
146 let dh = h - ENR_CENTER;
147 dh = dh - Math.round(dh / (2 * Math.PI)) * 2 * Math.PI;
148 let gauss = Math.exp(-0.5 * dh * dh / sig2);
149 let F = h + ag * gauss - hTarget;
150 let Fp = 1 + ag * gauss * (-dh / sig2);
151 let Fpp = ag * gauss * (-1 / sig2 + dh * dh / (sig2 * sig2));
152 let den = 2 * Fp * Fp - F * Fpp;
153 if (Math.abs(den) > 1e-30) {
154 h -= 2 * F * Fp / den;
155 }
156 }
157 return [C * Math.cos(h), C * Math.sin(h)];
158 }
159
160 // ── PW L correction ───────────────────────────────────────────────
161
162 function pwLForward (L) {
163 if (L <= 0 || L >= 1) {
164 return L;
165 }
166 let lo = 0, hi = PW_N - 1;
167 while (hi - lo > 1) {
168 let mid = (lo + hi) >> 1;
169 if (PW_L_IN[mid] <= L) {
170 lo = mid;
171 }
172 else {
173 hi = mid;
174 }
175 }
176 let t = (L - PW_L_IN[lo]) / (PW_L_IN[hi] - PW_L_IN[lo]);
177 return PW_L_OUT[lo] + t * (PW_L_OUT[hi] - PW_L_OUT[lo]);
178 }
179
180 function pwLInverse (L) {
181 if (L <= PW_L_OUT[0] || L >= PW_L_OUT[PW_N - 1]) {
182 return L;
183 }
184 let lo = 0, hi = PW_N - 1;
185 while (hi - lo > 1) {
186 let mid = (lo + hi) >> 1;
187 if (PW_L_OUT[mid] <= L) {
188 lo = mid;
189 }
190 else {
191 hi = mid;
192 }
193 }
194 let t = (L - PW_L_OUT[lo]) / (PW_L_OUT[hi] - PW_L_OUT[lo]);
195 return PW_L_IN[lo] + t * (PW_L_IN[hi] - PW_L_IN[lo]);
196 }
197
198 // ── Color space definition ─────────────────────────────────────────
199
200 export default new ColorSpace({
201 id: "helmgen",
202 name: "HelmGen",
203 cssId: "--helmgen",
204 coords: {
205 l: {
206 refRange: [0, 1],
207 name: "Lightness",
208 },
209 a: {
210 refRange: [-0.6, 0.6],
211 },
212 b: {
213 refRange: [-0.6, 0.6],
214 },
215 },
216 white: "D65",
217 base: XYZ_D65,
218
219 M,
220
221 fromBase (xyz) {
222 // Stage 1: XYZ → LMS (M1)
223 let lms = multiply_v3_m3x3(xyz, M.M1);
224
225 // Stage 2: Depressed cubic transfer (y³ + αy = x)
226 let c0 = depcubicFwd(Math.max(lms[0], 0));
227 let c1 = depcubicFwd(Math.max(lms[1], 0));
228 let c2 = depcubicFwd(Math.max(lms[2], 0));
229
230 // Stage 2.5: Smooth neutral blend (C∞ correction for achromatic precision)
231 {
232 let mean = (c0 + c1 + c2) / 3;
233 let mx = Math.max(c0, c1, c2);
234 let mn = Math.min(c0, c1, c2);
235 let spread = (mx - mn) / Math.max(Math.abs(mean), 1e-30);
236 let w = Math.exp(-((spread / 1e-5) ** 2));
237 c0 += w * (mean - c0);
238 c1 += w * (mean - c1);
239 c2 += w * (mean - c2);
240 }
241
242 // Stage 3: LMS_c → Lab (M2)
243 let [L, a, b] = multiply_v3_m3x3([c0, c1, c2], M.M2);
244
245 // Stage 3.5: Chroma power (cp=0.978)
246 {
247 let C = Math.sqrt(a * a + b * b);
248 if (C > 1e-12) {
249 let Cnew = Math.pow(C, CP);
250 let s = Cnew / C;
251 a *= s;
252 b *= s;
253 }
254 }
255
256 // Stage 4: Piecewise-linear L correction
257 L = pwLForward(L);
258
259 // Stage 5: L-gated hue enrichment
260 [a, b] = enrichFwd(L, a, b);
261
262 return [L, a, b];
263 },
264
265 toBase (lab) {
266 let [L, a, b] = lab;
267
268 // Undo Stage 5: L-gated hue enrichment
269 [a, b] = enrichInv(L, a, b);
270
271 // Undo Stage 4: PW L correction
272 L = pwLInverse(L);
273
274 // Undo Stage 3.5: Chroma power inverse (C^(1/cp))
275 {
276 let C = Math.sqrt(a * a + b * b);
277 if (C > 1e-12) {
278 let Corig = Math.pow(C, 1 / CP);
279 let s = Corig / C;
280 a *= s;
281 b *= s;
282 }
283 }
284
285 // Undo Stage 3: Lab → LMS_c (M2_inv)
286 let [lc0, lc1, lc2] = multiply_v3_m3x3([L, a, b], M.M2_INV);
287
288 // Undo Stage 2.5: Smooth neutral blend
289 {
290 let mean = (lc0 + lc1 + lc2) / 3;
291 let mx = Math.max(lc0, lc1, lc2);
292 let mn = Math.min(lc0, lc1, lc2);
293 let spread = (mx - mn) / Math.max(Math.abs(mean), 1e-30);
294 let w = Math.exp(-((spread / 1e-5) ** 2));
295 lc0 += w * (mean - lc0);
296 lc1 += w * (mean - lc1);
297 lc2 += w * (mean - lc2);
298 }
299
300 // Undo Stage 2: Inverse depressed cubic (x = y³ + αy)
301 let l0 = depcubicInv(lc0);
302 let l1 = depcubicInv(lc1);
303 let l2 = depcubicInv(lc2);
304
305 // Undo Stage 1: LMS → XYZ (M1_inv)
306 return multiply_v3_m3x3([l0, l1, l2], M.M1_INV);
307 },
308 });