@tenphi/glaze 0.0.0-snapshot.7f7cab2 → 0.0.0-snapshot.875bb2f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -1094
- package/dist/index.cjs +3145 -939
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1163 -104
- package/dist/index.d.mts +1163 -104
- package/dist/index.mjs +3114 -939
- package/dist/index.mjs.map +1 -1
- package/docs/api.md +1679 -0
- package/docs/methodology.md +636 -0
- package/docs/migration.md +315 -0
- package/docs/okhst.md +170 -0
- package/package.json +9 -9
package/dist/index.cjs
CHANGED
|
@@ -98,7 +98,12 @@ const K2 = .03;
|
|
|
98
98
|
const K3 = (1 + K1) / (1 + K2);
|
|
99
99
|
const EPSILON = 1e-10;
|
|
100
100
|
const constrainAngle = (angle) => (angle % 360 + 360) % 360;
|
|
101
|
+
/**
|
|
102
|
+
* OKHSL toe function: maps OKLab lightness L to perceptual lightness l.
|
|
103
|
+
* Exported for the OKHST tone transfers in `okhst.ts`.
|
|
104
|
+
*/
|
|
101
105
|
const toe = (x) => .5 * (K3 * x - K1 + Math.sqrt((K3 * x - K1) * (K3 * x - K1) + 4 * K2 * K3 * x));
|
|
106
|
+
/** Inverse OKHSL toe: maps perceptual lightness l back to OKLab lightness L. */
|
|
102
107
|
const toeInv = (x) => (x ** 2 + K1 * x) / (K3 * (x + K2));
|
|
103
108
|
const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
104
109
|
const dotXY = (a, b) => a[0] * b[0] + a[1] * b[1];
|
|
@@ -253,10 +258,48 @@ const getCs = (L, a, b, cusp) => {
|
|
|
253
258
|
cMax
|
|
254
259
|
];
|
|
255
260
|
};
|
|
261
|
+
const CYAN_A = Math.cos(199.8 * Math.PI / 180);
|
|
262
|
+
const CYAN_B = Math.sin(199.8 * Math.PI / 180);
|
|
263
|
+
const BLUE_A = Math.cos(267.4 * Math.PI / 180);
|
|
264
|
+
const BLUE_B = Math.sin(267.4 * Math.PI / 180);
|
|
265
|
+
let cyanCusp;
|
|
266
|
+
let blueCusp;
|
|
267
|
+
/**
|
|
268
|
+
* Computes the maximum safe OKLCH chroma that fits inside the sRGB gamut
|
|
269
|
+
* for all possible hues at a given OKLab lightness `L`.
|
|
270
|
+
*/
|
|
271
|
+
function computeSafeChromaOKLCH(L) {
|
|
272
|
+
if (!cyanCusp) cyanCusp = findCuspOKLCH(CYAN_A, CYAN_B);
|
|
273
|
+
if (!blueCusp) blueCusp = findCuspOKLCH(BLUE_A, BLUE_B);
|
|
274
|
+
const c1 = findGamutIntersectionOKLCH(CYAN_A, CYAN_B, L, 1, L, cyanCusp);
|
|
275
|
+
const c2 = findGamutIntersectionOKLCH(BLUE_A, BLUE_B, L, 1, L, blueCusp);
|
|
276
|
+
return Math.min(c1, c2);
|
|
277
|
+
}
|
|
278
|
+
/** Per-hue cusp-lightness cache. The cusp is mode-independent, so keying on
|
|
279
|
+
* a rounded hue is safe and keeps the cache small. */
|
|
280
|
+
const cuspLightnessCache = /* @__PURE__ */ new Map();
|
|
281
|
+
/**
|
|
282
|
+
* OKHSL lightness of the gamut cusp for a hue — the lightness where the
|
|
283
|
+
* realizable chroma peaks. Reuses the same `find_cusp` OKHSL already runs for
|
|
284
|
+
* its `s` normalization (no new color math); the OKLab cusp lightness is run
|
|
285
|
+
* through the OKHSL `toe` and clamped to `[0.001, 0.999]` so divisions that
|
|
286
|
+
* key off it stay safe. Cached per (rounded) hue.
|
|
287
|
+
*
|
|
288
|
+
* @param h Hue, 0–360.
|
|
289
|
+
*/
|
|
290
|
+
function cuspLightness(h) {
|
|
291
|
+
const key = Math.round(constrainAngle(h) * 100) / 100;
|
|
292
|
+
const cached = cuspLightnessCache.get(key);
|
|
293
|
+
if (cached !== void 0) return cached;
|
|
294
|
+
const hNorm = key / 360;
|
|
295
|
+
const lc = clampVal(toe(findCuspOKLCH(Math.cos(TAU * hNorm), Math.sin(TAU * hNorm))[0]), .001, .999);
|
|
296
|
+
cuspLightnessCache.set(key, lc);
|
|
297
|
+
return lc;
|
|
298
|
+
}
|
|
256
299
|
/**
|
|
257
300
|
* Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLab [L, a, b].
|
|
258
301
|
*/
|
|
259
|
-
function okhslToOklab(h, s, l) {
|
|
302
|
+
function okhslToOklab(h, s, l, pastel = false) {
|
|
260
303
|
const L = toeInv(l);
|
|
261
304
|
let a = 0;
|
|
262
305
|
let b = 0;
|
|
@@ -264,24 +307,30 @@ function okhslToOklab(h, s, l) {
|
|
|
264
307
|
if (L !== 0 && L !== 1 && s !== 0) {
|
|
265
308
|
const a_ = Math.cos(TAU * hNorm);
|
|
266
309
|
const b_ = Math.sin(TAU * hNorm);
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (s < mid) {
|
|
272
|
-
t = midInv * s;
|
|
273
|
-
k0 = 0;
|
|
274
|
-
k1 = mid * c0;
|
|
275
|
-
k2 = 1 - k1 / cMid;
|
|
310
|
+
if (pastel) {
|
|
311
|
+
const c = s * computeSafeChromaOKLCH(L);
|
|
312
|
+
a = c * a_;
|
|
313
|
+
b = c * b_;
|
|
276
314
|
} else {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
315
|
+
const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
|
|
316
|
+
const mid = .8;
|
|
317
|
+
const midInv = 1.25;
|
|
318
|
+
let t, k0, k1, k2;
|
|
319
|
+
if (s < mid) {
|
|
320
|
+
t = midInv * s;
|
|
321
|
+
k0 = 0;
|
|
322
|
+
k1 = mid * c0;
|
|
323
|
+
k2 = 1 - k1 / cMid;
|
|
324
|
+
} else {
|
|
325
|
+
t = 5 * (s - .8);
|
|
326
|
+
k0 = cMid;
|
|
327
|
+
k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
|
|
328
|
+
k2 = 1 - k1 / (cMax - cMid);
|
|
329
|
+
}
|
|
330
|
+
const c = k0 + t * k1 / (1 - k2 * t);
|
|
331
|
+
a = c * a_;
|
|
332
|
+
b = c * b_;
|
|
281
333
|
}
|
|
282
|
-
const c = k0 + t * k1 / (1 - k2 * t);
|
|
283
|
-
a = c * a_;
|
|
284
|
-
b = c * b_;
|
|
285
334
|
}
|
|
286
335
|
return [
|
|
287
336
|
L,
|
|
@@ -293,8 +342,8 @@ function okhslToOklab(h, s, l) {
|
|
|
293
342
|
* Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to linear sRGB.
|
|
294
343
|
* Channels may exceed [0, 1] near gamut boundaries — caller must clamp if needed.
|
|
295
344
|
*/
|
|
296
|
-
function okhslToLinearSrgb(h, s, l) {
|
|
297
|
-
return OKLabToLinearSRGB(okhslToOklab(h, s, l));
|
|
345
|
+
function okhslToLinearSrgb(h, s, l, pastel = false) {
|
|
346
|
+
return OKLabToLinearSRGB(okhslToOklab(h, s, l, pastel));
|
|
298
347
|
}
|
|
299
348
|
/**
|
|
300
349
|
* Compute relative luminance Y from linear sRGB channels.
|
|
@@ -324,8 +373,8 @@ const sRGBGammaToLinear = (val) => {
|
|
|
324
373
|
/**
|
|
325
374
|
* Convert OKHSL to gamma-encoded sRGB (clamped to 0–1).
|
|
326
375
|
*/
|
|
327
|
-
function okhslToSrgb(h, s, l) {
|
|
328
|
-
const lin = okhslToLinearSrgb(h, s, l);
|
|
376
|
+
function okhslToSrgb(h, s, l, pastel = false) {
|
|
377
|
+
const lin = okhslToLinearSrgb(h, s, l, pastel);
|
|
329
378
|
return [
|
|
330
379
|
Math.max(0, Math.min(1, sRGBLinearToGamma(lin[0]))),
|
|
331
380
|
Math.max(0, Math.min(1, sRGBLinearToGamma(lin[1]))),
|
|
@@ -343,10 +392,31 @@ function gamutClampedLuminance(linearRgb) {
|
|
|
343
392
|
const b = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2]))));
|
|
344
393
|
return .2126 * r + .7152 * g + .0722 * b;
|
|
345
394
|
}
|
|
395
|
+
/**
|
|
396
|
+
* Compute APCA screen luminance (`Ys`) from linear sRGB.
|
|
397
|
+
*
|
|
398
|
+
* APCA does not use the WCAG piecewise sRGB EOTF; it defines its own
|
|
399
|
+
* luminance as `0.2126·R^2.4 + 0.7152·G^2.4 + 0.0722·B^2.4` over the
|
|
400
|
+
* gamma-encoded (display) channels with a simple 2.4 exponent. The APCA
|
|
401
|
+
* soft-clamp threshold in `apcaContrast` is calibrated against this basis,
|
|
402
|
+
* so the solver must feed it `Ys`, not WCAG relative luminance. Channels
|
|
403
|
+
* are gamut-clamped to [0, 1] first, matching `gamutClampedLuminance`.
|
|
404
|
+
*/
|
|
405
|
+
function apcaLuminanceFromLinearRgb(linearRgb) {
|
|
406
|
+
const r = Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0])));
|
|
407
|
+
const g = Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1])));
|
|
408
|
+
const b = Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2])));
|
|
409
|
+
return .2126 * Math.pow(r, 2.4) + .7152 * Math.pow(g, 2.4) + .0722 * Math.pow(b, 2.4);
|
|
410
|
+
}
|
|
346
411
|
const linearSrgbToOklab = (rgb) => {
|
|
347
412
|
return transform(cbrt3(transform(rgb, linear_sRGB_to_LMS_M)), LMS_to_OKLab_M);
|
|
348
413
|
};
|
|
349
|
-
|
|
414
|
+
/**
|
|
415
|
+
* Convert OKLab to OKHSL.
|
|
416
|
+
* Input: [L, a, b] where L: 0–1, a/b: roughly -0.5 to 0.5.
|
|
417
|
+
* Returns [h, s, l] where h: 0–360, s: 0–1, l: 0–1.
|
|
418
|
+
*/
|
|
419
|
+
const oklabToOkhsl = (lab, pastel = false) => {
|
|
350
420
|
const L = lab[0];
|
|
351
421
|
const a = lab[1];
|
|
352
422
|
const b = lab[2];
|
|
@@ -356,23 +426,32 @@ const oklabToOkhsl = (lab) => {
|
|
|
356
426
|
0,
|
|
357
427
|
toe(L)
|
|
358
428
|
];
|
|
429
|
+
const L_EXTREME_EPSILON = 1e-6;
|
|
430
|
+
if (L >= 1 - L_EXTREME_EPSILON || L <= L_EXTREME_EPSILON) return [
|
|
431
|
+
0,
|
|
432
|
+
0,
|
|
433
|
+
toe(L)
|
|
434
|
+
];
|
|
359
435
|
const a_ = a / C;
|
|
360
436
|
const b_ = b / C;
|
|
361
437
|
let h = Math.atan2(b, a) * (180 / Math.PI);
|
|
362
438
|
h = constrainAngle(h);
|
|
363
|
-
const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
|
|
364
|
-
const mid = .8;
|
|
365
|
-
const midInv = 1.25;
|
|
366
439
|
let s;
|
|
367
|
-
if (C
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
440
|
+
if (pastel) s = C / computeSafeChromaOKLCH(L);
|
|
441
|
+
else {
|
|
442
|
+
const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
|
|
443
|
+
const mid = .8;
|
|
444
|
+
const midInv = 1.25;
|
|
445
|
+
if (C < cMid) {
|
|
446
|
+
const k1 = mid * c0;
|
|
447
|
+
s = C / (k1 + C * (1 - k1 / cMid)) / midInv;
|
|
448
|
+
} else {
|
|
449
|
+
const k0 = cMid;
|
|
450
|
+
const k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
|
|
451
|
+
const k2 = 1 - k1 / (cMax - cMid);
|
|
452
|
+
const cDiff = C - k0;
|
|
453
|
+
s = mid + cDiff / (k1 + cDiff * k2) / 5;
|
|
454
|
+
}
|
|
376
455
|
}
|
|
377
456
|
const l = toe(L);
|
|
378
457
|
return [
|
|
@@ -385,40 +464,116 @@ const oklabToOkhsl = (lab) => {
|
|
|
385
464
|
* Convert gamma-encoded sRGB (0–1 per channel) to OKHSL.
|
|
386
465
|
* Returns [h, s, l] where h: 0–360, s: 0–1, l: 0–1.
|
|
387
466
|
*/
|
|
388
|
-
function srgbToOkhsl(rgb) {
|
|
467
|
+
function srgbToOkhsl(rgb, pastel = false) {
|
|
389
468
|
return oklabToOkhsl(linearSrgbToOklab([
|
|
390
469
|
sRGBGammaToLinear(rgb[0]),
|
|
391
470
|
sRGBGammaToLinear(rgb[1]),
|
|
392
471
|
sRGBGammaToLinear(rgb[2])
|
|
393
|
-
]));
|
|
472
|
+
]), pastel);
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Convert CSS HSL (sRGB-based) to gamma-encoded sRGB [r, g, b] in 0–1 range.
|
|
476
|
+
* h: 0–360, s: 0–1, l: 0–1.
|
|
477
|
+
*
|
|
478
|
+
* Note: CSS HSL is not the same as OKHSL — it's HSL in the sRGB color space.
|
|
479
|
+
* Use this when parsing `hsl(...)` strings before passing to `srgbToOkhsl`.
|
|
480
|
+
*/
|
|
481
|
+
function hslToSrgb(h, s, l) {
|
|
482
|
+
const hh = (h % 360 + 360) % 360 / 360;
|
|
483
|
+
const ss = clampVal(s, 0, 1);
|
|
484
|
+
const ll = clampVal(l, 0, 1);
|
|
485
|
+
if (ss === 0) return [
|
|
486
|
+
ll,
|
|
487
|
+
ll,
|
|
488
|
+
ll
|
|
489
|
+
];
|
|
490
|
+
const q = ll < .5 ? ll * (1 + ss) : ll + ss - ll * ss;
|
|
491
|
+
const p = 2 * ll - q;
|
|
492
|
+
const hueToChannel = (t) => {
|
|
493
|
+
let tt = t;
|
|
494
|
+
if (tt < 0) tt += 1;
|
|
495
|
+
if (tt > 1) tt -= 1;
|
|
496
|
+
if (tt < 1 / 6) return p + (q - p) * 6 * tt;
|
|
497
|
+
if (tt < 1 / 2) return q;
|
|
498
|
+
if (tt < 2 / 3) return p + (q - p) * (2 / 3 - tt) * 6;
|
|
499
|
+
return p;
|
|
500
|
+
};
|
|
501
|
+
return [
|
|
502
|
+
hueToChannel(hh + 1 / 3),
|
|
503
|
+
hueToChannel(hh),
|
|
504
|
+
hueToChannel(hh - 1 / 3)
|
|
505
|
+
];
|
|
394
506
|
}
|
|
395
507
|
/**
|
|
396
508
|
* Parse a hex color string (#rgb or #rrggbb) to sRGB [r, g, b] in 0–1 range.
|
|
397
509
|
* Returns null if the string is not a valid hex color.
|
|
510
|
+
*
|
|
511
|
+
* For 8-digit hex (`#rrggbbaa`) and 4-digit hex (`#rgba`) with alpha,
|
|
512
|
+
* use {@link parseHexAlpha}.
|
|
398
513
|
*/
|
|
399
514
|
function parseHex(hex) {
|
|
515
|
+
const result = parseHexAlpha(hex);
|
|
516
|
+
if (!result || result.alpha !== void 0) return null;
|
|
517
|
+
return result.rgb;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Parse a hex color string (#rgb, #rrggbb, #rgba, or #rrggbbaa) to
|
|
521
|
+
* sRGB [r, g, b] in 0–1 range plus an optional alpha (0–1).
|
|
522
|
+
* Returns null if the string is not a valid hex color.
|
|
523
|
+
*/
|
|
524
|
+
function parseHexAlpha(hex) {
|
|
400
525
|
const h = hex.startsWith("#") ? hex.slice(1) : hex;
|
|
401
526
|
if (h.length === 3) {
|
|
402
527
|
const r = parseInt(h[0] + h[0], 16);
|
|
403
528
|
const g = parseInt(h[1] + h[1], 16);
|
|
404
529
|
const b = parseInt(h[2] + h[2], 16);
|
|
405
530
|
if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
|
|
406
|
-
return [
|
|
531
|
+
return { rgb: [
|
|
407
532
|
r / 255,
|
|
408
533
|
g / 255,
|
|
409
534
|
b / 255
|
|
410
|
-
];
|
|
535
|
+
] };
|
|
536
|
+
}
|
|
537
|
+
if (h.length === 4) {
|
|
538
|
+
const r = parseInt(h[0] + h[0], 16);
|
|
539
|
+
const g = parseInt(h[1] + h[1], 16);
|
|
540
|
+
const b = parseInt(h[2] + h[2], 16);
|
|
541
|
+
const a = parseInt(h[3] + h[3], 16);
|
|
542
|
+
if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return null;
|
|
543
|
+
return {
|
|
544
|
+
rgb: [
|
|
545
|
+
r / 255,
|
|
546
|
+
g / 255,
|
|
547
|
+
b / 255
|
|
548
|
+
],
|
|
549
|
+
alpha: a / 255
|
|
550
|
+
};
|
|
411
551
|
}
|
|
412
552
|
if (h.length === 6) {
|
|
413
553
|
const r = parseInt(h.slice(0, 2), 16);
|
|
414
554
|
const g = parseInt(h.slice(2, 4), 16);
|
|
415
555
|
const b = parseInt(h.slice(4, 6), 16);
|
|
416
556
|
if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
|
|
417
|
-
return [
|
|
557
|
+
return { rgb: [
|
|
418
558
|
r / 255,
|
|
419
559
|
g / 255,
|
|
420
560
|
b / 255
|
|
421
|
-
];
|
|
561
|
+
] };
|
|
562
|
+
}
|
|
563
|
+
if (h.length === 8) {
|
|
564
|
+
const r = parseInt(h.slice(0, 2), 16);
|
|
565
|
+
const g = parseInt(h.slice(2, 4), 16);
|
|
566
|
+
const b = parseInt(h.slice(4, 6), 16);
|
|
567
|
+
const a = parseInt(h.slice(6, 8), 16);
|
|
568
|
+
if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return null;
|
|
569
|
+
return {
|
|
570
|
+
rgb: [
|
|
571
|
+
r / 255,
|
|
572
|
+
g / 255,
|
|
573
|
+
b / 255
|
|
574
|
+
],
|
|
575
|
+
alpha: a / 255
|
|
576
|
+
};
|
|
422
577
|
}
|
|
423
578
|
return null;
|
|
424
579
|
}
|
|
@@ -429,24 +584,44 @@ function fmt$1(value, decimals) {
|
|
|
429
584
|
* Format OKHSL values as a CSS `okhsl(H S% L%)` string.
|
|
430
585
|
* h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
|
|
431
586
|
*/
|
|
432
|
-
function formatOkhsl(h, s, l) {
|
|
433
|
-
|
|
587
|
+
function formatOkhsl(h, s, l, pastel = false) {
|
|
588
|
+
let outS = s;
|
|
589
|
+
if (pastel) outS = oklabToOkhsl(okhslToOklab(h, s / 100, l / 100, true), false)[1] * 100;
|
|
590
|
+
return `okhsl(${fmt$1(h, 2)} ${fmt$1(outS, 2)}% ${fmt$1(l, 2)}%)`;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Format OKHST values as a CSS `okhst(H S% T%)` string.
|
|
594
|
+
* h: 0–360, s: 0–100, t: 0–100 (percentage scale for s and t).
|
|
595
|
+
*
|
|
596
|
+
* Pastel recompute matches `formatOkhsl`: convert via OKLab so external
|
|
597
|
+
* parsers that only understand non-pastel OKHST render identically.
|
|
598
|
+
*/
|
|
599
|
+
function formatOkhst(h, s, t, pastel = false) {
|
|
600
|
+
let outS = s;
|
|
601
|
+
if (pastel) {
|
|
602
|
+
const REF_EPS = .05;
|
|
603
|
+
const den = Math.log(1 + REF_EPS) - Math.log(REF_EPS);
|
|
604
|
+
const y = Math.exp(t / 100 * den + Math.log(REF_EPS)) - REF_EPS;
|
|
605
|
+
const l = toe(Math.cbrt(Math.max(0, y)));
|
|
606
|
+
outS = oklabToOkhsl(okhslToOklab(h, s / 100, l, true), false)[1] * 100;
|
|
607
|
+
}
|
|
608
|
+
return `okhst(${fmt$1(h, 2)} ${fmt$1(outS, 2)}% ${fmt$1(t, 2)}%)`;
|
|
434
609
|
}
|
|
435
610
|
/**
|
|
436
611
|
* Format OKHSL values as a CSS `rgb(R G B)` string.
|
|
437
612
|
* Uses 2 decimal places to avoid 8-bit quantization contrast loss.
|
|
438
613
|
* h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
|
|
439
614
|
*/
|
|
440
|
-
function formatRgb(h, s, l) {
|
|
441
|
-
const [r, g, b] = okhslToSrgb(h, s / 100, l / 100);
|
|
615
|
+
function formatRgb(h, s, l, pastel = false) {
|
|
616
|
+
const [r, g, b] = okhslToSrgb(h, s / 100, l / 100, pastel);
|
|
442
617
|
return `rgb(${parseFloat((r * 255).toFixed(2))} ${parseFloat((g * 255).toFixed(2))} ${parseFloat((b * 255).toFixed(2))})`;
|
|
443
618
|
}
|
|
444
619
|
/**
|
|
445
620
|
* Format OKHSL values as a CSS `hsl(H S% L%)` string.
|
|
446
621
|
* h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
|
|
447
622
|
*/
|
|
448
|
-
function formatHsl(h, s, l) {
|
|
449
|
-
const [r, g, b] = okhslToSrgb(h, s / 100, l / 100);
|
|
623
|
+
function formatHsl(h, s, l, pastel = false) {
|
|
624
|
+
const [r, g, b] = okhslToSrgb(h, s / 100, l / 100, pastel);
|
|
450
625
|
const max = Math.max(r, g, b);
|
|
451
626
|
const min = Math.min(r, g, b);
|
|
452
627
|
const delta = max - min;
|
|
@@ -465,392 +640,1047 @@ function formatHsl(h, s, l) {
|
|
|
465
640
|
* Format OKHSL values as a CSS `oklch(L C H)` string.
|
|
466
641
|
* h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
|
|
467
642
|
*/
|
|
468
|
-
function formatOklch(h, s, l) {
|
|
469
|
-
const [L,
|
|
470
|
-
const C = Math.sqrt(a * a + b * b);
|
|
471
|
-
let hh = Math.atan2(b, a) * (180 / Math.PI);
|
|
472
|
-
hh = constrainAngle(hh);
|
|
643
|
+
function formatOklch(h, s, l, pastel = false) {
|
|
644
|
+
const [L, C, hh] = okhslToOklch(h, s / 100, l / 100, pastel);
|
|
473
645
|
return `oklch(${fmt$1(L, 4)} ${fmt$1(C, 4)} ${fmt$1(hh, 2)})`;
|
|
474
646
|
}
|
|
647
|
+
/**
|
|
648
|
+
* Convert gamma-encoded sRGB channels (0–1) to a 6-digit lowercase hex
|
|
649
|
+
* string (`#rrggbb`). Channels are clamped to [0,1] and rounded to 8-bit.
|
|
650
|
+
* Alpha is not encoded here — DTCG carries it as a separate `alpha` field.
|
|
651
|
+
*/
|
|
652
|
+
function srgbToHex(rgb) {
|
|
653
|
+
const toByte = (c) => Math.max(0, Math.min(255, Math.round(c * 255)));
|
|
654
|
+
const r = toByte(rgb[0]);
|
|
655
|
+
const g = toByte(rgb[1]);
|
|
656
|
+
const b = toByte(rgb[2]);
|
|
657
|
+
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLCH components `[L, C, H]`.
|
|
661
|
+
* L: 0–1, C: 0–~0.4 (chroma), H: 0–360 (hue). Shared by `formatOklch` and
|
|
662
|
+
* the DTCG `oklch` colorSpace exporter so the two never drift apart.
|
|
663
|
+
*/
|
|
664
|
+
function okhslToOklch(h, s, l, pastel = false) {
|
|
665
|
+
const [L, a, b] = okhslToOklab(h, s, l, pastel);
|
|
666
|
+
return [
|
|
667
|
+
L,
|
|
668
|
+
Math.sqrt(a * a + b * b),
|
|
669
|
+
constrainAngle(Math.atan2(b, a) * (180 / Math.PI))
|
|
670
|
+
];
|
|
671
|
+
}
|
|
475
672
|
|
|
476
673
|
//#endregion
|
|
477
|
-
//#region src/
|
|
674
|
+
//#region src/config.ts
|
|
478
675
|
/**
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
* Finds the closest OKHSL lightness that satisfies a WCAG 2 contrast target
|
|
482
|
-
* against a base color. Used by glaze when resolving dependent colors
|
|
483
|
-
* with `contrast`.
|
|
676
|
+
* Build a fresh defaults object. Called from module init and from
|
|
677
|
+
* `resetConfig()` so the two paths can't drift.
|
|
484
678
|
*/
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
679
|
+
function defaultConfig() {
|
|
680
|
+
return {
|
|
681
|
+
lightTone: {
|
|
682
|
+
lo: 10,
|
|
683
|
+
hi: 100,
|
|
684
|
+
eps: .05
|
|
685
|
+
},
|
|
686
|
+
darkTone: {
|
|
687
|
+
lo: 15,
|
|
688
|
+
hi: 95,
|
|
689
|
+
eps: .05
|
|
690
|
+
},
|
|
691
|
+
darkDesaturation: .1,
|
|
692
|
+
states: {
|
|
693
|
+
dark: "@media(prefers-color-scheme: dark)",
|
|
694
|
+
highContrast: "@media(prefers-contrast: more)"
|
|
695
|
+
},
|
|
696
|
+
modes: {
|
|
697
|
+
dark: true,
|
|
698
|
+
highContrast: false
|
|
699
|
+
},
|
|
700
|
+
autoFlip: true,
|
|
701
|
+
pastel: false,
|
|
702
|
+
inferRole: true
|
|
703
|
+
};
|
|
494
704
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
luminanceCache.set(key, y);
|
|
509
|
-
cacheOrder.push(key);
|
|
510
|
-
return y;
|
|
705
|
+
let globalConfig = defaultConfig();
|
|
706
|
+
/**
|
|
707
|
+
* Monotonic counter incremented on every `configure()` / `resetConfig()`
|
|
708
|
+
* call. Theme / palette caches read this to invalidate stale resolve
|
|
709
|
+
* results when the config changes between exports.
|
|
710
|
+
*/
|
|
711
|
+
let configVersion = 0;
|
|
712
|
+
/** Live reference to the current config. Mutated by `configure()` / `resetConfig()`. */
|
|
713
|
+
function getConfig() {
|
|
714
|
+
return globalConfig;
|
|
715
|
+
}
|
|
716
|
+
function getConfigVersion() {
|
|
717
|
+
return configVersion;
|
|
511
718
|
}
|
|
512
719
|
/**
|
|
513
|
-
*
|
|
720
|
+
* Public-facing snapshot used by `glaze.getConfig()`. Returns a shallow
|
|
721
|
+
* copy so callers can't mutate the live config.
|
|
514
722
|
*/
|
|
515
|
-
function
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
const mid = (low + high) / 2;
|
|
537
|
-
if (contrastRatioFromLuminance(cachedLuminance(h, s, mid), yBase) >= target) if (mid < preferred) low = mid;
|
|
538
|
-
else high = mid;
|
|
539
|
-
else if (mid < preferred) high = mid;
|
|
540
|
-
else low = mid;
|
|
541
|
-
}
|
|
542
|
-
const yLow = cachedLuminance(h, s, low);
|
|
543
|
-
const yHigh = cachedLuminance(h, s, high);
|
|
544
|
-
const crLow = contrastRatioFromLuminance(yLow, yBase);
|
|
545
|
-
const crHigh = contrastRatioFromLuminance(yHigh, yBase);
|
|
546
|
-
const lowPasses = crLow >= target;
|
|
547
|
-
const highPasses = crHigh >= target;
|
|
548
|
-
if (lowPasses && highPasses) {
|
|
549
|
-
if (Math.abs(low - preferred) <= Math.abs(high - preferred)) return {
|
|
550
|
-
lightness: low,
|
|
551
|
-
contrast: crLow,
|
|
552
|
-
met: true
|
|
553
|
-
};
|
|
554
|
-
return {
|
|
555
|
-
lightness: high,
|
|
556
|
-
contrast: crHigh,
|
|
557
|
-
met: true
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
if (lowPasses) return {
|
|
561
|
-
lightness: low,
|
|
562
|
-
contrast: crLow,
|
|
563
|
-
met: true
|
|
564
|
-
};
|
|
565
|
-
if (highPasses) return {
|
|
566
|
-
lightness: high,
|
|
567
|
-
contrast: crHigh,
|
|
568
|
-
met: true
|
|
723
|
+
function snapshotConfig() {
|
|
724
|
+
return { ...globalConfig };
|
|
725
|
+
}
|
|
726
|
+
function configure(config) {
|
|
727
|
+
configVersion++;
|
|
728
|
+
globalConfig = {
|
|
729
|
+
lightTone: config.lightTone ?? globalConfig.lightTone,
|
|
730
|
+
darkTone: config.darkTone ?? globalConfig.darkTone,
|
|
731
|
+
darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
|
|
732
|
+
states: {
|
|
733
|
+
dark: config.states?.dark ?? globalConfig.states.dark,
|
|
734
|
+
highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
|
|
735
|
+
},
|
|
736
|
+
modes: {
|
|
737
|
+
dark: config.modes?.dark ?? globalConfig.modes.dark,
|
|
738
|
+
highContrast: config.modes?.highContrast ?? globalConfig.modes.highContrast
|
|
739
|
+
},
|
|
740
|
+
shadowTuning: config.shadowTuning ?? globalConfig.shadowTuning,
|
|
741
|
+
autoFlip: config.autoFlip ?? globalConfig.autoFlip,
|
|
742
|
+
pastel: config.pastel ?? globalConfig.pastel,
|
|
743
|
+
inferRole: config.inferRole ?? globalConfig.inferRole
|
|
569
744
|
};
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
const cr = contrastRatioFromLuminance(cachedLuminance(h, s, l), yBase);
|
|
584
|
-
if (cr >= target && !bestMet) {
|
|
585
|
-
bestL = l;
|
|
586
|
-
bestCr = cr;
|
|
587
|
-
bestMet = true;
|
|
588
|
-
} else if (cr >= target && bestMet) {
|
|
589
|
-
bestL = l;
|
|
590
|
-
bestCr = cr;
|
|
591
|
-
} else if (!bestMet && cr > bestCr) {
|
|
592
|
-
bestL = l;
|
|
593
|
-
bestCr = cr;
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
if (bestMet && bestL > lo + step) {
|
|
597
|
-
let rLo = bestL - step;
|
|
598
|
-
let rHi = bestL;
|
|
599
|
-
for (let i = 0; i < maxIter; i++) {
|
|
600
|
-
if (rHi - rLo < epsilon) break;
|
|
601
|
-
const mid = (rLo + rHi) / 2;
|
|
602
|
-
const cr = contrastRatioFromLuminance(cachedLuminance(h, s, mid), yBase);
|
|
603
|
-
if (cr >= target) {
|
|
604
|
-
rHi = mid;
|
|
605
|
-
bestL = mid;
|
|
606
|
-
bestCr = cr;
|
|
607
|
-
} else rLo = mid;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
745
|
+
}
|
|
746
|
+
function resetConfig() {
|
|
747
|
+
configVersion++;
|
|
748
|
+
globalConfig = defaultConfig();
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Merge a per-instance config override over a base resolved config.
|
|
752
|
+
* Only fields present in `override` are replaced; others fall through
|
|
753
|
+
* from `base`. `false` for tone windows passes through as-is
|
|
754
|
+
* (treated as the full range by `activeWindow()` in okhst.ts).
|
|
755
|
+
*/
|
|
756
|
+
function mergeConfig(base, override) {
|
|
757
|
+
if (!override) return base;
|
|
610
758
|
return {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
759
|
+
lightTone: override.lightTone !== void 0 ? override.lightTone : base.lightTone,
|
|
760
|
+
darkTone: override.darkTone !== void 0 ? override.darkTone : base.darkTone,
|
|
761
|
+
darkDesaturation: override.darkDesaturation ?? base.darkDesaturation,
|
|
762
|
+
states: base.states,
|
|
763
|
+
modes: base.modes,
|
|
764
|
+
shadowTuning: override.shadowTuning ?? base.shadowTuning,
|
|
765
|
+
autoFlip: override.autoFlip ?? base.autoFlip,
|
|
766
|
+
pastel: override.pastel ?? base.pastel,
|
|
767
|
+
inferRole: override.inferRole ?? base.inferRole
|
|
614
768
|
};
|
|
615
769
|
}
|
|
616
770
|
/**
|
|
617
|
-
*
|
|
618
|
-
*
|
|
771
|
+
* Freeze the resolve-relevant effective config as a plain override.
|
|
772
|
+
* Used by `theme.export()` and standalone color-token creation so
|
|
773
|
+
* restored snapshots ignore later `configure()` calls for these fields.
|
|
619
774
|
*/
|
|
620
|
-
function
|
|
621
|
-
const
|
|
622
|
-
const
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
met: true,
|
|
630
|
-
branch: "preferred"
|
|
631
|
-
};
|
|
632
|
-
const [minL, maxL] = lightnessRange;
|
|
633
|
-
const darkerResult = preferredLightness > minL ? searchBranch(hue, saturation, minL, preferredLightness, yBase, searchTarget, epsilon, maxIterations, preferredLightness) : null;
|
|
634
|
-
const lighterResult = preferredLightness < maxL ? searchBranch(hue, saturation, preferredLightness, maxL, yBase, searchTarget, epsilon, maxIterations, preferredLightness) : null;
|
|
635
|
-
if (darkerResult) darkerResult.met = darkerResult.contrast >= target;
|
|
636
|
-
if (lighterResult) lighterResult.met = lighterResult.contrast >= target;
|
|
637
|
-
const darkerPasses = darkerResult?.met ?? false;
|
|
638
|
-
const lighterPasses = lighterResult?.met ?? false;
|
|
639
|
-
if (darkerPasses && lighterPasses) {
|
|
640
|
-
if (Math.abs(darkerResult.lightness - preferredLightness) <= Math.abs(lighterResult.lightness - preferredLightness)) return {
|
|
641
|
-
...darkerResult,
|
|
642
|
-
branch: "darker"
|
|
643
|
-
};
|
|
644
|
-
return {
|
|
645
|
-
...lighterResult,
|
|
646
|
-
branch: "lighter"
|
|
647
|
-
};
|
|
648
|
-
}
|
|
649
|
-
if (darkerPasses) return {
|
|
650
|
-
...darkerResult,
|
|
651
|
-
branch: "darker"
|
|
652
|
-
};
|
|
653
|
-
if (lighterPasses) return {
|
|
654
|
-
...lighterResult,
|
|
655
|
-
branch: "lighter"
|
|
656
|
-
};
|
|
657
|
-
const candidates = [];
|
|
658
|
-
if (darkerResult) candidates.push({
|
|
659
|
-
...darkerResult,
|
|
660
|
-
branch: "darker"
|
|
661
|
-
});
|
|
662
|
-
if (lighterResult) candidates.push({
|
|
663
|
-
...lighterResult,
|
|
664
|
-
branch: "lighter"
|
|
665
|
-
});
|
|
666
|
-
if (candidates.length === 0) return {
|
|
667
|
-
lightness: preferredLightness,
|
|
668
|
-
contrast: crPref,
|
|
669
|
-
met: false,
|
|
670
|
-
branch: "preferred"
|
|
775
|
+
function buildEffectiveConfigOverride(userOverride) {
|
|
776
|
+
const effective = mergeConfig(getConfig(), userOverride);
|
|
777
|
+
const out = {
|
|
778
|
+
lightTone: effective.lightTone,
|
|
779
|
+
darkTone: effective.darkTone,
|
|
780
|
+
darkDesaturation: effective.darkDesaturation,
|
|
781
|
+
autoFlip: effective.autoFlip,
|
|
782
|
+
pastel: effective.pastel,
|
|
783
|
+
inferRole: effective.inferRole
|
|
671
784
|
};
|
|
672
|
-
|
|
673
|
-
return
|
|
785
|
+
if (effective.shadowTuning !== void 0) out.shadowTuning = { ...effective.shadowTuning };
|
|
786
|
+
return out;
|
|
674
787
|
}
|
|
788
|
+
|
|
789
|
+
//#endregion
|
|
790
|
+
//#region src/types.ts
|
|
675
791
|
/**
|
|
676
|
-
*
|
|
677
|
-
*
|
|
792
|
+
* Current authoring-export schema version. Bump when the export shape
|
|
793
|
+
* changes in a non-compatible way. Written on every `.export()` snapshot.
|
|
678
794
|
*/
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
lightness: hi,
|
|
690
|
-
contrast: crHi,
|
|
691
|
-
met: false
|
|
692
|
-
};
|
|
693
|
-
}
|
|
694
|
-
let low = lo;
|
|
695
|
-
let high = hi;
|
|
696
|
-
for (let i = 0; i < maxIter; i++) {
|
|
697
|
-
if (high - low < epsilon) break;
|
|
698
|
-
const mid = (low + high) / 2;
|
|
699
|
-
if (contrastRatioFromLuminance(luminanceAt(mid), yBase) >= target) if (mid < preferred) low = mid;
|
|
700
|
-
else high = mid;
|
|
701
|
-
else if (mid < preferred) high = mid;
|
|
702
|
-
else low = mid;
|
|
703
|
-
}
|
|
704
|
-
const crLow = contrastRatioFromLuminance(luminanceAt(low), yBase);
|
|
705
|
-
const crHigh = contrastRatioFromLuminance(luminanceAt(high), yBase);
|
|
706
|
-
const lowPasses = crLow >= target;
|
|
707
|
-
const highPasses = crHigh >= target;
|
|
708
|
-
if (lowPasses && highPasses) {
|
|
709
|
-
if (Math.abs(low - preferred) <= Math.abs(high - preferred)) return {
|
|
710
|
-
lightness: low,
|
|
711
|
-
contrast: crLow,
|
|
712
|
-
met: true
|
|
713
|
-
};
|
|
714
|
-
return {
|
|
715
|
-
lightness: high,
|
|
716
|
-
contrast: crHigh,
|
|
717
|
-
met: true
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
|
-
if (lowPasses) return {
|
|
721
|
-
lightness: low,
|
|
722
|
-
contrast: crLow,
|
|
723
|
-
met: true
|
|
724
|
-
};
|
|
725
|
-
if (highPasses) return {
|
|
726
|
-
lightness: high,
|
|
727
|
-
contrast: crHigh,
|
|
728
|
-
met: true
|
|
729
|
-
};
|
|
730
|
-
return crLow >= crHigh ? {
|
|
731
|
-
lightness: low,
|
|
732
|
-
contrast: crLow,
|
|
733
|
-
met: false
|
|
734
|
-
} : {
|
|
735
|
-
lightness: high,
|
|
736
|
-
contrast: crHigh,
|
|
737
|
-
met: false
|
|
738
|
-
};
|
|
795
|
+
const GLAZE_EXPORT_VERSION = 1;
|
|
796
|
+
|
|
797
|
+
//#endregion
|
|
798
|
+
//#region src/serialize.ts
|
|
799
|
+
/**
|
|
800
|
+
* Authoring-export helpers: schema version, type guards, and restore
|
|
801
|
+
* validation shared by `themeFrom` / `colorFrom` / `paletteFrom`.
|
|
802
|
+
*/
|
|
803
|
+
function isPlainObject(data) {
|
|
804
|
+
return typeof data === "object" && data !== null && !Array.isArray(data);
|
|
739
805
|
}
|
|
740
806
|
/**
|
|
741
|
-
*
|
|
742
|
-
*
|
|
807
|
+
* Reject unknown future schema versions. Missing `version` is allowed
|
|
808
|
+
* (legacy snapshots). Older versions than current are accepted.
|
|
743
809
|
*/
|
|
744
|
-
function
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
if (
|
|
785
|
-
|
|
786
|
-
branch: "lower"
|
|
787
|
-
});
|
|
788
|
-
if (lighterResult) candidates.push({
|
|
789
|
-
...lighterResult,
|
|
790
|
-
branch: "upper"
|
|
791
|
-
});
|
|
792
|
-
if (candidates.length === 0) return {
|
|
793
|
-
value: preferredValue,
|
|
794
|
-
contrast: crPref,
|
|
795
|
-
met: false
|
|
796
|
-
};
|
|
797
|
-
candidates.sort((a, b) => b.contrast - a.contrast);
|
|
798
|
-
return {
|
|
799
|
-
value: candidates[0].lightness,
|
|
800
|
-
contrast: candidates[0].contrast,
|
|
801
|
-
met: candidates[0].met
|
|
802
|
-
};
|
|
810
|
+
function assertExportVersion(data, factory) {
|
|
811
|
+
if (data.version === void 0) return;
|
|
812
|
+
if (typeof data.version !== "number" || !Number.isFinite(data.version)) throw new Error(`${factory}: invalid "version" field — expected a number (got ${JSON.stringify(data.version)}).`);
|
|
813
|
+
if (data.version > GLAZE_EXPORT_VERSION) throw new Error(`${factory}: unsupported export version ${data.version} (this library supports version ${GLAZE_EXPORT_VERSION}). Upgrade @tenphi/glaze to load this snapshot.`);
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* When `kind` is present, it must match the factory. Missing `kind` is
|
|
817
|
+
* allowed for legacy snapshots.
|
|
818
|
+
*/
|
|
819
|
+
function assertExportKind(data, expected, factory) {
|
|
820
|
+
if (data.kind === void 0) return;
|
|
821
|
+
if (data.kind !== expected) throw new Error(`${factory}: expected kind "${expected}", got ${JSON.stringify(data.kind)}.`);
|
|
822
|
+
}
|
|
823
|
+
function hasThemeShape(data) {
|
|
824
|
+
return typeof data.hue === "number" && typeof data.saturation === "number" && !("form" in data) && !("themes" in data);
|
|
825
|
+
}
|
|
826
|
+
function hasColorTokenShape(data) {
|
|
827
|
+
return data.form === "value" || data.form === "structured";
|
|
828
|
+
}
|
|
829
|
+
function hasPaletteShape(data) {
|
|
830
|
+
return isPlainObject(data.themes) && !("form" in data) && typeof data.hue !== "number";
|
|
831
|
+
}
|
|
832
|
+
/** Type guard for theme authoring snapshots (prefers `kind`, falls back to shape). */
|
|
833
|
+
function isThemeExport(data) {
|
|
834
|
+
if (!isPlainObject(data)) return false;
|
|
835
|
+
if (data.kind === "theme") return hasThemeShape(data);
|
|
836
|
+
if (data.kind !== void 0) return false;
|
|
837
|
+
return hasThemeShape(data);
|
|
838
|
+
}
|
|
839
|
+
/** Type guard for color-token authoring snapshots. */
|
|
840
|
+
function isColorTokenExport(data) {
|
|
841
|
+
if (!isPlainObject(data)) return false;
|
|
842
|
+
if (data.kind === "color") return hasColorTokenShape(data);
|
|
843
|
+
if (data.kind !== void 0) return false;
|
|
844
|
+
return hasColorTokenShape(data);
|
|
845
|
+
}
|
|
846
|
+
/** Type guard for palette authoring snapshots. */
|
|
847
|
+
function isPaletteExport(data) {
|
|
848
|
+
if (!isPlainObject(data)) return false;
|
|
849
|
+
if (data.kind === "palette") return hasPaletteShape(data);
|
|
850
|
+
if (data.kind !== void 0) return false;
|
|
851
|
+
return hasPaletteShape(data);
|
|
803
852
|
}
|
|
804
853
|
|
|
805
854
|
//#endregion
|
|
806
|
-
//#region src/
|
|
855
|
+
//#region src/format-guard.ts
|
|
856
|
+
const NON_NATIVE_FORMATS = new Set(["okhsl", "okhst"]);
|
|
807
857
|
/**
|
|
808
|
-
*
|
|
809
|
-
*
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
states: {
|
|
819
|
-
dark: "@dark",
|
|
820
|
-
highContrast: "@high-contrast"
|
|
858
|
+
* Throw when a non-native Glaze color space is requested for an export that
|
|
859
|
+
* emits raw CSS or non-Tasty token maps.
|
|
860
|
+
*/
|
|
861
|
+
function assertNativeFormat(format, method) {
|
|
862
|
+
if (format !== void 0 && NON_NATIVE_FORMATS.has(format)) throw new Error(`glaze: ${format} output is only supported by tasty() (not a native CSS color space). Use tasty({ format: '${format}' }) or pick a native format (oklch|hsl|rgb) for ${method}().`);
|
|
863
|
+
}
|
|
864
|
+
const SCHEME_FIELDS = [
|
|
865
|
+
{
|
|
866
|
+
field: "light",
|
|
867
|
+
modes: () => true
|
|
821
868
|
},
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
869
|
+
{
|
|
870
|
+
field: "dark",
|
|
871
|
+
modes: (m) => m.dark
|
|
872
|
+
},
|
|
873
|
+
{
|
|
874
|
+
field: "lightContrast",
|
|
875
|
+
modes: (m) => m.highContrast
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
field: "darkContrast",
|
|
879
|
+
modes: (m) => m.dark && m.highContrast
|
|
825
880
|
}
|
|
826
|
-
|
|
881
|
+
];
|
|
882
|
+
/**
|
|
883
|
+
* Throw when `splitHue` is enabled but any exported color is not pastel.
|
|
884
|
+
* Hue rotation is only clip-free when chroma is bounded by the hue-independent
|
|
885
|
+
* safe chroma (`computeSafeChromaOKLCH`).
|
|
886
|
+
*/
|
|
887
|
+
function assertAllPastel(resolved, modes) {
|
|
888
|
+
const nonPastel = [];
|
|
889
|
+
for (const [name, color] of resolved) for (const { field, modes: active } of SCHEME_FIELDS) {
|
|
890
|
+
if (!active(modes)) continue;
|
|
891
|
+
if (color[field].pastel !== true) {
|
|
892
|
+
if (!nonPastel.includes(name)) nonPastel.push(name);
|
|
893
|
+
break;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (nonPastel.length === 0) return;
|
|
897
|
+
throw new Error(`glaze: splitHue requires every color to be pastel (hue rotation is only clip-free when chroma is bounded by the hue-independent safe chroma). Non-pastel: ${nonPastel.join(", ")}. Set pastel: true (global or per-color) or drop splitHue.`);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
//#endregion
|
|
901
|
+
//#region src/hc-pair.ts
|
|
827
902
|
function pairNormal(p) {
|
|
828
903
|
return Array.isArray(p) ? p[0] : p;
|
|
829
904
|
}
|
|
830
905
|
function pairHC(p) {
|
|
831
906
|
return Array.isArray(p) ? p[1] : p;
|
|
832
907
|
}
|
|
833
|
-
function
|
|
834
|
-
return
|
|
908
|
+
function clamp(v, min, max) {
|
|
909
|
+
return Math.max(min, Math.min(max, v));
|
|
835
910
|
}
|
|
836
|
-
|
|
837
|
-
|
|
911
|
+
/** Whether a tone value is an extreme keyword (`'max'` / `'min'`). */
|
|
912
|
+
function isExtremeTone(value) {
|
|
913
|
+
return value === "max" || value === "min";
|
|
838
914
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
};
|
|
848
|
-
function resolveShadowTuning(perColor) {
|
|
915
|
+
/**
|
|
916
|
+
* Parse a value that can be absolute (number) or relative (signed string).
|
|
917
|
+
* Returns the numeric value and whether it's relative.
|
|
918
|
+
*/
|
|
919
|
+
function parseRelativeOrAbsolute(value) {
|
|
920
|
+
if (typeof value === "number") return {
|
|
921
|
+
value,
|
|
922
|
+
relative: false
|
|
923
|
+
};
|
|
849
924
|
return {
|
|
850
|
-
|
|
851
|
-
|
|
925
|
+
value: parseFloat(value),
|
|
926
|
+
relative: true
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Parse a tone value into a normalized shape.
|
|
931
|
+
* - `'max'` / `'min'` → `{ kind: 'extreme', value: 100 | 0 }` (an absolute
|
|
932
|
+
* author tone before scheme mapping — `'max'` is 100, `'min'` is 0).
|
|
933
|
+
* - `'+N'` / `'-N'` → `{ kind: 'relative', value: ±N }`.
|
|
934
|
+
* - number → `{ kind: 'absolute', value }`.
|
|
935
|
+
*/
|
|
936
|
+
function parseToneValue(value) {
|
|
937
|
+
if (value === "max") return {
|
|
938
|
+
kind: "extreme",
|
|
939
|
+
value: 100
|
|
940
|
+
};
|
|
941
|
+
if (value === "min") return {
|
|
942
|
+
kind: "extreme",
|
|
943
|
+
value: 0
|
|
944
|
+
};
|
|
945
|
+
if (typeof value === "number") return {
|
|
946
|
+
kind: "absolute",
|
|
947
|
+
value
|
|
948
|
+
};
|
|
949
|
+
return {
|
|
950
|
+
kind: "relative",
|
|
951
|
+
value: parseFloat(value)
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Compute the effective hue for a color, given the theme seed hue
|
|
956
|
+
* and an optional per-color hue override.
|
|
957
|
+
*/
|
|
958
|
+
function resolveEffectiveHue(seedHue, defHue) {
|
|
959
|
+
if (defHue === void 0) return seedHue;
|
|
960
|
+
const parsed = parseRelativeOrAbsolute(defHue);
|
|
961
|
+
if (parsed.relative) return ((seedHue + parsed.value) % 360 + 360) % 360;
|
|
962
|
+
return (parsed.value % 360 + 360) % 360;
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Check whether a tone value represents an absolute root definition
|
|
966
|
+
* (i.e. a number, not a relative string). Extreme keywords (`'max'` /
|
|
967
|
+
* `'min'`) also count — they need no base.
|
|
968
|
+
*/
|
|
969
|
+
function isAbsoluteTone(tone) {
|
|
970
|
+
if (tone === void 0) return false;
|
|
971
|
+
const normal = Array.isArray(tone) ? tone[0] : tone;
|
|
972
|
+
return typeof normal === "number" || isExtremeTone(normal);
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
//#endregion
|
|
976
|
+
//#region src/okhst.ts
|
|
977
|
+
/**
|
|
978
|
+
* OKHST — the contrast-uniform tone space.
|
|
979
|
+
*
|
|
980
|
+
* OKHST is OKHSL with its lightness axis replaced by a contrast-uniform
|
|
981
|
+
* "tone" axis. It shares `h` / `s` with OKHSL verbatim and swaps `l` for
|
|
982
|
+
* `t`. This module owns:
|
|
983
|
+
*
|
|
984
|
+
* - the closed-form tone transfers (`toTone` / `fromTone`) at a fixed
|
|
985
|
+
* reference eps, plus the gray luminance helpers (`lToY` / `yToL`),
|
|
986
|
+
* - the `{ h, s, t }` <-> `{ h, s, l }` color-space converters,
|
|
987
|
+
* - the resolved-variant edge adapter (`variantToOkhsl`),
|
|
988
|
+
* - the per-scheme tone mapping that replaced the Möbius dark curve
|
|
989
|
+
* (`mapToneForScheme`), the dark desaturation reducer, and the solver's scheme
|
|
990
|
+
* tone range.
|
|
991
|
+
*
|
|
992
|
+
* See `docs/okhst.md` for the full specification and the calibrated
|
|
993
|
+
* default constants.
|
|
994
|
+
*/
|
|
995
|
+
/**
|
|
996
|
+
* Reference eps for the OKHST color space. WCAG 2 contrast is
|
|
997
|
+
* `(Y_hi + 0.05) / (Y_lo + 0.05)`, so an eps of `0.05` makes equal tone
|
|
998
|
+
* steps yield equal WCAG contrast. This is the canonical eps used by
|
|
999
|
+
* `okhst()` input, `{ h, s, t }` input, stored `ResolvedColorVariant.t`,
|
|
1000
|
+
* relative `tone` offsets, and the contrast solver.
|
|
1001
|
+
*/
|
|
1002
|
+
const REF_EPS = .05;
|
|
1003
|
+
/**
|
|
1004
|
+
* Gray luminance from OKHSL lightness. For an achromatic color the OKLab
|
|
1005
|
+
* lightness is `toeInv(l)` and luminance is its cube.
|
|
1006
|
+
*/
|
|
1007
|
+
function lToY(l) {
|
|
1008
|
+
const L = toeInv(l);
|
|
1009
|
+
return L * L * L;
|
|
1010
|
+
}
|
|
1011
|
+
/** OKHSL lightness from gray luminance — exact inverse of {@link lToY}. */
|
|
1012
|
+
function yToL(y) {
|
|
1013
|
+
return toe(Math.cbrt(Math.max(0, y)));
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Map a luminance `Y` (0–1) to tone (0–100) at the given eps.
|
|
1017
|
+
* `toneFromY(0) === 0` and `toneFromY(1) === 100` for any eps.
|
|
1018
|
+
*/
|
|
1019
|
+
function toneFromY(y, eps = REF_EPS) {
|
|
1020
|
+
return (Math.log(y + eps) - Math.log(eps)) / (Math.log(1 + eps) - Math.log(eps)) * 100;
|
|
1021
|
+
}
|
|
1022
|
+
/** Map a tone (0–100) back to luminance (0–1). Inverse of {@link toneFromY}. */
|
|
1023
|
+
function yFromTone(t, eps = REF_EPS) {
|
|
1024
|
+
const den = Math.log(1 + eps) - Math.log(eps);
|
|
1025
|
+
return Math.exp(t / 100 * den + Math.log(eps)) - eps;
|
|
1026
|
+
}
|
|
1027
|
+
/** OKHSL lightness (0–1) -> tone (0–100). */
|
|
1028
|
+
function toTone(l, eps = REF_EPS) {
|
|
1029
|
+
return toneFromY(lToY(l), eps);
|
|
1030
|
+
}
|
|
1031
|
+
/** Tone (0–100) -> OKHSL lightness (0–1). Inverse of {@link toTone}. */
|
|
1032
|
+
function fromTone(t, eps = REF_EPS) {
|
|
1033
|
+
return yToL(yFromTone(t, eps));
|
|
1034
|
+
}
|
|
1035
|
+
/** Convert OKHST `{ h, s, t }` (t in 0–1) to OKHSL `{ h, s, l }`. */
|
|
1036
|
+
function okhstToOkhsl(c) {
|
|
1037
|
+
return {
|
|
1038
|
+
h: c.h,
|
|
1039
|
+
s: c.s,
|
|
1040
|
+
l: clamp(fromTone(c.t * 100), 0, 1)
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
/** Convert OKHSL `{ h, s, l }` to OKHST `{ h, s, t }` (t in 0–1). */
|
|
1044
|
+
function okhslToOkhst(c) {
|
|
1045
|
+
return {
|
|
1046
|
+
h: c.h,
|
|
1047
|
+
s: c.s,
|
|
1048
|
+
t: clamp(toTone(c.l) / 100, 0, 1)
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Edge adapter: a resolved variant stores canonical tone `t` (0–1). Convert
|
|
1053
|
+
* it to the OKHSL `{ h, s, l }` the formatters and luminance pipeline expect.
|
|
1054
|
+
*/
|
|
1055
|
+
function variantToOkhsl(v) {
|
|
1056
|
+
return {
|
|
1057
|
+
h: v.h,
|
|
1058
|
+
s: v.s,
|
|
1059
|
+
l: clamp(fromTone(v.t * 100), 0, 1)
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* Normalize any {@link ToneWindow} form to `{ lo, hi, eps }`.
|
|
1064
|
+
* - `false`: full range `[0, 100]` at the reference eps (boundaries removed,
|
|
1065
|
+
* curve preserved).
|
|
1066
|
+
* - `[lo, hi]`: endpoints at the reference eps (the common form).
|
|
1067
|
+
* - `{ lo, hi, eps }`: passed through (advanced eps tuning).
|
|
1068
|
+
*/
|
|
1069
|
+
function normalizeToneWindow(win) {
|
|
1070
|
+
if (win === false) return {
|
|
1071
|
+
lo: 0,
|
|
1072
|
+
hi: 100,
|
|
1073
|
+
eps: REF_EPS
|
|
1074
|
+
};
|
|
1075
|
+
if (Array.isArray(win)) return {
|
|
1076
|
+
lo: win[0],
|
|
1077
|
+
hi: win[1],
|
|
1078
|
+
eps: REF_EPS
|
|
1079
|
+
};
|
|
1080
|
+
return {
|
|
1081
|
+
lo: win.lo,
|
|
1082
|
+
hi: win.hi,
|
|
1083
|
+
eps: win.eps
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Resolve the active tone window for a scheme as OKHSL-lightness endpoints.
|
|
1088
|
+
* - HC variants always return the full range `[0, 100]` with the mode eps.
|
|
1089
|
+
* - `false` (= "no clamping") is treated as `[0, 100]` with the reference eps.
|
|
1090
|
+
*/
|
|
1091
|
+
function activeWindow(isHighContrast, kind, config) {
|
|
1092
|
+
const win = normalizeToneWindow(kind === "dark" ? config.darkTone : config.lightTone);
|
|
1093
|
+
if (isHighContrast) return {
|
|
1094
|
+
lo: 0,
|
|
1095
|
+
hi: 100,
|
|
1096
|
+
eps: win.eps
|
|
1097
|
+
};
|
|
1098
|
+
return win;
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Remap an authored tone (0–100) into a scheme window and return the final
|
|
1102
|
+
* OKHSL lightness (0–100). The window endpoints are OKHSL lightnesses; the
|
|
1103
|
+
* author tone is positioned within the window's tone interval (using the
|
|
1104
|
+
* window's render eps), then converted back to lightness.
|
|
1105
|
+
*/
|
|
1106
|
+
function remapToneToLightness(authorTone, win) {
|
|
1107
|
+
const loT = toTone(win.lo / 100, win.eps);
|
|
1108
|
+
const hiT = toTone(win.hi / 100, win.eps);
|
|
1109
|
+
return clamp(fromTone(loT + authorTone / 100 * (hiT - loT), win.eps) * 100, 0, 100);
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* Map an authored tone for a scheme and return the canonical stored tone
|
|
1113
|
+
* (0–100, reference eps).
|
|
1114
|
+
*
|
|
1115
|
+
* - `static`: identity — the same tone renders in every scheme.
|
|
1116
|
+
* - `auto` + dark: invert (`100 - tone`) then remap into the dark window.
|
|
1117
|
+
* - `auto`/`fixed` + light, or `fixed` + dark: remap, no inversion.
|
|
1118
|
+
*
|
|
1119
|
+
* The window remap uses the mode's render eps to land a final OKHSL
|
|
1120
|
+
* lightness; that lightness is then re-expressed as canonical tone so
|
|
1121
|
+
* relative offsets and contrast stay comparable across schemes.
|
|
1122
|
+
*/
|
|
1123
|
+
function mapToneForScheme(authorTone, mode, isDark, isHighContrast, config) {
|
|
1124
|
+
if (mode === "static") return clamp(authorTone, 0, 100);
|
|
1125
|
+
const win = activeWindow(isHighContrast, isDark ? "dark" : "light", config);
|
|
1126
|
+
return clamp(toTone(remapToneToLightness(clamp(isDark && mode === "auto" ? 100 - authorTone : authorTone, 0, 100), win) / 100), 0, 100);
|
|
1127
|
+
}
|
|
1128
|
+
/** Dark-scheme desaturation reducer (unchanged from the legacy pipeline). */
|
|
1129
|
+
function mapSaturationDark(s, mode, config) {
|
|
1130
|
+
if (mode === "static") return s;
|
|
1131
|
+
return s * (1 - config.darkDesaturation);
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Tone search range (0–1) for the contrast solver in a given scheme.
|
|
1135
|
+
* `static` searches the full range; otherwise the scheme window's tone
|
|
1136
|
+
* endpoints (HC bypasses to full range).
|
|
1137
|
+
*/
|
|
1138
|
+
function schemeToneRange(isDark, mode, isHighContrast, config) {
|
|
1139
|
+
if (mode === "static") return [0, 1];
|
|
1140
|
+
const win = activeWindow(isHighContrast, isDark ? "dark" : "light", config);
|
|
1141
|
+
return [clamp(toTone(win.lo / 100) / 100, 0, 1), clamp(toTone(win.hi / 100) / 100, 0, 1)];
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
//#endregion
|
|
1145
|
+
//#region src/contrast-solver.ts
|
|
1146
|
+
/**
|
|
1147
|
+
* Contrast solver — operates in OKHST tone.
|
|
1148
|
+
*
|
|
1149
|
+
* Finds the tone closest to a preferred tone that satisfies a contrast
|
|
1150
|
+
* floor (WCAG 2 ratio or APCA Lc) against a base color. Because tone is
|
|
1151
|
+
* contrast-uniform, the WCAG branch gets a closed-form seed and the search
|
|
1152
|
+
* converges quickly.
|
|
1153
|
+
*
|
|
1154
|
+
* Public API: `findToneForContrast`, `findValueForMixContrast`,
|
|
1155
|
+
* `resolveMinContrast`, `resolveContrastForMode`, `apcaContrast`.
|
|
1156
|
+
*/
|
|
1157
|
+
/**
|
|
1158
|
+
* Luminance of a linear-sRGB color in the basis the metric expects: WCAG
|
|
1159
|
+
* relative luminance for `wcag`, APCA screen luminance (`Ys`) for `apca`.
|
|
1160
|
+
*/
|
|
1161
|
+
function metricLuminance(metric, linearRgb) {
|
|
1162
|
+
return metric === "apca" ? apcaLuminanceFromLinearRgb(linearRgb) : gamutClampedLuminance(linearRgb);
|
|
1163
|
+
}
|
|
1164
|
+
const APCA_PRESETS = {
|
|
1165
|
+
preferred: 90,
|
|
1166
|
+
body: 75,
|
|
1167
|
+
content: 60,
|
|
1168
|
+
large: 45,
|
|
1169
|
+
"non-text": 30,
|
|
1170
|
+
min: 15
|
|
1171
|
+
};
|
|
1172
|
+
/**
|
|
1173
|
+
* APCA-W3 "Enhanced Level" delta added to a bare APCA target in high-contrast
|
|
1174
|
+
* mode when no explicit HC value is provided (analogous to WCAG AAA over AA).
|
|
1175
|
+
* Only applied when neither the outer `contrast` pair nor the inner `apca`
|
|
1176
|
+
* pair carries an explicit HC entry.
|
|
1177
|
+
*/
|
|
1178
|
+
const APCA_HC_ENHANCEMENT = 15;
|
|
1179
|
+
/** Upper bound for an APCA Lc target after HC enhancement. */
|
|
1180
|
+
const APCA_MAX_LC = 106;
|
|
1181
|
+
/**
|
|
1182
|
+
* Resolve an APCA target — a raw Lc number (kept as-is) or an `ApcaPreset`
|
|
1183
|
+
* keyword mapped to its Lc value. The magnitude is forced non-negative.
|
|
1184
|
+
*/
|
|
1185
|
+
function resolveApcaTarget(value) {
|
|
1186
|
+
if (typeof value === "number") return Math.abs(value);
|
|
1187
|
+
return APCA_PRESETS[value];
|
|
1188
|
+
}
|
|
1189
|
+
const CONTRAST_PRESETS = {
|
|
1190
|
+
AA: 4.5,
|
|
1191
|
+
AAA: 7,
|
|
1192
|
+
"AA-large": 3,
|
|
1193
|
+
"AAA-large": 4.5
|
|
1194
|
+
};
|
|
1195
|
+
/**
|
|
1196
|
+
* WCAG high-contrast auto-promotion (analog of APCA's Enhanced Level). A bare
|
|
1197
|
+
* AA / AA-large preset is promoted to its spec-defined "Enhanced" successor
|
|
1198
|
+
* (SC 1.4.3 → SC 1.4.6) in high-contrast mode. AAA / AAA-large are already
|
|
1199
|
+
* the top WCAG tier and are left unchanged. Bare numeric targets have no
|
|
1200
|
+
* defined successor tier and are also left unchanged. An explicit HC value
|
|
1201
|
+
* (outer or inner pair) always overrides.
|
|
1202
|
+
*/
|
|
1203
|
+
const WCAG_HC_PROMOTION = {
|
|
1204
|
+
AA: "AAA",
|
|
1205
|
+
"AA-large": "AAA-large"
|
|
1206
|
+
};
|
|
1207
|
+
function resolveMinContrast(value) {
|
|
1208
|
+
if (typeof value === "number") return Math.max(1, value);
|
|
1209
|
+
return CONTRAST_PRESETS[value];
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Resolve a WCAG target (number or preset) for a mode, applying the
|
|
1213
|
+
* high-contrast auto-promotion when `explicitHC` is false and the value is an
|
|
1214
|
+
* AA-family preset. Bare numbers and AAA-family presets pass through.
|
|
1215
|
+
*/
|
|
1216
|
+
function resolveWcagTarget(value, isHighContrast, explicitHC) {
|
|
1217
|
+
if (typeof value === "number") return resolveMinContrast(value);
|
|
1218
|
+
if (isHighContrast && !explicitHC) {
|
|
1219
|
+
const promoted = WCAG_HC_PROMOTION[value];
|
|
1220
|
+
if (promoted !== void 0) return resolveMinContrast(promoted);
|
|
1221
|
+
}
|
|
1222
|
+
return resolveMinContrast(value);
|
|
1223
|
+
}
|
|
1224
|
+
function pickPair(p, isHighContrast) {
|
|
1225
|
+
return Array.isArray(p) ? isHighContrast ? p[1] : p[0] : p;
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Resolve a `ContrastSpec` (already selected from any outer HC pair) for a
|
|
1229
|
+
* given mode into `{ metric, target }`. Handles the inner metric HC pair and
|
|
1230
|
+
* preset resolution. `polarity` is passed through to the result for the APCA
|
|
1231
|
+
* branch (it controls argument order in the solver); WCAG ignores it.
|
|
1232
|
+
*
|
|
1233
|
+
* `outerExplicitHC` indicates whether the caller selected this `spec` from an
|
|
1234
|
+
* explicit high-contrast entry of the outer `contrast` pair. Together with the
|
|
1235
|
+
* inner metric pair, it decides whether the HC auto-enhancement fires:
|
|
1236
|
+
* - APCA: +15 Lc "Enhanced Level" boost when neither level is explicit.
|
|
1237
|
+
* - WCAG: AA → AAA / AA-large → AAA-large promotion (SC 1.4.3 → 1.4.6) when
|
|
1238
|
+
* neither level is explicit. AAA-family presets and bare numbers are left
|
|
1239
|
+
* unchanged (AAA is the top WCAG tier).
|
|
1240
|
+
* Defaults to `false` (correct for direct callers, which pass a single
|
|
1241
|
+
* selected spec rather than an outer pair).
|
|
1242
|
+
*/
|
|
1243
|
+
function resolveContrastForMode(spec, isHighContrast, polarity, outerExplicitHC) {
|
|
1244
|
+
if (typeof spec === "number" || typeof spec === "string") return {
|
|
1245
|
+
metric: "wcag",
|
|
1246
|
+
target: resolveWcagTarget(spec, isHighContrast, !!outerExplicitHC)
|
|
1247
|
+
};
|
|
1248
|
+
if ("apca" in spec) {
|
|
1249
|
+
const baseTarget = resolveApcaTarget(pickPair(spec.apca, isHighContrast));
|
|
1250
|
+
const innerExplicitHC = Array.isArray(spec.apca);
|
|
1251
|
+
return {
|
|
1252
|
+
metric: "apca",
|
|
1253
|
+
target: isHighContrast && !outerExplicitHC && !innerExplicitHC ? Math.min(baseTarget + APCA_HC_ENHANCEMENT, APCA_MAX_LC) : baseTarget,
|
|
1254
|
+
polarity: polarity ?? "fg"
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1257
|
+
const innerExplicitHC = Array.isArray(spec.wcag);
|
|
1258
|
+
return {
|
|
1259
|
+
metric: "wcag",
|
|
1260
|
+
target: resolveWcagTarget(pickPair(spec.wcag, isHighContrast), isHighContrast, !!outerExplicitHC || innerExplicitHC)
|
|
1261
|
+
};
|
|
1262
|
+
}
|
|
1263
|
+
const APCA_EXPONENTS = {
|
|
1264
|
+
mainTRC: 2.4,
|
|
1265
|
+
normBG: .56,
|
|
1266
|
+
normTXT: .57,
|
|
1267
|
+
revTXT: .62,
|
|
1268
|
+
revBG: .65
|
|
1269
|
+
};
|
|
1270
|
+
const APCA_BLACK_THRESH = .022;
|
|
1271
|
+
const APCA_BLACK_CLIP = 1.414;
|
|
1272
|
+
const APCA_DELTA_Y_MIN = 5e-4;
|
|
1273
|
+
const APCA_SCALE = 1.14;
|
|
1274
|
+
const APCA_LO_OFFSET = .027;
|
|
1275
|
+
function apcaSoftClamp(y) {
|
|
1276
|
+
const yc = Math.max(0, y);
|
|
1277
|
+
if (yc >= APCA_BLACK_THRESH) return yc;
|
|
1278
|
+
return yc + Math.pow(APCA_BLACK_THRESH - yc, APCA_BLACK_CLIP);
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* APCA lightness contrast (Lc), signed: positive for dark text on light bg,
|
|
1282
|
+
* negative for light text on dark bg. Inputs are screen luminances (0–1).
|
|
1283
|
+
*/
|
|
1284
|
+
function apcaContrast(yText, yBg) {
|
|
1285
|
+
const txt = apcaSoftClamp(yText);
|
|
1286
|
+
const bg = apcaSoftClamp(yBg);
|
|
1287
|
+
if (Math.abs(bg - txt) < APCA_DELTA_Y_MIN) return 0;
|
|
1288
|
+
let sapc;
|
|
1289
|
+
if (bg > txt) {
|
|
1290
|
+
sapc = (Math.pow(bg, APCA_EXPONENTS.normBG) - Math.pow(txt, APCA_EXPONENTS.normTXT)) * APCA_SCALE;
|
|
1291
|
+
return sapc < .1 ? 0 : (sapc - APCA_LO_OFFSET) * 100;
|
|
1292
|
+
}
|
|
1293
|
+
sapc = (Math.pow(bg, APCA_EXPONENTS.revBG) - Math.pow(txt, APCA_EXPONENTS.revTXT)) * APCA_SCALE;
|
|
1294
|
+
return sapc > -.1 ? 0 : (sapc + APCA_LO_OFFSET) * 100;
|
|
1295
|
+
}
|
|
1296
|
+
const CACHE_SIZE = 512;
|
|
1297
|
+
const luminanceCache = /* @__PURE__ */ new Map();
|
|
1298
|
+
const cacheOrder = [];
|
|
1299
|
+
/**
|
|
1300
|
+
* Luminance of an OKHST color `(h, s, t)` with t in 0–1 (reference eps), in
|
|
1301
|
+
* the metric's luminance basis. The metric is part of the cache key because
|
|
1302
|
+
* WCAG and APCA derive different luminances from the same color.
|
|
1303
|
+
*/
|
|
1304
|
+
function cachedLuminance(metric, h, s, t, pastel) {
|
|
1305
|
+
const tRounded = Math.round(t * 1e4) / 1e4;
|
|
1306
|
+
const key = `${metric}|${h}|${s}|${tRounded}|${pastel}`;
|
|
1307
|
+
const cached = luminanceCache.get(key);
|
|
1308
|
+
if (cached !== void 0) return cached;
|
|
1309
|
+
const y = metricLuminance(metric, okhslToLinearSrgb(h, s, fromTone(tRounded * 100, REF_EPS), pastel));
|
|
1310
|
+
if (luminanceCache.size >= CACHE_SIZE) {
|
|
1311
|
+
const evict = cacheOrder.shift();
|
|
1312
|
+
luminanceCache.delete(evict);
|
|
1313
|
+
}
|
|
1314
|
+
luminanceCache.set(key, y);
|
|
1315
|
+
cacheOrder.push(key);
|
|
1316
|
+
return y;
|
|
1317
|
+
}
|
|
1318
|
+
/**
|
|
1319
|
+
* Score a candidate luminance against the base for a metric. Returns a value
|
|
1320
|
+
* that is `>= target` exactly when the floor is met (WCAG ratio, or APCA Lc
|
|
1321
|
+
* magnitude). For APCA, `polarity` selects the argument order: `'fg'` (the
|
|
1322
|
+
* default) treats the candidate as the text against a background base
|
|
1323
|
+
* (`apcaContrast(yCandidate, yBase)`); `'bg'` treats the candidate as the
|
|
1324
|
+
* background (`apcaContrast(yBase, yCandidate)`). The magnitude is taken
|
|
1325
|
+
* either way. WCAG is symmetric, so polarity is ignored there.
|
|
1326
|
+
*/
|
|
1327
|
+
function metricScore(metric, yCandidate, yBase, polarity) {
|
|
1328
|
+
if (metric === "wcag") return contrastRatioFromLuminance(yCandidate, yBase);
|
|
1329
|
+
const lc = polarity === "bg" ? apcaContrast(yBase, yCandidate) : apcaContrast(yCandidate, yBase);
|
|
1330
|
+
return Math.abs(lc);
|
|
1331
|
+
}
|
|
1332
|
+
/**
|
|
1333
|
+
* Binary search one branch `[lo, hi]` for the position nearest to `anchor`
|
|
1334
|
+
* that meets `target`. The domain is whatever `lum` interprets (tone 0–1 or
|
|
1335
|
+
* mix parameter 0–1); the search is identical in both cases.
|
|
1336
|
+
*/
|
|
1337
|
+
function searchBranch(lum, lo, hi, yBase, metric, target, epsilon, maxIter, anchor, polarity) {
|
|
1338
|
+
const scoreLo = metricScore(metric, lum(lo), yBase, polarity);
|
|
1339
|
+
const scoreHi = metricScore(metric, lum(hi), yBase, polarity);
|
|
1340
|
+
if (scoreLo < target && scoreHi < target) return scoreLo >= scoreHi ? {
|
|
1341
|
+
pos: lo,
|
|
1342
|
+
contrast: scoreLo,
|
|
1343
|
+
met: false
|
|
1344
|
+
} : {
|
|
1345
|
+
pos: hi,
|
|
1346
|
+
contrast: scoreHi,
|
|
1347
|
+
met: false
|
|
1348
|
+
};
|
|
1349
|
+
let low = lo;
|
|
1350
|
+
let high = hi;
|
|
1351
|
+
for (let i = 0; i < maxIter; i++) {
|
|
1352
|
+
if (high - low < epsilon) break;
|
|
1353
|
+
const mid = (low + high) / 2;
|
|
1354
|
+
if (metricScore(metric, lum(mid), yBase, polarity) >= target) if (mid < anchor) low = mid;
|
|
1355
|
+
else high = mid;
|
|
1356
|
+
else if (mid < anchor) high = mid;
|
|
1357
|
+
else low = mid;
|
|
1358
|
+
}
|
|
1359
|
+
const scoreLow = metricScore(metric, lum(low), yBase, polarity);
|
|
1360
|
+
const scoreHigh = metricScore(metric, lum(high), yBase, polarity);
|
|
1361
|
+
const lowPasses = scoreLow >= target;
|
|
1362
|
+
const highPasses = scoreHigh >= target;
|
|
1363
|
+
if (lowPasses && highPasses) return Math.abs(low - anchor) <= Math.abs(high - anchor) ? {
|
|
1364
|
+
pos: low,
|
|
1365
|
+
contrast: scoreLow,
|
|
1366
|
+
met: true
|
|
1367
|
+
} : {
|
|
1368
|
+
pos: high,
|
|
1369
|
+
contrast: scoreHigh,
|
|
1370
|
+
met: true
|
|
1371
|
+
};
|
|
1372
|
+
if (lowPasses) return {
|
|
1373
|
+
pos: low,
|
|
1374
|
+
contrast: scoreLow,
|
|
1375
|
+
met: true
|
|
1376
|
+
};
|
|
1377
|
+
if (highPasses) return {
|
|
1378
|
+
pos: high,
|
|
1379
|
+
contrast: scoreHigh,
|
|
1380
|
+
met: true
|
|
1381
|
+
};
|
|
1382
|
+
return scoreLow >= scoreHigh ? {
|
|
1383
|
+
pos: low,
|
|
1384
|
+
contrast: scoreLow,
|
|
1385
|
+
met: false
|
|
1386
|
+
} : {
|
|
1387
|
+
pos: high,
|
|
1388
|
+
contrast: scoreHigh,
|
|
1389
|
+
met: false
|
|
1390
|
+
};
|
|
1391
|
+
}
|
|
1392
|
+
/**
|
|
1393
|
+
* Closed-form WCAG tone seed: the gray tone whose luminance produces exactly
|
|
1394
|
+
* the target ratio against the base, on the requested side. Used to bias the
|
|
1395
|
+
* preferred tone before the search so chromatic refinement starts close.
|
|
1396
|
+
*/
|
|
1397
|
+
function wcagToneSeed(yBase, target, darker) {
|
|
1398
|
+
const yTarget = darker ? (yBase + .05) / target - .05 : target * (yBase + .05) - .05;
|
|
1399
|
+
const yClamped = Math.max(0, Math.min(1, yTarget));
|
|
1400
|
+
return Math.max(0, Math.min(1, toneFromY(yClamped, REF_EPS) / 100));
|
|
1401
|
+
}
|
|
1402
|
+
function solveNearestContrast(opts) {
|
|
1403
|
+
const { lum, yBase, metric, target, searchTarget, lo, hi, searchAnchor, distanceAnchor, epsilon, maxIterations, flip, initialIsLower, polarity } = opts;
|
|
1404
|
+
const runBranch = (lower) => lower ? searchBranch(lum, lo, searchAnchor, yBase, metric, searchTarget, epsilon, maxIterations, searchAnchor, polarity) : searchBranch(lum, searchAnchor, hi, yBase, metric, searchTarget, epsilon, maxIterations, searchAnchor, polarity);
|
|
1405
|
+
const initialResult = runBranch(initialIsLower);
|
|
1406
|
+
initialResult.met = initialResult.contrast >= target;
|
|
1407
|
+
if (initialResult.met && !flip) return {
|
|
1408
|
+
...initialResult,
|
|
1409
|
+
lower: initialIsLower
|
|
1410
|
+
};
|
|
1411
|
+
if (flip) {
|
|
1412
|
+
const oppositeResult = (initialIsLower ? distanceAnchor < hi : distanceAnchor > lo) ? runBranch(!initialIsLower) : null;
|
|
1413
|
+
if (oppositeResult) oppositeResult.met = oppositeResult.contrast >= target;
|
|
1414
|
+
if (initialResult.met && oppositeResult?.met) return Math.abs(initialResult.pos - distanceAnchor) <= Math.abs(oppositeResult.pos - distanceAnchor) ? {
|
|
1415
|
+
...initialResult,
|
|
1416
|
+
lower: initialIsLower
|
|
1417
|
+
} : {
|
|
1418
|
+
...oppositeResult,
|
|
1419
|
+
lower: !initialIsLower,
|
|
1420
|
+
flipped: true
|
|
1421
|
+
};
|
|
1422
|
+
if (initialResult.met) return {
|
|
1423
|
+
...initialResult,
|
|
1424
|
+
lower: initialIsLower
|
|
1425
|
+
};
|
|
1426
|
+
if (oppositeResult?.met) return {
|
|
1427
|
+
...oppositeResult,
|
|
1428
|
+
lower: !initialIsLower,
|
|
1429
|
+
flipped: true
|
|
1430
|
+
};
|
|
1431
|
+
}
|
|
1432
|
+
const extreme = initialIsLower ? lo : hi;
|
|
1433
|
+
return {
|
|
1434
|
+
pos: extreme,
|
|
1435
|
+
contrast: metricScore(metric, lum(extreme), yBase, polarity),
|
|
1436
|
+
met: false,
|
|
1437
|
+
lower: initialIsLower
|
|
1438
|
+
};
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* Find the tone that satisfies a contrast floor against a base color,
|
|
1442
|
+
* staying as close to `preferredTone` as possible.
|
|
1443
|
+
*/
|
|
1444
|
+
function findToneForContrast(options) {
|
|
1445
|
+
const { hue, saturation, preferredTone, baseLinearRgb, contrast, toneRange = [0, 1], epsilon = 1e-4, maxIterations = 18, pastel = false } = options;
|
|
1446
|
+
const { metric, target, polarity } = contrast;
|
|
1447
|
+
const searchTarget = metric === "wcag" ? target * 1.01 : target + .5;
|
|
1448
|
+
const yBase = metricLuminance(metric, baseLinearRgb);
|
|
1449
|
+
const lum = (t) => cachedLuminance(metric, hue, saturation, t, pastel);
|
|
1450
|
+
const scorePref = metricScore(metric, lum(preferredTone), yBase, polarity);
|
|
1451
|
+
if (scorePref >= searchTarget) return {
|
|
1452
|
+
tone: preferredTone,
|
|
1453
|
+
contrast: scorePref,
|
|
1454
|
+
met: true,
|
|
1455
|
+
branch: "preferred"
|
|
1456
|
+
};
|
|
1457
|
+
const [minT, maxT] = toneRange;
|
|
1458
|
+
const canDarker = preferredTone > minT;
|
|
1459
|
+
const canLighter = preferredTone < maxT;
|
|
1460
|
+
let initialIsDarker;
|
|
1461
|
+
if (options.initialDirection !== void 0) initialIsDarker = options.initialDirection === "darker";
|
|
1462
|
+
else if (canDarker && !canLighter) initialIsDarker = true;
|
|
1463
|
+
else if (!canDarker && canLighter) initialIsDarker = false;
|
|
1464
|
+
else if (!canDarker && !canLighter) return {
|
|
1465
|
+
tone: preferredTone,
|
|
1466
|
+
contrast: scorePref,
|
|
1467
|
+
met: false,
|
|
1468
|
+
branch: "preferred"
|
|
1469
|
+
};
|
|
1470
|
+
else initialIsDarker = metricScore(metric, lum(minT), yBase, polarity) >= metricScore(metric, lum(maxT), yBase, polarity);
|
|
1471
|
+
const solved = solveNearestContrast({
|
|
1472
|
+
lum,
|
|
1473
|
+
yBase,
|
|
1474
|
+
metric,
|
|
1475
|
+
target,
|
|
1476
|
+
searchTarget,
|
|
1477
|
+
lo: minT,
|
|
1478
|
+
hi: maxT,
|
|
1479
|
+
searchAnchor: metric === "wcag" ? clamp(initialIsDarker ? Math.min(preferredTone, wcagToneSeed(yBase, target, true)) : Math.max(preferredTone, wcagToneSeed(yBase, target, false)), minT, maxT) : preferredTone,
|
|
1480
|
+
distanceAnchor: preferredTone,
|
|
1481
|
+
epsilon,
|
|
1482
|
+
maxIterations,
|
|
1483
|
+
flip: options.flip ?? false,
|
|
1484
|
+
initialIsLower: initialIsDarker,
|
|
1485
|
+
polarity
|
|
1486
|
+
});
|
|
1487
|
+
return {
|
|
1488
|
+
tone: solved.pos,
|
|
1489
|
+
contrast: solved.contrast,
|
|
1490
|
+
met: solved.met,
|
|
1491
|
+
branch: solved.lower ? "darker" : "lighter",
|
|
1492
|
+
...solved.flipped ? { flipped: true } : {}
|
|
1493
|
+
};
|
|
1494
|
+
}
|
|
1495
|
+
/**
|
|
1496
|
+
* Find the mix parameter (ratio or opacity) that satisfies a contrast floor
|
|
1497
|
+
* against a base color, staying as close to `preferredValue` as possible.
|
|
1498
|
+
*/
|
|
1499
|
+
function findValueForMixContrast(options) {
|
|
1500
|
+
const { preferredValue, baseLinearRgb, contrast, luminanceAtValue, epsilon = 1e-4, maxIterations = 20 } = options;
|
|
1501
|
+
const { metric, target, polarity } = contrast;
|
|
1502
|
+
const searchTarget = metric === "wcag" ? target * 1.01 : target + .5;
|
|
1503
|
+
const yBase = metricLuminance(metric, baseLinearRgb);
|
|
1504
|
+
const scorePref = metricScore(metric, luminanceAtValue(preferredValue), yBase, polarity);
|
|
1505
|
+
if (scorePref >= searchTarget) return {
|
|
1506
|
+
value: preferredValue,
|
|
1507
|
+
contrast: scorePref,
|
|
1508
|
+
met: true
|
|
1509
|
+
};
|
|
1510
|
+
const canLower = preferredValue > 0;
|
|
1511
|
+
const canUpper = preferredValue < 1;
|
|
1512
|
+
let initialIsLower;
|
|
1513
|
+
if (canLower && !canUpper) initialIsLower = true;
|
|
1514
|
+
else if (!canLower && canUpper) initialIsLower = false;
|
|
1515
|
+
else if (!canLower && !canUpper) return {
|
|
1516
|
+
value: preferredValue,
|
|
1517
|
+
contrast: scorePref,
|
|
1518
|
+
met: false
|
|
1519
|
+
};
|
|
1520
|
+
else initialIsLower = metricScore(metric, luminanceAtValue(0), yBase, polarity) >= metricScore(metric, luminanceAtValue(1), yBase, polarity);
|
|
1521
|
+
const solved = solveNearestContrast({
|
|
1522
|
+
lum: luminanceAtValue,
|
|
1523
|
+
yBase,
|
|
1524
|
+
metric,
|
|
1525
|
+
target,
|
|
1526
|
+
searchTarget,
|
|
1527
|
+
lo: 0,
|
|
1528
|
+
hi: 1,
|
|
1529
|
+
searchAnchor: preferredValue,
|
|
1530
|
+
distanceAnchor: preferredValue,
|
|
1531
|
+
epsilon,
|
|
1532
|
+
maxIterations,
|
|
1533
|
+
flip: options.flip ?? false,
|
|
1534
|
+
initialIsLower,
|
|
1535
|
+
polarity
|
|
1536
|
+
});
|
|
1537
|
+
return {
|
|
1538
|
+
value: solved.pos,
|
|
1539
|
+
contrast: solved.contrast,
|
|
1540
|
+
met: solved.met,
|
|
1541
|
+
...solved.flipped ? { flipped: true } : {}
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
//#endregion
|
|
1546
|
+
//#region src/roles.ts
|
|
1547
|
+
const SURFACE_KEYWORDS = new Set([
|
|
1548
|
+
"surface",
|
|
1549
|
+
"bg",
|
|
1550
|
+
"background",
|
|
1551
|
+
"fill",
|
|
1552
|
+
"canvas",
|
|
1553
|
+
"paper",
|
|
1554
|
+
"layer"
|
|
1555
|
+
]);
|
|
1556
|
+
const TEXT_KEYWORDS = new Set([
|
|
1557
|
+
"text",
|
|
1558
|
+
"fg",
|
|
1559
|
+
"foreground",
|
|
1560
|
+
"content",
|
|
1561
|
+
"ink",
|
|
1562
|
+
"label",
|
|
1563
|
+
"stroke"
|
|
1564
|
+
]);
|
|
1565
|
+
const BORDER_KEYWORDS = new Set([
|
|
1566
|
+
"border",
|
|
1567
|
+
"divider",
|
|
1568
|
+
"outline",
|
|
1569
|
+
"separator",
|
|
1570
|
+
"hairline",
|
|
1571
|
+
"rule"
|
|
1572
|
+
]);
|
|
1573
|
+
const ALIAS_TO_ROLE = {
|
|
1574
|
+
surface: "surface",
|
|
1575
|
+
bg: "surface",
|
|
1576
|
+
background: "surface",
|
|
1577
|
+
fill: "surface",
|
|
1578
|
+
canvas: "surface",
|
|
1579
|
+
paper: "surface",
|
|
1580
|
+
layer: "surface",
|
|
1581
|
+
text: "text",
|
|
1582
|
+
fg: "text",
|
|
1583
|
+
foreground: "text",
|
|
1584
|
+
content: "text",
|
|
1585
|
+
ink: "text",
|
|
1586
|
+
label: "text",
|
|
1587
|
+
stroke: "text",
|
|
1588
|
+
border: "border",
|
|
1589
|
+
divider: "border",
|
|
1590
|
+
outline: "border",
|
|
1591
|
+
separator: "border",
|
|
1592
|
+
hairline: "border",
|
|
1593
|
+
rule: "border"
|
|
1594
|
+
};
|
|
1595
|
+
/**
|
|
1596
|
+
* Normalize a `RoleInput` (canonical value or alias) into a canonical `Role`.
|
|
1597
|
+
* Returns `undefined` for unrecognized strings so callers can fall through to
|
|
1598
|
+
* the next step of the resolution chain.
|
|
1599
|
+
*/
|
|
1600
|
+
function normalizeRole(input) {
|
|
1601
|
+
if (input === void 0) return void 0;
|
|
1602
|
+
return ALIAS_TO_ROLE[input];
|
|
1603
|
+
}
|
|
1604
|
+
/**
|
|
1605
|
+
* Tokenize a color name into lowercase keyword tokens, splitting on
|
|
1606
|
+
* non-alphanumeric boundaries and at camelCase boundaries. Examples:
|
|
1607
|
+
* - `'button-text'` → `['button', 'text']`
|
|
1608
|
+
* - `'inputBg'` → `['input', 'bg']`
|
|
1609
|
+
* - `'card_border-outline'` → `['card', 'border', 'outline']`
|
|
1610
|
+
*/
|
|
1611
|
+
function tokenizeName(name) {
|
|
1612
|
+
const pieces = name.split(/[^0-9a-zA-Z]+/).filter(Boolean);
|
|
1613
|
+
const tokens = [];
|
|
1614
|
+
for (const piece of pieces) {
|
|
1615
|
+
const sub = piece.replace(/([a-z0-9])([A-Z])/g, "$1 $2").split(/\s+/).filter(Boolean);
|
|
1616
|
+
for (const s of sub) tokens.push(s.toLowerCase());
|
|
1617
|
+
}
|
|
1618
|
+
return tokens;
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* Infer a `Role` from a color name by matching its tokens against the role
|
|
1622
|
+
* keyword sets. When multiple tokens match, the **last** recognized token
|
|
1623
|
+
* wins (so `button-text` → `text`, `input-bg` → `surface`, `card-border` →
|
|
1624
|
+
* `border`). Returns `undefined` when no token matches.
|
|
1625
|
+
*/
|
|
1626
|
+
function inferRoleFromName(name) {
|
|
1627
|
+
const tokens = tokenizeName(name);
|
|
1628
|
+
let inferred;
|
|
1629
|
+
for (const token of tokens) if (SURFACE_KEYWORDS.has(token)) inferred = "surface";
|
|
1630
|
+
else if (TEXT_KEYWORDS.has(token)) inferred = "text";
|
|
1631
|
+
else if (BORDER_KEYWORDS.has(token)) inferred = "border";
|
|
1632
|
+
return inferred;
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Map a role to its APCA polarity. `text` and `border` are foreground spots
|
|
1636
|
+
* against their base (the candidate is the text argument); `surface` is the
|
|
1637
|
+
* background (the base is the text argument).
|
|
1638
|
+
*/
|
|
1639
|
+
function roleToPolarity(role) {
|
|
1640
|
+
return role === "surface" ? "bg" : "fg";
|
|
1641
|
+
}
|
|
1642
|
+
/**
|
|
1643
|
+
* The opposite role of `role`, used when a color with no explicit role and no
|
|
1644
|
+
* inferable name depends on a base: the dependent color plays the opposite
|
|
1645
|
+
* role of its base. `surface` ↔ `text`; `border` is treated as a foreground
|
|
1646
|
+
* spot, so its opposite is `surface`.
|
|
1647
|
+
*/
|
|
1648
|
+
function oppositeRole(role) {
|
|
1649
|
+
if (role === "surface") return "text";
|
|
1650
|
+
return "surface";
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
//#endregion
|
|
1654
|
+
//#region src/shadow.ts
|
|
1655
|
+
/**
|
|
1656
|
+
* Shadow color computation.
|
|
1657
|
+
*
|
|
1658
|
+
* Owns the shadow / mix def predicates, default tuning constants, the
|
|
1659
|
+
* tuning merge, and the actual `computeShadow` math (hue blend,
|
|
1660
|
+
* saturation cap, lightness clamp, alpha curve). The resolver consumes
|
|
1661
|
+
* this module per scheme variant.
|
|
1662
|
+
*/
|
|
1663
|
+
function isShadowDef(def) {
|
|
1664
|
+
return def.type === "shadow";
|
|
1665
|
+
}
|
|
1666
|
+
function isMixDef(def) {
|
|
1667
|
+
return def.type === "mix";
|
|
1668
|
+
}
|
|
1669
|
+
const DEFAULT_SHADOW_TUNING = {
|
|
1670
|
+
saturationFactor: .18,
|
|
1671
|
+
maxSaturation: .25,
|
|
1672
|
+
lightnessFactor: .25,
|
|
1673
|
+
lightnessBounds: [.05, .2],
|
|
1674
|
+
minGapTarget: .05,
|
|
1675
|
+
alphaMax: 1,
|
|
1676
|
+
bgHueBlend: .2
|
|
1677
|
+
};
|
|
1678
|
+
function resolveShadowTuning(perColor, globalTuning) {
|
|
1679
|
+
return {
|
|
1680
|
+
...DEFAULT_SHADOW_TUNING,
|
|
1681
|
+
...globalTuning,
|
|
852
1682
|
...perColor,
|
|
853
|
-
lightnessBounds: perColor?.lightnessBounds ??
|
|
1683
|
+
lightnessBounds: perColor?.lightnessBounds ?? globalTuning?.lightnessBounds ?? DEFAULT_SHADOW_TUNING.lightnessBounds
|
|
854
1684
|
};
|
|
855
1685
|
}
|
|
856
1686
|
function circularLerp(a, b, t) {
|
|
@@ -891,36 +1721,49 @@ function computeShadow(bg, fg, intensity, tuning) {
|
|
|
891
1721
|
alpha
|
|
892
1722
|
};
|
|
893
1723
|
}
|
|
894
|
-
|
|
895
|
-
|
|
1724
|
+
|
|
1725
|
+
//#endregion
|
|
1726
|
+
//#region src/validation.ts
|
|
1727
|
+
/**
|
|
1728
|
+
* Color graph validation and topological sort.
|
|
1729
|
+
*
|
|
1730
|
+
* `validateColorDefs` rejects bad references (missing / shadow-referencing /
|
|
1731
|
+
* base/contrast/tone mismatches) and detects cycles before the
|
|
1732
|
+
* resolver runs. `topoSort` orders defs so each color is processed after
|
|
1733
|
+
* its base / bg / fg / target dependencies.
|
|
1734
|
+
*/
|
|
1735
|
+
function validateColorDefs(defs, externalBases) {
|
|
1736
|
+
const localNames = new Set(Object.keys(defs));
|
|
1737
|
+
const allNames = new Set([...localNames, ...externalBases ? externalBases.keys() : []]);
|
|
896
1738
|
for (const [name, def] of Object.entries(defs)) {
|
|
897
1739
|
if (isShadowDef(def)) {
|
|
898
|
-
if (!
|
|
899
|
-
if (isShadowDef(defs[def.bg])) throw new Error(`glaze: shadow "${name}" bg "${def.bg}" references another shadow color.`);
|
|
1740
|
+
if (!allNames.has(def.bg)) throw new Error(`glaze: shadow "${name}" references non-existent bg "${def.bg}".`);
|
|
1741
|
+
if (localNames.has(def.bg) && isShadowDef(defs[def.bg])) throw new Error(`glaze: shadow "${name}" bg "${def.bg}" references another shadow color.`);
|
|
900
1742
|
if (def.fg !== void 0) {
|
|
901
|
-
if (!
|
|
902
|
-
if (isShadowDef(defs[def.fg])) throw new Error(`glaze: shadow "${name}" fg "${def.fg}" references another shadow color.`);
|
|
1743
|
+
if (!allNames.has(def.fg)) throw new Error(`glaze: shadow "${name}" references non-existent fg "${def.fg}".`);
|
|
1744
|
+
if (localNames.has(def.fg) && isShadowDef(defs[def.fg])) throw new Error(`glaze: shadow "${name}" fg "${def.fg}" references another shadow color.`);
|
|
903
1745
|
}
|
|
904
1746
|
continue;
|
|
905
1747
|
}
|
|
906
1748
|
if (isMixDef(def)) {
|
|
907
|
-
if (!
|
|
908
|
-
if (!
|
|
909
|
-
if (isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
|
|
910
|
-
if (isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
|
|
1749
|
+
if (!allNames.has(def.base)) throw new Error(`glaze: mix "${name}" references non-existent base "${def.base}".`);
|
|
1750
|
+
if (!allNames.has(def.target)) throw new Error(`glaze: mix "${name}" references non-existent target "${def.target}".`);
|
|
1751
|
+
if (localNames.has(def.base) && isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
|
|
1752
|
+
if (localNames.has(def.target) && isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
|
|
911
1753
|
continue;
|
|
912
1754
|
}
|
|
913
1755
|
const regDef = def;
|
|
914
1756
|
if (regDef.contrast !== void 0 && !regDef.base) throw new Error(`glaze: color "${name}" has "contrast" without "base".`);
|
|
915
|
-
if (regDef.
|
|
916
|
-
if (regDef.base && !
|
|
917
|
-
if (regDef.base && isShadowDef(defs[regDef.base])) throw new Error(`glaze: color "${name}" base "${regDef.base}" references a shadow color.`);
|
|
918
|
-
if (!
|
|
919
|
-
if (regDef.contrast !== void 0 && regDef.opacity !== void 0) console.warn(`glaze: color "${name}" has both "contrast" and "opacity". Opacity makes perceived
|
|
1757
|
+
if (regDef.tone !== void 0 && !isAbsoluteTone(regDef.tone) && !regDef.base) throw new Error(`glaze: color "${name}" has relative "tone" without "base".`);
|
|
1758
|
+
if (regDef.base && !allNames.has(regDef.base)) throw new Error(`glaze: color "${name}" references non-existent base "${regDef.base}".`);
|
|
1759
|
+
if (regDef.base && localNames.has(regDef.base) && isShadowDef(defs[regDef.base])) throw new Error(`glaze: color "${name}" base "${regDef.base}" references a shadow color.`);
|
|
1760
|
+
if (!isAbsoluteTone(regDef.tone) && regDef.base === void 0) throw new Error(`glaze: color "${name}" must have either absolute "tone" (root) or "base" (dependent).`);
|
|
1761
|
+
if (regDef.contrast !== void 0 && regDef.opacity !== void 0) console.warn(`glaze: color "${name}" has both "contrast" and "opacity". Opacity makes perceived tone unpredictable.`);
|
|
920
1762
|
}
|
|
921
1763
|
const visited = /* @__PURE__ */ new Set();
|
|
922
1764
|
const inStack = /* @__PURE__ */ new Set();
|
|
923
1765
|
function dfs(name) {
|
|
1766
|
+
if (!localNames.has(name)) return;
|
|
924
1767
|
if (inStack.has(name)) throw new Error(`glaze: circular base reference detected involving "${name}".`);
|
|
925
1768
|
if (visited.has(name)) return;
|
|
926
1769
|
inStack.add(name);
|
|
@@ -938,7 +1781,7 @@ function validateColorDefs(defs) {
|
|
|
938
1781
|
inStack.delete(name);
|
|
939
1782
|
visited.add(name);
|
|
940
1783
|
}
|
|
941
|
-
for (const name of
|
|
1784
|
+
for (const name of localNames) dfs(name);
|
|
942
1785
|
}
|
|
943
1786
|
function topoSort(defs) {
|
|
944
1787
|
const result = [];
|
|
@@ -947,6 +1790,7 @@ function topoSort(defs) {
|
|
|
947
1790
|
if (visited.has(name)) return;
|
|
948
1791
|
visited.add(name);
|
|
949
1792
|
const def = defs[name];
|
|
1793
|
+
if (def === void 0) return;
|
|
950
1794
|
if (isShadowDef(def)) {
|
|
951
1795
|
visit(def.bg);
|
|
952
1796
|
if (def.fg) visit(def.fg);
|
|
@@ -959,424 +1803,1574 @@ function topoSort(defs) {
|
|
|
959
1803
|
}
|
|
960
1804
|
result.push(name);
|
|
961
1805
|
}
|
|
962
|
-
for (const name of Object.keys(defs)) visit(name);
|
|
1806
|
+
for (const name of Object.keys(defs)) visit(name);
|
|
1807
|
+
return result;
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
//#endregion
|
|
1811
|
+
//#region src/warnings.ts
|
|
1812
|
+
/**
|
|
1813
|
+
* Contrast-warning dispatcher.
|
|
1814
|
+
*
|
|
1815
|
+
* Tokens memoize their resolution, but a long-lived process (e.g. a dev
|
|
1816
|
+
* server with HMR) can re-resolve the same theme many times. The cache
|
|
1817
|
+
* here dedupes warnings within a session with a soft cap to keep noise
|
|
1818
|
+
* bounded.
|
|
1819
|
+
*/
|
|
1820
|
+
const CONTRAST_WARN_CACHE_LIMIT = 256;
|
|
1821
|
+
const contrastWarnCache = /* @__PURE__ */ new Set();
|
|
1822
|
+
/**
|
|
1823
|
+
* Slack factor below the requested target before we emit a warning.
|
|
1824
|
+
* The contrast solver overshoots to absorb rounding noise, so an actual
|
|
1825
|
+
* value within ~2x that overshoot is effectively a pass.
|
|
1826
|
+
*/
|
|
1827
|
+
const CONTRAST_WARN_SLACK_WCAG = .98;
|
|
1828
|
+
/** APCA Lc is on a 0–106 scale; allow a small absolute slack. */
|
|
1829
|
+
const CONTRAST_WARN_SLACK_APCA = 1.5;
|
|
1830
|
+
function schemeLabel(isDark, isHighContrast) {
|
|
1831
|
+
if (isDark && isHighContrast) return "darkContrast";
|
|
1832
|
+
if (isDark) return "dark";
|
|
1833
|
+
if (isHighContrast) return "lightContrast";
|
|
1834
|
+
return "light";
|
|
1835
|
+
}
|
|
1836
|
+
function metricLabel(c) {
|
|
1837
|
+
return c.metric === "apca" ? `APCA Lc ${c.target.toFixed(1)}` : `WCAG ${c.target.toFixed(2)}`;
|
|
1838
|
+
}
|
|
1839
|
+
function dedupe(key) {
|
|
1840
|
+
if (contrastWarnCache.has(key)) return true;
|
|
1841
|
+
if (contrastWarnCache.size >= CONTRAST_WARN_CACHE_LIMIT) contrastWarnCache.clear();
|
|
1842
|
+
contrastWarnCache.add(key);
|
|
1843
|
+
return false;
|
|
1844
|
+
}
|
|
1845
|
+
/** Warn when the solver could not reach the requested contrast floor. */
|
|
1846
|
+
function warnContrastUnmet(name, isDark, isHighContrast, contrast, actual) {
|
|
1847
|
+
if (actual >= (contrast.metric === "apca" ? contrast.target - CONTRAST_WARN_SLACK_APCA : contrast.target * CONTRAST_WARN_SLACK_WCAG)) return;
|
|
1848
|
+
const scheme = schemeLabel(isDark, isHighContrast);
|
|
1849
|
+
if (dedupe(`unmet|${name}|${scheme}|${contrast.metric}|${contrast.target.toFixed(2)}|${actual.toFixed(2)}`)) return;
|
|
1850
|
+
console.warn(`glaze: color "${name}" cannot meet ${metricLabel(contrast)} in ${scheme} scheme (got ${actual.toFixed(2)}). Try widening the tone window, lowering the contrast target, or picking a base color further from this color's tone.`);
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Verification (§10): a chromatic swatch inherits the gray tone's
|
|
1854
|
+
* lightness but drifts in real luminance, so a contrast-floored color may
|
|
1855
|
+
* land slightly under the contrast its tone implies. Emit an advisory
|
|
1856
|
+
* warning when the actual measured contrast drifts below the target.
|
|
1857
|
+
*/
|
|
1858
|
+
function warnContrastDrift(name, isDark, isHighContrast, contrast, yColor, yBase) {
|
|
1859
|
+
const actual = contrast.metric === "apca" ? Math.abs(contrast.polarity === "bg" ? apcaContrast(yBase, yColor) : apcaContrast(yColor, yBase)) : contrastRatioFromLuminance(yColor, yBase);
|
|
1860
|
+
if (actual >= (contrast.metric === "apca" ? contrast.target - CONTRAST_WARN_SLACK_APCA : contrast.target * CONTRAST_WARN_SLACK_WCAG)) return;
|
|
1861
|
+
const scheme = schemeLabel(isDark, isHighContrast);
|
|
1862
|
+
if (dedupe(`drift|${name}|${scheme}|${contrast.metric}|${contrast.target.toFixed(2)}|${actual.toFixed(2)}`)) return;
|
|
1863
|
+
console.warn(`glaze: color "${name}" drifts below ${metricLabel(contrast)} in ${scheme} scheme (measured ${actual.toFixed(2)}). Chromatic luminance differs from the gray tone; nudge the tone or saturation if the floor matters.`);
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
//#endregion
|
|
1867
|
+
//#region src/resolver.ts
|
|
1868
|
+
/**
|
|
1869
|
+
* Color resolution engine.
|
|
1870
|
+
*
|
|
1871
|
+
* Runs the four-pass solver (light → light-HC → dark → dark-HC) that
|
|
1872
|
+
* turns a `ColorMap` into a fully resolved `ResolvedColor` per name.
|
|
1873
|
+
* Owns the per-scheme resolve helpers for regular, shadow, and mix
|
|
1874
|
+
* color defs.
|
|
1875
|
+
*
|
|
1876
|
+
* Variants are stored in OKHST: `h` / `s` are OKHSL hue/saturation and
|
|
1877
|
+
* `t` is the canonical contrast-uniform tone (0–1, reference eps). The
|
|
1878
|
+
* resolver works in tone for regular colors and converts to/from OKHSL
|
|
1879
|
+
* lightness only at the mix/shadow and luminance edges.
|
|
1880
|
+
*
|
|
1881
|
+
* Every function receives a single `GlazeConfigResolved` so the full
|
|
1882
|
+
* per-instance config (including overrides) is available without
|
|
1883
|
+
* re-reading the global singleton mid-resolve.
|
|
1884
|
+
*/
|
|
1885
|
+
function getSchemeVariant(color, isDark, isHighContrast) {
|
|
1886
|
+
if (isDark && isHighContrast) return color.darkContrast;
|
|
1887
|
+
if (isDark) return color.dark;
|
|
1888
|
+
if (isHighContrast) return color.lightContrast;
|
|
1889
|
+
return color.light;
|
|
1890
|
+
}
|
|
1891
|
+
/** Edge adapter: resolved variant (`t`) → OKHSL-lightness variant. */
|
|
1892
|
+
function toOkhslVariant(v) {
|
|
1893
|
+
const c = variantToOkhsl(v);
|
|
1894
|
+
return {
|
|
1895
|
+
h: c.h,
|
|
1896
|
+
s: c.s,
|
|
1897
|
+
l: c.l,
|
|
1898
|
+
alpha: v.alpha,
|
|
1899
|
+
pastel: v.pastel
|
|
1900
|
+
};
|
|
1901
|
+
}
|
|
1902
|
+
/** Edge adapter: OKHSL-lightness variant → resolved variant (`t`). */
|
|
1903
|
+
function toToneVariant(v) {
|
|
1904
|
+
const c = okhslToOkhst({
|
|
1905
|
+
h: v.h,
|
|
1906
|
+
s: v.s,
|
|
1907
|
+
l: v.l
|
|
1908
|
+
});
|
|
1909
|
+
return {
|
|
1910
|
+
h: c.h,
|
|
1911
|
+
s: c.s,
|
|
1912
|
+
t: c.t,
|
|
1913
|
+
alpha: v.alpha
|
|
1914
|
+
};
|
|
1915
|
+
}
|
|
1916
|
+
/**
|
|
1917
|
+
* Resolve the role of a base color referenced by `baseName`, returning the
|
|
1918
|
+
* role the *dependent* color should take (the opposite of the base's role).
|
|
1919
|
+
* A base that lives in `defs` recursively resolves and is inverted via
|
|
1920
|
+
* `oppositeRole`; an external base (no local def, e.g. an injected standalone
|
|
1921
|
+
* token) is treated as a background, so the dependent defaults to foreground
|
|
1922
|
+
* (`'text'`).
|
|
1923
|
+
*/
|
|
1924
|
+
function resolveBaseRoleInMap(baseName, defs, inferRole, roles) {
|
|
1925
|
+
if (!baseName) return void 0;
|
|
1926
|
+
const baseDef = defs[baseName];
|
|
1927
|
+
if (!baseDef) return "text";
|
|
1928
|
+
return oppositeRole(resolveRoleInMap(baseName, baseDef, defs, inferRole, roles));
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* Role-resolution core that does not need a full `ResolveContext`. Shared by
|
|
1932
|
+
* the resolver (via `resolveRole`) and `verifyContrastDrift`.
|
|
1933
|
+
*/
|
|
1934
|
+
function resolveRoleInMap(name, def, defs, inferRole, roles) {
|
|
1935
|
+
const cached = roles.get(name);
|
|
1936
|
+
if (cached) return cached;
|
|
1937
|
+
let role;
|
|
1938
|
+
if (isShadowDef(def)) role = "surface";
|
|
1939
|
+
else if (isMixDef(def)) role = normalizeRole(def.role) ?? (inferRole ? inferRoleFromName(name) : void 0) ?? resolveBaseRoleInMap(def.base, defs, inferRole, roles) ?? "text";
|
|
1940
|
+
else {
|
|
1941
|
+
const regDef = def;
|
|
1942
|
+
role = normalizeRole(regDef.role) ?? (inferRole ? inferRoleFromName(name) : void 0) ?? resolveBaseRoleInMap(regDef.base, defs, inferRole, roles) ?? "text";
|
|
1943
|
+
}
|
|
1944
|
+
const finalRole = role ?? "text";
|
|
1945
|
+
roles.set(name, finalRole);
|
|
1946
|
+
return finalRole;
|
|
1947
|
+
}
|
|
1948
|
+
/**
|
|
1949
|
+
* Resolve a color's semantic `role` (text / surface / border) per the chain:
|
|
1950
|
+
* 1. explicit `def.role` (normalized)
|
|
1951
|
+
* 2. inferred from the color name when `config.inferRole` is on
|
|
1952
|
+
* 3. opposite of the base's role
|
|
1953
|
+
* 4. `'text'` (foreground) default
|
|
1954
|
+
*
|
|
1955
|
+
* Memoized on `ctx.roles` so the four scheme passes share one resolution.
|
|
1956
|
+
* Shadows have no contrast participation and default to `'surface'`.
|
|
1957
|
+
*/
|
|
1958
|
+
function resolveRole(name, def, ctx) {
|
|
1959
|
+
return resolveRoleInMap(name, def, ctx.defs, ctx.config.inferRole, ctx.roles);
|
|
1960
|
+
}
|
|
1961
|
+
function resolveContrastSpec(spec, isHighContrast, polarity) {
|
|
1962
|
+
const outerExplicitHC = Array.isArray(spec);
|
|
1963
|
+
return resolveContrastForMode(isHighContrast ? pairHC(spec) : pairNormal(spec), isHighContrast, polarity, outerExplicitHC);
|
|
1964
|
+
}
|
|
1965
|
+
/**
|
|
1966
|
+
* Apply the relative-tone delta against a base, honoring `flip`.
|
|
1967
|
+
*
|
|
1968
|
+
* When `flip` is on and `base + delta` falls outside `[0, 100]`, mirror the
|
|
1969
|
+
* delta to the other side of the base (so an offset that would clamp instead
|
|
1970
|
+
* reflects back into range). When off, the caller clamps as usual.
|
|
1971
|
+
*/
|
|
1972
|
+
function applyToneFlip(delta, baseTone, flip) {
|
|
1973
|
+
if (!flip) return delta;
|
|
1974
|
+
const target = baseTone + delta;
|
|
1975
|
+
if (target >= 0 && target <= 100) return delta;
|
|
1976
|
+
return -delta;
|
|
1977
|
+
}
|
|
1978
|
+
function resolveRootColor(def, isHighContrast) {
|
|
1979
|
+
const rawT = def.tone;
|
|
1980
|
+
return {
|
|
1981
|
+
authorTone: clamp(parseToneValue(isHighContrast ? pairHC(rawT) : pairNormal(rawT)).value, 0, 100),
|
|
1982
|
+
satFactor: clamp(def.saturation ?? 1, 0, 1)
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
function resolveDependentColor(name, def, ctx, isHighContrast, isDark, effectiveHue, polarity, effectivePastel) {
|
|
1986
|
+
const baseName = def.base;
|
|
1987
|
+
const baseResolved = ctx.resolved.get(baseName);
|
|
1988
|
+
if (!baseResolved) throw new Error(`glaze: base "${baseName}" not yet resolved for "${name}".`);
|
|
1989
|
+
const mode = def.mode ?? "auto";
|
|
1990
|
+
const satFactor = clamp(def.saturation ?? 1, 0, 1);
|
|
1991
|
+
const flip = def.autoFlip ?? ctx.config.autoFlip;
|
|
1992
|
+
const pastel = effectivePastel;
|
|
1993
|
+
const baseVariant = getSchemeVariant(baseResolved, isDark, isHighContrast);
|
|
1994
|
+
const baseTone = baseVariant.t * 100;
|
|
1995
|
+
let preferredTone;
|
|
1996
|
+
const rawTone = def.tone;
|
|
1997
|
+
if (rawTone === void 0) preferredTone = baseTone;
|
|
1998
|
+
else {
|
|
1999
|
+
const parsed = parseToneValue(isHighContrast ? pairHC(rawTone) : pairNormal(rawTone));
|
|
2000
|
+
if (parsed.kind === "relative") if (isDark && mode === "auto") {
|
|
2001
|
+
const baseLightTone = getSchemeVariant(baseResolved, false, isHighContrast).t * 100;
|
|
2002
|
+
preferredTone = mapToneForScheme(clamp(baseLightTone + applyToneFlip(parsed.value, baseLightTone, flip), 0, 100), "auto", true, isHighContrast, ctx.config);
|
|
2003
|
+
} else preferredTone = clamp(baseTone + applyToneFlip(parsed.value, baseTone, flip), 0, 100);
|
|
2004
|
+
else preferredTone = mapToneForScheme(parsed.value, mode, isDark, isHighContrast, ctx.config);
|
|
2005
|
+
}
|
|
2006
|
+
const rawContrast = def.contrast;
|
|
2007
|
+
if (rawContrast !== void 0) {
|
|
2008
|
+
const resolvedContrast = resolveContrastSpec(rawContrast, isHighContrast, polarity);
|
|
2009
|
+
const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode, ctx.config) : satFactor * ctx.saturation / 100;
|
|
2010
|
+
const baseOkhsl = toOkhslVariant(baseVariant);
|
|
2011
|
+
const baseLinearRgb = okhslToLinearSrgb(baseOkhsl.h, baseOkhsl.s, baseOkhsl.l, baseVariant.pastel ?? ctx.config.pastel);
|
|
2012
|
+
const toneRange = schemeToneRange(isDark, mode, isHighContrast, ctx.config);
|
|
2013
|
+
let initialDirection;
|
|
2014
|
+
if (preferredTone < baseTone) initialDirection = "darker";
|
|
2015
|
+
else if (preferredTone > baseTone) initialDirection = "lighter";
|
|
2016
|
+
const result = findToneForContrast({
|
|
2017
|
+
hue: effectiveHue,
|
|
2018
|
+
saturation: effectiveSat,
|
|
2019
|
+
preferredTone: clamp(preferredTone / 100, toneRange[0], toneRange[1]),
|
|
2020
|
+
baseLinearRgb,
|
|
2021
|
+
contrast: resolvedContrast,
|
|
2022
|
+
toneRange: [0, 1],
|
|
2023
|
+
initialDirection,
|
|
2024
|
+
flip,
|
|
2025
|
+
pastel
|
|
2026
|
+
});
|
|
2027
|
+
if (!result.met) warnContrastUnmet(name, isDark, isHighContrast, resolvedContrast, result.contrast);
|
|
2028
|
+
return {
|
|
2029
|
+
tone: result.tone * 100,
|
|
2030
|
+
satFactor
|
|
2031
|
+
};
|
|
2032
|
+
}
|
|
2033
|
+
return {
|
|
2034
|
+
tone: clamp(preferredTone, 0, 100),
|
|
2035
|
+
satFactor
|
|
2036
|
+
};
|
|
2037
|
+
}
|
|
2038
|
+
function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
|
|
2039
|
+
if (isShadowDef(def)) return resolveShadowForScheme(def, ctx, isDark, isHighContrast);
|
|
2040
|
+
if (isMixDef(def)) return resolveMixForScheme(name, def, ctx, isDark, isHighContrast);
|
|
2041
|
+
const regDef = def;
|
|
2042
|
+
const mode = regDef.mode ?? "auto";
|
|
2043
|
+
const isRoot = isAbsoluteTone(regDef.tone) && !regDef.base;
|
|
2044
|
+
const effectiveHue = resolveEffectiveHue(ctx.hue, regDef.hue);
|
|
2045
|
+
const polarity = roleToPolarity(resolveRole(name, def, ctx));
|
|
2046
|
+
const pastel = regDef.pastel ?? ctx.config.pastel;
|
|
2047
|
+
let finalTone;
|
|
2048
|
+
let satFactor;
|
|
2049
|
+
if (isRoot) {
|
|
2050
|
+
const root = resolveRootColor(regDef, isHighContrast);
|
|
2051
|
+
finalTone = mapToneForScheme(root.authorTone, mode, isDark, isHighContrast, ctx.config);
|
|
2052
|
+
satFactor = root.satFactor;
|
|
2053
|
+
} else {
|
|
2054
|
+
const dep = resolveDependentColor(name, regDef, ctx, isHighContrast, isDark, effectiveHue, polarity, pastel);
|
|
2055
|
+
finalTone = dep.tone;
|
|
2056
|
+
satFactor = dep.satFactor;
|
|
2057
|
+
}
|
|
2058
|
+
const baseSat = satFactor * ctx.saturation / 100;
|
|
2059
|
+
const finalSat = isDark ? mapSaturationDark(baseSat, mode, ctx.config) : baseSat;
|
|
2060
|
+
const toneFraction = clamp(finalTone / 100, 0, 1);
|
|
2061
|
+
return {
|
|
2062
|
+
h: effectiveHue,
|
|
2063
|
+
s: clamp(finalSat, 0, 1),
|
|
2064
|
+
t: toneFraction,
|
|
2065
|
+
alpha: regDef.opacity ?? 1,
|
|
2066
|
+
pastel
|
|
2067
|
+
};
|
|
2068
|
+
}
|
|
2069
|
+
function resolveShadowForScheme(def, ctx, isDark, isHighContrast) {
|
|
2070
|
+
const bgVariant = toOkhslVariant(getSchemeVariant(ctx.resolved.get(def.bg), isDark, isHighContrast));
|
|
2071
|
+
let fgVariant;
|
|
2072
|
+
if (def.fg) fgVariant = toOkhslVariant(getSchemeVariant(ctx.resolved.get(def.fg), isDark, isHighContrast));
|
|
2073
|
+
const intensity = isHighContrast ? pairHC(def.intensity) : pairNormal(def.intensity);
|
|
2074
|
+
const tuning = resolveShadowTuning(def.tuning, ctx.config.shadowTuning);
|
|
2075
|
+
return {
|
|
2076
|
+
...toToneVariant(computeShadow(bgVariant, fgVariant, intensity, tuning)),
|
|
2077
|
+
pastel: def.pastel ?? ctx.config.pastel
|
|
2078
|
+
};
|
|
2079
|
+
}
|
|
2080
|
+
function okhslVariantToLinearRgb(v, pastel) {
|
|
2081
|
+
return okhslToLinearSrgb(v.h, v.s, v.l, pastel);
|
|
2082
|
+
}
|
|
2083
|
+
/**
|
|
2084
|
+
* Resolve hue for OKHSL mixing, handling achromatic colors.
|
|
2085
|
+
* When one color has no saturation, its hue is meaningless —
|
|
2086
|
+
* use the hue from the color that has saturation (matches CSS
|
|
2087
|
+
* color-mix "missing component" behavior).
|
|
2088
|
+
*/
|
|
2089
|
+
function mixHue(base, target, t) {
|
|
2090
|
+
const SAT_EPSILON = 1e-6;
|
|
2091
|
+
const baseHasSat = base.s > SAT_EPSILON;
|
|
2092
|
+
const targetHasSat = target.s > SAT_EPSILON;
|
|
2093
|
+
if (baseHasSat && targetHasSat) return circularLerp(base.h, target.h, t);
|
|
2094
|
+
if (targetHasSat) return target.h;
|
|
2095
|
+
return base.h;
|
|
2096
|
+
}
|
|
2097
|
+
function linearSrgbLerp(base, target, t) {
|
|
2098
|
+
return [
|
|
2099
|
+
base[0] + (target[0] - base[0]) * t,
|
|
2100
|
+
base[1] + (target[1] - base[1]) * t,
|
|
2101
|
+
base[2] + (target[2] - base[2]) * t
|
|
2102
|
+
];
|
|
2103
|
+
}
|
|
2104
|
+
function linearRgbToToneVariant(rgb, pastel) {
|
|
2105
|
+
const [h, s, l] = srgbToOkhsl([
|
|
2106
|
+
Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[0]))),
|
|
2107
|
+
Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[1]))),
|
|
2108
|
+
Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[2])))
|
|
2109
|
+
], pastel);
|
|
2110
|
+
return toToneVariant({
|
|
2111
|
+
h,
|
|
2112
|
+
s,
|
|
2113
|
+
l,
|
|
2114
|
+
alpha: 1
|
|
2115
|
+
});
|
|
2116
|
+
}
|
|
2117
|
+
function resolveMixForScheme(name, def, ctx, isDark, isHighContrast) {
|
|
2118
|
+
const baseResolved = ctx.resolved.get(def.base);
|
|
2119
|
+
const targetResolved = ctx.resolved.get(def.target);
|
|
2120
|
+
const baseVariant = toOkhslVariant(getSchemeVariant(baseResolved, isDark, isHighContrast));
|
|
2121
|
+
const targetVariant = toOkhslVariant(getSchemeVariant(targetResolved, isDark, isHighContrast));
|
|
2122
|
+
let t = clamp(isHighContrast ? pairHC(def.value) : pairNormal(def.value), 0, 100) / 100;
|
|
2123
|
+
const blend = def.blend ?? "opaque";
|
|
2124
|
+
const space = def.space ?? "okhsl";
|
|
2125
|
+
const polarity = roleToPolarity(resolveRole(name, def, ctx));
|
|
2126
|
+
const pastel = def.pastel ?? ctx.config.pastel;
|
|
2127
|
+
const baseLinear = okhslVariantToLinearRgb(baseVariant, baseVariant.pastel ?? ctx.config.pastel);
|
|
2128
|
+
const targetLinear = okhslVariantToLinearRgb(targetVariant, targetVariant.pastel ?? ctx.config.pastel);
|
|
2129
|
+
if (def.contrast !== void 0) {
|
|
2130
|
+
const resolvedContrast = resolveContrastSpec(def.contrast, isHighContrast, polarity);
|
|
2131
|
+
const metric = resolvedContrast.metric;
|
|
2132
|
+
let luminanceAt;
|
|
2133
|
+
if (blend === "transparent" || space === "srgb") luminanceAt = (v) => metricLuminance(metric, linearSrgbLerp(baseLinear, targetLinear, v));
|
|
2134
|
+
else luminanceAt = (v) => {
|
|
2135
|
+
return metricLuminance(metric, okhslToLinearSrgb(mixHue(baseVariant, targetVariant, v), baseVariant.s + (targetVariant.s - baseVariant.s) * v, baseVariant.l + (targetVariant.l - baseVariant.l) * v, pastel));
|
|
2136
|
+
};
|
|
2137
|
+
t = findValueForMixContrast({
|
|
2138
|
+
preferredValue: t,
|
|
2139
|
+
baseLinearRgb: baseLinear,
|
|
2140
|
+
targetLinearRgb: targetLinear,
|
|
2141
|
+
contrast: resolvedContrast,
|
|
2142
|
+
luminanceAtValue: luminanceAt,
|
|
2143
|
+
flip: ctx.config.autoFlip
|
|
2144
|
+
}).value;
|
|
2145
|
+
}
|
|
2146
|
+
if (blend === "transparent") return {
|
|
2147
|
+
...toToneVariant({
|
|
2148
|
+
h: targetVariant.h,
|
|
2149
|
+
s: targetVariant.s,
|
|
2150
|
+
l: targetVariant.l,
|
|
2151
|
+
alpha: clamp(t, 0, 1)
|
|
2152
|
+
}),
|
|
2153
|
+
pastel
|
|
2154
|
+
};
|
|
2155
|
+
if (space === "srgb") return {
|
|
2156
|
+
...linearRgbToToneVariant(linearSrgbLerp(baseLinear, targetLinear, t), pastel),
|
|
2157
|
+
pastel
|
|
2158
|
+
};
|
|
2159
|
+
return {
|
|
2160
|
+
...toToneVariant({
|
|
2161
|
+
h: mixHue(baseVariant, targetVariant, t),
|
|
2162
|
+
s: clamp(baseVariant.s + (targetVariant.s - baseVariant.s) * t, 0, 1),
|
|
2163
|
+
l: clamp(baseVariant.l + (targetVariant.l - baseVariant.l) * t, 0, 1),
|
|
2164
|
+
alpha: 1
|
|
2165
|
+
}),
|
|
2166
|
+
pastel
|
|
2167
|
+
};
|
|
2168
|
+
}
|
|
2169
|
+
function defMode(def) {
|
|
2170
|
+
if (isShadowDef(def) || isMixDef(def)) return void 0;
|
|
2171
|
+
return def.mode ?? "auto";
|
|
2172
|
+
}
|
|
2173
|
+
/**
|
|
2174
|
+
* Run a single resolve pass over all local names. Pass 1 lazily creates
|
|
2175
|
+
* each `ResolvedColor` (all four slots seeded with the just-resolved
|
|
2176
|
+
* variant) the first time it sees a name; later passes update the
|
|
2177
|
+
* `target` slot on the existing record.
|
|
2178
|
+
*/
|
|
2179
|
+
function runPass(order, defs, ctx, isDark, isHighContrast, target) {
|
|
2180
|
+
const out = /* @__PURE__ */ new Map();
|
|
2181
|
+
for (const name of order) {
|
|
2182
|
+
const variant = resolveColorForScheme(name, defs[name], ctx, isDark, isHighContrast);
|
|
2183
|
+
out.set(name, variant);
|
|
2184
|
+
const existing = ctx.resolved.get(name);
|
|
2185
|
+
if (existing) ctx.resolved.set(name, {
|
|
2186
|
+
...existing,
|
|
2187
|
+
[target]: variant
|
|
2188
|
+
});
|
|
2189
|
+
else ctx.resolved.set(name, {
|
|
2190
|
+
name,
|
|
2191
|
+
light: variant,
|
|
2192
|
+
dark: variant,
|
|
2193
|
+
lightContrast: variant,
|
|
2194
|
+
darkContrast: variant,
|
|
2195
|
+
mode: defMode(defs[name])
|
|
2196
|
+
});
|
|
2197
|
+
}
|
|
2198
|
+
return out;
|
|
2199
|
+
}
|
|
2200
|
+
/**
|
|
2201
|
+
* Re-seed a single variant slot with a previously-resolved map so the
|
|
2202
|
+
* upcoming pass reads sensible fallbacks via `getSchemeVariant`.
|
|
2203
|
+
*/
|
|
2204
|
+
function seedField(order, ctx, field, source) {
|
|
2205
|
+
for (const name of order) {
|
|
2206
|
+
const existing = ctx.resolved.get(name);
|
|
2207
|
+
ctx.resolved.set(name, {
|
|
2208
|
+
...existing,
|
|
2209
|
+
[field]: source.get(name)
|
|
2210
|
+
});
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
/**
|
|
2214
|
+
* After the four passes, surface chromatic contrast drift (§10): a color
|
|
2215
|
+
* resolved with a `base` + `contrast` may land slightly under the contrast
|
|
2216
|
+
* its tone implies because chromatic luminance drifts from the gray tone.
|
|
2217
|
+
*/
|
|
2218
|
+
function verifyContrastDrift(order, defs, result, config) {
|
|
2219
|
+
const roles = /* @__PURE__ */ new Map();
|
|
2220
|
+
for (const name of order) {
|
|
2221
|
+
const def = defs[name];
|
|
2222
|
+
if (isShadowDef(def) || isMixDef(def)) continue;
|
|
2223
|
+
const regDef = def;
|
|
2224
|
+
if (regDef.contrast === void 0 || !regDef.base) continue;
|
|
2225
|
+
const color = result.get(name);
|
|
2226
|
+
const base = result.get(regDef.base);
|
|
2227
|
+
if (!color || !base) continue;
|
|
2228
|
+
const polarity = roleToPolarity(resolveRoleInMap(name, def, defs, config.inferRole, roles));
|
|
2229
|
+
for (const s of [
|
|
2230
|
+
{
|
|
2231
|
+
isDark: false,
|
|
2232
|
+
isHighContrast: false,
|
|
2233
|
+
field: "light"
|
|
2234
|
+
},
|
|
2235
|
+
{
|
|
2236
|
+
isDark: false,
|
|
2237
|
+
isHighContrast: true,
|
|
2238
|
+
field: "lightContrast"
|
|
2239
|
+
},
|
|
2240
|
+
{
|
|
2241
|
+
isDark: true,
|
|
2242
|
+
isHighContrast: false,
|
|
2243
|
+
field: "dark"
|
|
2244
|
+
},
|
|
2245
|
+
{
|
|
2246
|
+
isDark: true,
|
|
2247
|
+
isHighContrast: true,
|
|
2248
|
+
field: "darkContrast"
|
|
2249
|
+
}
|
|
2250
|
+
]) {
|
|
2251
|
+
const spec = resolveContrastSpec(regDef.contrast, s.isHighContrast, polarity);
|
|
2252
|
+
const cVariant = color[s.field];
|
|
2253
|
+
const bVariant = base[s.field];
|
|
2254
|
+
const cOkhsl = toOkhslVariant(cVariant);
|
|
2255
|
+
const bOkhsl = toOkhslVariant(bVariant);
|
|
2256
|
+
const cPastel = cVariant.pastel ?? config.pastel;
|
|
2257
|
+
const bPastel = bVariant.pastel ?? config.pastel;
|
|
2258
|
+
const yC = metricLuminance(spec.metric, okhslToLinearSrgb(cOkhsl.h, cOkhsl.s, cOkhsl.l, cPastel));
|
|
2259
|
+
const yB = metricLuminance(spec.metric, okhslToLinearSrgb(bOkhsl.h, bOkhsl.s, bOkhsl.l, bPastel));
|
|
2260
|
+
warnContrastDrift(name, s.isDark, s.isHighContrast, spec, yC, yB);
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
function resolveAllColors(hue, saturation, defs, config, externalBases) {
|
|
2265
|
+
validateColorDefs(defs, externalBases);
|
|
2266
|
+
const order = topoSort(defs);
|
|
2267
|
+
const ctx = {
|
|
2268
|
+
hue,
|
|
2269
|
+
saturation,
|
|
2270
|
+
defs,
|
|
2271
|
+
resolved: /* @__PURE__ */ new Map(),
|
|
2272
|
+
config,
|
|
2273
|
+
roles: /* @__PURE__ */ new Map()
|
|
2274
|
+
};
|
|
2275
|
+
if (externalBases) for (const [name, color] of externalBases) ctx.resolved.set(name, color);
|
|
2276
|
+
const lightMap = runPass(order, defs, ctx, false, false, "light");
|
|
2277
|
+
seedField(order, ctx, "lightContrast", lightMap);
|
|
2278
|
+
const lightHCMap = runPass(order, defs, ctx, false, true, "lightContrast");
|
|
2279
|
+
seedField(order, ctx, "dark", lightMap);
|
|
2280
|
+
seedField(order, ctx, "darkContrast", lightHCMap);
|
|
2281
|
+
const darkMap = runPass(order, defs, ctx, true, false, "dark");
|
|
2282
|
+
seedField(order, ctx, "darkContrast", darkMap);
|
|
2283
|
+
const darkHCMap = runPass(order, defs, ctx, true, true, "darkContrast");
|
|
2284
|
+
const result = /* @__PURE__ */ new Map();
|
|
2285
|
+
for (const name of order) result.set(name, {
|
|
2286
|
+
name,
|
|
2287
|
+
light: lightMap.get(name),
|
|
2288
|
+
dark: darkMap.get(name),
|
|
2289
|
+
lightContrast: lightHCMap.get(name),
|
|
2290
|
+
darkContrast: darkHCMap.get(name),
|
|
2291
|
+
mode: defMode(defs[name])
|
|
2292
|
+
});
|
|
2293
|
+
verifyContrastDrift(order, defs, result, config);
|
|
2294
|
+
return result;
|
|
2295
|
+
}
|
|
2296
|
+
|
|
2297
|
+
//#endregion
|
|
2298
|
+
//#region src/channels.ts
|
|
2299
|
+
/**
|
|
2300
|
+
* Hue channel planning for `splitHue` exports.
|
|
2301
|
+
*
|
|
2302
|
+
* Builds per-color hue var references and scheme-independent `--*-hue`
|
|
2303
|
+
* declarations for oklch CSS / Tasty output when every color is pastel.
|
|
2304
|
+
*/
|
|
2305
|
+
const ACHROMATIC_EPSILON = 1e-6;
|
|
2306
|
+
function cssProp(prefix, name, suffix) {
|
|
2307
|
+
return `--${prefix}${name}${suffix}`;
|
|
2308
|
+
}
|
|
2309
|
+
function isAchromatic(v) {
|
|
2310
|
+
return v.s <= ACHROMATIC_EPSILON;
|
|
2311
|
+
}
|
|
2312
|
+
function themeHuePlan(name, def, variant, ctx) {
|
|
2313
|
+
if (def === void 0 || isShadowDef(def) || isMixDef(def) || isAchromatic(variant)) return {
|
|
2314
|
+
hueVar: "",
|
|
2315
|
+
inline: true,
|
|
2316
|
+
declarations: []
|
|
2317
|
+
};
|
|
2318
|
+
const regDef = def;
|
|
2319
|
+
const baseHueVar = `var(--${ctx.baseName}-hue)`;
|
|
2320
|
+
if (regDef.hue === void 0) return {
|
|
2321
|
+
hueVar: baseHueVar,
|
|
2322
|
+
inline: false,
|
|
2323
|
+
declarations: []
|
|
2324
|
+
};
|
|
2325
|
+
const parsed = parseRelativeOrAbsolute(regDef.hue);
|
|
2326
|
+
const prop = cssProp(ctx.prefix, name, "-hue");
|
|
2327
|
+
if (parsed.relative) {
|
|
2328
|
+
const sign = parsed.value >= 0 ? "+" : "-";
|
|
2329
|
+
const magnitude = Math.abs(parsed.value);
|
|
2330
|
+
const value = `calc(var(--${ctx.baseName}-hue) ${sign} ${magnitude})`;
|
|
2331
|
+
return {
|
|
2332
|
+
hueVar: `var(${prop})`,
|
|
2333
|
+
inline: false,
|
|
2334
|
+
declarations: [{
|
|
2335
|
+
prop,
|
|
2336
|
+
value
|
|
2337
|
+
}]
|
|
2338
|
+
};
|
|
2339
|
+
}
|
|
2340
|
+
const absHue = (parsed.value % 360 + 360) % 360;
|
|
2341
|
+
return {
|
|
2342
|
+
hueVar: `var(${prop})`,
|
|
2343
|
+
inline: false,
|
|
2344
|
+
declarations: [{
|
|
2345
|
+
prop,
|
|
2346
|
+
value: String(absHue)
|
|
2347
|
+
}]
|
|
2348
|
+
};
|
|
2349
|
+
}
|
|
2350
|
+
function standaloneHuePlan(name, variant, ctx) {
|
|
2351
|
+
if (isAchromatic(variant)) return {
|
|
2352
|
+
hueVar: "",
|
|
2353
|
+
inline: true,
|
|
2354
|
+
declarations: []
|
|
2355
|
+
};
|
|
2356
|
+
const hue = ctx.resolvedHue ?? variant.h;
|
|
2357
|
+
const prop = cssProp(ctx.prefix, name, "-hue");
|
|
2358
|
+
return {
|
|
2359
|
+
hueVar: `var(${prop})`,
|
|
2360
|
+
inline: false,
|
|
2361
|
+
declarations: [{
|
|
2362
|
+
prop,
|
|
2363
|
+
value: String(hue)
|
|
2364
|
+
}]
|
|
2365
|
+
};
|
|
2366
|
+
}
|
|
2367
|
+
function buildHuePlan(name, def, variant, ctx) {
|
|
2368
|
+
if (ctx.mode === "standalone") return standaloneHuePlan(name, variant, ctx);
|
|
2369
|
+
return themeHuePlan(name, def, variant, ctx);
|
|
2370
|
+
}
|
|
2371
|
+
/** Collect unique hue declarations across all colors (theme + per-color). */
|
|
2372
|
+
function collectHueDeclarations(resolved, ctx) {
|
|
2373
|
+
if (ctx.emitDeclarations === false) return [];
|
|
2374
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2375
|
+
const out = [];
|
|
2376
|
+
const push = (decl) => {
|
|
2377
|
+
if (seen.has(decl.prop)) return;
|
|
2378
|
+
seen.add(decl.prop);
|
|
2379
|
+
out.push(decl);
|
|
2380
|
+
};
|
|
2381
|
+
if (ctx.mode === "theme") push({
|
|
2382
|
+
prop: `--${ctx.baseName}-hue`,
|
|
2383
|
+
value: String(ctx.seedHue)
|
|
2384
|
+
});
|
|
2385
|
+
for (const [name, color] of resolved) {
|
|
2386
|
+
const def = ctx.defs[name];
|
|
2387
|
+
const plan = buildHuePlan(name, def, color.light, ctx);
|
|
2388
|
+
for (const decl of plan.declarations) push(decl);
|
|
2389
|
+
}
|
|
2390
|
+
return out;
|
|
2391
|
+
}
|
|
2392
|
+
function buildHuePlans(resolved, ctx) {
|
|
2393
|
+
const plans = /* @__PURE__ */ new Map();
|
|
2394
|
+
for (const [name, color] of resolved) plans.set(name, buildHuePlan(name, ctx.defs[name], color.light, ctx));
|
|
2395
|
+
return plans;
|
|
2396
|
+
}
|
|
2397
|
+
|
|
2398
|
+
//#endregion
|
|
2399
|
+
//#region src/formatters.ts
|
|
2400
|
+
/**
|
|
2401
|
+
* Output formatting for resolved color maps.
|
|
2402
|
+
*
|
|
2403
|
+
* Owns the CSS-string formatter dispatch table (`okhsl` / `rgb` / `hsl` /
|
|
2404
|
+
* `oklch`) and the token-map shapes Glaze emits:
|
|
2405
|
+
* - `buildTokenMap` — Tasty style-to-state bindings (`#name` keys, state aliases).
|
|
2406
|
+
* - `buildFlatTokenMap` — `{ light, dark, ... }` per-variant maps.
|
|
2407
|
+
* - `buildJsonMap` — `{ name: { light, dark, ... } }` per-color JSON.
|
|
2408
|
+
* - `buildCssMap` — CSS custom property declaration strings per variant.
|
|
2409
|
+
* - `buildDtcgMap` — W3C DTCG (2025.10) token documents, one per scheme.
|
|
2410
|
+
* - `buildDtcgResolver` — W3C DTCG Resolver-Module document (one modifier, a context per scheme).
|
|
2411
|
+
* - `buildTailwindMap` — Tailwind v4 `@theme` block + dark/HC overrides.
|
|
2412
|
+
*/
|
|
2413
|
+
const formatters = {
|
|
2414
|
+
okhsl: formatOkhsl,
|
|
2415
|
+
rgb: formatRgb,
|
|
2416
|
+
hsl: formatHsl,
|
|
2417
|
+
oklch: formatOklch
|
|
2418
|
+
};
|
|
2419
|
+
function fmt(value, decimals) {
|
|
2420
|
+
return parseFloat(value.toFixed(decimals)).toString();
|
|
2421
|
+
}
|
|
2422
|
+
function formatVariant(v, format = "oklch", pastel = false) {
|
|
2423
|
+
const effectivePastel = v.pastel ?? pastel;
|
|
2424
|
+
let base;
|
|
2425
|
+
if (format === "okhst") base = formatOkhst(v.h, v.s * 100, v.t * 100, effectivePastel);
|
|
2426
|
+
else {
|
|
2427
|
+
const { l } = variantToOkhsl(v);
|
|
2428
|
+
base = formatters[format](v.h, v.s * 100, l * 100, effectivePastel);
|
|
2429
|
+
}
|
|
2430
|
+
if (v.alpha >= 1) return base;
|
|
2431
|
+
const closing = base.lastIndexOf(")");
|
|
2432
|
+
return `${base.slice(0, closing)} / ${fmt(v.alpha, 4)})`;
|
|
2433
|
+
}
|
|
2434
|
+
/**
|
|
2435
|
+
* Format a resolved variant as `oklch(L C <hueVar>)`, splicing a CSS hue var
|
|
2436
|
+
* for `splitHue` exports. Falls back to inline when the plan is inline.
|
|
2437
|
+
*/
|
|
2438
|
+
function formatVariantHue(v, plan, pastel = false) {
|
|
2439
|
+
const effectivePastel = v.pastel ?? pastel;
|
|
2440
|
+
const { l } = variantToOkhsl(v);
|
|
2441
|
+
const [L, C] = okhslToOklch(v.h, v.s, l, effectivePastel);
|
|
2442
|
+
let base;
|
|
2443
|
+
if (plan.inline) if (v.s <= 1e-6) base = `oklch(${fmt(L, 4)} 0 0)`;
|
|
2444
|
+
else base = formatOklch(v.h, v.s * 100, l * 100, effectivePastel);
|
|
2445
|
+
else base = `oklch(${fmt(L, 4)} ${fmt(C, 4)} ${plan.hueVar})`;
|
|
2446
|
+
if (v.alpha >= 1) return base;
|
|
2447
|
+
const closing = base.lastIndexOf(")");
|
|
2448
|
+
return `${base.slice(0, closing)} / ${fmt(v.alpha, 4)})`;
|
|
2449
|
+
}
|
|
2450
|
+
function formatColorValue(v, format, pastel, huePlan) {
|
|
2451
|
+
if (format === "oklch" && huePlan !== void 0) return formatVariantHue(v, huePlan, pastel);
|
|
2452
|
+
return formatVariant(v, format, pastel);
|
|
2453
|
+
}
|
|
2454
|
+
function resolveModes(override) {
|
|
2455
|
+
const cfg = getConfig();
|
|
2456
|
+
return {
|
|
2457
|
+
dark: override?.dark ?? cfg.modes.dark,
|
|
2458
|
+
highContrast: override?.highContrast ?? cfg.modes.highContrast
|
|
2459
|
+
};
|
|
2460
|
+
}
|
|
2461
|
+
function buildTokenMap(resolved, prefix, states, modes, format = "oklch", pastel = false, channelCtx) {
|
|
2462
|
+
const tokens = {};
|
|
2463
|
+
const huePlans = channelCtx !== void 0 && format === "oklch" ? buildHuePlans(resolved, channelCtx) : void 0;
|
|
2464
|
+
if (huePlans !== void 0 && channelCtx !== void 0) {
|
|
2465
|
+
const emitDecls = channelCtx.emitDeclarations !== false;
|
|
2466
|
+
if (emitDecls && channelCtx.mode === "theme") tokens[`$${channelCtx.baseName}-hue`] = { "": String(channelCtx.seedHue) };
|
|
2467
|
+
for (const [name, color] of resolved) {
|
|
2468
|
+
const plan = huePlans.get(name);
|
|
2469
|
+
if (emitDecls) for (const decl of plan.declarations) {
|
|
2470
|
+
const key = `$${decl.prop.slice(2)}`;
|
|
2471
|
+
if (!(key in tokens)) tokens[key] = { "": decl.value };
|
|
2472
|
+
}
|
|
2473
|
+
const colorKey = `#${prefix}${name}`;
|
|
2474
|
+
tokens[colorKey] = buildTokenEntry(color, states, modes, format, pastel, huePlans.get(name));
|
|
2475
|
+
}
|
|
2476
|
+
return tokens;
|
|
2477
|
+
}
|
|
2478
|
+
for (const [name, color] of resolved) {
|
|
2479
|
+
const key = `#${prefix}${name}`;
|
|
2480
|
+
tokens[key] = buildTokenEntry(color, states, modes, format, pastel);
|
|
2481
|
+
}
|
|
2482
|
+
return tokens;
|
|
2483
|
+
}
|
|
2484
|
+
function buildTokenEntry(color, states, modes, format, pastel, huePlan) {
|
|
2485
|
+
const entry = { "": formatColorValue(color.light, format, pastel, huePlan) };
|
|
2486
|
+
if (modes.dark) entry[states.dark] = formatColorValue(color.dark, format, pastel, huePlan);
|
|
2487
|
+
if (modes.highContrast) entry[states.highContrast] = formatColorValue(color.lightContrast, format, pastel, huePlan);
|
|
2488
|
+
if (modes.dark && modes.highContrast) entry[`${states.dark} & ${states.highContrast}`] = formatColorValue(color.darkContrast, format, pastel, huePlan);
|
|
2489
|
+
return entry;
|
|
2490
|
+
}
|
|
2491
|
+
function buildFlatTokenMap(resolved, prefix, modes, format = "oklch", pastel = false) {
|
|
2492
|
+
const result = { light: {} };
|
|
2493
|
+
if (modes.dark) result.dark = {};
|
|
2494
|
+
if (modes.highContrast) result.lightContrast = {};
|
|
2495
|
+
if (modes.dark && modes.highContrast) result.darkContrast = {};
|
|
2496
|
+
for (const [name, color] of resolved) {
|
|
2497
|
+
const key = `${prefix}${name}`;
|
|
2498
|
+
result.light[key] = formatVariant(color.light, format, pastel);
|
|
2499
|
+
if (modes.dark) result.dark[key] = formatVariant(color.dark, format, pastel);
|
|
2500
|
+
if (modes.highContrast) result.lightContrast[key] = formatVariant(color.lightContrast, format, pastel);
|
|
2501
|
+
if (modes.dark && modes.highContrast) result.darkContrast[key] = formatVariant(color.darkContrast, format, pastel);
|
|
2502
|
+
}
|
|
963
2503
|
return result;
|
|
964
2504
|
}
|
|
965
|
-
function
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
if (beta >= 1) return t;
|
|
976
|
-
return t / (t + beta * (1 - t));
|
|
977
|
-
}
|
|
978
|
-
function mapLightnessDark(l, mode, isHighContrast) {
|
|
979
|
-
if (mode === "static") return l;
|
|
980
|
-
const beta = isHighContrast ? pairHC(globalConfig.darkCurve) : pairNormal(globalConfig.darkCurve);
|
|
981
|
-
const [darkLo, darkHi] = lightnessWindow(isHighContrast, "dark");
|
|
982
|
-
if (mode === "fixed") return l * (darkHi - darkLo) / 100 + darkLo;
|
|
983
|
-
const [lightLo, lightHi] = lightnessWindow(isHighContrast, "light");
|
|
984
|
-
const t = (lightHi - (l * (lightHi - lightLo) / 100 + lightLo)) / (lightHi - lightLo);
|
|
985
|
-
return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
|
|
986
|
-
}
|
|
987
|
-
function lightMappedToDark(lightL, isHighContrast) {
|
|
988
|
-
const beta = isHighContrast ? pairHC(globalConfig.darkCurve) : pairNormal(globalConfig.darkCurve);
|
|
989
|
-
const [lightLo, lightHi] = lightnessWindow(isHighContrast, "light");
|
|
990
|
-
const [darkLo, darkHi] = lightnessWindow(isHighContrast, "dark");
|
|
991
|
-
const t = (lightHi - clamp(lightL, lightLo, lightHi)) / (lightHi - lightLo);
|
|
992
|
-
return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
|
|
993
|
-
}
|
|
994
|
-
function mapSaturationDark(s, mode) {
|
|
995
|
-
if (mode === "static") return s;
|
|
996
|
-
return s * (1 - globalConfig.darkDesaturation);
|
|
2505
|
+
function buildJsonMap(resolved, modes, format = "oklch", pastel = false) {
|
|
2506
|
+
const result = {};
|
|
2507
|
+
for (const [name, color] of resolved) {
|
|
2508
|
+
const entry = { light: formatVariant(color.light, format, pastel) };
|
|
2509
|
+
if (modes.dark) entry.dark = formatVariant(color.dark, format, pastel);
|
|
2510
|
+
if (modes.highContrast) entry.lightContrast = formatVariant(color.lightContrast, format, pastel);
|
|
2511
|
+
if (modes.dark && modes.highContrast) entry.darkContrast = formatVariant(color.darkContrast, format, pastel);
|
|
2512
|
+
result[name] = entry;
|
|
2513
|
+
}
|
|
2514
|
+
return result;
|
|
997
2515
|
}
|
|
998
|
-
function
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
2516
|
+
function buildCssMap(resolved, prefix, suffix, format, pastel = false, channelCtx) {
|
|
2517
|
+
const lines = {
|
|
2518
|
+
light: [],
|
|
2519
|
+
dark: [],
|
|
2520
|
+
lightContrast: [],
|
|
2521
|
+
darkContrast: []
|
|
2522
|
+
};
|
|
2523
|
+
const huePlans = channelCtx !== void 0 && format === "oklch" ? buildHuePlans(resolved, channelCtx) : void 0;
|
|
2524
|
+
if (huePlans !== void 0 && channelCtx !== void 0) for (const decl of collectHueDeclarations(resolved, channelCtx)) lines.light.push(`${decl.prop}: ${decl.value};`);
|
|
2525
|
+
for (const [name, color] of resolved) {
|
|
2526
|
+
const prop = `--${prefix}${name}${suffix}`;
|
|
2527
|
+
const plan = huePlans?.get(name);
|
|
2528
|
+
lines.light.push(`${prop}: ${formatColorValue(color.light, format, pastel, plan)};`);
|
|
2529
|
+
lines.dark.push(`${prop}: ${formatColorValue(color.dark, format, pastel, plan)};`);
|
|
2530
|
+
lines.lightContrast.push(`${prop}: ${formatColorValue(color.lightContrast, format, pastel, plan)};`);
|
|
2531
|
+
lines.darkContrast.push(`${prop}: ${formatColorValue(color.darkContrast, format, pastel, plan)};`);
|
|
2532
|
+
}
|
|
2533
|
+
return {
|
|
2534
|
+
light: lines.light.join("\n"),
|
|
2535
|
+
dark: lines.dark.join("\n"),
|
|
2536
|
+
lightContrast: lines.lightContrast.join("\n"),
|
|
2537
|
+
darkContrast: lines.darkContrast.join("\n")
|
|
2538
|
+
};
|
|
1002
2539
|
}
|
|
1003
|
-
function
|
|
1004
|
-
return
|
|
2540
|
+
function roundTo(value, decimals) {
|
|
2541
|
+
return parseFloat(value.toFixed(decimals));
|
|
1005
2542
|
}
|
|
1006
2543
|
/**
|
|
1007
|
-
*
|
|
1008
|
-
*
|
|
2544
|
+
* Build a DTCG `$value` color object for a resolved variant.
|
|
2545
|
+
*
|
|
2546
|
+
* `srgb` (default) emits gamma sRGB components in 0–1 plus a 6-digit `hex`
|
|
2547
|
+
* hint — the most universally understood form (Figma, Tokens Studio, Style
|
|
2548
|
+
* Dictionary). `oklch` emits `[L, C, H]` components with no hex — Glaze-native
|
|
2549
|
+
* and wide-gamut. `alpha` is included only when below 1.
|
|
1009
2550
|
*/
|
|
1010
|
-
function
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
2551
|
+
function dtcgColorValue(v, colorSpace = "srgb", pastel = false) {
|
|
2552
|
+
const effectivePastel = v.pastel ?? pastel;
|
|
2553
|
+
const { l } = variantToOkhsl(v);
|
|
2554
|
+
const alpha = v.alpha < 1 ? roundTo(v.alpha, 6) : void 0;
|
|
2555
|
+
if (colorSpace === "oklch") {
|
|
2556
|
+
const [L, C, H] = okhslToOklch(v.h, v.s, l, effectivePastel);
|
|
2557
|
+
const value = {
|
|
2558
|
+
colorSpace: "oklch",
|
|
2559
|
+
components: [
|
|
2560
|
+
roundTo(L, 6),
|
|
2561
|
+
roundTo(C, 6),
|
|
2562
|
+
roundTo(H, 4)
|
|
2563
|
+
]
|
|
2564
|
+
};
|
|
2565
|
+
if (alpha !== void 0) value.alpha = alpha;
|
|
2566
|
+
return value;
|
|
2567
|
+
}
|
|
2568
|
+
const [r, g, b] = okhslToSrgb(v.h, v.s, l, effectivePastel);
|
|
2569
|
+
const value = {
|
|
2570
|
+
colorSpace: "srgb",
|
|
2571
|
+
components: [
|
|
2572
|
+
roundTo(r, 6),
|
|
2573
|
+
roundTo(g, 6),
|
|
2574
|
+
roundTo(b, 6)
|
|
2575
|
+
],
|
|
2576
|
+
hex: srgbToHex([
|
|
2577
|
+
r,
|
|
2578
|
+
g,
|
|
2579
|
+
b
|
|
2580
|
+
])
|
|
1014
2581
|
};
|
|
2582
|
+
if (alpha !== void 0) value.alpha = alpha;
|
|
2583
|
+
return value;
|
|
2584
|
+
}
|
|
2585
|
+
function dtcgToken(v, colorSpace, pastel) {
|
|
1015
2586
|
return {
|
|
1016
|
-
|
|
1017
|
-
|
|
2587
|
+
$type: "color",
|
|
2588
|
+
$value: dtcgColorValue(v, colorSpace, pastel)
|
|
1018
2589
|
};
|
|
1019
2590
|
}
|
|
1020
2591
|
/**
|
|
1021
|
-
*
|
|
1022
|
-
*
|
|
2592
|
+
* Build a `GlazeDtcgResult`: one spec-conformant DTCG token document per
|
|
2593
|
+
* scheme variant, gated by `modes`. Light is always present.
|
|
1023
2594
|
*/
|
|
1024
|
-
function
|
|
1025
|
-
|
|
1026
|
-
const
|
|
1027
|
-
|
|
1028
|
-
|
|
2595
|
+
function buildDtcgMap(resolved, prefix, modes, colorSpace = "srgb", pastel = false) {
|
|
2596
|
+
const light = {};
|
|
2597
|
+
const dark = modes.dark ? {} : void 0;
|
|
2598
|
+
const lightContrast = modes.highContrast ? {} : void 0;
|
|
2599
|
+
const darkContrast = modes.dark && modes.highContrast ? {} : void 0;
|
|
2600
|
+
for (const [name, color] of resolved) {
|
|
2601
|
+
const key = `${prefix}${name}`;
|
|
2602
|
+
light[key] = dtcgToken(color.light, colorSpace, pastel);
|
|
2603
|
+
if (dark) dark[key] = dtcgToken(color.dark, colorSpace, pastel);
|
|
2604
|
+
if (lightContrast) lightContrast[key] = dtcgToken(color.lightContrast, colorSpace, pastel);
|
|
2605
|
+
if (darkContrast) darkContrast[key] = dtcgToken(color.darkContrast, colorSpace, pastel);
|
|
2606
|
+
}
|
|
2607
|
+
return {
|
|
2608
|
+
light,
|
|
2609
|
+
dark,
|
|
2610
|
+
lightContrast,
|
|
2611
|
+
darkContrast
|
|
2612
|
+
};
|
|
1029
2613
|
}
|
|
1030
2614
|
/**
|
|
1031
|
-
*
|
|
1032
|
-
*
|
|
2615
|
+
* Default context names emitted on the `scheme` modifier — the Glaze variant
|
|
2616
|
+
* keys, so the resolver document mirrors `GlazeDtcgResult` exactly.
|
|
1033
2617
|
*/
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
2618
|
+
const DEFAULT_DTCG_CONTEXT_NAMES = {
|
|
2619
|
+
light: "light",
|
|
2620
|
+
dark: "dark",
|
|
2621
|
+
lightContrast: "lightContrast",
|
|
2622
|
+
darkContrast: "darkContrast"
|
|
2623
|
+
};
|
|
2624
|
+
/**
|
|
2625
|
+
* Wrap a per-scheme `GlazeDtcgResult` into a single W3C DTCG Resolver-Module
|
|
2626
|
+
* document. The light document becomes `sets[setName].sources[0]` (the default
|
|
2627
|
+
* context); each other present variant becomes a `contexts[ctx]` override
|
|
2628
|
+
* array on a single `modifiers[modifierName]`. Absent variants (per the
|
|
2629
|
+
* `modes` already applied to `result`) are omitted — light is always present
|
|
2630
|
+
* and is the modifier `default`. Only the resolver-specific options are read;
|
|
2631
|
+
* `modes` / `colorSpace` were already consumed by the `buildDtcgMap` call that
|
|
2632
|
+
* produced `result`.
|
|
2633
|
+
*/
|
|
2634
|
+
function buildDtcgResolver(result, options) {
|
|
2635
|
+
const setName = options?.setName ?? "base";
|
|
2636
|
+
const modifierName = options?.modifierName ?? "scheme";
|
|
2637
|
+
const ctx = {
|
|
2638
|
+
...DEFAULT_DTCG_CONTEXT_NAMES,
|
|
2639
|
+
...options?.contextNames
|
|
2640
|
+
};
|
|
2641
|
+
const contexts = { [ctx.light]: [] };
|
|
2642
|
+
if (result.dark) contexts[ctx.dark] = [result.dark];
|
|
2643
|
+
if (result.lightContrast) contexts[ctx.lightContrast] = [result.lightContrast];
|
|
2644
|
+
if (result.darkContrast) contexts[ctx.darkContrast] = [result.darkContrast];
|
|
1040
2645
|
return {
|
|
1041
|
-
|
|
1042
|
-
|
|
2646
|
+
version: options?.version ?? "2025.10",
|
|
2647
|
+
sets: { [setName]: { sources: [result.light] } },
|
|
2648
|
+
modifiers: { [modifierName]: {
|
|
2649
|
+
default: ctx.light,
|
|
2650
|
+
contexts
|
|
2651
|
+
} },
|
|
2652
|
+
resolutionOrder: [{ $ref: `#/sets/${setName}` }, { $ref: `#/modifiers/${modifierName}` }]
|
|
1043
2653
|
};
|
|
1044
2654
|
}
|
|
1045
|
-
function
|
|
1046
|
-
const
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
const
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
if (parsed.relative) {
|
|
1059
|
-
const delta = parsed.value;
|
|
1060
|
-
if (isDark && mode === "auto") preferredL = lightMappedToDark(clamp(getSchemeVariant(baseResolved, false, isHighContrast).l * 100 + delta, 0, 100), isHighContrast);
|
|
1061
|
-
else preferredL = clamp(baseL + delta, 0, 100);
|
|
1062
|
-
} else if (isDark) preferredL = mapLightnessDark(parsed.value, mode, isHighContrast);
|
|
1063
|
-
else preferredL = mapLightnessLight(parsed.value, mode, isHighContrast);
|
|
2655
|
+
function tailwindLinesFor(resolved, themePrefix, cssPrefix, format, pastel) {
|
|
2656
|
+
const lines = {
|
|
2657
|
+
light: [],
|
|
2658
|
+
dark: [],
|
|
2659
|
+
lightContrast: [],
|
|
2660
|
+
darkContrast: []
|
|
2661
|
+
};
|
|
2662
|
+
for (const [name, color] of resolved) {
|
|
2663
|
+
const prop = `--${cssPrefix}${themePrefix}${name}`;
|
|
2664
|
+
lines.light.push(`${prop}: ${formatVariant(color.light, format, pastel)};`);
|
|
2665
|
+
lines.dark.push(`${prop}: ${formatVariant(color.dark, format, pastel)};`);
|
|
2666
|
+
lines.lightContrast.push(`${prop}: ${formatVariant(color.lightContrast, format, pastel)};`);
|
|
2667
|
+
lines.darkContrast.push(`${prop}: ${formatVariant(color.darkContrast, format, pastel)};`);
|
|
1064
2668
|
}
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
2669
|
+
return lines;
|
|
2670
|
+
}
|
|
2671
|
+
function indentBlock(text, pad) {
|
|
2672
|
+
return text.split("\n").map((line) => line.length === 0 ? line : pad + line).join("\n");
|
|
2673
|
+
}
|
|
2674
|
+
function emitRule(selector, body) {
|
|
2675
|
+
return `${selector} {\n${indentBlock(body, " ")}\n}`;
|
|
2676
|
+
}
|
|
2677
|
+
/**
|
|
2678
|
+
* Emit a CSS block for a set of declarations scoped by one or more selectors
|
|
2679
|
+
* / at-rules. Class-like selectors concatenate (`.dark.high-contrast`);
|
|
2680
|
+
* at-rules (`@media …`) nest `:root` (or the chained selector) inside.
|
|
2681
|
+
*/
|
|
2682
|
+
function emitScoped(scopes, declarations) {
|
|
2683
|
+
if (declarations.length === 0) return void 0;
|
|
2684
|
+
const atRules = [];
|
|
2685
|
+
let selectorChain = "";
|
|
2686
|
+
for (const scope of scopes) if (scope.startsWith("@")) atRules.push(scope);
|
|
2687
|
+
else selectorChain += scope;
|
|
2688
|
+
let css = emitRule(selectorChain || ":root", declarations.join("\n"));
|
|
2689
|
+
for (const rule of atRules) css = emitRule(rule, css);
|
|
2690
|
+
return css;
|
|
2691
|
+
}
|
|
2692
|
+
/**
|
|
2693
|
+
* Render accumulated per-scheme declaration lines as a Tailwind v4 CSS string:
|
|
2694
|
+
* an `@theme` block (light baseline) plus dark / high-contrast overrides under
|
|
2695
|
+
* the configured selectors. Empty blocks are skipped.
|
|
2696
|
+
*/
|
|
2697
|
+
function emitTailwindCss(lines, modes, darkSelector, highContrastSelector) {
|
|
2698
|
+
const blocks = [];
|
|
2699
|
+
if (lines.light.length > 0) blocks.push(emitRule("@theme", lines.light.join("\n")));
|
|
2700
|
+
if (modes.dark) {
|
|
2701
|
+
const dark = emitScoped([darkSelector], lines.dark);
|
|
2702
|
+
if (dark) blocks.push(dark);
|
|
2703
|
+
}
|
|
2704
|
+
if (modes.highContrast) {
|
|
2705
|
+
const hc = emitScoped([highContrastSelector], lines.lightContrast);
|
|
2706
|
+
if (hc) blocks.push(hc);
|
|
2707
|
+
}
|
|
2708
|
+
if (modes.dark && modes.highContrast) {
|
|
2709
|
+
const dhc = emitScoped([darkSelector, highContrastSelector], lines.darkContrast);
|
|
2710
|
+
if (dhc) blocks.push(dhc);
|
|
2711
|
+
}
|
|
2712
|
+
return blocks.join("\n\n");
|
|
2713
|
+
}
|
|
2714
|
+
/**
|
|
2715
|
+
* Build per-scheme declaration lines for a single theme (used by
|
|
2716
|
+
* `theme.tailwind()` and as the palette `buildOne` step).
|
|
2717
|
+
*/
|
|
2718
|
+
function buildTailwindLines(resolved, themePrefix, cssPrefix, format, pastel) {
|
|
2719
|
+
return tailwindLinesFor(resolved, themePrefix, cssPrefix, format, pastel);
|
|
2720
|
+
}
|
|
2721
|
+
/**
|
|
2722
|
+
* Build a complete Tailwind v4 CSS string for a single theme.
|
|
2723
|
+
*/
|
|
2724
|
+
function buildTailwindMap(resolved, themePrefix, cssPrefix, modes, format, darkSelector, highContrastSelector, pastel = false) {
|
|
2725
|
+
return emitTailwindCss(tailwindLinesFor(resolved, themePrefix, cssPrefix, format, pastel), modes, darkSelector, highContrastSelector);
|
|
2726
|
+
}
|
|
2727
|
+
|
|
2728
|
+
//#endregion
|
|
2729
|
+
//#region src/color-token.ts
|
|
2730
|
+
/**
|
|
2731
|
+
* Standalone single-color tokens (`glaze.color()` / `glaze.colorFrom()`).
|
|
2732
|
+
*
|
|
2733
|
+
* Owns the value-shorthand parser (hex, `rgb()` / `hsl()` / `okhsl()` /
|
|
2734
|
+
* `okhst()` / `oklch()`, `{ r, g, b }`, `{ h, s, l }`, `{ h, s, t }`,
|
|
2735
|
+
* `{ l, c, h }`), the structured-input validator, the two factory paths
|
|
2736
|
+
* (value vs structured), and the JSON-safe export / rehydration round-trip.
|
|
2737
|
+
*
|
|
2738
|
+
* Standalone tokens snapshot the full effective config at create time
|
|
2739
|
+
* so later `configure()` calls do not retroactively change exported
|
|
2740
|
+
* tokens. The snapshot is built eagerly in
|
|
2741
|
+
* `buildValueFormConfigOverride()` / `buildStructuredConfigOverride()`.
|
|
2742
|
+
* The token's resolved variants are then memoized on first
|
|
2743
|
+
* `.resolve()` / `.token()` / ... call.
|
|
2744
|
+
*/
|
|
2745
|
+
/** Internal name of the user-facing standalone color in the synthesized def map. */
|
|
2746
|
+
const STANDALONE_VALUE = "value";
|
|
2747
|
+
/** Internal name of the hidden static-anchor seed used for relative tone / contrast. */
|
|
2748
|
+
const STANDALONE_SEED = "seed";
|
|
2749
|
+
/** Internal name of an externally-resolved `GlazeColorToken` injected as a base reference. */
|
|
2750
|
+
const STANDALONE_BASE = "externalBase";
|
|
2751
|
+
/** Reserved internal names that user-supplied `name` must not collide with. */
|
|
2752
|
+
const RESERVED_STANDALONE_NAMES = new Set([
|
|
2753
|
+
STANDALONE_VALUE,
|
|
2754
|
+
STANDALONE_SEED,
|
|
2755
|
+
STANDALONE_BASE
|
|
2756
|
+
]);
|
|
2757
|
+
/**
|
|
2758
|
+
* Build the per-token effective config override for a value-form color.
|
|
2759
|
+
*
|
|
2760
|
+
* Light window defaults to `false` (preserve input tone exactly).
|
|
2761
|
+
* All other fields snapshot from global at create time via
|
|
2762
|
+
* `buildEffectiveConfigOverride`. User override fields win.
|
|
2763
|
+
*/
|
|
2764
|
+
function buildValueFormConfigOverride(userOverride) {
|
|
2765
|
+
return buildEffectiveConfigOverride({
|
|
2766
|
+
...userOverride,
|
|
2767
|
+
lightTone: userOverride?.lightTone !== void 0 ? userOverride.lightTone : false
|
|
2768
|
+
});
|
|
2769
|
+
}
|
|
2770
|
+
/**
|
|
2771
|
+
* Build the per-token effective config override for a structured-form color.
|
|
2772
|
+
* Snapshots the full effective config (incl. pastel / inferRole).
|
|
2773
|
+
*/
|
|
2774
|
+
function buildStructuredConfigOverride(userOverride) {
|
|
2775
|
+
return buildEffectiveConfigOverride(userOverride);
|
|
2776
|
+
}
|
|
2777
|
+
/**
|
|
2778
|
+
* Build the `GlazeConfigResolved` to pass to `resolveAllColors` from a
|
|
2779
|
+
* snapshot override. Uses `defaultConfig()` as the base so all required
|
|
2780
|
+
* fields are present; the snapshot fields win.
|
|
2781
|
+
*/
|
|
2782
|
+
function resolvedConfigFromOverride(override) {
|
|
2783
|
+
return mergeConfig(defaultConfig(), override);
|
|
2784
|
+
}
|
|
2785
|
+
/**
|
|
2786
|
+
* Matches the CSS color functions Glaze itself emits (`rgb()`, `hsl()`,
|
|
2787
|
+
* `okhsl()`, `oklch()`) plus their legacy alpha aliases (`rgba()`, `hsla()`).
|
|
2788
|
+
*
|
|
2789
|
+
* Only bare numeric components are supported. Named colors (`red`),
|
|
2790
|
+
* relative-color syntax (`from <color> ...`), and angle units other
|
|
2791
|
+
* than bare degrees (`deg` is the only suffix tolerated by `parseFloat`)
|
|
2792
|
+
* are out of scope.
|
|
2793
|
+
*/
|
|
2794
|
+
const COLOR_FN_RE = /^(rgba?|hsla?|okhsl|okhst|oklch)\(\s*([^)]*)\s*\)$/i;
|
|
2795
|
+
function parseNumberOrPercent(raw, percentScale) {
|
|
2796
|
+
if (raw.endsWith("%")) return parseFloat(raw) / 100 * percentScale;
|
|
2797
|
+
return parseFloat(raw);
|
|
2798
|
+
}
|
|
2799
|
+
/**
|
|
2800
|
+
* Split the body of a CSS color function into its components and detect
|
|
2801
|
+
* whether an alpha channel was present.
|
|
2802
|
+
*
|
|
2803
|
+
* Handles both modern slash syntax (`R G B / A` or `R, G, B / A`) and
|
|
2804
|
+
* legacy comma syntax (`R, G, B, A`). The alpha value itself is discarded
|
|
2805
|
+
* by the caller — standalone Glaze colors have no opacity field.
|
|
2806
|
+
*/
|
|
2807
|
+
function splitColorBody(body) {
|
|
2808
|
+
const slashIdx = body.indexOf("/");
|
|
2809
|
+
if (slashIdx !== -1) return {
|
|
2810
|
+
components: body.slice(0, slashIdx).trim().split(/[\s,]+/).filter(Boolean),
|
|
2811
|
+
hadAlpha: body.slice(slashIdx + 1).trim().length > 0
|
|
2812
|
+
};
|
|
2813
|
+
const components = body.split(/[\s,]+/).filter(Boolean);
|
|
2814
|
+
if (components.length === 4) {
|
|
2815
|
+
components.pop();
|
|
1071
2816
|
return {
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
saturation: effectiveSat,
|
|
1075
|
-
preferredLightness: clamp(preferredL / 100, windowRange[0], windowRange[1]),
|
|
1076
|
-
baseLinearRgb,
|
|
1077
|
-
contrast: minCr,
|
|
1078
|
-
lightnessRange: [0, 1]
|
|
1079
|
-
}).lightness * 100,
|
|
1080
|
-
satFactor
|
|
2817
|
+
components,
|
|
2818
|
+
hadAlpha: true
|
|
1081
2819
|
};
|
|
1082
2820
|
}
|
|
1083
2821
|
return {
|
|
1084
|
-
|
|
1085
|
-
|
|
2822
|
+
components,
|
|
2823
|
+
hadAlpha: false
|
|
1086
2824
|
};
|
|
1087
2825
|
}
|
|
1088
|
-
function
|
|
1089
|
-
|
|
1090
|
-
if (isDark) return color.dark;
|
|
1091
|
-
if (isHighContrast) return color.lightContrast;
|
|
1092
|
-
return color.light;
|
|
2826
|
+
function warnDroppedAlpha(input) {
|
|
2827
|
+
console.warn(`glaze: alpha component dropped from "${input}" (standalone color has no opacity field).`);
|
|
1093
2828
|
}
|
|
1094
|
-
function
|
|
1095
|
-
if (
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
lightL = root.lightL;
|
|
1106
|
-
satFactor = root.satFactor;
|
|
1107
|
-
} else {
|
|
1108
|
-
const dep = resolveDependentColor(name, regDef, ctx, isHighContrast, isDark, effectiveHue);
|
|
1109
|
-
lightL = dep.l;
|
|
1110
|
-
satFactor = dep.satFactor;
|
|
2829
|
+
function parseColorString(input) {
|
|
2830
|
+
if (input.startsWith("#")) {
|
|
2831
|
+
const parsed = parseHexAlpha(input);
|
|
2832
|
+
if (!parsed) throw new Error(`glaze: invalid hex color "${input}".`);
|
|
2833
|
+
if (parsed.alpha !== void 0) warnDroppedAlpha(input);
|
|
2834
|
+
const [h, s, l] = srgbToOkhsl(parsed.rgb);
|
|
2835
|
+
return {
|
|
2836
|
+
h,
|
|
2837
|
+
s,
|
|
2838
|
+
l
|
|
2839
|
+
};
|
|
1111
2840
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
2841
|
+
const m = input.match(COLOR_FN_RE);
|
|
2842
|
+
if (!m) throw new Error(`glaze: unsupported color string "${input}".`);
|
|
2843
|
+
const fn = m[1].toLowerCase();
|
|
2844
|
+
const { components, hadAlpha } = splitColorBody(m[2].trim());
|
|
2845
|
+
if (hadAlpha) warnDroppedAlpha(input);
|
|
2846
|
+
if (components.length !== 3) throw new Error(`glaze: expected 3 components in "${input}".`);
|
|
2847
|
+
switch (fn) {
|
|
2848
|
+
case "rgb":
|
|
2849
|
+
case "rgba": {
|
|
2850
|
+
const [h, s, l] = srgbToOkhsl([
|
|
2851
|
+
parseNumberOrPercent(components[0], 255) / 255,
|
|
2852
|
+
parseNumberOrPercent(components[1], 255) / 255,
|
|
2853
|
+
parseNumberOrPercent(components[2], 255) / 255
|
|
2854
|
+
]);
|
|
2855
|
+
return {
|
|
2856
|
+
h,
|
|
2857
|
+
s,
|
|
2858
|
+
l
|
|
2859
|
+
};
|
|
2860
|
+
}
|
|
2861
|
+
case "hsl":
|
|
2862
|
+
case "hsla": {
|
|
2863
|
+
const [oh, os, ol] = srgbToOkhsl(hslToSrgb(parseFloat(components[0]), parseNumberOrPercent(components[1], 1), parseNumberOrPercent(components[2], 1)));
|
|
2864
|
+
return {
|
|
2865
|
+
h: oh,
|
|
2866
|
+
s: os,
|
|
2867
|
+
l: ol
|
|
2868
|
+
};
|
|
2869
|
+
}
|
|
2870
|
+
case "okhsl": return {
|
|
2871
|
+
h: parseFloat(components[0]),
|
|
2872
|
+
s: parseNumberOrPercent(components[1], 1),
|
|
2873
|
+
l: parseNumberOrPercent(components[2], 1)
|
|
2874
|
+
};
|
|
2875
|
+
case "okhst": return okhstToOkhsl({
|
|
2876
|
+
h: parseFloat(components[0]),
|
|
2877
|
+
s: parseNumberOrPercent(components[1], 1),
|
|
2878
|
+
t: parseNumberOrPercent(components[2], 1)
|
|
2879
|
+
});
|
|
2880
|
+
case "oklch": {
|
|
2881
|
+
const L = parseNumberOrPercent(components[0], 1);
|
|
2882
|
+
const C = parseNumberOrPercent(components[1], .4);
|
|
2883
|
+
const hRad = parseFloat(components[2]) * Math.PI / 180;
|
|
2884
|
+
const [h, s, l] = oklabToOkhsl([
|
|
2885
|
+
L,
|
|
2886
|
+
C * Math.cos(hRad),
|
|
2887
|
+
C * Math.sin(hRad)
|
|
2888
|
+
]);
|
|
2889
|
+
return {
|
|
2890
|
+
h,
|
|
2891
|
+
s,
|
|
2892
|
+
l
|
|
2893
|
+
};
|
|
2894
|
+
}
|
|
1126
2895
|
}
|
|
1127
|
-
|
|
1128
|
-
h: effectiveHue,
|
|
1129
|
-
s: clamp(finalSat, 0, 1),
|
|
1130
|
-
l: clamp(finalL / 100, 0, 1),
|
|
1131
|
-
alpha: regDef.opacity ?? 1
|
|
1132
|
-
};
|
|
1133
|
-
}
|
|
1134
|
-
function resolveShadowForScheme(def, ctx, isDark, isHighContrast) {
|
|
1135
|
-
const bgVariant = getSchemeVariant(ctx.resolved.get(def.bg), isDark, isHighContrast);
|
|
1136
|
-
let fgVariant;
|
|
1137
|
-
if (def.fg) fgVariant = getSchemeVariant(ctx.resolved.get(def.fg), isDark, isHighContrast);
|
|
1138
|
-
const intensity = isHighContrast ? pairHC(def.intensity) : pairNormal(def.intensity);
|
|
1139
|
-
const tuning = resolveShadowTuning(def.tuning);
|
|
1140
|
-
return computeShadow(bgVariant, fgVariant, intensity, tuning);
|
|
1141
|
-
}
|
|
1142
|
-
function variantToLinearRgb(v) {
|
|
1143
|
-
return okhslToLinearSrgb(v.h, v.s, v.l);
|
|
2896
|
+
throw new Error(`glaze: unsupported color function "${fn}".`);
|
|
1144
2897
|
}
|
|
1145
2898
|
/**
|
|
1146
|
-
*
|
|
1147
|
-
*
|
|
1148
|
-
* use the hue from the color that has saturation (matches CSS
|
|
1149
|
-
* color-mix "missing component" behavior).
|
|
2899
|
+
* Validate a user-supplied `OkhslColor`. Catches the common 0-100 vs 0-1
|
|
2900
|
+
* confusion (the structured form uses 0-100, OKHSL objects use 0-1).
|
|
1150
2901
|
*/
|
|
1151
|
-
function
|
|
1152
|
-
const
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
2902
|
+
function validateOkhslColor(value) {
|
|
2903
|
+
const { h, s, l } = value;
|
|
2904
|
+
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) throw new Error("glaze.color: OkhslColor h/s/l must be finite numbers.");
|
|
2905
|
+
if (s > 1.5 || l > 1.5) throw new Error("glaze.color: OkhslColor s/l must be in 0–1 range. Did you mean the structured form { hue, saturation, tone } (which uses 0–100)?");
|
|
2906
|
+
}
|
|
2907
|
+
/** Validate a user-supplied `{ r, g, b }` object in 0–255. */
|
|
2908
|
+
function validateRgbColor(value) {
|
|
2909
|
+
for (const key of [
|
|
2910
|
+
"r",
|
|
2911
|
+
"g",
|
|
2912
|
+
"b"
|
|
2913
|
+
]) {
|
|
2914
|
+
const n = value[key];
|
|
2915
|
+
if (!Number.isFinite(n) || n < 0 || n > 255) throw new Error(`glaze.color: RgbColor ${key} must be a finite number in 0–255 (got ${n}).`);
|
|
2916
|
+
}
|
|
1158
2917
|
}
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
];
|
|
2918
|
+
/** Validate a user-supplied `{ l, c, h }` OKLCh object. */
|
|
2919
|
+
function validateOklchColor(value) {
|
|
2920
|
+
const { l, c, h } = value;
|
|
2921
|
+
if (!Number.isFinite(l) || !Number.isFinite(c) || !Number.isFinite(h)) throw new Error("glaze.color: OklchColor l/c/h must be finite numbers.");
|
|
2922
|
+
if (l > 1.5 || c > 1.5) throw new Error("glaze.color: OklchColor l/c must be in 0–1 range (matching oklch() strings).");
|
|
1165
2923
|
}
|
|
1166
|
-
function
|
|
1167
|
-
const
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
2924
|
+
function oklchComponentsToOkhsl(l, c, hDeg) {
|
|
2925
|
+
const hRad = hDeg * Math.PI / 180;
|
|
2926
|
+
const [h, s, outL] = oklabToOkhsl([
|
|
2927
|
+
l,
|
|
2928
|
+
c * Math.cos(hRad),
|
|
2929
|
+
c * Math.sin(hRad)
|
|
1171
2930
|
]);
|
|
1172
2931
|
return {
|
|
1173
2932
|
h,
|
|
1174
2933
|
s,
|
|
1175
|
-
l
|
|
1176
|
-
alpha: 1
|
|
2934
|
+
l: outL
|
|
1177
2935
|
};
|
|
1178
2936
|
}
|
|
1179
|
-
function
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
2937
|
+
function isRgbColorObject(value) {
|
|
2938
|
+
return "r" in value && "g" in value && "b" in value;
|
|
2939
|
+
}
|
|
2940
|
+
function isOklchColorObject(value) {
|
|
2941
|
+
return "c" in value && "l" in value && "h" in value;
|
|
2942
|
+
}
|
|
2943
|
+
function isOkhstColorObject(value) {
|
|
2944
|
+
return "t" in value && "h" in value && "s" in value;
|
|
2945
|
+
}
|
|
2946
|
+
/** Validate a user-supplied `{ h, s, t }` OKHST object (s/t in 0–1). */
|
|
2947
|
+
function validateOkhstColor(value) {
|
|
2948
|
+
const { h, s, t } = value;
|
|
2949
|
+
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(t)) throw new Error("glaze.color: OkhstColor h/s/t must be finite numbers.");
|
|
2950
|
+
if (s > 1.5 || t > 1.5) throw new Error("glaze.color: OkhstColor s/t must be in 0–1 range. Did you mean the structured form { hue, saturation, tone } (which uses 0–100)?");
|
|
2951
|
+
}
|
|
2952
|
+
/**
|
|
2953
|
+
* Validate a user-supplied `opacity` override on `glaze.color()`.
|
|
2954
|
+
* Must be a finite number in `0..=1`.
|
|
2955
|
+
*/
|
|
2956
|
+
function validateStandaloneOpacity(value) {
|
|
2957
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) throw new Error(`glaze.color: opacity must be a finite number in 0–1 (got ${value}).`);
|
|
2958
|
+
}
|
|
2959
|
+
/**
|
|
2960
|
+
* Validate a structured `GlazeColorInput`. Range-checks the `hue` /
|
|
2961
|
+
* `saturation` / `tone` numerics (and any HC-pair second value)
|
|
2962
|
+
* before the resolver sees them so out-of-range or non-finite inputs
|
|
2963
|
+
* fail with a helpful, top-level error rather than producing a
|
|
2964
|
+
* NaN-laden token. `opacity` is checked here too so all input
|
|
2965
|
+
* validation lives in one place.
|
|
2966
|
+
*/
|
|
2967
|
+
function validateStructuredInput(input) {
|
|
2968
|
+
if (!Number.isFinite(input.hue)) throw new Error(`glaze.color: structured hue must be a finite number (got ${input.hue}).`);
|
|
2969
|
+
if (!Number.isFinite(input.saturation) || input.saturation < 0 || input.saturation > 100) throw new Error(`glaze.color: structured saturation must be a finite number in 0–100 (got ${input.saturation}).`);
|
|
2970
|
+
const checkTone = (value, label) => {
|
|
2971
|
+
if (value === "max" || value === "min") return;
|
|
2972
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 100) throw new Error(`glaze.color: structured ${label} must be a finite number in 0–100 or 'max'/'min' (got ${String(value)}).`);
|
|
2973
|
+
};
|
|
2974
|
+
if (Array.isArray(input.tone)) {
|
|
2975
|
+
checkTone(input.tone[0], "tone[normal]");
|
|
2976
|
+
checkTone(input.tone[1], "tone[hc]");
|
|
2977
|
+
} else checkTone(input.tone, "tone");
|
|
2978
|
+
if (input.saturationFactor !== void 0) {
|
|
2979
|
+
if (!Number.isFinite(input.saturationFactor) || input.saturationFactor < 0 || input.saturationFactor > 1) throw new Error(`glaze.color: structured saturationFactor must be a finite number in 0–1 (got ${input.saturationFactor}).`);
|
|
2980
|
+
}
|
|
2981
|
+
if (input.opacity !== void 0) validateStandaloneOpacity(input.opacity);
|
|
2982
|
+
}
|
|
2983
|
+
/**
|
|
2984
|
+
* Validate a user-supplied `name` override. Rejects empty / whitespace-only
|
|
2985
|
+
* strings and names colliding with `glaze`'s reserved internal sentinels.
|
|
2986
|
+
*/
|
|
2987
|
+
function validateStandaloneName(name) {
|
|
2988
|
+
if (typeof name !== "string" || name.trim() === "") throw new Error("glaze.color: name must be a non-empty string. Omit `name` if you do not want to set a debug label.");
|
|
2989
|
+
if (RESERVED_STANDALONE_NAMES.has(name)) {
|
|
2990
|
+
const reserved = [...RESERVED_STANDALONE_NAMES].map((n) => `"${n}"`).join(", ");
|
|
2991
|
+
throw new Error(`glaze.color: name "${name}" is reserved (used internally). Reserved names are: ${reserved}. Pick a different name.`);
|
|
2992
|
+
}
|
|
2993
|
+
}
|
|
2994
|
+
/**
|
|
2995
|
+
* Extract an OKHSL color from any `GlazeColorValue` form. Also used by
|
|
2996
|
+
* `glaze.shadow()` so all shadow inputs (hex, color functions, OKHSL,
|
|
2997
|
+
* literal objects) go through one parser.
|
|
2998
|
+
*/
|
|
2999
|
+
function extractOkhslFromValue(value) {
|
|
3000
|
+
if (typeof value === "string") return parseColorString(value);
|
|
3001
|
+
if (Array.isArray(value)) throw new Error("glaze.color: RGB tuple [r, g, b] is no longer supported — use { r, g, b } instead.");
|
|
3002
|
+
if (isRgbColorObject(value)) {
|
|
3003
|
+
validateRgbColor(value);
|
|
3004
|
+
const [h, s, l] = srgbToOkhsl([
|
|
3005
|
+
value.r / 255,
|
|
3006
|
+
value.g / 255,
|
|
3007
|
+
value.b / 255
|
|
3008
|
+
]);
|
|
3009
|
+
return {
|
|
3010
|
+
h,
|
|
3011
|
+
s,
|
|
3012
|
+
l
|
|
1196
3013
|
};
|
|
1197
|
-
t = findValueForMixContrast({
|
|
1198
|
-
preferredValue: t,
|
|
1199
|
-
baseLinearRgb: baseLinear,
|
|
1200
|
-
targetLinearRgb: targetLinear,
|
|
1201
|
-
contrast: minCr,
|
|
1202
|
-
luminanceAtValue: luminanceAt
|
|
1203
|
-
}).value;
|
|
1204
3014
|
}
|
|
1205
|
-
if (
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
3015
|
+
if (isOklchColorObject(value)) {
|
|
3016
|
+
validateOklchColor(value);
|
|
3017
|
+
return oklchComponentsToOkhsl(value.l, value.c, value.h);
|
|
3018
|
+
}
|
|
3019
|
+
if (isOkhstColorObject(value)) {
|
|
3020
|
+
validateOkhstColor(value);
|
|
3021
|
+
return okhstToOkhsl(value);
|
|
3022
|
+
}
|
|
3023
|
+
validateOkhslColor(value);
|
|
3024
|
+
return value;
|
|
3025
|
+
}
|
|
3026
|
+
/**
|
|
3027
|
+
* Build the `ColorMap` for a value-shorthand `glaze.color()` call.
|
|
3028
|
+
*
|
|
3029
|
+
* The user-facing color (`STANDALONE_VALUE`) defaults to `mode: 'auto'`
|
|
3030
|
+
* across every value-shorthand form.
|
|
3031
|
+
*
|
|
3032
|
+
* When the user requests `contrast` or relative `tone`, a hidden
|
|
3033
|
+
* `STANDALONE_SEED` def is synthesized at `mode: 'static'`. That keeps
|
|
3034
|
+
* the seed pinned to the literal user-provided color across all four
|
|
3035
|
+
* variants, so the contrast solver always anchors against it.
|
|
3036
|
+
*/
|
|
3037
|
+
function buildStandaloneValueDefs(main, options) {
|
|
3038
|
+
const seedHue = typeof options?.hue === "number" ? options.hue : main.h;
|
|
3039
|
+
const seedSaturation = options?.saturation ?? main.s * 100;
|
|
3040
|
+
const relativeHue = typeof options?.hue === "string" ? options.hue : void 0;
|
|
3041
|
+
const toneOption = options?.tone;
|
|
3042
|
+
const hasExternalBase = options?.base !== void 0;
|
|
3043
|
+
const needsSeedAnchor = !hasExternalBase && (options?.contrast !== void 0 || toneOption !== void 0 && !isAbsoluteTone(toneOption));
|
|
3044
|
+
if (options?.opacity !== void 0) validateStandaloneOpacity(options.opacity);
|
|
3045
|
+
const userName = options?.name;
|
|
3046
|
+
if (userName !== void 0) validateStandaloneName(userName);
|
|
3047
|
+
const primary = userName ?? STANDALONE_VALUE;
|
|
3048
|
+
const seedTone = toTone(main.l);
|
|
3049
|
+
const valueDef = {
|
|
3050
|
+
hue: relativeHue,
|
|
3051
|
+
saturation: options?.saturationFactor,
|
|
3052
|
+
tone: toneOption ?? seedTone,
|
|
3053
|
+
contrast: options?.contrast,
|
|
3054
|
+
mode: options?.mode ?? "auto",
|
|
3055
|
+
autoFlip: options?.autoFlip,
|
|
3056
|
+
opacity: options?.opacity,
|
|
3057
|
+
pastel: options?.pastel,
|
|
3058
|
+
role: options?.role,
|
|
3059
|
+
base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
|
|
3060
|
+
};
|
|
3061
|
+
const defs = { [primary]: valueDef };
|
|
3062
|
+
if (needsSeedAnchor) defs[STANDALONE_SEED] = {
|
|
3063
|
+
hue: main.h,
|
|
3064
|
+
saturation: 1,
|
|
3065
|
+
tone: seedTone,
|
|
3066
|
+
mode: "static"
|
|
1210
3067
|
};
|
|
1211
|
-
if (space === "srgb") return linearRgbToVariant(linearSrgbLerp(baseLinear, targetLinear, t));
|
|
1212
3068
|
return {
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
3069
|
+
seedHue,
|
|
3070
|
+
seedSaturation,
|
|
3071
|
+
defs,
|
|
3072
|
+
primary
|
|
3073
|
+
};
|
|
3074
|
+
}
|
|
3075
|
+
function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveConfig, baseToken, exportData) {
|
|
3076
|
+
let cached;
|
|
3077
|
+
const resolveOnce = () => {
|
|
3078
|
+
if (cached) return cached;
|
|
3079
|
+
cached = resolveAllColors(seedHue, seedSaturation, defs, effectiveConfig, baseToken ? new Map([[STANDALONE_BASE, baseToken.resolve()]]) : void 0);
|
|
3080
|
+
return cached;
|
|
3081
|
+
};
|
|
3082
|
+
const resolveStates = (options) => {
|
|
3083
|
+
const cfg = getConfig();
|
|
3084
|
+
return {
|
|
3085
|
+
dark: options?.states?.dark ?? cfg.states.dark,
|
|
3086
|
+
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
3087
|
+
};
|
|
3088
|
+
};
|
|
3089
|
+
const tokenLike = (options) => {
|
|
3090
|
+
return buildTokenMap(resolveOnce(), "", resolveStates(options), resolveModes(options?.modes), options?.format ?? "oklch", effectiveConfig.pastel)[`#${primary}`];
|
|
3091
|
+
};
|
|
3092
|
+
return {
|
|
3093
|
+
resolve() {
|
|
3094
|
+
return resolveOnce().get(primary);
|
|
3095
|
+
},
|
|
3096
|
+
token: tokenLike,
|
|
3097
|
+
tasty: tokenLike,
|
|
3098
|
+
json(options) {
|
|
3099
|
+
const format = options?.format ?? "oklch";
|
|
3100
|
+
assertNativeFormat(format, "json");
|
|
3101
|
+
return buildJsonMap(resolveOnce(), resolveModes(options?.modes), format, effectiveConfig.pastel)[primary];
|
|
3102
|
+
},
|
|
3103
|
+
css(options) {
|
|
3104
|
+
const format = options.format ?? "oklch";
|
|
3105
|
+
assertNativeFormat(format, "css");
|
|
3106
|
+
const resolved = resolveOnce().get(primary);
|
|
3107
|
+
const renamed = new Map([[options.name, resolved]]);
|
|
3108
|
+
let channelCtx;
|
|
3109
|
+
if (options.splitHue && format === "oklch") {
|
|
3110
|
+
assertAllPastel(renamed, resolveModes());
|
|
3111
|
+
channelCtx = {
|
|
3112
|
+
seedHue,
|
|
3113
|
+
baseName: options.name,
|
|
3114
|
+
prefix: "",
|
|
3115
|
+
defs: { [options.name]: defs[primary] },
|
|
3116
|
+
mode: "standalone",
|
|
3117
|
+
resolvedHue: resolved.light.h
|
|
3118
|
+
};
|
|
3119
|
+
}
|
|
3120
|
+
return buildCssMap(renamed, "", options.suffix ?? "-color", format, effectiveConfig.pastel, channelCtx);
|
|
3121
|
+
},
|
|
3122
|
+
dtcg(options) {
|
|
3123
|
+
const modes = resolveModes(options?.modes);
|
|
3124
|
+
const doc = buildDtcgMap(resolveOnce(), "", modes, options?.colorSpace ?? "srgb", effectiveConfig.pastel);
|
|
3125
|
+
const result = { light: doc.light[primary] };
|
|
3126
|
+
if (doc.dark) result.dark = doc.dark[primary];
|
|
3127
|
+
if (doc.lightContrast) result.lightContrast = doc.lightContrast[primary];
|
|
3128
|
+
if (doc.darkContrast) result.darkContrast = doc.darkContrast[primary];
|
|
3129
|
+
return result;
|
|
3130
|
+
},
|
|
3131
|
+
dtcgResolver(options) {
|
|
3132
|
+
const doc = buildDtcgMap(resolveOnce(), "", resolveModes(options?.modes), options?.colorSpace ?? "srgb", effectiveConfig.pastel);
|
|
3133
|
+
const name = options.name;
|
|
3134
|
+
const result = { light: { [name]: doc.light[primary] } };
|
|
3135
|
+
if (doc.dark) result.dark = { [name]: doc.dark[primary] };
|
|
3136
|
+
if (doc.lightContrast) result.lightContrast = { [name]: doc.lightContrast[primary] };
|
|
3137
|
+
if (doc.darkContrast) result.darkContrast = { [name]: doc.darkContrast[primary] };
|
|
3138
|
+
return buildDtcgResolver(result, options);
|
|
3139
|
+
},
|
|
3140
|
+
tailwind(options) {
|
|
3141
|
+
const format = options.format ?? "oklch";
|
|
3142
|
+
assertNativeFormat(format, "tailwind");
|
|
3143
|
+
return buildTailwindMap(new Map([[options.name, resolveOnce().get(primary)]]), "", options.namespace ?? "color-", resolveModes(options?.modes), format, options.darkSelector ?? ".dark", options.highContrastSelector ?? ".high-contrast", effectiveConfig.pastel);
|
|
3144
|
+
},
|
|
3145
|
+
export: exportData
|
|
1217
3146
|
};
|
|
1218
3147
|
}
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
3148
|
+
/**
|
|
3149
|
+
* When a value/`from` color links to a base that was created via the
|
|
3150
|
+
* structured form (with explicit `hue`/`saturation`/`tone`), resolve
|
|
3151
|
+
* that base with `lightTone: false` for the linking math so the
|
|
3152
|
+
* contrast/tone anchor matches the input tone — not the
|
|
3153
|
+
* windowed output. The original base token's `.resolve()` is unaffected.
|
|
3154
|
+
*/
|
|
3155
|
+
function toLinkingBase(base) {
|
|
3156
|
+
if (!base) return void 0;
|
|
3157
|
+
const exp = base.export();
|
|
3158
|
+
if (exp.form !== "structured") return base;
|
|
3159
|
+
const linkingConfig = {
|
|
3160
|
+
...exp.config ?? {},
|
|
3161
|
+
lightTone: false
|
|
1227
3162
|
};
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
}
|
|
1232
|
-
const lightMap = /* @__PURE__ */ new Map();
|
|
1233
|
-
for (const name of order) {
|
|
1234
|
-
const variant = resolveColorForScheme(name, defs[name], ctx, false, false);
|
|
1235
|
-
lightMap.set(name, variant);
|
|
1236
|
-
ctx.resolved.set(name, {
|
|
1237
|
-
name,
|
|
1238
|
-
light: variant,
|
|
1239
|
-
dark: variant,
|
|
1240
|
-
lightContrast: variant,
|
|
1241
|
-
darkContrast: variant,
|
|
1242
|
-
mode: defMode(defs[name])
|
|
1243
|
-
});
|
|
1244
|
-
}
|
|
1245
|
-
const lightHCMap = /* @__PURE__ */ new Map();
|
|
1246
|
-
for (const name of order) ctx.resolved.set(name, {
|
|
1247
|
-
...ctx.resolved.get(name),
|
|
1248
|
-
lightContrast: lightMap.get(name)
|
|
1249
|
-
});
|
|
1250
|
-
for (const name of order) {
|
|
1251
|
-
const variant = resolveColorForScheme(name, defs[name], ctx, false, true);
|
|
1252
|
-
lightHCMap.set(name, variant);
|
|
1253
|
-
ctx.resolved.set(name, {
|
|
1254
|
-
...ctx.resolved.get(name),
|
|
1255
|
-
lightContrast: variant
|
|
1256
|
-
});
|
|
1257
|
-
}
|
|
1258
|
-
const darkMap = /* @__PURE__ */ new Map();
|
|
1259
|
-
for (const name of order) ctx.resolved.set(name, {
|
|
1260
|
-
name,
|
|
1261
|
-
light: lightMap.get(name),
|
|
1262
|
-
dark: lightMap.get(name),
|
|
1263
|
-
lightContrast: lightHCMap.get(name),
|
|
1264
|
-
darkContrast: lightHCMap.get(name),
|
|
1265
|
-
mode: defMode(defs[name])
|
|
3163
|
+
return colorFromExport({
|
|
3164
|
+
...exp,
|
|
3165
|
+
config: linkingConfig
|
|
1266
3166
|
});
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
3167
|
+
}
|
|
3168
|
+
/**
|
|
3169
|
+
* Resolve `base` (which may be a token reference or a raw color value)
|
|
3170
|
+
* into a `GlazeColorToken`. Raw values are auto-wrapped via
|
|
3171
|
+
* `createColorTokenFromValue` so they pick up the same auto-invert
|
|
3172
|
+
* defaults as an explicit wrap. Returns `undefined` when no base is provided.
|
|
3173
|
+
*/
|
|
3174
|
+
function resolveBaseToken(base) {
|
|
3175
|
+
if (base === void 0) return void 0;
|
|
3176
|
+
if (isGlazeColorToken(base)) return base;
|
|
3177
|
+
return createColorTokenFromValue(base, void 0, void 0);
|
|
3178
|
+
}
|
|
3179
|
+
/**
|
|
3180
|
+
* Discriminate a `GlazeColorToken` from a raw `GlazeColorValue`.
|
|
3181
|
+
*/
|
|
3182
|
+
function isGlazeColorToken(candidate) {
|
|
3183
|
+
return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "resolve" in candidate && typeof candidate.resolve === "function";
|
|
3184
|
+
}
|
|
3185
|
+
function createColorToken(input, configOverride) {
|
|
3186
|
+
validateStructuredInput(input);
|
|
3187
|
+
const userName = input.name;
|
|
3188
|
+
if (userName !== void 0) validateStandaloneName(userName);
|
|
3189
|
+
const primary = userName ?? STANDALONE_VALUE;
|
|
3190
|
+
const baseToken = resolveBaseToken(input.base);
|
|
3191
|
+
const hasExternalBase = baseToken !== void 0;
|
|
3192
|
+
const needsSeedAnchor = !hasExternalBase && input.contrast !== void 0;
|
|
3193
|
+
const defs = { [primary]: {
|
|
3194
|
+
tone: input.tone,
|
|
3195
|
+
saturation: input.saturationFactor,
|
|
3196
|
+
mode: input.mode ?? "auto",
|
|
3197
|
+
autoFlip: input.autoFlip,
|
|
3198
|
+
contrast: input.contrast,
|
|
3199
|
+
opacity: input.opacity,
|
|
3200
|
+
pastel: input.pastel,
|
|
3201
|
+
role: input.role,
|
|
3202
|
+
base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
|
|
3203
|
+
} };
|
|
3204
|
+
if (needsSeedAnchor) {
|
|
3205
|
+
const seedTone = pairNormal(input.tone);
|
|
3206
|
+
defs[STANDALONE_SEED] = {
|
|
3207
|
+
tone: seedTone === "max" ? 100 : seedTone === "min" ? 0 : seedTone,
|
|
3208
|
+
saturation: 1,
|
|
3209
|
+
mode: "static"
|
|
3210
|
+
};
|
|
1274
3211
|
}
|
|
1275
|
-
const
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
3212
|
+
const effectiveConfigOverride = buildStructuredConfigOverride(configOverride);
|
|
3213
|
+
const effectiveConfig = resolvedConfigFromOverride(effectiveConfigOverride);
|
|
3214
|
+
const exportData = () => ({
|
|
3215
|
+
kind: "color",
|
|
3216
|
+
version: GLAZE_EXPORT_VERSION,
|
|
3217
|
+
form: "structured",
|
|
3218
|
+
input: buildStructuredInputExport(input),
|
|
3219
|
+
config: effectiveConfigOverride
|
|
1279
3220
|
});
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
const
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
mode: defMode(defs[name])
|
|
3221
|
+
return createColorTokenFromDefs(input.hue, input.saturation, defs, primary, effectiveConfig, baseToken, exportData);
|
|
3222
|
+
}
|
|
3223
|
+
function createColorTokenFromValue(value, options, configOverride) {
|
|
3224
|
+
const main = extractOkhslFromValue(value);
|
|
3225
|
+
const linkingBase = toLinkingBase(resolveBaseToken(options?.base));
|
|
3226
|
+
const { seedHue, seedSaturation, defs, primary } = buildStandaloneValueDefs(main, options);
|
|
3227
|
+
const effectiveConfigOverride = buildValueFormConfigOverride(configOverride);
|
|
3228
|
+
const effectiveConfig = resolvedConfigFromOverride(effectiveConfigOverride);
|
|
3229
|
+
const exportData = () => ({
|
|
3230
|
+
kind: "color",
|
|
3231
|
+
version: GLAZE_EXPORT_VERSION,
|
|
3232
|
+
form: "value",
|
|
3233
|
+
input: value,
|
|
3234
|
+
...options !== void 0 ? { overrides: buildOverridesExport(options) } : {},
|
|
3235
|
+
config: effectiveConfigOverride
|
|
1296
3236
|
});
|
|
1297
|
-
return
|
|
1298
|
-
}
|
|
1299
|
-
const formatters = {
|
|
1300
|
-
okhsl: formatOkhsl,
|
|
1301
|
-
rgb: formatRgb,
|
|
1302
|
-
hsl: formatHsl,
|
|
1303
|
-
oklch: formatOklch
|
|
1304
|
-
};
|
|
1305
|
-
function fmt(value, decimals) {
|
|
1306
|
-
return parseFloat(value.toFixed(decimals)).toString();
|
|
3237
|
+
return createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveConfig, linkingBase, exportData);
|
|
1307
3238
|
}
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
3239
|
+
/**
|
|
3240
|
+
* Build a JSON-safe snapshot of `GlazeColorOverrides`. `base` is
|
|
3241
|
+
* recursively serialized when it was originally a token; raw values are
|
|
3242
|
+
* preserved as-is so `glaze.colorFrom(...)` round-trips them.
|
|
3243
|
+
*/
|
|
3244
|
+
function buildOverridesExport(options) {
|
|
3245
|
+
const out = {};
|
|
3246
|
+
if (options.hue !== void 0) out.hue = options.hue;
|
|
3247
|
+
if (options.saturation !== void 0) out.saturation = options.saturation;
|
|
3248
|
+
if (options.tone !== void 0) out.tone = options.tone;
|
|
3249
|
+
if (options.saturationFactor !== void 0) out.saturationFactor = options.saturationFactor;
|
|
3250
|
+
if (options.mode !== void 0) out.mode = options.mode;
|
|
3251
|
+
if (options.autoFlip !== void 0) out.autoFlip = options.autoFlip;
|
|
3252
|
+
if (options.contrast !== void 0) out.contrast = options.contrast;
|
|
3253
|
+
if (options.opacity !== void 0) out.opacity = options.opacity;
|
|
3254
|
+
if (options.name !== void 0) out.name = options.name;
|
|
3255
|
+
if (options.pastel !== void 0) out.pastel = options.pastel;
|
|
3256
|
+
if (options.role !== void 0) out.role = options.role;
|
|
3257
|
+
if (options.base !== void 0) out.base = isGlazeColorToken(options.base) ? options.base.export() : options.base;
|
|
3258
|
+
return out;
|
|
3259
|
+
}
|
|
3260
|
+
function buildStructuredInputExport(input) {
|
|
3261
|
+
const out = {
|
|
3262
|
+
hue: input.hue,
|
|
3263
|
+
saturation: input.saturation,
|
|
3264
|
+
tone: input.tone
|
|
3265
|
+
};
|
|
3266
|
+
if (input.saturationFactor !== void 0) out.saturationFactor = input.saturationFactor;
|
|
3267
|
+
if (input.mode !== void 0) out.mode = input.mode;
|
|
3268
|
+
if (input.autoFlip !== void 0) out.autoFlip = input.autoFlip;
|
|
3269
|
+
if (input.opacity !== void 0) out.opacity = input.opacity;
|
|
3270
|
+
if (input.contrast !== void 0) out.contrast = input.contrast;
|
|
3271
|
+
if (input.name !== void 0) out.name = input.name;
|
|
3272
|
+
if (input.pastel !== void 0) out.pastel = input.pastel;
|
|
3273
|
+
if (input.role !== void 0) out.role = input.role;
|
|
3274
|
+
if (input.base !== void 0) out.base = isGlazeColorToken(input.base) ? input.base.export() : input.base;
|
|
3275
|
+
return out;
|
|
1313
3276
|
}
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
3277
|
+
/**
|
|
3278
|
+
* Discriminate a `GlazeColorTokenExport` from a raw `GlazeColorValue`.
|
|
3279
|
+
*/
|
|
3280
|
+
function isExportedToken(candidate) {
|
|
3281
|
+
return isColorTokenExport(candidate);
|
|
3282
|
+
}
|
|
3283
|
+
function rehydrateOverrides(data) {
|
|
3284
|
+
const out = {};
|
|
3285
|
+
if (data.hue !== void 0) out.hue = data.hue;
|
|
3286
|
+
if (data.saturation !== void 0) out.saturation = data.saturation;
|
|
3287
|
+
if (data.tone !== void 0) out.tone = data.tone;
|
|
3288
|
+
if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
|
|
3289
|
+
if (data.mode !== void 0) out.mode = data.mode;
|
|
3290
|
+
if (data.autoFlip !== void 0) out.autoFlip = data.autoFlip;
|
|
3291
|
+
if (data.contrast !== void 0) out.contrast = data.contrast;
|
|
3292
|
+
if (data.opacity !== void 0) out.opacity = data.opacity;
|
|
3293
|
+
if (data.name !== void 0) out.name = data.name;
|
|
3294
|
+
if (data.pastel !== void 0) out.pastel = data.pastel;
|
|
3295
|
+
if (data.role !== void 0) out.role = data.role;
|
|
3296
|
+
if (data.base !== void 0) out.base = isExportedToken(data.base) ? colorFromExport(data.base) : data.base;
|
|
3297
|
+
return out;
|
|
3298
|
+
}
|
|
3299
|
+
function rehydrateStructuredInput(data) {
|
|
3300
|
+
const out = {
|
|
3301
|
+
hue: data.hue,
|
|
3302
|
+
saturation: data.saturation,
|
|
3303
|
+
tone: data.tone
|
|
1318
3304
|
};
|
|
3305
|
+
if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
|
|
3306
|
+
if (data.mode !== void 0) out.mode = data.mode;
|
|
3307
|
+
if (data.autoFlip !== void 0) out.autoFlip = data.autoFlip;
|
|
3308
|
+
if (data.opacity !== void 0) out.opacity = data.opacity;
|
|
3309
|
+
if (data.contrast !== void 0) out.contrast = data.contrast;
|
|
3310
|
+
if (data.name !== void 0) out.name = data.name;
|
|
3311
|
+
if (data.pastel !== void 0) out.pastel = data.pastel;
|
|
3312
|
+
if (data.role !== void 0) out.role = data.role;
|
|
3313
|
+
if (data.base !== void 0) out.base = isExportedToken(data.base) ? colorFromExport(data.base) : data.base;
|
|
3314
|
+
return out;
|
|
1319
3315
|
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
3316
|
+
/**
|
|
3317
|
+
* Rehydrate a token from its `.export()` snapshot. Recursively rebuilds
|
|
3318
|
+
* any base dependency. Inverse of `GlazeColorToken.export()`.
|
|
3319
|
+
*
|
|
3320
|
+
* The stored `config` field contains the full effective config override
|
|
3321
|
+
* snapshotted at creation time, so the rehydrated token is deterministic
|
|
3322
|
+
* regardless of subsequent `glaze.configure()` calls.
|
|
3323
|
+
*/
|
|
3324
|
+
function colorFromExport(data) {
|
|
3325
|
+
if (data === null || typeof data !== "object") throw new Error(`glaze.colorFrom: expected an object from token.export(), got ${data === null ? "null" : typeof data}.`);
|
|
3326
|
+
assertExportKind(data, "color", "glaze.colorFrom");
|
|
3327
|
+
assertExportVersion(data, "glaze.colorFrom");
|
|
3328
|
+
if (data.form !== "value" && data.form !== "structured") throw new Error(`glaze.colorFrom: invalid "form" field — expected "value" or "structured" (got ${JSON.stringify(data.form)}).`);
|
|
3329
|
+
if (data.input === void 0) throw new Error(`glaze.colorFrom: missing "input" field — expected the original ${data.form === "value" ? "GlazeColorValue" : "GlazeColorInput"}.`);
|
|
3330
|
+
if (data.form === "value") {
|
|
3331
|
+
const value = data.input;
|
|
3332
|
+
return createColorTokenFromValue(value, data.overrides ? rehydrateOverrides(data.overrides) : void 0, data.config);
|
|
1329
3333
|
}
|
|
1330
|
-
return
|
|
3334
|
+
return createColorToken(rehydrateStructuredInput(data.input), data.config);
|
|
1331
3335
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
if (
|
|
1341
|
-
|
|
1342
|
-
if (modes.dark && modes.highContrast) result.darkContrast[key] = formatVariant(color.darkContrast, format);
|
|
3336
|
+
|
|
3337
|
+
//#endregion
|
|
3338
|
+
//#region src/theme.ts
|
|
3339
|
+
function createTheme(hue, saturation, initialColors, configOverride) {
|
|
3340
|
+
let colorDefs = initialColors ? { ...initialColors } : {};
|
|
3341
|
+
let cache = null;
|
|
3342
|
+
function getEffectiveConfig() {
|
|
3343
|
+
const version = getConfigVersion();
|
|
3344
|
+
if (cache && cache.version === version) return cache.effectiveConfig;
|
|
3345
|
+
return mergeConfig(getConfig(), configOverride);
|
|
1343
3346
|
}
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
3347
|
+
function resolveCached() {
|
|
3348
|
+
const version = getConfigVersion();
|
|
3349
|
+
if (cache && cache.version === version) return cache.map;
|
|
3350
|
+
const effectiveConfig = mergeConfig(getConfig(), configOverride);
|
|
3351
|
+
const map = resolveAllColors(hue, saturation, colorDefs, effectiveConfig);
|
|
3352
|
+
cache = {
|
|
3353
|
+
map,
|
|
3354
|
+
version,
|
|
3355
|
+
effectiveConfig
|
|
3356
|
+
};
|
|
3357
|
+
return map;
|
|
1354
3358
|
}
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
lines.darkContrast.push(`${prop}: ${formatVariant(color.darkContrast, format)};`);
|
|
3359
|
+
function invalidate() {
|
|
3360
|
+
cache = null;
|
|
3361
|
+
}
|
|
3362
|
+
function channelCtxFor(options, formatDefault, prefix) {
|
|
3363
|
+
const format = options?.format ?? formatDefault;
|
|
3364
|
+
if (!options?.splitHue || format !== "oklch") return void 0;
|
|
3365
|
+
assertAllPastel(resolveCached(), resolveModes(options?.modes));
|
|
3366
|
+
return {
|
|
3367
|
+
seedHue: hue,
|
|
3368
|
+
baseName: options.name ?? "theme",
|
|
3369
|
+
prefix,
|
|
3370
|
+
defs: colorDefs,
|
|
3371
|
+
mode: "theme"
|
|
3372
|
+
};
|
|
1370
3373
|
}
|
|
1371
|
-
return {
|
|
1372
|
-
light: lines.light.join("\n"),
|
|
1373
|
-
dark: lines.dark.join("\n"),
|
|
1374
|
-
lightContrast: lines.lightContrast.join("\n"),
|
|
1375
|
-
darkContrast: lines.darkContrast.join("\n")
|
|
1376
|
-
};
|
|
1377
|
-
}
|
|
1378
|
-
function createTheme(hue, saturation, initialColors) {
|
|
1379
|
-
let colorDefs = initialColors ? { ...initialColors } : {};
|
|
1380
3374
|
return {
|
|
1381
3375
|
get hue() {
|
|
1382
3376
|
return hue;
|
|
@@ -1384,19 +3378,25 @@ function createTheme(hue, saturation, initialColors) {
|
|
|
1384
3378
|
get saturation() {
|
|
1385
3379
|
return saturation;
|
|
1386
3380
|
},
|
|
3381
|
+
getConfig() {
|
|
3382
|
+
return getEffectiveConfig();
|
|
3383
|
+
},
|
|
1387
3384
|
colors(defs) {
|
|
1388
3385
|
colorDefs = {
|
|
1389
3386
|
...colorDefs,
|
|
1390
3387
|
...defs
|
|
1391
3388
|
};
|
|
3389
|
+
invalidate();
|
|
1392
3390
|
},
|
|
1393
3391
|
color(name, def) {
|
|
1394
3392
|
if (def === void 0) return colorDefs[name];
|
|
1395
3393
|
colorDefs[name] = def;
|
|
3394
|
+
invalidate();
|
|
1396
3395
|
},
|
|
1397
3396
|
remove(names) {
|
|
1398
3397
|
const list = Array.isArray(names) ? names : [names];
|
|
1399
3398
|
for (const name of list) delete colorDefs[name];
|
|
3399
|
+
invalidate();
|
|
1400
3400
|
},
|
|
1401
3401
|
has(name) {
|
|
1402
3402
|
return name in colorDefs;
|
|
@@ -1406,12 +3406,16 @@ function createTheme(hue, saturation, initialColors) {
|
|
|
1406
3406
|
},
|
|
1407
3407
|
reset() {
|
|
1408
3408
|
colorDefs = {};
|
|
3409
|
+
invalidate();
|
|
1409
3410
|
},
|
|
1410
3411
|
export() {
|
|
1411
3412
|
return {
|
|
3413
|
+
kind: "theme",
|
|
3414
|
+
version: GLAZE_EXPORT_VERSION,
|
|
1412
3415
|
hue,
|
|
1413
3416
|
saturation,
|
|
1414
|
-
colors:
|
|
3417
|
+
colors: structuredClone(colorDefs),
|
|
3418
|
+
config: structuredClone(buildEffectiveConfigOverride(configOverride))
|
|
1415
3419
|
};
|
|
1416
3420
|
},
|
|
1417
3421
|
extend(options) {
|
|
@@ -1422,28 +3426,61 @@ function createTheme(hue, saturation, initialColors) {
|
|
|
1422
3426
|
return createTheme(newHue, newSat, options.colors ? {
|
|
1423
3427
|
...inheritedColors,
|
|
1424
3428
|
...options.colors
|
|
1425
|
-
} : { ...inheritedColors }
|
|
3429
|
+
} : { ...inheritedColors }, configOverride || options.config ? {
|
|
3430
|
+
...configOverride ?? {},
|
|
3431
|
+
...options.config ?? {}
|
|
3432
|
+
} : void 0);
|
|
1426
3433
|
},
|
|
1427
3434
|
resolve() {
|
|
1428
|
-
return
|
|
3435
|
+
return new Map(resolveCached());
|
|
1429
3436
|
},
|
|
1430
3437
|
tokens(options) {
|
|
1431
|
-
|
|
3438
|
+
const format = options?.format ?? "oklch";
|
|
3439
|
+
assertNativeFormat(format, "tokens");
|
|
3440
|
+
const modes = resolveModes(options?.modes);
|
|
3441
|
+
return buildFlatTokenMap(resolveCached(), "", modes, format, getEffectiveConfig().pastel);
|
|
1432
3442
|
},
|
|
1433
3443
|
tasty(options) {
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
3444
|
+
const cfg = getEffectiveConfig();
|
|
3445
|
+
const states = {
|
|
3446
|
+
dark: options?.states?.dark ?? cfg.states.dark,
|
|
3447
|
+
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
3448
|
+
};
|
|
3449
|
+
const modes = resolveModes(options?.modes);
|
|
3450
|
+
const format = options?.format ?? "oklch";
|
|
3451
|
+
const channelCtx = channelCtxFor(options, "oklch", "");
|
|
3452
|
+
return buildTokenMap(resolveCached(), "", states, modes, format, cfg.pastel, channelCtx);
|
|
1438
3453
|
},
|
|
1439
3454
|
json(options) {
|
|
1440
|
-
|
|
3455
|
+
const format = options?.format ?? "oklch";
|
|
3456
|
+
assertNativeFormat(format, "json");
|
|
3457
|
+
const modes = resolveModes(options?.modes);
|
|
3458
|
+
return buildJsonMap(resolveCached(), modes, format, getEffectiveConfig().pastel);
|
|
1441
3459
|
},
|
|
1442
3460
|
css(options) {
|
|
1443
|
-
|
|
3461
|
+
const format = options?.format ?? "oklch";
|
|
3462
|
+
assertNativeFormat(format, "css");
|
|
3463
|
+
const channelCtx = channelCtxFor(options, "oklch", "");
|
|
3464
|
+
return buildCssMap(resolveCached(), "", options?.suffix ?? "-color", format, getEffectiveConfig().pastel, channelCtx);
|
|
3465
|
+
},
|
|
3466
|
+
dtcg(options) {
|
|
3467
|
+
const modes = resolveModes(options?.modes);
|
|
3468
|
+
return buildDtcgMap(resolveCached(), "", modes, options?.colorSpace ?? "srgb", getEffectiveConfig().pastel);
|
|
3469
|
+
},
|
|
3470
|
+
dtcgResolver(options) {
|
|
3471
|
+
return buildDtcgResolver(buildDtcgMap(resolveCached(), "", resolveModes(options?.modes), options?.colorSpace ?? "srgb", getEffectiveConfig().pastel), options);
|
|
3472
|
+
},
|
|
3473
|
+
tailwind(options) {
|
|
3474
|
+
const format = options?.format ?? "oklch";
|
|
3475
|
+
assertNativeFormat(format, "tailwind");
|
|
3476
|
+
const modes = resolveModes(options?.modes);
|
|
3477
|
+
return buildTailwindMap(resolveCached(), "", options?.namespace ?? "color-", modes, format, options?.darkSelector ?? ".dark", options?.highContrastSelector ?? ".high-contrast", getEffectiveConfig().pastel);
|
|
1444
3478
|
}
|
|
1445
3479
|
};
|
|
1446
3480
|
}
|
|
3481
|
+
|
|
3482
|
+
//#endregion
|
|
3483
|
+
//#region src/palette.ts
|
|
1447
3484
|
function resolvePrefix(options, themeName, defaultPrefix = false) {
|
|
1448
3485
|
const prefix = options?.prefix ?? defaultPrefix;
|
|
1449
3486
|
if (prefix === true) return `${themeName}-`;
|
|
@@ -1483,212 +3520,335 @@ function filterCollisions(resolved, prefix, seen, themeName, isPrimary) {
|
|
|
1483
3520
|
}
|
|
1484
3521
|
return filtered;
|
|
1485
3522
|
}
|
|
3523
|
+
function colorMapFromTheme(theme) {
|
|
3524
|
+
const defs = {};
|
|
3525
|
+
for (const name of theme.list()) {
|
|
3526
|
+
const def = theme.color(name);
|
|
3527
|
+
if (def !== void 0) defs[name] = def;
|
|
3528
|
+
}
|
|
3529
|
+
return defs;
|
|
3530
|
+
}
|
|
3531
|
+
function channelCtxForTheme(theme, themeName, passPrefix, themedPrefix, splitHue, format, modes, filtered) {
|
|
3532
|
+
if (!splitHue || format !== "oklch") return void 0;
|
|
3533
|
+
assertAllPastel(filtered, modes);
|
|
3534
|
+
return {
|
|
3535
|
+
seedHue: theme.hue,
|
|
3536
|
+
baseName: themeName,
|
|
3537
|
+
prefix: themedPrefix,
|
|
3538
|
+
defs: colorMapFromTheme(theme),
|
|
3539
|
+
mode: "theme",
|
|
3540
|
+
emitDeclarations: passPrefix === themedPrefix
|
|
3541
|
+
};
|
|
3542
|
+
}
|
|
3543
|
+
/**
|
|
3544
|
+
* Shared per-theme driver for `tokens` / `tasty` / `css`. `json` skips
|
|
3545
|
+
* this because it doesn't do collision filtering or primary duplication.
|
|
3546
|
+
*/
|
|
3547
|
+
function buildPaletteOutput(themes, paletteOptions, options, buildOne, merge, empty) {
|
|
3548
|
+
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
3549
|
+
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
3550
|
+
const acc = empty();
|
|
3551
|
+
const seen = /* @__PURE__ */ new Map();
|
|
3552
|
+
for (const [themeName, theme] of Object.entries(themes)) {
|
|
3553
|
+
const resolved = theme.resolve();
|
|
3554
|
+
const pastel = theme.getConfig().pastel;
|
|
3555
|
+
const prefix = resolvePrefix(options, themeName, true);
|
|
3556
|
+
merge(acc, buildOne(filterCollisions(resolved, prefix, seen, themeName), prefix, pastel, themeName, theme));
|
|
3557
|
+
if (themeName === effectivePrimary) merge(acc, buildOne(filterCollisions(resolved, "", seen, themeName, true), "", pastel, themeName, theme));
|
|
3558
|
+
}
|
|
3559
|
+
return acc;
|
|
3560
|
+
}
|
|
3561
|
+
function themeFromExportData(data) {
|
|
3562
|
+
assertExportKind(data, "theme", "glaze.themeFrom");
|
|
3563
|
+
assertExportVersion(data, "glaze.themeFrom");
|
|
3564
|
+
if (typeof data.hue !== "number" || typeof data.saturation !== "number") throw new Error(`glaze.themeFrom: expected numeric "hue" and "saturation" fields.`);
|
|
3565
|
+
return createTheme(data.hue, data.saturation, data.colors, data.config);
|
|
3566
|
+
}
|
|
3567
|
+
/**
|
|
3568
|
+
* Rebuild a palette from a `palette.export()` snapshot.
|
|
3569
|
+
*/
|
|
3570
|
+
function createPaletteFromExport(data) {
|
|
3571
|
+
if (data === null || typeof data !== "object") throw new Error(`glaze.paletteFrom: expected an object from palette.export(), got ${data === null ? "null" : typeof data}.`);
|
|
3572
|
+
assertExportKind(data, "palette", "glaze.paletteFrom");
|
|
3573
|
+
assertExportVersion(data, "glaze.paletteFrom");
|
|
3574
|
+
if (data.themes === null || typeof data.themes !== "object") throw new Error(`glaze.paletteFrom: expected a "themes" object map of theme exports.`);
|
|
3575
|
+
const rebuilt = {};
|
|
3576
|
+
for (const [name, themeExport] of Object.entries(data.themes)) {
|
|
3577
|
+
if (themeExport === null || typeof themeExport !== "object") throw new Error(`glaze.paletteFrom: theme "${name}" is not a valid theme export.`);
|
|
3578
|
+
rebuilt[name] = themeFromExportData(themeExport);
|
|
3579
|
+
}
|
|
3580
|
+
return createPalette(rebuilt, { primary: data.primary });
|
|
3581
|
+
}
|
|
3582
|
+
/** Rebuild a theme from a `theme.export()` snapshot. */
|
|
3583
|
+
function createThemeFromExport(data) {
|
|
3584
|
+
if (data === null || typeof data !== "object") throw new Error(`glaze.themeFrom: expected an object from theme.export(), got ${data === null ? "null" : typeof data}.`);
|
|
3585
|
+
return themeFromExportData(data);
|
|
3586
|
+
}
|
|
1486
3587
|
function createPalette(themes, paletteOptions) {
|
|
1487
3588
|
validatePrimaryTheme(paletteOptions?.primary, themes);
|
|
3589
|
+
const buildDtcgResult = (options) => {
|
|
3590
|
+
const modes = resolveModes(options?.modes);
|
|
3591
|
+
const colorSpace = options?.colorSpace ?? "srgb";
|
|
3592
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, _themeName, _theme) => buildDtcgMap(filtered, prefix, modes, colorSpace, pastel), (acc, part) => {
|
|
3593
|
+
Object.assign(acc.light, part.light);
|
|
3594
|
+
if (part.dark) acc.dark = Object.assign(acc.dark ?? {}, part.dark);
|
|
3595
|
+
if (part.lightContrast) acc.lightContrast = Object.assign(acc.lightContrast ?? {}, part.lightContrast);
|
|
3596
|
+
if (part.darkContrast) acc.darkContrast = Object.assign(acc.darkContrast ?? {}, part.darkContrast);
|
|
3597
|
+
}, () => ({ light: {} }));
|
|
3598
|
+
};
|
|
1488
3599
|
return {
|
|
3600
|
+
list() {
|
|
3601
|
+
return Object.keys(themes);
|
|
3602
|
+
},
|
|
3603
|
+
get primary() {
|
|
3604
|
+
return paletteOptions?.primary;
|
|
3605
|
+
},
|
|
3606
|
+
theme(name) {
|
|
3607
|
+
return themes[name];
|
|
3608
|
+
},
|
|
3609
|
+
themes() {
|
|
3610
|
+
return { ...themes };
|
|
3611
|
+
},
|
|
3612
|
+
export() {
|
|
3613
|
+
const themesExport = {};
|
|
3614
|
+
for (const [name, theme] of Object.entries(themes)) themesExport[name] = theme.export();
|
|
3615
|
+
const out = {
|
|
3616
|
+
kind: "palette",
|
|
3617
|
+
version: GLAZE_EXPORT_VERSION,
|
|
3618
|
+
themes: themesExport
|
|
3619
|
+
};
|
|
3620
|
+
if (paletteOptions?.primary !== void 0) out.primary = paletteOptions.primary;
|
|
3621
|
+
return out;
|
|
3622
|
+
},
|
|
1489
3623
|
tokens(options) {
|
|
1490
|
-
const
|
|
1491
|
-
|
|
3624
|
+
const format = options?.format ?? "oklch";
|
|
3625
|
+
assertNativeFormat(format, "tokens");
|
|
1492
3626
|
const modes = resolveModes(options?.modes);
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
const prefix = resolvePrefix(options, themeName, true);
|
|
1498
|
-
const tokens = buildFlatTokenMap(filterCollisions(resolved, prefix, seen, themeName), prefix, modes, options?.format);
|
|
1499
|
-
for (const variant of Object.keys(tokens)) {
|
|
1500
|
-
if (!allTokens[variant]) allTokens[variant] = {};
|
|
1501
|
-
Object.assign(allTokens[variant], tokens[variant]);
|
|
1502
|
-
}
|
|
1503
|
-
if (themeName === effectivePrimary) {
|
|
1504
|
-
const unprefixed = buildFlatTokenMap(filterCollisions(resolved, "", seen, themeName, true), "", modes, options?.format);
|
|
1505
|
-
for (const variant of Object.keys(unprefixed)) Object.assign(allTokens[variant], unprefixed[variant]);
|
|
3627
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel) => buildFlatTokenMap(filtered, prefix, modes, format, pastel), (acc, part) => {
|
|
3628
|
+
for (const variant of Object.keys(part)) {
|
|
3629
|
+
if (!acc[variant]) acc[variant] = {};
|
|
3630
|
+
Object.assign(acc[variant], part[variant]);
|
|
1506
3631
|
}
|
|
1507
|
-
}
|
|
1508
|
-
return allTokens;
|
|
3632
|
+
}, () => ({}));
|
|
1509
3633
|
},
|
|
1510
3634
|
tasty(options) {
|
|
1511
|
-
const
|
|
1512
|
-
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
3635
|
+
const cfg = getConfig();
|
|
1513
3636
|
const states = {
|
|
1514
|
-
dark: options?.states?.dark ??
|
|
1515
|
-
highContrast: options?.states?.highContrast ??
|
|
3637
|
+
dark: options?.states?.dark ?? cfg.states.dark,
|
|
3638
|
+
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
1516
3639
|
};
|
|
1517
3640
|
const modes = resolveModes(options?.modes);
|
|
1518
|
-
const
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
const prefix = resolvePrefix(options, themeName, true);
|
|
1523
|
-
const tokens = buildTokenMap(filterCollisions(resolved, prefix, seen, themeName), prefix, states, modes, options?.format);
|
|
1524
|
-
Object.assign(allTokens, tokens);
|
|
1525
|
-
if (themeName === effectivePrimary) {
|
|
1526
|
-
const unprefixed = buildTokenMap(filterCollisions(resolved, "", seen, themeName, true), "", states, modes, options?.format);
|
|
1527
|
-
Object.assign(allTokens, unprefixed);
|
|
1528
|
-
}
|
|
1529
|
-
}
|
|
1530
|
-
return allTokens;
|
|
3641
|
+
const format = options?.format ?? "oklch";
|
|
3642
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, themeName, theme) => {
|
|
3643
|
+
return buildTokenMap(filtered, prefix, states, modes, format, pastel, channelCtxForTheme(theme, themeName, prefix, resolvePrefix(options, themeName, true), options?.splitHue, format, modes, filtered));
|
|
3644
|
+
}, (acc, part) => Object.assign(acc, part), () => ({}));
|
|
1531
3645
|
},
|
|
1532
3646
|
json(options) {
|
|
3647
|
+
const format = options?.format ?? "oklch";
|
|
3648
|
+
assertNativeFormat(format, "json");
|
|
1533
3649
|
const modes = resolveModes(options?.modes);
|
|
1534
3650
|
const result = {};
|
|
1535
|
-
for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes,
|
|
3651
|
+
for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes, format, theme.getConfig().pastel);
|
|
1536
3652
|
return result;
|
|
1537
3653
|
},
|
|
1538
3654
|
css(options) {
|
|
1539
|
-
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
1540
|
-
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
1541
3655
|
const suffix = options?.suffix ?? "-color";
|
|
1542
|
-
const format = options?.format ?? "
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
};
|
|
1549
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1550
|
-
for (const [themeName, theme] of Object.entries(themes)) {
|
|
1551
|
-
const resolved = theme.resolve();
|
|
1552
|
-
const prefix = resolvePrefix(options, themeName, true);
|
|
1553
|
-
const css = buildCssMap(filterCollisions(resolved, prefix, seen, themeName), prefix, suffix, format);
|
|
3656
|
+
const format = options?.format ?? "oklch";
|
|
3657
|
+
assertNativeFormat(format, "css");
|
|
3658
|
+
const modes = resolveModes();
|
|
3659
|
+
const lines = buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, themeName, theme) => {
|
|
3660
|
+
return buildCssMap(filtered, prefix, suffix, format, pastel, channelCtxForTheme(theme, themeName, prefix, resolvePrefix(options, themeName, true), options?.splitHue, format, modes, filtered));
|
|
3661
|
+
}, (acc, part) => {
|
|
1554
3662
|
for (const key of [
|
|
1555
3663
|
"light",
|
|
1556
3664
|
"dark",
|
|
1557
3665
|
"lightContrast",
|
|
1558
3666
|
"darkContrast"
|
|
1559
|
-
]) if (
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
"darkContrast"
|
|
1567
|
-
]) if (unprefixed[key]) allLines[key].push(unprefixed[key]);
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
3667
|
+
]) if (part[key]) acc[key].push(part[key]);
|
|
3668
|
+
}, () => ({
|
|
3669
|
+
light: [],
|
|
3670
|
+
dark: [],
|
|
3671
|
+
lightContrast: [],
|
|
3672
|
+
darkContrast: []
|
|
3673
|
+
}));
|
|
1570
3674
|
return {
|
|
1571
|
-
light:
|
|
1572
|
-
dark:
|
|
1573
|
-
lightContrast:
|
|
1574
|
-
darkContrast:
|
|
3675
|
+
light: lines.light.join("\n"),
|
|
3676
|
+
dark: lines.dark.join("\n"),
|
|
3677
|
+
lightContrast: lines.lightContrast.join("\n"),
|
|
3678
|
+
darkContrast: lines.darkContrast.join("\n")
|
|
1575
3679
|
};
|
|
1576
|
-
}
|
|
1577
|
-
};
|
|
1578
|
-
}
|
|
1579
|
-
function createColorToken(input) {
|
|
1580
|
-
const defs = { __color__: {
|
|
1581
|
-
lightness: input.lightness,
|
|
1582
|
-
saturation: input.saturationFactor,
|
|
1583
|
-
mode: input.mode
|
|
1584
|
-
} };
|
|
1585
|
-
return {
|
|
1586
|
-
resolve() {
|
|
1587
|
-
return resolveAllColors(input.hue, input.saturation, defs).get("__color__");
|
|
1588
3680
|
},
|
|
1589
|
-
|
|
1590
|
-
return
|
|
1591
|
-
dark: options?.states?.dark ?? globalConfig.states.dark,
|
|
1592
|
-
highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
|
|
1593
|
-
}, resolveModes(options?.modes), options?.format)["#__color__"];
|
|
3681
|
+
dtcg(options) {
|
|
3682
|
+
return buildDtcgResult(options);
|
|
1594
3683
|
},
|
|
1595
|
-
|
|
1596
|
-
return
|
|
1597
|
-
dark: options?.states?.dark ?? globalConfig.states.dark,
|
|
1598
|
-
highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
|
|
1599
|
-
}, resolveModes(options?.modes), options?.format)["#__color__"];
|
|
3684
|
+
dtcgResolver(options) {
|
|
3685
|
+
return buildDtcgResolver(buildDtcgResult(options), options);
|
|
1600
3686
|
},
|
|
1601
|
-
|
|
1602
|
-
|
|
3687
|
+
tailwind(options) {
|
|
3688
|
+
const modes = resolveModes(options?.modes);
|
|
3689
|
+
const cssPrefix = options?.namespace ?? "color-";
|
|
3690
|
+
const format = options?.format ?? "oklch";
|
|
3691
|
+
assertNativeFormat(format, "tailwind");
|
|
3692
|
+
const darkSelector = options?.darkSelector ?? ".dark";
|
|
3693
|
+
const highContrastSelector = options?.highContrastSelector ?? ".high-contrast";
|
|
3694
|
+
return emitTailwindCss(buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, _themeName, _theme) => buildTailwindLines(filtered, prefix, cssPrefix, format, pastel), (acc, part) => {
|
|
3695
|
+
for (const variant of [
|
|
3696
|
+
"light",
|
|
3697
|
+
"dark",
|
|
3698
|
+
"lightContrast",
|
|
3699
|
+
"darkContrast"
|
|
3700
|
+
]) acc[variant].push(...part[variant]);
|
|
3701
|
+
}, () => ({
|
|
3702
|
+
light: [],
|
|
3703
|
+
dark: [],
|
|
3704
|
+
lightContrast: [],
|
|
3705
|
+
darkContrast: []
|
|
3706
|
+
})), modes, darkSelector, highContrastSelector);
|
|
1603
3707
|
}
|
|
1604
3708
|
};
|
|
1605
3709
|
}
|
|
3710
|
+
|
|
3711
|
+
//#endregion
|
|
3712
|
+
//#region src/glaze.ts
|
|
3713
|
+
/**
|
|
3714
|
+
* Glaze — OKHST color theme generator.
|
|
3715
|
+
*
|
|
3716
|
+
* Public API entry. Wires `glaze()` and its attached static methods to
|
|
3717
|
+
* the focused modules in this folder:
|
|
3718
|
+
* - `theme.ts` — single-theme factory
|
|
3719
|
+
* - `palette.ts` — multi-theme composition
|
|
3720
|
+
* - `color-token.ts` — standalone single-color tokens (`glaze.color`)
|
|
3721
|
+
* - `shadow.ts` — standalone shadow factory (`glaze.shadow`)
|
|
3722
|
+
* - `formatters.ts` — variant → string (`glaze.format`)
|
|
3723
|
+
* - `config.ts` — global config singleton
|
|
3724
|
+
* - `serialize.ts` — authoring export type guards / version checks
|
|
3725
|
+
*/
|
|
1606
3726
|
/**
|
|
1607
3727
|
* Create a single-hue glaze theme.
|
|
1608
3728
|
*
|
|
3729
|
+
* An optional `config` override can be supplied to customize the resolve
|
|
3730
|
+
* behavior for this theme (tone windows, etc.). The
|
|
3731
|
+
* override is **merged over the live global config at resolve time** —
|
|
3732
|
+
* the theme still reacts to later `configure()` calls for fields it
|
|
3733
|
+
* didn't override.
|
|
3734
|
+
*
|
|
1609
3735
|
* @example
|
|
1610
3736
|
* ```ts
|
|
1611
|
-
* const primary = glaze({ hue: 280, saturation: 80 });
|
|
1612
|
-
* // or shorthand:
|
|
1613
3737
|
* const primary = glaze(280, 80);
|
|
3738
|
+
* // or shorthand:
|
|
3739
|
+
* const primary = glaze({ hue: 280, saturation: 80 });
|
|
3740
|
+
* // with config override:
|
|
3741
|
+
* const raw = glaze(280, 80, { lightTone: false });
|
|
1614
3742
|
* ```
|
|
1615
3743
|
*/
|
|
1616
|
-
function glaze(hueOrOptions, saturation) {
|
|
1617
|
-
if (typeof hueOrOptions === "number") return createTheme(hueOrOptions, saturation ?? 100);
|
|
1618
|
-
return createTheme(hueOrOptions.hue, hueOrOptions.saturation);
|
|
3744
|
+
function glaze(hueOrOptions, saturation, config) {
|
|
3745
|
+
if (typeof hueOrOptions === "number") return createTheme(hueOrOptions, saturation ?? 100, void 0, config);
|
|
3746
|
+
return createTheme(hueOrOptions.hue, hueOrOptions.saturation, void 0, config);
|
|
1619
3747
|
}
|
|
1620
|
-
/**
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
glaze.configure = function configure(config) {
|
|
1624
|
-
globalConfig = {
|
|
1625
|
-
lightLightness: config.lightLightness ?? globalConfig.lightLightness,
|
|
1626
|
-
darkLightness: config.darkLightness ?? globalConfig.darkLightness,
|
|
1627
|
-
darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
|
|
1628
|
-
darkCurve: config.darkCurve ?? globalConfig.darkCurve,
|
|
1629
|
-
states: {
|
|
1630
|
-
dark: config.states?.dark ?? globalConfig.states.dark,
|
|
1631
|
-
highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
|
|
1632
|
-
},
|
|
1633
|
-
modes: {
|
|
1634
|
-
dark: config.modes?.dark ?? globalConfig.modes.dark,
|
|
1635
|
-
highContrast: config.modes?.highContrast ?? globalConfig.modes.highContrast
|
|
1636
|
-
},
|
|
1637
|
-
shadowTuning: config.shadowTuning ?? globalConfig.shadowTuning
|
|
1638
|
-
};
|
|
3748
|
+
/** Configure global glaze settings. */
|
|
3749
|
+
glaze.configure = function configure$1(config) {
|
|
3750
|
+
configure(config);
|
|
1639
3751
|
};
|
|
1640
|
-
/**
|
|
1641
|
-
* Compose multiple themes into a palette.
|
|
1642
|
-
*/
|
|
3752
|
+
/** Compose multiple themes into a palette. */
|
|
1643
3753
|
glaze.palette = function palette(themes, options) {
|
|
1644
3754
|
return createPalette(themes, options);
|
|
1645
3755
|
};
|
|
1646
3756
|
/**
|
|
1647
|
-
* Create a theme from a serialized export.
|
|
3757
|
+
* Create a theme from a serialized `theme.export()` snapshot.
|
|
3758
|
+
* Prefer this over the legacy `glaze.from` alias.
|
|
1648
3759
|
*/
|
|
1649
|
-
glaze.
|
|
1650
|
-
return
|
|
3760
|
+
glaze.themeFrom = function themeFrom(data) {
|
|
3761
|
+
return createThemeFromExport(data);
|
|
1651
3762
|
};
|
|
3763
|
+
/** Compat alias for `glaze.themeFrom`. */
|
|
3764
|
+
glaze.from = glaze.themeFrom;
|
|
1652
3765
|
/**
|
|
1653
3766
|
* Create a standalone single-color token.
|
|
3767
|
+
*
|
|
3768
|
+
* **arg1 — the color** (four accepted shapes, discriminated by structure):
|
|
3769
|
+
*
|
|
3770
|
+
* | Shape | Example | Notes |
|
|
3771
|
+
* |---|---|---|
|
|
3772
|
+
* | Bare string | `'#26fcb2'`, `'rgb(38 252 178)'` | Hex or CSS color function (incl. `okhst()`) |
|
|
3773
|
+
* | Value object | `{ h: 152, s: 0.95, l: 0.74 }` | OKHSL, OKHST (`{h,s,t}`), `{r,g,b}`, `{l,c,h}` |
|
|
3774
|
+
* | `{ from, ...overrides }` | `{ from: '#fff', base: bg, contrast: 'AA' }` | Value + color overrides |
|
|
3775
|
+
* | Structured | `{ hue: 152, saturation: 95, tone: 74 }` | Full theme-style token |
|
|
3776
|
+
*
|
|
3777
|
+
* **arg2 — config override** (optional, all shapes):
|
|
3778
|
+
* Overrides the resolve-relevant global config fields for this token.
|
|
3779
|
+
* Fields that are omitted fall through to the live global config at
|
|
3780
|
+
* create time (and are snapshotted). Pass `false` for a tone window
|
|
3781
|
+
* to disable clamping entirely.
|
|
3782
|
+
*
|
|
3783
|
+
* ```ts
|
|
3784
|
+
* // Bare string — no overrides
|
|
3785
|
+
* glaze.color('#26fcb2')
|
|
3786
|
+
*
|
|
3787
|
+
* // From form — value + color overrides
|
|
3788
|
+
* glaze.color({ from: '#fff', base: bg, contrast: 'AA' })
|
|
3789
|
+
*
|
|
3790
|
+
* // Structured form — full theme-style token
|
|
3791
|
+
* glaze.color({ hue: 152, saturation: 95, tone: 74 })
|
|
3792
|
+
*
|
|
3793
|
+
* // Config override on any form
|
|
3794
|
+
* glaze.color('#26fcb2', { darkTone: false, autoFlip: false })
|
|
3795
|
+
* glaze.color({ from: '#fff', base: bg })
|
|
3796
|
+
* ```
|
|
3797
|
+
*
|
|
3798
|
+
* Defaults: every form defaults to `mode: 'auto'`. Value-shorthand forms
|
|
3799
|
+
* (bare strings and value objects) preserve light tone exactly
|
|
3800
|
+
* (`lightTone: false` internally). Structured form snapshots both
|
|
3801
|
+
* tone windows from `globalConfig` at create time.
|
|
3802
|
+
*
|
|
3803
|
+
* Relative `tone: '+N'` and `contrast` anchor to the literal seed by
|
|
3804
|
+
* default; when `base` is set they anchor to the base's resolved variant
|
|
3805
|
+
* per scheme. Relative `hue: '+N'` always anchors to the seed, not the base.
|
|
1654
3806
|
*/
|
|
1655
|
-
glaze.color = function color(input) {
|
|
1656
|
-
return
|
|
3807
|
+
glaze.color = function color(input, config) {
|
|
3808
|
+
if (typeof input === "string") return createColorTokenFromValue(input, void 0, config);
|
|
3809
|
+
const obj = input;
|
|
3810
|
+
if ("from" in obj) {
|
|
3811
|
+
const { from, ...overrides } = input;
|
|
3812
|
+
return createColorTokenFromValue(from, overrides, config);
|
|
3813
|
+
}
|
|
3814
|
+
if ("hue" in obj) return createColorToken(input, config);
|
|
3815
|
+
return createColorTokenFromValue(input, void 0, config);
|
|
1657
3816
|
};
|
|
1658
3817
|
/**
|
|
1659
3818
|
* Compute a shadow color from a bg/fg pair and intensity.
|
|
3819
|
+
*
|
|
3820
|
+
* Both `bg` and `fg` accept any `GlazeColorValue` form: hex (`#rgb` /
|
|
3821
|
+
* `#rrggbb` / `#rrggbbaa`), `rgb()` / `hsl()` / `okhsl()` / `oklch()`
|
|
3822
|
+
* strings, or `{ r, g, b }` / `{ h, s, l }` / `{ l, c, h }` objects.
|
|
1660
3823
|
*/
|
|
1661
3824
|
glaze.shadow = function shadow(input) {
|
|
1662
|
-
const bg =
|
|
1663
|
-
const fg = input.fg ?
|
|
1664
|
-
const
|
|
1665
|
-
|
|
3825
|
+
const bg = extractOkhslFromValue(input.bg);
|
|
3826
|
+
const fg = input.fg ? extractOkhslFromValue(input.fg) : void 0;
|
|
3827
|
+
const cfg = getConfig();
|
|
3828
|
+
const tuning = resolveShadowTuning(input.tuning, cfg.shadowTuning);
|
|
3829
|
+
const result = computeShadow({
|
|
1666
3830
|
...bg,
|
|
1667
3831
|
alpha: 1
|
|
1668
3832
|
}, fg ? {
|
|
1669
3833
|
...fg,
|
|
1670
3834
|
alpha: 1
|
|
1671
3835
|
} : void 0, input.intensity, tuning);
|
|
3836
|
+
const { h, s, t } = okhslToOkhst({
|
|
3837
|
+
h: result.h,
|
|
3838
|
+
s: result.s,
|
|
3839
|
+
l: result.l
|
|
3840
|
+
});
|
|
3841
|
+
return {
|
|
3842
|
+
h,
|
|
3843
|
+
s,
|
|
3844
|
+
t,
|
|
3845
|
+
alpha: result.alpha
|
|
3846
|
+
};
|
|
1672
3847
|
};
|
|
1673
|
-
/**
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
glaze.format = function format(variant, colorFormat) {
|
|
1677
|
-
return formatVariant(variant, colorFormat);
|
|
3848
|
+
/** Format a resolved color variant as a CSS string. */
|
|
3849
|
+
glaze.format = function format(variant, colorFormat, pastel) {
|
|
3850
|
+
return formatVariant(variant, colorFormat, pastel);
|
|
1678
3851
|
};
|
|
1679
|
-
function parseOkhslInput(input) {
|
|
1680
|
-
if (typeof input === "string") {
|
|
1681
|
-
const rgb = parseHex(input);
|
|
1682
|
-
if (!rgb) throw new Error(`glaze: invalid hex color "${input}".`);
|
|
1683
|
-
const [h, s, l] = srgbToOkhsl(rgb);
|
|
1684
|
-
return {
|
|
1685
|
-
h,
|
|
1686
|
-
s,
|
|
1687
|
-
l
|
|
1688
|
-
};
|
|
1689
|
-
}
|
|
1690
|
-
return input;
|
|
1691
|
-
}
|
|
1692
3852
|
/**
|
|
1693
3853
|
* Create a theme from a hex color string.
|
|
1694
3854
|
* Extracts hue and saturation from the color.
|
|
@@ -1712,46 +3872,92 @@ glaze.fromRgb = function fromRgb(r, g, b) {
|
|
|
1712
3872
|
return createTheme(h, s * 100);
|
|
1713
3873
|
};
|
|
1714
3874
|
/**
|
|
1715
|
-
*
|
|
3875
|
+
* Rehydrate a `glaze.color()` token from a `.export()` snapshot.
|
|
3876
|
+
*
|
|
3877
|
+
* The snapshot is a plain JSON-safe object containing the original
|
|
3878
|
+
* input value, overrides (with any `base` token recursively serialized),
|
|
3879
|
+
* and the effective config snapshot. The reconstructed token is identical
|
|
3880
|
+
* in behavior to the original at the time of export.
|
|
3881
|
+
*
|
|
3882
|
+
* @example
|
|
3883
|
+
* ```ts
|
|
3884
|
+
* const text = glaze.color({ from: '#1a1a1a', contrast: 'AA' });
|
|
3885
|
+
* const data = text.export(); // JSON-safe
|
|
3886
|
+
* localStorage.setItem('text', JSON.stringify(data));
|
|
3887
|
+
* // ...later...
|
|
3888
|
+
* const restored = glaze.colorFrom(JSON.parse(localStorage.getItem('text')!));
|
|
3889
|
+
* ```
|
|
1716
3890
|
*/
|
|
1717
|
-
glaze.
|
|
1718
|
-
return
|
|
3891
|
+
glaze.colorFrom = function colorFrom(data) {
|
|
3892
|
+
return colorFromExport(data);
|
|
1719
3893
|
};
|
|
1720
3894
|
/**
|
|
1721
|
-
*
|
|
3895
|
+
* Rehydrate a palette from a `palette.export()` snapshot.
|
|
1722
3896
|
*/
|
|
1723
|
-
glaze.
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
3897
|
+
glaze.paletteFrom = function paletteFrom(data) {
|
|
3898
|
+
return createPaletteFromExport(data);
|
|
3899
|
+
};
|
|
3900
|
+
/** Type guard for theme authoring snapshots. */
|
|
3901
|
+
glaze.isThemeExport = isThemeExport;
|
|
3902
|
+
/** Type guard for color-token authoring snapshots. */
|
|
3903
|
+
glaze.isColorTokenExport = isColorTokenExport;
|
|
3904
|
+
/** Type guard for palette authoring snapshots. */
|
|
3905
|
+
glaze.isPaletteExport = isPaletteExport;
|
|
3906
|
+
/** Get the current global configuration (for testing/debugging). */
|
|
3907
|
+
glaze.getConfig = function getConfig() {
|
|
3908
|
+
return snapshotConfig();
|
|
3909
|
+
};
|
|
3910
|
+
/** Reset global configuration to defaults. */
|
|
3911
|
+
glaze.resetConfig = function resetConfig$1() {
|
|
3912
|
+
resetConfig();
|
|
1738
3913
|
};
|
|
1739
3914
|
|
|
1740
3915
|
//#endregion
|
|
3916
|
+
exports.APCA_HC_ENHANCEMENT = APCA_HC_ENHANCEMENT;
|
|
3917
|
+
exports.APCA_MAX_LC = APCA_MAX_LC;
|
|
3918
|
+
exports.APCA_PRESETS = APCA_PRESETS;
|
|
3919
|
+
exports.GLAZE_EXPORT_VERSION = GLAZE_EXPORT_VERSION;
|
|
3920
|
+
exports.REF_EPS = REF_EPS;
|
|
3921
|
+
exports.apcaContrast = apcaContrast;
|
|
3922
|
+
exports.assertAllPastel = assertAllPastel;
|
|
3923
|
+
exports.assertNativeFormat = assertNativeFormat;
|
|
1741
3924
|
exports.contrastRatioFromLuminance = contrastRatioFromLuminance;
|
|
1742
|
-
exports.
|
|
3925
|
+
exports.cuspLightness = cuspLightness;
|
|
3926
|
+
exports.findToneForContrast = findToneForContrast;
|
|
1743
3927
|
exports.findValueForMixContrast = findValueForMixContrast;
|
|
1744
3928
|
exports.formatHsl = formatHsl;
|
|
1745
3929
|
exports.formatOkhsl = formatOkhsl;
|
|
3930
|
+
exports.formatOkhst = formatOkhst;
|
|
1746
3931
|
exports.formatOklch = formatOklch;
|
|
1747
3932
|
exports.formatRgb = formatRgb;
|
|
3933
|
+
exports.fromTone = fromTone;
|
|
1748
3934
|
exports.gamutClampedLuminance = gamutClampedLuminance;
|
|
1749
3935
|
exports.glaze = glaze;
|
|
3936
|
+
exports.hslToSrgb = hslToSrgb;
|
|
3937
|
+
exports.inferRoleFromName = inferRoleFromName;
|
|
3938
|
+
exports.isColorTokenExport = isColorTokenExport;
|
|
3939
|
+
exports.isPaletteExport = isPaletteExport;
|
|
3940
|
+
exports.isThemeExport = isThemeExport;
|
|
3941
|
+
exports.normalizeRole = normalizeRole;
|
|
1750
3942
|
exports.okhslToLinearSrgb = okhslToLinearSrgb;
|
|
3943
|
+
exports.okhslToOkhst = okhslToOkhst;
|
|
1751
3944
|
exports.okhslToOklab = okhslToOklab;
|
|
3945
|
+
exports.okhslToOklch = okhslToOklch;
|
|
1752
3946
|
exports.okhslToSrgb = okhslToSrgb;
|
|
3947
|
+
exports.okhstToOkhsl = okhstToOkhsl;
|
|
3948
|
+
exports.oklabToOkhsl = oklabToOkhsl;
|
|
3949
|
+
exports.oppositeRole = oppositeRole;
|
|
1753
3950
|
exports.parseHex = parseHex;
|
|
3951
|
+
exports.parseHexAlpha = parseHexAlpha;
|
|
1754
3952
|
exports.relativeLuminanceFromLinearRgb = relativeLuminanceFromLinearRgb;
|
|
3953
|
+
exports.resolveApcaTarget = resolveApcaTarget;
|
|
3954
|
+
exports.resolveContrastForMode = resolveContrastForMode;
|
|
1755
3955
|
exports.resolveMinContrast = resolveMinContrast;
|
|
3956
|
+
exports.roleToPolarity = roleToPolarity;
|
|
3957
|
+
exports.srgbToHex = srgbToHex;
|
|
1756
3958
|
exports.srgbToOkhsl = srgbToOkhsl;
|
|
3959
|
+
exports.toTone = toTone;
|
|
3960
|
+
exports.toneFromY = toneFromY;
|
|
3961
|
+
exports.variantToOkhsl = variantToOkhsl;
|
|
3962
|
+
exports.yFromTone = yFromTone;
|
|
1757
3963
|
//# sourceMappingURL=index.cjs.map
|