@tenphi/glaze 0.0.0-snapshot.7c1fc7d → 0.0.0-snapshot.7dca259

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/dist/index.cjs CHANGED
@@ -81,8 +81,8 @@ const OKLab_to_linear_sRGB_coefficients = [
81
81
  .73956515,
82
82
  -.45954404,
83
83
  .08285427,
84
- .12541073,
85
- -.14503204
84
+ .1254107,
85
+ .14503204
86
86
  ]],
87
87
  [[.13110757611180954, 1.813339709266608], [
88
88
  1.35733652,
@@ -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];
@@ -254,10 +259,9 @@ const getCs = (L, a, b, cusp) => {
254
259
  ];
255
260
  };
256
261
  /**
257
- * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to linear sRGB.
258
- * Channels may exceed [0, 1] near gamut boundaries — caller must clamp if needed.
262
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLab [L, a, b].
259
263
  */
260
- function okhslToLinearSrgb(h, s, l) {
264
+ function okhslToOklab(h, s, l) {
261
265
  const L = toeInv(l);
262
266
  let a = 0;
263
267
  let b = 0;
@@ -284,11 +288,18 @@ function okhslToLinearSrgb(h, s, l) {
284
288
  a = c * a_;
285
289
  b = c * b_;
286
290
  }
287
- return OKLabToLinearSRGB([
291
+ return [
288
292
  L,
289
293
  a,
290
294
  b
291
- ]);
295
+ ];
296
+ }
297
+ /**
298
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to linear sRGB.
299
+ * Channels may exceed [0, 1] near gamut boundaries — caller must clamp if needed.
300
+ */
301
+ function okhslToLinearSrgb(h, s, l) {
302
+ return OKLabToLinearSRGB(okhslToOklab(h, s, l));
292
303
  }
293
304
  /**
294
305
  * Compute relative luminance Y from linear sRGB channels.
@@ -327,44 +338,40 @@ function okhslToSrgb(h, s, l) {
327
338
  ];
328
339
  }
329
340
  /**
330
- * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLab [L, a, b].
341
+ * Compute WCAG 2 relative luminance from linear sRGB, matching the browser
342
+ * rendering pipeline: gamma-encode, clamp to sRGB gamut [0,1], then linearize.
343
+ * This avoids over/under-estimating luminance for out-of-gamut OKHSL colors.
331
344
  */
332
- function okhslToOklab(h, s, l) {
333
- const L = toeInv(l);
334
- let a = 0;
335
- let b = 0;
336
- const hNorm = constrainAngle(h) / 360;
337
- if (L !== 0 && L !== 1 && s !== 0) {
338
- const a_ = Math.cos(TAU * hNorm);
339
- const b_ = Math.sin(TAU * hNorm);
340
- const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
341
- const mid = .8;
342
- const midInv = 1.25;
343
- let t, k0, k1, k2;
344
- if (s < mid) {
345
- t = midInv * s;
346
- k0 = 0;
347
- k1 = mid * c0;
348
- k2 = 1 - k1 / cMid;
349
- } else {
350
- t = 5 * (s - .8);
351
- k0 = cMid;
352
- k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
353
- k2 = 1 - k1 / (cMax - cMid);
354
- }
355
- const c = k0 + t * k1 / (1 - k2 * t);
356
- a = c * a_;
357
- b = c * b_;
358
- }
359
- return [
360
- L,
361
- a,
362
- b
363
- ];
345
+ function gamutClampedLuminance(linearRgb) {
346
+ const r = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0]))));
347
+ const g = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1]))));
348
+ const b = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2]))));
349
+ return .2126 * r + .7152 * g + .0722 * b;
350
+ }
351
+ /**
352
+ * Compute APCA screen luminance (`Ys`) from linear sRGB.
353
+ *
354
+ * APCA does not use the WCAG piecewise sRGB EOTF; it defines its own
355
+ * luminance as `0.2126·R^2.4 + 0.7152·G^2.4 + 0.0722·B^2.4` over the
356
+ * gamma-encoded (display) channels with a simple 2.4 exponent. The APCA
357
+ * soft-clamp threshold in `apcaContrast` is calibrated against this basis,
358
+ * so the solver must feed it `Ys`, not WCAG relative luminance. Channels
359
+ * are gamut-clamped to [0, 1] first, matching `gamutClampedLuminance`.
360
+ */
361
+ function apcaLuminanceFromLinearRgb(linearRgb) {
362
+ const r = Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0])));
363
+ const g = Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1])));
364
+ const b = Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2])));
365
+ return .2126 * Math.pow(r, 2.4) + .7152 * Math.pow(g, 2.4) + .0722 * Math.pow(b, 2.4);
364
366
  }
365
367
  const linearSrgbToOklab = (rgb) => {
366
368
  return transform(cbrt3(transform(rgb, linear_sRGB_to_LMS_M)), LMS_to_OKLab_M);
367
369
  };
370
+ /**
371
+ * Convert OKLab to OKHSL.
372
+ * Input: [L, a, b] where L: 0–1, a/b: roughly -0.5 to 0.5.
373
+ * Returns [h, s, l] where h: 0–360, s: 0–1, l: 0–1.
374
+ */
368
375
  const oklabToOkhsl = (lab) => {
369
376
  const L = lab[0];
370
377
  const a = lab[1];
@@ -375,6 +382,12 @@ const oklabToOkhsl = (lab) => {
375
382
  0,
376
383
  toe(L)
377
384
  ];
385
+ const L_EXTREME_EPSILON = 1e-6;
386
+ if (L >= 1 - L_EXTREME_EPSILON || L <= L_EXTREME_EPSILON) return [
387
+ 0,
388
+ 0,
389
+ toe(L)
390
+ ];
378
391
  const a_ = a / C;
379
392
  const b_ = b / C;
380
393
  let h = Math.atan2(b, a) * (180 / Math.PI);
@@ -412,32 +425,108 @@ function srgbToOkhsl(rgb) {
412
425
  ]));
413
426
  }
414
427
  /**
428
+ * Convert CSS HSL (sRGB-based) to gamma-encoded sRGB [r, g, b] in 0–1 range.
429
+ * h: 0–360, s: 0–1, l: 0–1.
430
+ *
431
+ * Note: CSS HSL is not the same as OKHSL — it's HSL in the sRGB color space.
432
+ * Use this when parsing `hsl(...)` strings before passing to `srgbToOkhsl`.
433
+ */
434
+ function hslToSrgb(h, s, l) {
435
+ const hh = (h % 360 + 360) % 360 / 360;
436
+ const ss = clampVal(s, 0, 1);
437
+ const ll = clampVal(l, 0, 1);
438
+ if (ss === 0) return [
439
+ ll,
440
+ ll,
441
+ ll
442
+ ];
443
+ const q = ll < .5 ? ll * (1 + ss) : ll + ss - ll * ss;
444
+ const p = 2 * ll - q;
445
+ const hueToChannel = (t) => {
446
+ let tt = t;
447
+ if (tt < 0) tt += 1;
448
+ if (tt > 1) tt -= 1;
449
+ if (tt < 1 / 6) return p + (q - p) * 6 * tt;
450
+ if (tt < 1 / 2) return q;
451
+ if (tt < 2 / 3) return p + (q - p) * (2 / 3 - tt) * 6;
452
+ return p;
453
+ };
454
+ return [
455
+ hueToChannel(hh + 1 / 3),
456
+ hueToChannel(hh),
457
+ hueToChannel(hh - 1 / 3)
458
+ ];
459
+ }
460
+ /**
415
461
  * Parse a hex color string (#rgb or #rrggbb) to sRGB [r, g, b] in 0–1 range.
416
462
  * Returns null if the string is not a valid hex color.
463
+ *
464
+ * For 8-digit hex (`#rrggbbaa`) and 4-digit hex (`#rgba`) with alpha,
465
+ * use {@link parseHexAlpha}.
417
466
  */
418
467
  function parseHex(hex) {
468
+ const result = parseHexAlpha(hex);
469
+ if (!result || result.alpha !== void 0) return null;
470
+ return result.rgb;
471
+ }
472
+ /**
473
+ * Parse a hex color string (#rgb, #rrggbb, #rgba, or #rrggbbaa) to
474
+ * sRGB [r, g, b] in 0–1 range plus an optional alpha (0–1).
475
+ * Returns null if the string is not a valid hex color.
476
+ */
477
+ function parseHexAlpha(hex) {
419
478
  const h = hex.startsWith("#") ? hex.slice(1) : hex;
420
479
  if (h.length === 3) {
421
480
  const r = parseInt(h[0] + h[0], 16);
422
481
  const g = parseInt(h[1] + h[1], 16);
423
482
  const b = parseInt(h[2] + h[2], 16);
424
483
  if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
425
- return [
484
+ return { rgb: [
426
485
  r / 255,
427
486
  g / 255,
428
487
  b / 255
429
- ];
488
+ ] };
489
+ }
490
+ if (h.length === 4) {
491
+ const r = parseInt(h[0] + h[0], 16);
492
+ const g = parseInt(h[1] + h[1], 16);
493
+ const b = parseInt(h[2] + h[2], 16);
494
+ const a = parseInt(h[3] + h[3], 16);
495
+ if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return null;
496
+ return {
497
+ rgb: [
498
+ r / 255,
499
+ g / 255,
500
+ b / 255
501
+ ],
502
+ alpha: a / 255
503
+ };
430
504
  }
431
505
  if (h.length === 6) {
432
506
  const r = parseInt(h.slice(0, 2), 16);
433
507
  const g = parseInt(h.slice(2, 4), 16);
434
508
  const b = parseInt(h.slice(4, 6), 16);
435
509
  if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
436
- return [
510
+ return { rgb: [
437
511
  r / 255,
438
512
  g / 255,
439
513
  b / 255
440
- ];
514
+ ] };
515
+ }
516
+ if (h.length === 8) {
517
+ const r = parseInt(h.slice(0, 2), 16);
518
+ const g = parseInt(h.slice(2, 4), 16);
519
+ const b = parseInt(h.slice(4, 6), 16);
520
+ const a = parseInt(h.slice(6, 8), 16);
521
+ if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return null;
522
+ return {
523
+ rgb: [
524
+ r / 255,
525
+ g / 255,
526
+ b / 255
527
+ ],
528
+ alpha: a / 255
529
+ };
441
530
  }
442
531
  return null;
443
532
  }
@@ -452,12 +541,13 @@ function formatOkhsl(h, s, l) {
452
541
  return `okhsl(${fmt$1(h, 2)} ${fmt$1(s, 2)}% ${fmt$1(l, 2)}%)`;
453
542
  }
454
543
  /**
455
- * Format OKHSL values as a CSS `rgb(R G B)` string with rounded integer values.
544
+ * Format OKHSL values as a CSS `rgb(R G B)` string.
545
+ * Uses 2 decimal places to avoid 8-bit quantization contrast loss.
456
546
  * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
457
547
  */
458
548
  function formatRgb(h, s, l) {
459
549
  const [r, g, b] = okhslToSrgb(h, s / 100, l / 100);
460
- return `rgb(${Math.round(r * 255)} ${Math.round(g * 255)} ${Math.round(b * 255)})`;
550
+ return `rgb(${parseFloat((r * 255).toFixed(2))} ${parseFloat((g * 255).toFixed(2))} ${parseFloat((b * 255).toFixed(2))})`;
461
551
  }
462
552
  /**
463
553
  * Format OKHSL values as a CSS `hsl(H S% L%)` string.
@@ -488,289 +578,760 @@ function formatOklch(h, s, l) {
488
578
  const C = Math.sqrt(a * a + b * b);
489
579
  let hh = Math.atan2(b, a) * (180 / Math.PI);
490
580
  hh = constrainAngle(hh);
491
- return `oklch(${fmt$1(L, 4)} ${fmt$1(C, 4)} ${fmt$1(hh, 1)})`;
581
+ return `oklch(${fmt$1(L, 4)} ${fmt$1(C, 4)} ${fmt$1(hh, 2)})`;
492
582
  }
493
583
 
494
584
  //#endregion
495
- //#region src/contrast-solver.ts
585
+ //#region src/config.ts
496
586
  /**
497
- * OKHSL Contrast Solver
498
- *
499
- * Finds the closest OKHSL lightness that satisfies a WCAG 2 contrast target
500
- * against a base color. Used by glaze when resolving dependent colors
501
- * with `contrast`.
587
+ * Build a fresh defaults object. Called from module init and from
588
+ * `resetConfig()` so the two paths can't drift.
502
589
  */
503
- const CONTRAST_PRESETS = {
504
- AA: 4.5,
505
- AAA: 7,
506
- "AA-large": 3,
507
- "AAA-large": 4.5
508
- };
509
- function resolveMinContrast(value) {
510
- if (typeof value === "number") return Math.max(1, value);
511
- return CONTRAST_PRESETS[value];
590
+ function defaultConfig() {
591
+ return {
592
+ lightTone: {
593
+ lo: 13,
594
+ hi: 100,
595
+ eps: .05
596
+ },
597
+ darkTone: {
598
+ lo: 10,
599
+ hi: 95,
600
+ eps: .05
601
+ },
602
+ darkDesaturation: .1,
603
+ saturationTaper: .15,
604
+ states: {
605
+ dark: "@dark",
606
+ highContrast: "@high-contrast"
607
+ },
608
+ modes: {
609
+ dark: true,
610
+ highContrast: false
611
+ },
612
+ autoFlip: true
613
+ };
512
614
  }
513
- const CACHE_SIZE = 512;
514
- const luminanceCache = /* @__PURE__ */ new Map();
515
- const cacheOrder = [];
615
+ let globalConfig = defaultConfig();
516
616
  /**
517
- * Compute WCAG 2 relative luminance from linear sRGB, matching the browser
518
- * rendering pipeline: gamma-encode, clamp to sRGB gamut [0,1], then linearize.
519
- * This avoids over/under-estimating luminance for out-of-gamut OKHSL colors.
617
+ * Monotonic counter incremented on every `configure()` / `resetConfig()`
618
+ * call. Theme / palette caches read this to invalidate stale resolve
619
+ * results when the config changes between exports.
520
620
  */
521
- function gamutClampedLuminance(linearRgb) {
522
- const r = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0]))));
523
- const g = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1]))));
524
- const b = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2]))));
525
- return .2126 * r + .7152 * g + .0722 * b;
621
+ let configVersion = 0;
622
+ /** Live reference to the current config. Mutated by `configure()` / `resetConfig()`. */
623
+ function getConfig() {
624
+ return globalConfig;
526
625
  }
527
- function cachedLuminance(h, s, l) {
528
- const lRounded = Math.round(l * 1e4) / 1e4;
529
- const key = `${h}|${s}|${lRounded}`;
530
- const cached = luminanceCache.get(key);
531
- if (cached !== void 0) return cached;
532
- const y = gamutClampedLuminance(okhslToLinearSrgb(h, s, lRounded));
533
- if (luminanceCache.size >= CACHE_SIZE) {
534
- const evict = cacheOrder.shift();
535
- luminanceCache.delete(evict);
536
- }
537
- luminanceCache.set(key, y);
538
- cacheOrder.push(key);
539
- return y;
626
+ function getConfigVersion() {
627
+ return configVersion;
540
628
  }
541
629
  /**
542
- * Binary search one branch [lo, hi] for the nearest passing lightness to `preferred`.
630
+ * Public-facing snapshot used by `glaze.getConfig()`. Returns a shallow
631
+ * copy so callers can't mutate the live config.
543
632
  */
544
- function searchBranch(h, s, lo, hi, yBase, target, epsilon, maxIter, preferred) {
545
- const yLo = cachedLuminance(h, s, lo);
546
- const yHi = cachedLuminance(h, s, hi);
547
- const crLo = contrastRatioFromLuminance(yLo, yBase);
548
- const crHi = contrastRatioFromLuminance(yHi, yBase);
549
- if (crLo < target && crHi < target) {
550
- if (crLo >= crHi) return {
551
- lightness: lo,
552
- contrast: crLo,
553
- met: false
554
- };
555
- return {
556
- lightness: hi,
557
- contrast: crHi,
558
- met: false
559
- };
560
- }
561
- let low = lo;
562
- let high = hi;
563
- for (let i = 0; i < maxIter; i++) {
564
- if (high - low < epsilon) break;
565
- const mid = (low + high) / 2;
566
- if (contrastRatioFromLuminance(cachedLuminance(h, s, mid), yBase) >= target) if (mid < preferred) low = mid;
567
- else high = mid;
568
- else if (mid < preferred) high = mid;
569
- else low = mid;
570
- }
571
- const yLow = cachedLuminance(h, s, low);
572
- const yHigh = cachedLuminance(h, s, high);
573
- const crLow = contrastRatioFromLuminance(yLow, yBase);
574
- const crHigh = contrastRatioFromLuminance(yHigh, yBase);
575
- const lowPasses = crLow >= target;
576
- const highPasses = crHigh >= target;
577
- if (lowPasses && highPasses) {
578
- if (Math.abs(low - preferred) <= Math.abs(high - preferred)) return {
579
- lightness: low,
580
- contrast: crLow,
581
- met: true
582
- };
583
- return {
584
- lightness: high,
585
- contrast: crHigh,
586
- met: true
587
- };
588
- }
589
- if (lowPasses) return {
590
- lightness: low,
591
- contrast: crLow,
592
- met: true
593
- };
594
- if (highPasses) return {
595
- lightness: high,
596
- contrast: crHigh,
597
- met: true
598
- };
599
- return coarseScan(h, s, lo, hi, yBase, target, epsilon, maxIter);
600
- }
601
- /**
602
- * Fallback coarse scan when binary search is unstable near gamut edges.
603
- */
604
- function coarseScan(h, s, lo, hi, yBase, target, epsilon, maxIter) {
605
- const STEPS = 64;
606
- const step = (hi - lo) / STEPS;
607
- let bestL = lo;
608
- let bestCr = 0;
609
- let bestMet = false;
610
- for (let i = 0; i <= STEPS; i++) {
611
- const l = lo + step * i;
612
- const cr = contrastRatioFromLuminance(cachedLuminance(h, s, l), yBase);
613
- if (cr >= target && !bestMet) {
614
- bestL = l;
615
- bestCr = cr;
616
- bestMet = true;
617
- } else if (cr >= target && bestMet) {
618
- bestL = l;
619
- bestCr = cr;
620
- } else if (!bestMet && cr > bestCr) {
621
- bestL = l;
622
- bestCr = cr;
623
- }
624
- }
625
- if (bestMet && bestL > lo + step) {
626
- let rLo = bestL - step;
627
- let rHi = bestL;
628
- for (let i = 0; i < maxIter; i++) {
629
- if (rHi - rLo < epsilon) break;
630
- const mid = (rLo + rHi) / 2;
631
- const cr = contrastRatioFromLuminance(cachedLuminance(h, s, mid), yBase);
632
- if (cr >= target) {
633
- rHi = mid;
634
- bestL = mid;
635
- bestCr = cr;
636
- } else rLo = mid;
637
- }
638
- }
639
- return {
640
- lightness: bestL,
641
- contrast: bestCr,
642
- met: bestMet
633
+ function snapshotConfig() {
634
+ return { ...globalConfig };
635
+ }
636
+ function configure(config) {
637
+ configVersion++;
638
+ globalConfig = {
639
+ lightTone: config.lightTone ?? globalConfig.lightTone,
640
+ darkTone: config.darkTone ?? globalConfig.darkTone,
641
+ darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
642
+ saturationTaper: config.saturationTaper ?? globalConfig.saturationTaper,
643
+ states: {
644
+ dark: config.states?.dark ?? globalConfig.states.dark,
645
+ highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
646
+ },
647
+ modes: {
648
+ dark: config.modes?.dark ?? globalConfig.modes.dark,
649
+ highContrast: config.modes?.highContrast ?? globalConfig.modes.highContrast
650
+ },
651
+ shadowTuning: config.shadowTuning ?? globalConfig.shadowTuning,
652
+ autoFlip: config.autoFlip ?? globalConfig.autoFlip
643
653
  };
644
654
  }
655
+ function resetConfig() {
656
+ configVersion++;
657
+ globalConfig = defaultConfig();
658
+ }
645
659
  /**
646
- * Find the OKHSL lightness that satisfies a WCAG 2 contrast target
647
- * against a base color, staying as close to `preferredLightness` as possible.
660
+ * Merge a per-instance config override over a base resolved config.
661
+ * Only fields present in `override` are replaced; others fall through
662
+ * from `base`. `false` for tone windows passes through as-is
663
+ * (treated as the full range by `activeWindow()` in okhst.ts).
648
664
  */
649
- function findLightnessForContrast(options) {
650
- const { hue, saturation, preferredLightness, baseLinearRgb, contrast: contrastInput, lightnessRange = [0, 1], epsilon = 1e-4, maxIterations = 14 } = options;
651
- const target = resolveMinContrast(contrastInput);
652
- const searchTarget = target + .01;
653
- const yBase = gamutClampedLuminance(baseLinearRgb);
654
- const crPref = contrastRatioFromLuminance(cachedLuminance(hue, saturation, preferredLightness), yBase);
655
- if (crPref >= searchTarget) return {
656
- lightness: preferredLightness,
657
- contrast: crPref,
658
- met: true,
659
- branch: "preferred"
660
- };
661
- const [minL, maxL] = lightnessRange;
662
- const darkerResult = preferredLightness > minL ? searchBranch(hue, saturation, minL, preferredLightness, yBase, searchTarget, epsilon, maxIterations, preferredLightness) : null;
663
- const lighterResult = preferredLightness < maxL ? searchBranch(hue, saturation, preferredLightness, maxL, yBase, searchTarget, epsilon, maxIterations, preferredLightness) : null;
664
- if (darkerResult) darkerResult.met = darkerResult.contrast >= target;
665
- if (lighterResult) lighterResult.met = lighterResult.contrast >= target;
666
- const darkerPasses = darkerResult?.met ?? false;
667
- const lighterPasses = lighterResult?.met ?? false;
668
- if (darkerPasses && lighterPasses) {
669
- if (Math.abs(darkerResult.lightness - preferredLightness) <= Math.abs(lighterResult.lightness - preferredLightness)) return {
670
- ...darkerResult,
671
- branch: "darker"
672
- };
673
- return {
674
- ...lighterResult,
675
- branch: "lighter"
676
- };
677
- }
678
- if (darkerPasses) return {
679
- ...darkerResult,
680
- branch: "darker"
681
- };
682
- if (lighterPasses) return {
683
- ...lighterResult,
684
- branch: "lighter"
685
- };
686
- const candidates = [];
687
- if (darkerResult) candidates.push({
688
- ...darkerResult,
689
- branch: "darker"
690
- });
691
- if (lighterResult) candidates.push({
692
- ...lighterResult,
693
- branch: "lighter"
694
- });
695
- if (candidates.length === 0) return {
696
- lightness: preferredLightness,
697
- contrast: crPref,
698
- met: false,
699
- branch: "preferred"
665
+ function mergeConfig(base, override) {
666
+ if (!override) return base;
667
+ return {
668
+ lightTone: override.lightTone !== void 0 ? override.lightTone : base.lightTone,
669
+ darkTone: override.darkTone !== void 0 ? override.darkTone : base.darkTone,
670
+ darkDesaturation: override.darkDesaturation ?? base.darkDesaturation,
671
+ saturationTaper: override.saturationTaper ?? base.saturationTaper,
672
+ states: base.states,
673
+ modes: base.modes,
674
+ shadowTuning: override.shadowTuning ?? base.shadowTuning,
675
+ autoFlip: override.autoFlip ?? base.autoFlip
700
676
  };
701
- candidates.sort((a, b) => b.contrast - a.contrast);
702
- return candidates[0];
703
677
  }
704
678
 
705
679
  //#endregion
706
- //#region src/glaze.ts
707
- /**
708
- * Glaze — OKHSL-based color theme generator.
709
- *
710
- * Generates robust light, dark, and high-contrast colors from a hue/saturation
711
- * seed, preserving contrast for UI pairs via explicit dependencies.
712
- */
713
- let globalConfig = {
714
- lightLightness: [10, 100],
715
- darkLightness: [15, 95],
716
- darkDesaturation: .1,
717
- states: {
718
- dark: "@dark",
719
- highContrast: "@high-contrast"
720
- },
721
- modes: {
722
- dark: true,
723
- highContrast: false
724
- }
725
- };
680
+ //#region src/hc-pair.ts
726
681
  function pairNormal(p) {
727
682
  return Array.isArray(p) ? p[0] : p;
728
683
  }
729
684
  function pairHC(p) {
730
685
  return Array.isArray(p) ? p[1] : p;
731
686
  }
732
- function isShadowDef(def) {
733
- return def.type === "shadow";
687
+ function clamp(v, min, max) {
688
+ return Math.max(min, Math.min(max, v));
734
689
  }
735
- const DEFAULT_SHADOW_TUNING = {
736
- saturationFactor: .18,
737
- maxSaturation: .25,
738
- lightnessFactor: .25,
739
- lightnessBounds: [.05, .2],
740
- minGapTarget: .05,
741
- alphaMax: 1,
742
- bgHueBlend: .2
743
- };
744
- function resolveShadowTuning(perColor) {
690
+ /** Whether a tone value is an extreme keyword (`'max'` / `'min'`). */
691
+ function isExtremeTone(value) {
692
+ return value === "max" || value === "min";
693
+ }
694
+ /**
695
+ * Parse a value that can be absolute (number) or relative (signed string).
696
+ * Returns the numeric value and whether it's relative.
697
+ */
698
+ function parseRelativeOrAbsolute(value) {
699
+ if (typeof value === "number") return {
700
+ value,
701
+ relative: false
702
+ };
745
703
  return {
746
- ...DEFAULT_SHADOW_TUNING,
747
- ...globalConfig.shadowTuning,
748
- ...perColor,
749
- lightnessBounds: perColor?.lightnessBounds ?? globalConfig.shadowTuning?.lightnessBounds ?? DEFAULT_SHADOW_TUNING.lightnessBounds
704
+ value: parseFloat(value),
705
+ relative: true
750
706
  };
751
707
  }
752
- function circularLerp(a, b, t) {
753
- let diff = b - a;
754
- if (diff > 180) diff -= 360;
755
- else if (diff < -180) diff += 360;
756
- return ((a + diff * t) % 360 + 360) % 360;
708
+ /**
709
+ * Parse a tone value into a normalized shape.
710
+ * - `'max'` / `'min'` `{ kind: 'extreme', value: 100 | 0 }` (an absolute
711
+ * author tone before scheme mapping `'max'` is 100, `'min'` is 0).
712
+ * - `'+N'` / `'-N'` `{ kind: 'relative', value: ±N }`.
713
+ * - number → `{ kind: 'absolute', value }`.
714
+ */
715
+ function parseToneValue(value) {
716
+ if (value === "max") return {
717
+ kind: "extreme",
718
+ value: 100
719
+ };
720
+ if (value === "min") return {
721
+ kind: "extreme",
722
+ value: 0
723
+ };
724
+ if (typeof value === "number") return {
725
+ kind: "absolute",
726
+ value
727
+ };
728
+ return {
729
+ kind: "relative",
730
+ value: parseFloat(value)
731
+ };
757
732
  }
758
733
  /**
759
- * Compute the canonical max-contrast reference t value for normalization.
760
- * Uses bg.l=1, fg.l=0, intensity=100 the theoretical maximum.
761
- * This is a fixed constant per tuning configuration, ensuring uniform
762
- * scaling across all bg/fg pairs at low intensities.
734
+ * Compute the effective hue for a color, given the theme seed hue
735
+ * and an optional per-color hue override.
763
736
  */
764
- function computeRefT(tuning) {
765
- const EPSILON = 1e-6;
766
- let lShRef = clamp(tuning.lightnessFactor, tuning.lightnessBounds[0], tuning.lightnessBounds[1]);
767
- lShRef = Math.max(Math.min(lShRef, 1 - tuning.minGapTarget), 0);
768
- return 1 / Math.max(1 - lShRef, EPSILON);
737
+ function resolveEffectiveHue(seedHue, defHue) {
738
+ if (defHue === void 0) return seedHue;
739
+ const parsed = parseRelativeOrAbsolute(defHue);
740
+ if (parsed.relative) return ((seedHue + parsed.value) % 360 + 360) % 360;
741
+ return (parsed.value % 360 + 360) % 360;
769
742
  }
770
- function computeShadow(bg, fg, intensity, tuning) {
771
- const EPSILON = 1e-6;
772
- const clampedIntensity = clamp(intensity, 0, 100);
773
- const contrastWeight = fg ? Math.abs(bg.l - fg.l) : 1;
743
+ /**
744
+ * Check whether a tone value represents an absolute root definition
745
+ * (i.e. a number, not a relative string). Extreme keywords (`'max'` /
746
+ * `'min'`) also count they need no base.
747
+ */
748
+ function isAbsoluteTone(tone) {
749
+ if (tone === void 0) return false;
750
+ const normal = Array.isArray(tone) ? tone[0] : tone;
751
+ return typeof normal === "number" || isExtremeTone(normal);
752
+ }
753
+
754
+ //#endregion
755
+ //#region src/okhst.ts
756
+ /**
757
+ * OKHST — the contrast-uniform tone space.
758
+ *
759
+ * OKHST is OKHSL with its lightness axis replaced by a contrast-uniform
760
+ * "tone" axis. It shares `h` / `s` with OKHSL verbatim and swaps `l` for
761
+ * `t`. This module owns:
762
+ *
763
+ * - the closed-form tone transfers (`toTone` / `fromTone`) at a fixed
764
+ * reference eps, plus the gray luminance helpers (`lToY` / `yToL`),
765
+ * - the `{ h, s, t }` <-> `{ h, s, l }` color-space converters,
766
+ * - the resolved-variant edge adapter (`variantToOkhsl`),
767
+ * - the per-scheme tone mapping that replaced the Möbius dark curve
768
+ * (`mapToneForScheme`), the saturation reducers, and the solver's
769
+ * scheme tone range.
770
+ *
771
+ * See `docs/okhst.md` for the full specification and the calibrated
772
+ * default constants.
773
+ */
774
+ /**
775
+ * Reference eps for the OKHST color space. WCAG 2 contrast is
776
+ * `(Y_hi + 0.05) / (Y_lo + 0.05)`, so an eps of `0.05` makes equal tone
777
+ * steps yield equal WCAG contrast. This is the canonical eps used by
778
+ * `okhst()` input, `{ h, s, t }` input, stored `ResolvedColorVariant.t`,
779
+ * relative `tone` offsets, and the contrast solver.
780
+ */
781
+ const REF_EPS = .05;
782
+ /**
783
+ * Gray luminance from OKHSL lightness. For an achromatic color the OKLab
784
+ * lightness is `toeInv(l)` and luminance is its cube.
785
+ */
786
+ function lToY(l) {
787
+ const L = toeInv(l);
788
+ return L * L * L;
789
+ }
790
+ /** OKHSL lightness from gray luminance — exact inverse of {@link lToY}. */
791
+ function yToL(y) {
792
+ return toe(Math.cbrt(Math.max(0, y)));
793
+ }
794
+ /**
795
+ * Map a luminance `Y` (0–1) to tone (0–100) at the given eps.
796
+ * `toneFromY(0) === 0` and `toneFromY(1) === 100` for any eps.
797
+ */
798
+ function toneFromY(y, eps = REF_EPS) {
799
+ return (Math.log(y + eps) - Math.log(eps)) / (Math.log(1 + eps) - Math.log(eps)) * 100;
800
+ }
801
+ /** Map a tone (0–100) back to luminance (0–1). Inverse of {@link toneFromY}. */
802
+ function yFromTone(t, eps = REF_EPS) {
803
+ const den = Math.log(1 + eps) - Math.log(eps);
804
+ return Math.exp(t / 100 * den + Math.log(eps)) - eps;
805
+ }
806
+ /** OKHSL lightness (0–1) -> tone (0–100). */
807
+ function toTone(l, eps = REF_EPS) {
808
+ return toneFromY(lToY(l), eps);
809
+ }
810
+ /** Tone (0–100) -> OKHSL lightness (0–1). Inverse of {@link toTone}. */
811
+ function fromTone(t, eps = REF_EPS) {
812
+ return yToL(yFromTone(t, eps));
813
+ }
814
+ /** Convert OKHST `{ h, s, t }` (t in 0–1) to OKHSL `{ h, s, l }`. */
815
+ function okhstToOkhsl(c) {
816
+ return {
817
+ h: c.h,
818
+ s: c.s,
819
+ l: clamp(fromTone(c.t * 100), 0, 1)
820
+ };
821
+ }
822
+ /** Convert OKHSL `{ h, s, l }` to OKHST `{ h, s, t }` (t in 0–1). */
823
+ function okhslToOkhst(c) {
824
+ return {
825
+ h: c.h,
826
+ s: c.s,
827
+ t: clamp(toTone(c.l) / 100, 0, 1)
828
+ };
829
+ }
830
+ /**
831
+ * Edge adapter: a resolved variant stores canonical tone `t` (0–1). Convert
832
+ * it to the OKHSL `{ h, s, l }` the formatters and luminance pipeline expect.
833
+ */
834
+ function variantToOkhsl(v) {
835
+ return {
836
+ h: v.h,
837
+ s: v.s,
838
+ l: clamp(fromTone(v.t * 100), 0, 1)
839
+ };
840
+ }
841
+ /**
842
+ * Normalize any {@link ToneWindow} form to `{ lo, hi, eps }`.
843
+ * - `false`: full range `[0, 100]` at the reference eps (boundaries removed,
844
+ * curve preserved).
845
+ * - `[lo, hi]`: endpoints at the reference eps (the common form).
846
+ * - `{ lo, hi, eps }`: passed through (advanced eps tuning).
847
+ */
848
+ function normalizeToneWindow(win) {
849
+ if (win === false) return {
850
+ lo: 0,
851
+ hi: 100,
852
+ eps: REF_EPS
853
+ };
854
+ if (Array.isArray(win)) return {
855
+ lo: win[0],
856
+ hi: win[1],
857
+ eps: REF_EPS
858
+ };
859
+ return {
860
+ lo: win.lo,
861
+ hi: win.hi,
862
+ eps: win.eps
863
+ };
864
+ }
865
+ /**
866
+ * Resolve the active tone window for a scheme as OKHSL-lightness endpoints.
867
+ * - HC variants always return the full range `[0, 100]` with the mode eps.
868
+ * - `false` (= "no clamping") is treated as `[0, 100]` with the reference eps.
869
+ */
870
+ function activeWindow(isHighContrast, kind, config) {
871
+ const win = normalizeToneWindow(kind === "dark" ? config.darkTone : config.lightTone);
872
+ if (isHighContrast) return {
873
+ lo: 0,
874
+ hi: 100,
875
+ eps: win.eps
876
+ };
877
+ return win;
878
+ }
879
+ /**
880
+ * Remap an authored tone (0–100) into a scheme window and return the final
881
+ * OKHSL lightness (0–100). The window endpoints are OKHSL lightnesses; the
882
+ * author tone is positioned within the window's tone interval (using the
883
+ * window's render eps), then converted back to lightness.
884
+ */
885
+ function remapToneToLightness(authorTone, win) {
886
+ const loT = toTone(win.lo / 100, win.eps);
887
+ const hiT = toTone(win.hi / 100, win.eps);
888
+ return clamp(fromTone(loT + authorTone / 100 * (hiT - loT), win.eps) * 100, 0, 100);
889
+ }
890
+ /**
891
+ * Map an authored tone for a scheme and return the canonical stored tone
892
+ * (0–100, reference eps).
893
+ *
894
+ * - `static`: identity — the same tone renders in every scheme.
895
+ * - `auto` + dark: invert (`100 - tone`) then remap into the dark window.
896
+ * - `auto`/`fixed` + light, or `fixed` + dark: remap, no inversion.
897
+ *
898
+ * The window remap uses the mode's render eps to land a final OKHSL
899
+ * lightness; that lightness is then re-expressed as canonical tone so
900
+ * relative offsets and contrast stay comparable across schemes.
901
+ */
902
+ function mapToneForScheme(authorTone, mode, isDark, isHighContrast, config) {
903
+ if (mode === "static") return clamp(authorTone, 0, 100);
904
+ const win = activeWindow(isHighContrast, isDark ? "dark" : "light", config);
905
+ return clamp(toTone(remapToneToLightness(clamp(isDark && mode === "auto" ? 100 - authorTone : authorTone, 0, 100), win) / 100), 0, 100);
906
+ }
907
+ /** Dark-scheme desaturation reducer (unchanged from the legacy pipeline). */
908
+ function mapSaturationDark(s, mode, config) {
909
+ if (mode === "static") return s;
910
+ return s * (1 - config.darkDesaturation);
911
+ }
912
+ /** Smoothstep `0..1`. */
913
+ function smoothstep(x) {
914
+ const t = clamp(x, 0, 1);
915
+ return t * t * (3 - 2 * t);
916
+ }
917
+ /** Fraction of the tone range over which the taper ramps in, per end. */
918
+ const TAPER_REGION = .15;
919
+ /**
920
+ * Gently taper saturation toward the tone extremes, where in-gamut chroma
921
+ * collapses and high saturation reads as noise. `taper` is the *strength*
922
+ * (0–1): the maximum fraction of saturation removed at the very edges. The
923
+ * rolloff ramps in smoothly over the outer {@link TAPER_REGION} of tone on
924
+ * each end, so mid-tones are untouched and high-tone surfaces keep most of
925
+ * their color. `taper = 0` disables the effect.
926
+ *
927
+ * @param s Saturation (0–1).
928
+ * @param toneFinal Stored canonical tone (0–1).
929
+ * @param taper Strength (0–1); default config is a gentle 0.15.
930
+ */
931
+ function saturationEnvelope(s, toneFinal, taper) {
932
+ if (taper <= 0) return s;
933
+ const t = clamp(toneFinal, 0, 1);
934
+ const strength = clamp(taper, 0, 1);
935
+ const edge = Math.min(t, 1 - t);
936
+ if (edge >= TAPER_REGION) return s;
937
+ return s * (1 - strength * (1 - smoothstep(edge / TAPER_REGION)));
938
+ }
939
+ /**
940
+ * Tone search range (0–1) for the contrast solver in a given scheme.
941
+ * `static` searches the full range; otherwise the scheme window's tone
942
+ * endpoints (HC bypasses to full range).
943
+ */
944
+ function schemeToneRange(isDark, mode, isHighContrast, config) {
945
+ if (mode === "static") return [0, 1];
946
+ const win = activeWindow(isHighContrast, isDark ? "dark" : "light", config);
947
+ return [clamp(toTone(win.lo / 100) / 100, 0, 1), clamp(toTone(win.hi / 100) / 100, 0, 1)];
948
+ }
949
+
950
+ //#endregion
951
+ //#region src/contrast-solver.ts
952
+ /**
953
+ * Contrast solver — operates in OKHST tone.
954
+ *
955
+ * Finds the tone closest to a preferred tone that satisfies a contrast
956
+ * floor (WCAG 2 ratio or APCA Lc) against a base color. Because tone is
957
+ * contrast-uniform, the WCAG branch gets a closed-form seed and the search
958
+ * converges quickly.
959
+ *
960
+ * Public API: `findToneForContrast`, `findValueForMixContrast`,
961
+ * `resolveMinContrast`, `resolveContrastForMode`, `apcaContrast`.
962
+ */
963
+ /**
964
+ * Luminance of a linear-sRGB color in the basis the metric expects: WCAG
965
+ * relative luminance for `wcag`, APCA screen luminance (`Ys`) for `apca`.
966
+ */
967
+ function metricLuminance(metric, linearRgb) {
968
+ return metric === "apca" ? apcaLuminanceFromLinearRgb(linearRgb) : gamutClampedLuminance(linearRgb);
969
+ }
970
+ const CONTRAST_PRESETS = {
971
+ AA: 4.5,
972
+ AAA: 7,
973
+ "AA-large": 3,
974
+ "AAA-large": 4.5
975
+ };
976
+ function resolveMinContrast(value) {
977
+ if (typeof value === "number") return Math.max(1, value);
978
+ return CONTRAST_PRESETS[value];
979
+ }
980
+ function pickPair(p, isHighContrast) {
981
+ return Array.isArray(p) ? isHighContrast ? p[1] : p[0] : p;
982
+ }
983
+ /**
984
+ * Resolve a `ContrastSpec` (already selected from any outer HC pair) for a
985
+ * given mode into `{ metric, target }`. Handles the inner metric HC pair and
986
+ * preset resolution.
987
+ */
988
+ function resolveContrastForMode(spec, isHighContrast) {
989
+ if (typeof spec === "number" || typeof spec === "string") return {
990
+ metric: "wcag",
991
+ target: resolveMinContrast(spec)
992
+ };
993
+ if ("apca" in spec) return {
994
+ metric: "apca",
995
+ target: Math.abs(pickPair(spec.apca, isHighContrast))
996
+ };
997
+ return {
998
+ metric: "wcag",
999
+ target: resolveMinContrast(pickPair(spec.wcag, isHighContrast))
1000
+ };
1001
+ }
1002
+ const APCA_EXPONENTS = {
1003
+ mainTRC: 2.4,
1004
+ normBG: .56,
1005
+ normTXT: .57,
1006
+ revTXT: .62,
1007
+ revBG: .65
1008
+ };
1009
+ const APCA_BLACK_THRESH = .022;
1010
+ const APCA_BLACK_CLIP = 1.414;
1011
+ const APCA_DELTA_Y_MIN = 5e-4;
1012
+ const APCA_SCALE = 1.14;
1013
+ const APCA_LO_OFFSET = .027;
1014
+ function apcaSoftClamp(y) {
1015
+ const yc = Math.max(0, y);
1016
+ if (yc >= APCA_BLACK_THRESH) return yc;
1017
+ return yc + Math.pow(APCA_BLACK_THRESH - yc, APCA_BLACK_CLIP);
1018
+ }
1019
+ /**
1020
+ * APCA lightness contrast (Lc), signed: positive for dark text on light bg,
1021
+ * negative for light text on dark bg. Inputs are screen luminances (0–1).
1022
+ */
1023
+ function apcaContrast(yText, yBg) {
1024
+ const txt = apcaSoftClamp(yText);
1025
+ const bg = apcaSoftClamp(yBg);
1026
+ if (Math.abs(bg - txt) < APCA_DELTA_Y_MIN) return 0;
1027
+ let sapc;
1028
+ if (bg > txt) {
1029
+ sapc = (Math.pow(bg, APCA_EXPONENTS.normBG) - Math.pow(txt, APCA_EXPONENTS.normTXT)) * APCA_SCALE;
1030
+ return sapc < .1 ? 0 : (sapc - APCA_LO_OFFSET) * 100;
1031
+ }
1032
+ sapc = (Math.pow(bg, APCA_EXPONENTS.revBG) - Math.pow(txt, APCA_EXPONENTS.revTXT)) * APCA_SCALE;
1033
+ return sapc > -.1 ? 0 : (sapc + APCA_LO_OFFSET) * 100;
1034
+ }
1035
+ const CACHE_SIZE = 512;
1036
+ const luminanceCache = /* @__PURE__ */ new Map();
1037
+ const cacheOrder = [];
1038
+ /**
1039
+ * Luminance of an OKHST color `(h, s, t)` with t in 0–1 (reference eps), in
1040
+ * the metric's luminance basis. The metric is part of the cache key because
1041
+ * WCAG and APCA derive different luminances from the same color.
1042
+ */
1043
+ function cachedLuminance(metric, h, s, t) {
1044
+ const tRounded = Math.round(t * 1e4) / 1e4;
1045
+ const key = `${metric}|${h}|${s}|${tRounded}`;
1046
+ const cached = luminanceCache.get(key);
1047
+ if (cached !== void 0) return cached;
1048
+ const y = metricLuminance(metric, okhslToLinearSrgb(h, s, fromTone(tRounded * 100, REF_EPS)));
1049
+ if (luminanceCache.size >= CACHE_SIZE) {
1050
+ const evict = cacheOrder.shift();
1051
+ luminanceCache.delete(evict);
1052
+ }
1053
+ luminanceCache.set(key, y);
1054
+ cacheOrder.push(key);
1055
+ return y;
1056
+ }
1057
+ /**
1058
+ * Score a candidate luminance against the base for a metric. Returns a value
1059
+ * that is `>= target` exactly when the floor is met (WCAG ratio, or APCA Lc
1060
+ * magnitude).
1061
+ */
1062
+ function metricScore(metric, yCandidate, yBase) {
1063
+ if (metric === "wcag") return contrastRatioFromLuminance(yCandidate, yBase);
1064
+ return Math.abs(apcaContrast(yCandidate, yBase));
1065
+ }
1066
+ /**
1067
+ * Binary search one branch `[lo, hi]` for the position nearest to `anchor`
1068
+ * that meets `target`. The domain is whatever `lum` interprets (tone 0–1 or
1069
+ * mix parameter 0–1); the search is identical in both cases.
1070
+ */
1071
+ function searchBranch(lum, lo, hi, yBase, metric, target, epsilon, maxIter, anchor) {
1072
+ const scoreLo = metricScore(metric, lum(lo), yBase);
1073
+ const scoreHi = metricScore(metric, lum(hi), yBase);
1074
+ if (scoreLo < target && scoreHi < target) return scoreLo >= scoreHi ? {
1075
+ pos: lo,
1076
+ contrast: scoreLo,
1077
+ met: false
1078
+ } : {
1079
+ pos: hi,
1080
+ contrast: scoreHi,
1081
+ met: false
1082
+ };
1083
+ let low = lo;
1084
+ let high = hi;
1085
+ for (let i = 0; i < maxIter; i++) {
1086
+ if (high - low < epsilon) break;
1087
+ const mid = (low + high) / 2;
1088
+ if (metricScore(metric, lum(mid), yBase) >= target) if (mid < anchor) low = mid;
1089
+ else high = mid;
1090
+ else if (mid < anchor) high = mid;
1091
+ else low = mid;
1092
+ }
1093
+ const scoreLow = metricScore(metric, lum(low), yBase);
1094
+ const scoreHigh = metricScore(metric, lum(high), yBase);
1095
+ const lowPasses = scoreLow >= target;
1096
+ const highPasses = scoreHigh >= target;
1097
+ if (lowPasses && highPasses) return Math.abs(low - anchor) <= Math.abs(high - anchor) ? {
1098
+ pos: low,
1099
+ contrast: scoreLow,
1100
+ met: true
1101
+ } : {
1102
+ pos: high,
1103
+ contrast: scoreHigh,
1104
+ met: true
1105
+ };
1106
+ if (lowPasses) return {
1107
+ pos: low,
1108
+ contrast: scoreLow,
1109
+ met: true
1110
+ };
1111
+ if (highPasses) return {
1112
+ pos: high,
1113
+ contrast: scoreHigh,
1114
+ met: true
1115
+ };
1116
+ return scoreLow >= scoreHigh ? {
1117
+ pos: low,
1118
+ contrast: scoreLow,
1119
+ met: false
1120
+ } : {
1121
+ pos: high,
1122
+ contrast: scoreHigh,
1123
+ met: false
1124
+ };
1125
+ }
1126
+ /**
1127
+ * Closed-form WCAG tone seed: the gray tone whose luminance produces exactly
1128
+ * the target ratio against the base, on the requested side. Used to bias the
1129
+ * preferred tone before the search so chromatic refinement starts close.
1130
+ */
1131
+ function wcagToneSeed(yBase, target, darker) {
1132
+ const yTarget = darker ? (yBase + .05) / target - .05 : target * (yBase + .05) - .05;
1133
+ const yClamped = Math.max(0, Math.min(1, yTarget));
1134
+ return Math.max(0, Math.min(1, toneFromY(yClamped, REF_EPS) / 100));
1135
+ }
1136
+ function solveNearestContrast(opts) {
1137
+ const { lum, yBase, metric, target, searchTarget, lo, hi, searchAnchor, distanceAnchor, epsilon, maxIterations, flip, initialIsLower } = opts;
1138
+ const runBranch = (lower) => lower ? searchBranch(lum, lo, searchAnchor, yBase, metric, searchTarget, epsilon, maxIterations, searchAnchor) : searchBranch(lum, searchAnchor, hi, yBase, metric, searchTarget, epsilon, maxIterations, searchAnchor);
1139
+ const initialResult = runBranch(initialIsLower);
1140
+ initialResult.met = initialResult.contrast >= target;
1141
+ if (initialResult.met && !flip) return {
1142
+ ...initialResult,
1143
+ lower: initialIsLower
1144
+ };
1145
+ if (flip) {
1146
+ const oppositeResult = (initialIsLower ? distanceAnchor < hi : distanceAnchor > lo) ? runBranch(!initialIsLower) : null;
1147
+ if (oppositeResult) oppositeResult.met = oppositeResult.contrast >= target;
1148
+ if (initialResult.met && oppositeResult?.met) return Math.abs(initialResult.pos - distanceAnchor) <= Math.abs(oppositeResult.pos - distanceAnchor) ? {
1149
+ ...initialResult,
1150
+ lower: initialIsLower
1151
+ } : {
1152
+ ...oppositeResult,
1153
+ lower: !initialIsLower,
1154
+ flipped: true
1155
+ };
1156
+ if (initialResult.met) return {
1157
+ ...initialResult,
1158
+ lower: initialIsLower
1159
+ };
1160
+ if (oppositeResult?.met) return {
1161
+ ...oppositeResult,
1162
+ lower: !initialIsLower,
1163
+ flipped: true
1164
+ };
1165
+ }
1166
+ const extreme = initialIsLower ? lo : hi;
1167
+ return {
1168
+ pos: extreme,
1169
+ contrast: metricScore(metric, lum(extreme), yBase),
1170
+ met: false,
1171
+ lower: initialIsLower
1172
+ };
1173
+ }
1174
+ /**
1175
+ * Find the tone that satisfies a contrast floor against a base color,
1176
+ * staying as close to `preferredTone` as possible.
1177
+ */
1178
+ function findToneForContrast(options) {
1179
+ const { hue, saturation, preferredTone, baseLinearRgb, contrast, toneRange = [0, 1], epsilon = 1e-4, maxIterations = 18 } = options;
1180
+ const { metric, target } = contrast;
1181
+ const searchTarget = metric === "wcag" ? target * 1.01 : target + .5;
1182
+ const yBase = metricLuminance(metric, baseLinearRgb);
1183
+ const taper = options.saturationTaper ?? 0;
1184
+ const lum = taper > 0 ? (t) => {
1185
+ return metricLuminance(metric, okhslToLinearSrgb(hue, saturationEnvelope(saturation, t, taper), fromTone(t * 100, REF_EPS)));
1186
+ } : (t) => cachedLuminance(metric, hue, saturation, t);
1187
+ const scorePref = metricScore(metric, lum(preferredTone), yBase);
1188
+ if (scorePref >= searchTarget) return {
1189
+ tone: preferredTone,
1190
+ contrast: scorePref,
1191
+ met: true,
1192
+ branch: "preferred"
1193
+ };
1194
+ const [minT, maxT] = toneRange;
1195
+ const canDarker = preferredTone > minT;
1196
+ const canLighter = preferredTone < maxT;
1197
+ let initialIsDarker;
1198
+ if (options.initialDirection !== void 0) initialIsDarker = options.initialDirection === "darker";
1199
+ else if (canDarker && !canLighter) initialIsDarker = true;
1200
+ else if (!canDarker && canLighter) initialIsDarker = false;
1201
+ else if (!canDarker && !canLighter) return {
1202
+ tone: preferredTone,
1203
+ contrast: scorePref,
1204
+ met: false,
1205
+ branch: "preferred"
1206
+ };
1207
+ else initialIsDarker = metricScore(metric, lum(minT), yBase) >= metricScore(metric, lum(maxT), yBase);
1208
+ const solved = solveNearestContrast({
1209
+ lum,
1210
+ yBase,
1211
+ metric,
1212
+ target,
1213
+ searchTarget,
1214
+ lo: minT,
1215
+ hi: maxT,
1216
+ searchAnchor: metric === "wcag" ? clamp(initialIsDarker ? Math.min(preferredTone, wcagToneSeed(yBase, target, true)) : Math.max(preferredTone, wcagToneSeed(yBase, target, false)), minT, maxT) : preferredTone,
1217
+ distanceAnchor: preferredTone,
1218
+ epsilon,
1219
+ maxIterations,
1220
+ flip: options.flip ?? false,
1221
+ initialIsLower: initialIsDarker
1222
+ });
1223
+ return {
1224
+ tone: solved.pos,
1225
+ contrast: solved.contrast,
1226
+ met: solved.met,
1227
+ branch: solved.lower ? "darker" : "lighter",
1228
+ ...solved.flipped ? { flipped: true } : {}
1229
+ };
1230
+ }
1231
+ /**
1232
+ * Find the mix parameter (ratio or opacity) that satisfies a contrast floor
1233
+ * against a base color, staying as close to `preferredValue` as possible.
1234
+ */
1235
+ function findValueForMixContrast(options) {
1236
+ const { preferredValue, baseLinearRgb, contrast, luminanceAtValue, epsilon = 1e-4, maxIterations = 20 } = options;
1237
+ const { metric, target } = contrast;
1238
+ const searchTarget = metric === "wcag" ? target * 1.01 : target + .5;
1239
+ const yBase = metricLuminance(metric, baseLinearRgb);
1240
+ const scorePref = metricScore(metric, luminanceAtValue(preferredValue), yBase);
1241
+ if (scorePref >= searchTarget) return {
1242
+ value: preferredValue,
1243
+ contrast: scorePref,
1244
+ met: true
1245
+ };
1246
+ const canLower = preferredValue > 0;
1247
+ const canUpper = preferredValue < 1;
1248
+ let initialIsLower;
1249
+ if (canLower && !canUpper) initialIsLower = true;
1250
+ else if (!canLower && canUpper) initialIsLower = false;
1251
+ else if (!canLower && !canUpper) return {
1252
+ value: preferredValue,
1253
+ contrast: scorePref,
1254
+ met: false
1255
+ };
1256
+ else initialIsLower = metricScore(metric, luminanceAtValue(0), yBase) >= metricScore(metric, luminanceAtValue(1), yBase);
1257
+ const solved = solveNearestContrast({
1258
+ lum: luminanceAtValue,
1259
+ yBase,
1260
+ metric,
1261
+ target,
1262
+ searchTarget,
1263
+ lo: 0,
1264
+ hi: 1,
1265
+ searchAnchor: preferredValue,
1266
+ distanceAnchor: preferredValue,
1267
+ epsilon,
1268
+ maxIterations,
1269
+ flip: options.flip ?? false,
1270
+ initialIsLower
1271
+ });
1272
+ return {
1273
+ value: solved.pos,
1274
+ contrast: solved.contrast,
1275
+ met: solved.met,
1276
+ ...solved.flipped ? { flipped: true } : {}
1277
+ };
1278
+ }
1279
+
1280
+ //#endregion
1281
+ //#region src/shadow.ts
1282
+ /**
1283
+ * Shadow color computation.
1284
+ *
1285
+ * Owns the shadow / mix def predicates, default tuning constants, the
1286
+ * tuning merge, and the actual `computeShadow` math (hue blend,
1287
+ * saturation cap, lightness clamp, alpha curve). The resolver consumes
1288
+ * this module per scheme variant.
1289
+ */
1290
+ function isShadowDef(def) {
1291
+ return def.type === "shadow";
1292
+ }
1293
+ function isMixDef(def) {
1294
+ return def.type === "mix";
1295
+ }
1296
+ const DEFAULT_SHADOW_TUNING = {
1297
+ saturationFactor: .18,
1298
+ maxSaturation: .25,
1299
+ lightnessFactor: .25,
1300
+ lightnessBounds: [.05, .2],
1301
+ minGapTarget: .05,
1302
+ alphaMax: 1,
1303
+ bgHueBlend: .2
1304
+ };
1305
+ function resolveShadowTuning(perColor, globalTuning) {
1306
+ return {
1307
+ ...DEFAULT_SHADOW_TUNING,
1308
+ ...globalTuning,
1309
+ ...perColor,
1310
+ lightnessBounds: perColor?.lightnessBounds ?? globalTuning?.lightnessBounds ?? DEFAULT_SHADOW_TUNING.lightnessBounds
1311
+ };
1312
+ }
1313
+ function circularLerp(a, b, t) {
1314
+ let diff = b - a;
1315
+ if (diff > 180) diff -= 360;
1316
+ else if (diff < -180) diff += 360;
1317
+ return ((a + diff * t) % 360 + 360) % 360;
1318
+ }
1319
+ /**
1320
+ * Compute the canonical max-contrast reference t value for normalization.
1321
+ * Uses bg.l=1, fg.l=0, intensity=100 — the theoretical maximum.
1322
+ * This is a fixed constant per tuning configuration, ensuring uniform
1323
+ * scaling across all bg/fg pairs at low intensities.
1324
+ */
1325
+ function computeRefT(tuning) {
1326
+ const EPSILON = 1e-6;
1327
+ let lShRef = clamp(tuning.lightnessFactor, tuning.lightnessBounds[0], tuning.lightnessBounds[1]);
1328
+ lShRef = Math.max(Math.min(lShRef, 1 - tuning.minGapTarget), 0);
1329
+ return 1 / Math.max(1 - lShRef, EPSILON);
1330
+ }
1331
+ function computeShadow(bg, fg, intensity, tuning) {
1332
+ const EPSILON = 1e-6;
1333
+ const clampedIntensity = clamp(intensity, 0, 100);
1334
+ const contrastWeight = fg ? Math.abs(bg.l - fg.l) : 1;
774
1335
  const deltaL = clampedIntensity / 100 * contrastWeight;
775
1336
  const h = fg ? circularLerp(fg.h, bg.h, tuning.bgHueBlend) : bg.h;
776
1337
  const s = fg ? Math.min(fg.s * tuning.saturationFactor, tuning.maxSaturation) : 0;
@@ -787,29 +1348,49 @@ function computeShadow(bg, fg, intensity, tuning) {
787
1348
  alpha
788
1349
  };
789
1350
  }
790
- function validateColorDefs(defs) {
791
- const names = new Set(Object.keys(defs));
1351
+
1352
+ //#endregion
1353
+ //#region src/validation.ts
1354
+ /**
1355
+ * Color graph validation and topological sort.
1356
+ *
1357
+ * `validateColorDefs` rejects bad references (missing / shadow-referencing /
1358
+ * base/contrast/tone mismatches) and detects cycles before the
1359
+ * resolver runs. `topoSort` orders defs so each color is processed after
1360
+ * its base / bg / fg / target dependencies.
1361
+ */
1362
+ function validateColorDefs(defs, externalBases) {
1363
+ const localNames = new Set(Object.keys(defs));
1364
+ const allNames = new Set([...localNames, ...externalBases ? externalBases.keys() : []]);
792
1365
  for (const [name, def] of Object.entries(defs)) {
793
1366
  if (isShadowDef(def)) {
794
- if (!names.has(def.bg)) throw new Error(`glaze: shadow "${name}" references non-existent bg "${def.bg}".`);
795
- if (isShadowDef(defs[def.bg])) throw new Error(`glaze: shadow "${name}" bg "${def.bg}" references another shadow color.`);
1367
+ if (!allNames.has(def.bg)) throw new Error(`glaze: shadow "${name}" references non-existent bg "${def.bg}".`);
1368
+ if (localNames.has(def.bg) && isShadowDef(defs[def.bg])) throw new Error(`glaze: shadow "${name}" bg "${def.bg}" references another shadow color.`);
796
1369
  if (def.fg !== void 0) {
797
- if (!names.has(def.fg)) throw new Error(`glaze: shadow "${name}" references non-existent fg "${def.fg}".`);
798
- if (isShadowDef(defs[def.fg])) throw new Error(`glaze: shadow "${name}" fg "${def.fg}" references another shadow color.`);
1370
+ if (!allNames.has(def.fg)) throw new Error(`glaze: shadow "${name}" references non-existent fg "${def.fg}".`);
1371
+ if (localNames.has(def.fg) && isShadowDef(defs[def.fg])) throw new Error(`glaze: shadow "${name}" fg "${def.fg}" references another shadow color.`);
799
1372
  }
800
1373
  continue;
801
1374
  }
1375
+ if (isMixDef(def)) {
1376
+ if (!allNames.has(def.base)) throw new Error(`glaze: mix "${name}" references non-existent base "${def.base}".`);
1377
+ if (!allNames.has(def.target)) throw new Error(`glaze: mix "${name}" references non-existent target "${def.target}".`);
1378
+ if (localNames.has(def.base) && isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
1379
+ if (localNames.has(def.target) && isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
1380
+ continue;
1381
+ }
802
1382
  const regDef = def;
803
1383
  if (regDef.contrast !== void 0 && !regDef.base) throw new Error(`glaze: color "${name}" has "contrast" without "base".`);
804
- if (regDef.lightness !== void 0 && !isAbsoluteLightness(regDef.lightness) && !regDef.base) throw new Error(`glaze: color "${name}" has relative "lightness" without "base".`);
805
- if (regDef.base && !names.has(regDef.base)) throw new Error(`glaze: color "${name}" references non-existent base "${regDef.base}".`);
806
- if (regDef.base && isShadowDef(defs[regDef.base])) throw new Error(`glaze: color "${name}" base "${regDef.base}" references a shadow color.`);
807
- if (!isAbsoluteLightness(regDef.lightness) && regDef.base === void 0) throw new Error(`glaze: color "${name}" must have either absolute "lightness" (root) or "base" (dependent).`);
808
- if (regDef.contrast !== void 0 && regDef.opacity !== void 0) console.warn(`glaze: color "${name}" has both "contrast" and "opacity". Opacity makes perceived lightness unpredictable.`);
1384
+ if (regDef.tone !== void 0 && !isAbsoluteTone(regDef.tone) && !regDef.base) throw new Error(`glaze: color "${name}" has relative "tone" without "base".`);
1385
+ if (regDef.base && !allNames.has(regDef.base)) throw new Error(`glaze: color "${name}" references non-existent base "${regDef.base}".`);
1386
+ if (regDef.base && localNames.has(regDef.base) && isShadowDef(defs[regDef.base])) throw new Error(`glaze: color "${name}" base "${regDef.base}" references a shadow color.`);
1387
+ if (!isAbsoluteTone(regDef.tone) && regDef.base === void 0) throw new Error(`glaze: color "${name}" must have either absolute "tone" (root) or "base" (dependent).`);
1388
+ if (regDef.contrast !== void 0 && regDef.opacity !== void 0) console.warn(`glaze: color "${name}" has both "contrast" and "opacity". Opacity makes perceived tone unpredictable.`);
809
1389
  }
810
1390
  const visited = /* @__PURE__ */ new Set();
811
1391
  const inStack = /* @__PURE__ */ new Set();
812
1392
  function dfs(name) {
1393
+ if (!localNames.has(name)) return;
813
1394
  if (inStack.has(name)) throw new Error(`glaze: circular base reference detected involving "${name}".`);
814
1395
  if (visited.has(name)) return;
815
1396
  inStack.add(name);
@@ -817,6 +1398,9 @@ function validateColorDefs(defs) {
817
1398
  if (isShadowDef(def)) {
818
1399
  dfs(def.bg);
819
1400
  if (def.fg) dfs(def.fg);
1401
+ } else if (isMixDef(def)) {
1402
+ dfs(def.base);
1403
+ dfs(def.target);
820
1404
  } else {
821
1405
  const regDef = def;
822
1406
  if (regDef.base) dfs(regDef.base);
@@ -824,7 +1408,7 @@ function validateColorDefs(defs) {
824
1408
  inStack.delete(name);
825
1409
  visited.add(name);
826
1410
  }
827
- for (const name of names) dfs(name);
1411
+ for (const name of localNames) dfs(name);
828
1412
  }
829
1413
  function topoSort(defs) {
830
1414
  const result = [];
@@ -833,9 +1417,13 @@ function topoSort(defs) {
833
1417
  if (visited.has(name)) return;
834
1418
  visited.add(name);
835
1419
  const def = defs[name];
1420
+ if (def === void 0) return;
836
1421
  if (isShadowDef(def)) {
837
1422
  visit(def.bg);
838
1423
  if (def.fg) visit(def.fg);
1424
+ } else if (isMixDef(def)) {
1425
+ visit(def.base);
1426
+ visit(def.target);
839
1427
  } else {
840
1428
  const regDef = def;
841
1429
  if (regDef.base) visit(regDef.base);
@@ -845,60 +1433,132 @@ function topoSort(defs) {
845
1433
  for (const name of Object.keys(defs)) visit(name);
846
1434
  return result;
847
1435
  }
848
- function mapLightnessLight(l, mode) {
849
- if (mode === "static") return l;
850
- const [lo, hi] = globalConfig.lightLightness;
851
- return l * (hi - lo) / 100 + lo;
1436
+
1437
+ //#endregion
1438
+ //#region src/warnings.ts
1439
+ /**
1440
+ * Contrast-warning dispatcher.
1441
+ *
1442
+ * Tokens memoize their resolution, but a long-lived process (e.g. a dev
1443
+ * server with HMR) can re-resolve the same theme many times. The cache
1444
+ * here dedupes warnings within a session with a soft cap to keep noise
1445
+ * bounded.
1446
+ */
1447
+ const CONTRAST_WARN_CACHE_LIMIT = 256;
1448
+ const contrastWarnCache = /* @__PURE__ */ new Set();
1449
+ /**
1450
+ * Slack factor below the requested target before we emit a warning.
1451
+ * The contrast solver overshoots to absorb rounding noise, so an actual
1452
+ * value within ~2x that overshoot is effectively a pass.
1453
+ */
1454
+ const CONTRAST_WARN_SLACK_WCAG = .98;
1455
+ /** APCA Lc is on a 0–106 scale; allow a small absolute slack. */
1456
+ const CONTRAST_WARN_SLACK_APCA = 1.5;
1457
+ function schemeLabel(isDark, isHighContrast) {
1458
+ if (isDark && isHighContrast) return "darkContrast";
1459
+ if (isDark) return "dark";
1460
+ if (isHighContrast) return "lightContrast";
1461
+ return "light";
852
1462
  }
853
- function mapLightnessDark(l, mode) {
854
- if (mode === "static") return l;
855
- const [lo, hi] = globalConfig.darkLightness;
856
- if (mode === "fixed") return l * (hi - lo) / 100 + lo;
857
- return (100 - l) * (hi - lo) / 100 + lo;
1463
+ function metricLabel(c) {
1464
+ return c.metric === "apca" ? `APCA Lc ${c.target.toFixed(1)}` : `WCAG ${c.target.toFixed(2)}`;
858
1465
  }
859
- function mapSaturationDark(s, mode) {
860
- if (mode === "static") return s;
861
- return s * (1 - globalConfig.darkDesaturation);
1466
+ function dedupe(key) {
1467
+ if (contrastWarnCache.has(key)) return true;
1468
+ if (contrastWarnCache.size >= CONTRAST_WARN_CACHE_LIMIT) contrastWarnCache.clear();
1469
+ contrastWarnCache.add(key);
1470
+ return false;
862
1471
  }
863
- function clamp(v, min, max) {
864
- return Math.max(min, Math.min(max, v));
1472
+ /** Warn when the solver could not reach the requested contrast floor. */
1473
+ function warnContrastUnmet(name, isDark, isHighContrast, contrast, actual) {
1474
+ if (actual >= (contrast.metric === "apca" ? contrast.target - CONTRAST_WARN_SLACK_APCA : contrast.target * CONTRAST_WARN_SLACK_WCAG)) return;
1475
+ const scheme = schemeLabel(isDark, isHighContrast);
1476
+ if (dedupe(`unmet|${name}|${scheme}|${contrast.metric}|${contrast.target.toFixed(2)}|${actual.toFixed(2)}`)) return;
1477
+ 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.`);
865
1478
  }
866
1479
  /**
867
- * Parse a value that can be absolute (number) or relative (signed string).
868
- * Returns the numeric value and whether it's relative.
1480
+ * Verification (§10): a chromatic swatch inherits the gray tone's
1481
+ * lightness but drifts in real luminance, so a contrast-floored color may
1482
+ * land slightly under the contrast its tone implies. Emit an advisory
1483
+ * warning when the actual measured contrast drifts below the target.
869
1484
  */
870
- function parseRelativeOrAbsolute(value) {
871
- if (typeof value === "number") return {
872
- value,
873
- relative: false
874
- };
875
- return {
876
- value: parseFloat(value),
877
- relative: true
878
- };
1485
+ function warnContrastDrift(name, isDark, isHighContrast, contrast, yColor, yBase) {
1486
+ const actual = contrast.metric === "apca" ? Math.abs(apcaContrast(yColor, yBase)) : contrastRatioFromLuminance(yColor, yBase);
1487
+ if (actual >= (contrast.metric === "apca" ? contrast.target - CONTRAST_WARN_SLACK_APCA : contrast.target * CONTRAST_WARN_SLACK_WCAG)) return;
1488
+ const scheme = schemeLabel(isDark, isHighContrast);
1489
+ if (dedupe(`drift|${name}|${scheme}|${contrast.metric}|${contrast.target.toFixed(2)}|${actual.toFixed(2)}`)) return;
1490
+ 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.`);
879
1491
  }
1492
+
1493
+ //#endregion
1494
+ //#region src/resolver.ts
880
1495
  /**
881
- * Compute the effective hue for a color, given the theme seed hue
882
- * and an optional per-color hue override.
1496
+ * Color resolution engine.
1497
+ *
1498
+ * Runs the four-pass solver (light → light-HC → dark → dark-HC) that
1499
+ * turns a `ColorMap` into a fully resolved `ResolvedColor` per name.
1500
+ * Owns the per-scheme resolve helpers for regular, shadow, and mix
1501
+ * color defs.
1502
+ *
1503
+ * Variants are stored in OKHST: `h` / `s` are OKHSL hue/saturation and
1504
+ * `t` is the canonical contrast-uniform tone (0–1, reference eps). The
1505
+ * resolver works in tone for regular colors and converts to/from OKHSL
1506
+ * lightness only at the mix/shadow and luminance edges.
1507
+ *
1508
+ * Every function receives a single `GlazeConfigResolved` so the full
1509
+ * per-instance config (including overrides) is available without
1510
+ * re-reading the global singleton mid-resolve.
883
1511
  */
884
- function resolveEffectiveHue(seedHue, defHue) {
885
- if (defHue === void 0) return seedHue;
886
- const parsed = parseRelativeOrAbsolute(defHue);
887
- if (parsed.relative) return ((seedHue + parsed.value) % 360 + 360) % 360;
888
- return (parsed.value % 360 + 360) % 360;
889
- }
1512
+ function getSchemeVariant(color, isDark, isHighContrast) {
1513
+ if (isDark && isHighContrast) return color.darkContrast;
1514
+ if (isDark) return color.dark;
1515
+ if (isHighContrast) return color.lightContrast;
1516
+ return color.light;
1517
+ }
1518
+ /** Edge adapter: resolved variant (`t`) → OKHSL-lightness variant. */
1519
+ function toOkhslVariant(v) {
1520
+ const c = variantToOkhsl(v);
1521
+ return {
1522
+ h: c.h,
1523
+ s: c.s,
1524
+ l: c.l,
1525
+ alpha: v.alpha
1526
+ };
1527
+ }
1528
+ /** Edge adapter: OKHSL-lightness variant → resolved variant (`t`). */
1529
+ function toToneVariant(v) {
1530
+ const c = okhslToOkhst({
1531
+ h: v.h,
1532
+ s: v.s,
1533
+ l: v.l
1534
+ });
1535
+ return {
1536
+ h: c.h,
1537
+ s: c.s,
1538
+ t: c.t,
1539
+ alpha: v.alpha
1540
+ };
1541
+ }
1542
+ function resolveContrastSpec(spec, isHighContrast) {
1543
+ return resolveContrastForMode(isHighContrast ? pairHC(spec) : pairNormal(spec), isHighContrast);
1544
+ }
890
1545
  /**
891
- * Check whether a lightness value represents an absolute root definition
892
- * (i.e. a number, not a relative string).
1546
+ * Apply the relative-tone delta against a base, honoring `flip`.
1547
+ *
1548
+ * When `flip` is on and `base + delta` falls outside `[0, 100]`, mirror the
1549
+ * delta to the other side of the base (so an offset that would clamp instead
1550
+ * reflects back into range). When off, the caller clamps as usual.
893
1551
  */
894
- function isAbsoluteLightness(lightness) {
895
- if (lightness === void 0) return false;
896
- return typeof (Array.isArray(lightness) ? lightness[0] : lightness) === "number";
1552
+ function applyToneFlip(delta, baseTone, flip) {
1553
+ if (!flip) return delta;
1554
+ const target = baseTone + delta;
1555
+ if (target >= 0 && target <= 100) return delta;
1556
+ return -delta;
897
1557
  }
898
- function resolveRootColor(_name, def, _ctx, isHighContrast) {
899
- const rawL = def.lightness;
1558
+ function resolveRootColor(def, isHighContrast) {
1559
+ const rawT = def.tone;
900
1560
  return {
901
- lightL: clamp(parseRelativeOrAbsolute(isHighContrast ? pairHC(rawL) : pairNormal(rawL)).value, 0, 100),
1561
+ authorTone: clamp(parseToneValue(isHighContrast ? pairHC(rawT) : pairNormal(rawT)).value, 0, 100),
902
1562
  satFactor: clamp(def.saturation ?? 1, 0, 1)
903
1563
  };
904
1564
  }
@@ -908,111 +1568,188 @@ function resolveDependentColor(name, def, ctx, isHighContrast, isDark, effective
908
1568
  if (!baseResolved) throw new Error(`glaze: base "${baseName}" not yet resolved for "${name}".`);
909
1569
  const mode = def.mode ?? "auto";
910
1570
  const satFactor = clamp(def.saturation ?? 1, 0, 1);
1571
+ const flip = def.flip ?? ctx.config.autoFlip;
911
1572
  const baseVariant = getSchemeVariant(baseResolved, isDark, isHighContrast);
912
- const baseL = baseVariant.l * 100;
913
- let preferredL;
914
- const rawLightness = def.lightness;
915
- if (rawLightness === void 0) preferredL = baseL;
1573
+ const baseTone = baseVariant.t * 100;
1574
+ let preferredTone;
1575
+ const rawTone = def.tone;
1576
+ if (rawTone === void 0) preferredTone = baseTone;
916
1577
  else {
917
- const parsed = parseRelativeOrAbsolute(isHighContrast ? pairHC(rawLightness) : pairNormal(rawLightness));
918
- if (parsed.relative) {
919
- let delta = parsed.value;
920
- if (isDark && mode === "auto") delta = -delta;
921
- preferredL = clamp(baseL + delta, 0, 100);
922
- } else if (isDark) preferredL = mapLightnessDark(parsed.value, mode);
923
- else preferredL = clamp(parsed.value, 0, 100);
1578
+ const parsed = parseToneValue(isHighContrast ? pairHC(rawTone) : pairNormal(rawTone));
1579
+ if (parsed.kind === "relative") if (isDark && mode === "auto") {
1580
+ const baseLightTone = getSchemeVariant(baseResolved, false, isHighContrast).t * 100;
1581
+ preferredTone = mapToneForScheme(clamp(baseLightTone + applyToneFlip(parsed.value, baseLightTone, flip), 0, 100), "auto", true, isHighContrast, ctx.config);
1582
+ } else preferredTone = clamp(baseTone + applyToneFlip(parsed.value, baseTone, flip), 0, 100);
1583
+ else preferredTone = mapToneForScheme(parsed.value, mode, isDark, isHighContrast, ctx.config);
924
1584
  }
925
1585
  const rawContrast = def.contrast;
926
1586
  if (rawContrast !== void 0) {
927
- const minCr = isHighContrast ? pairHC(rawContrast) : pairNormal(rawContrast);
928
- const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode) : satFactor * ctx.saturation / 100;
929
- const baseLinearRgb = okhslToLinearSrgb(baseVariant.h, baseVariant.s, baseVariant.l);
1587
+ const resolvedContrast = resolveContrastSpec(rawContrast, isHighContrast);
1588
+ const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode, ctx.config) : satFactor * ctx.saturation / 100;
1589
+ const baseOkhsl = toOkhslVariant(baseVariant);
1590
+ const baseLinearRgb = okhslToLinearSrgb(baseOkhsl.h, baseOkhsl.s, baseOkhsl.l);
1591
+ const toneRange = schemeToneRange(isDark, mode, isHighContrast, ctx.config);
1592
+ let initialDirection;
1593
+ if (preferredTone < baseTone) initialDirection = "darker";
1594
+ else if (preferredTone > baseTone) initialDirection = "lighter";
1595
+ const result = findToneForContrast({
1596
+ hue: effectiveHue,
1597
+ saturation: effectiveSat,
1598
+ preferredTone: clamp(preferredTone / 100, toneRange[0], toneRange[1]),
1599
+ baseLinearRgb,
1600
+ contrast: resolvedContrast,
1601
+ toneRange: [0, 1],
1602
+ initialDirection,
1603
+ flip,
1604
+ saturationTaper: ctx.config.saturationTaper
1605
+ });
1606
+ if (!result.met) warnContrastUnmet(name, isDark, isHighContrast, resolvedContrast, result.contrast);
930
1607
  return {
931
- l: findLightnessForContrast({
932
- hue: effectiveHue,
933
- saturation: effectiveSat,
934
- preferredLightness: preferredL / 100,
935
- baseLinearRgb,
936
- contrast: minCr
937
- }).lightness * 100,
1608
+ tone: result.tone * 100,
938
1609
  satFactor
939
1610
  };
940
1611
  }
941
1612
  return {
942
- l: clamp(preferredL, 0, 100),
1613
+ tone: clamp(preferredTone, 0, 100),
943
1614
  satFactor
944
1615
  };
945
1616
  }
946
- function getSchemeVariant(color, isDark, isHighContrast) {
947
- if (isDark && isHighContrast) return color.darkContrast;
948
- if (isDark) return color.dark;
949
- if (isHighContrast) return color.lightContrast;
950
- return color.light;
951
- }
952
1617
  function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
953
1618
  if (isShadowDef(def)) return resolveShadowForScheme(def, ctx, isDark, isHighContrast);
1619
+ if (isMixDef(def)) return resolveMixForScheme(def, ctx, isDark, isHighContrast);
954
1620
  const regDef = def;
955
1621
  const mode = regDef.mode ?? "auto";
956
- const isRoot = isAbsoluteLightness(regDef.lightness) && !regDef.base;
1622
+ const isRoot = isAbsoluteTone(regDef.tone) && !regDef.base;
957
1623
  const effectiveHue = resolveEffectiveHue(ctx.hue, regDef.hue);
958
- let lightL;
1624
+ let finalTone;
959
1625
  let satFactor;
960
1626
  if (isRoot) {
961
- const root = resolveRootColor(name, regDef, ctx, isHighContrast);
962
- lightL = root.lightL;
1627
+ const root = resolveRootColor(regDef, isHighContrast);
1628
+ finalTone = mapToneForScheme(root.authorTone, mode, isDark, isHighContrast, ctx.config);
963
1629
  satFactor = root.satFactor;
964
1630
  } else {
965
1631
  const dep = resolveDependentColor(name, regDef, ctx, isHighContrast, isDark, effectiveHue);
966
- lightL = dep.l;
1632
+ finalTone = dep.tone;
967
1633
  satFactor = dep.satFactor;
968
1634
  }
969
- let finalL;
970
- let finalSat;
971
- if (isDark && isRoot) {
972
- finalL = mapLightnessDark(lightL, mode);
973
- finalSat = mapSaturationDark(satFactor * ctx.saturation / 100, mode);
974
- } else if (isDark && !isRoot) {
975
- finalL = lightL;
976
- finalSat = mapSaturationDark(satFactor * ctx.saturation / 100, mode);
977
- } else if (isRoot) {
978
- finalL = mapLightnessLight(lightL, mode);
979
- finalSat = satFactor * ctx.saturation / 100;
980
- } else {
981
- finalL = lightL;
982
- finalSat = satFactor * ctx.saturation / 100;
983
- }
1635
+ const baseSat = satFactor * ctx.saturation / 100;
1636
+ let finalSat = isDark ? mapSaturationDark(baseSat, mode, ctx.config) : baseSat;
1637
+ const toneFraction = clamp(finalTone / 100, 0, 1);
1638
+ finalSat = saturationEnvelope(finalSat, toneFraction, ctx.config.saturationTaper);
984
1639
  return {
985
1640
  h: effectiveHue,
986
1641
  s: clamp(finalSat, 0, 1),
987
- l: clamp(finalL / 100, 0, 1),
1642
+ t: toneFraction,
988
1643
  alpha: regDef.opacity ?? 1
989
1644
  };
990
1645
  }
991
1646
  function resolveShadowForScheme(def, ctx, isDark, isHighContrast) {
992
- const bgVariant = getSchemeVariant(ctx.resolved.get(def.bg), isDark, isHighContrast);
1647
+ const bgVariant = toOkhslVariant(getSchemeVariant(ctx.resolved.get(def.bg), isDark, isHighContrast));
993
1648
  let fgVariant;
994
- if (def.fg) fgVariant = getSchemeVariant(ctx.resolved.get(def.fg), isDark, isHighContrast);
1649
+ if (def.fg) fgVariant = toOkhslVariant(getSchemeVariant(ctx.resolved.get(def.fg), isDark, isHighContrast));
995
1650
  const intensity = isHighContrast ? pairHC(def.intensity) : pairNormal(def.intensity);
996
- const tuning = resolveShadowTuning(def.tuning);
997
- return computeShadow(bgVariant, fgVariant, intensity, tuning);
1651
+ const tuning = resolveShadowTuning(def.tuning, ctx.config.shadowTuning);
1652
+ return toToneVariant(computeShadow(bgVariant, fgVariant, intensity, tuning));
998
1653
  }
999
- function resolveAllColors(hue, saturation, defs) {
1000
- validateColorDefs(defs);
1001
- const order = topoSort(defs);
1002
- const ctx = {
1003
- hue,
1004
- saturation,
1005
- defs,
1006
- resolved: /* @__PURE__ */ new Map()
1007
- };
1008
- function defMode(def) {
1009
- return isShadowDef(def) ? void 0 : def.mode ?? "auto";
1654
+ function okhslVariantToLinearRgb(v) {
1655
+ return okhslToLinearSrgb(v.h, v.s, v.l);
1656
+ }
1657
+ /**
1658
+ * Resolve hue for OKHSL mixing, handling achromatic colors.
1659
+ * When one color has no saturation, its hue is meaningless —
1660
+ * use the hue from the color that has saturation (matches CSS
1661
+ * color-mix "missing component" behavior).
1662
+ */
1663
+ function mixHue(base, target, t) {
1664
+ const SAT_EPSILON = 1e-6;
1665
+ const baseHasSat = base.s > SAT_EPSILON;
1666
+ const targetHasSat = target.s > SAT_EPSILON;
1667
+ if (baseHasSat && targetHasSat) return circularLerp(base.h, target.h, t);
1668
+ if (targetHasSat) return target.h;
1669
+ return base.h;
1670
+ }
1671
+ function linearSrgbLerp(base, target, t) {
1672
+ return [
1673
+ base[0] + (target[0] - base[0]) * t,
1674
+ base[1] + (target[1] - base[1]) * t,
1675
+ base[2] + (target[2] - base[2]) * t
1676
+ ];
1677
+ }
1678
+ function linearRgbToToneVariant(rgb) {
1679
+ const [h, s, l] = srgbToOkhsl([
1680
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[0]))),
1681
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[1]))),
1682
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[2])))
1683
+ ]);
1684
+ return toToneVariant({
1685
+ h,
1686
+ s,
1687
+ l,
1688
+ alpha: 1
1689
+ });
1690
+ }
1691
+ function resolveMixForScheme(def, ctx, isDark, isHighContrast) {
1692
+ const baseResolved = ctx.resolved.get(def.base);
1693
+ const targetResolved = ctx.resolved.get(def.target);
1694
+ const baseVariant = toOkhslVariant(getSchemeVariant(baseResolved, isDark, isHighContrast));
1695
+ const targetVariant = toOkhslVariant(getSchemeVariant(targetResolved, isDark, isHighContrast));
1696
+ let t = clamp(isHighContrast ? pairHC(def.value) : pairNormal(def.value), 0, 100) / 100;
1697
+ const blend = def.blend ?? "opaque";
1698
+ const space = def.space ?? "okhsl";
1699
+ const baseLinear = okhslVariantToLinearRgb(baseVariant);
1700
+ const targetLinear = okhslVariantToLinearRgb(targetVariant);
1701
+ if (def.contrast !== void 0) {
1702
+ const resolvedContrast = resolveContrastSpec(def.contrast, isHighContrast);
1703
+ const metric = resolvedContrast.metric;
1704
+ let luminanceAt;
1705
+ if (blend === "transparent" || space === "srgb") luminanceAt = (v) => metricLuminance(metric, linearSrgbLerp(baseLinear, targetLinear, v));
1706
+ else luminanceAt = (v) => {
1707
+ return metricLuminance(metric, okhslToLinearSrgb(mixHue(baseVariant, targetVariant, v), baseVariant.s + (targetVariant.s - baseVariant.s) * v, baseVariant.l + (targetVariant.l - baseVariant.l) * v));
1708
+ };
1709
+ t = findValueForMixContrast({
1710
+ preferredValue: t,
1711
+ baseLinearRgb: baseLinear,
1712
+ targetLinearRgb: targetLinear,
1713
+ contrast: resolvedContrast,
1714
+ luminanceAtValue: luminanceAt,
1715
+ flip: ctx.config.autoFlip
1716
+ }).value;
1010
1717
  }
1011
- const lightMap = /* @__PURE__ */ new Map();
1718
+ if (blend === "transparent") return toToneVariant({
1719
+ h: targetVariant.h,
1720
+ s: targetVariant.s,
1721
+ l: targetVariant.l,
1722
+ alpha: clamp(t, 0, 1)
1723
+ });
1724
+ if (space === "srgb") return linearRgbToToneVariant(linearSrgbLerp(baseLinear, targetLinear, t));
1725
+ return toToneVariant({
1726
+ h: mixHue(baseVariant, targetVariant, t),
1727
+ s: clamp(baseVariant.s + (targetVariant.s - baseVariant.s) * t, 0, 1),
1728
+ l: clamp(baseVariant.l + (targetVariant.l - baseVariant.l) * t, 0, 1),
1729
+ alpha: 1
1730
+ });
1731
+ }
1732
+ function defMode(def) {
1733
+ if (isShadowDef(def) || isMixDef(def)) return void 0;
1734
+ return def.mode ?? "auto";
1735
+ }
1736
+ /**
1737
+ * Run a single resolve pass over all local names. Pass 1 lazily creates
1738
+ * each `ResolvedColor` (all four slots seeded with the just-resolved
1739
+ * variant) the first time it sees a name; later passes update the
1740
+ * `target` slot on the existing record.
1741
+ */
1742
+ function runPass(order, defs, ctx, isDark, isHighContrast, target) {
1743
+ const out = /* @__PURE__ */ new Map();
1012
1744
  for (const name of order) {
1013
- const variant = resolveColorForScheme(name, defs[name], ctx, false, false);
1014
- lightMap.set(name, variant);
1015
- ctx.resolved.set(name, {
1745
+ const variant = resolveColorForScheme(name, defs[name], ctx, isDark, isHighContrast);
1746
+ out.set(name, variant);
1747
+ const existing = ctx.resolved.get(name);
1748
+ if (existing) ctx.resolved.set(name, {
1749
+ ...existing,
1750
+ [target]: variant
1751
+ });
1752
+ else ctx.resolved.set(name, {
1016
1753
  name,
1017
1754
  light: variant,
1018
1755
  dark: variant,
@@ -1021,49 +1758,87 @@ function resolveAllColors(hue, saturation, defs) {
1021
1758
  mode: defMode(defs[name])
1022
1759
  });
1023
1760
  }
1024
- const lightHCMap = /* @__PURE__ */ new Map();
1025
- for (const name of order) ctx.resolved.set(name, {
1026
- ...ctx.resolved.get(name),
1027
- lightContrast: lightMap.get(name)
1028
- });
1029
- for (const name of order) {
1030
- const variant = resolveColorForScheme(name, defs[name], ctx, false, true);
1031
- lightHCMap.set(name, variant);
1032
- ctx.resolved.set(name, {
1033
- ...ctx.resolved.get(name),
1034
- lightContrast: variant
1035
- });
1036
- }
1037
- const darkMap = /* @__PURE__ */ new Map();
1038
- for (const name of order) ctx.resolved.set(name, {
1039
- name,
1040
- light: lightMap.get(name),
1041
- dark: lightMap.get(name),
1042
- lightContrast: lightHCMap.get(name),
1043
- darkContrast: lightHCMap.get(name),
1044
- mode: defMode(defs[name])
1045
- });
1761
+ return out;
1762
+ }
1763
+ /**
1764
+ * Re-seed a single variant slot with a previously-resolved map so the
1765
+ * upcoming pass reads sensible fallbacks via `getSchemeVariant`.
1766
+ */
1767
+ function seedField(order, ctx, field, source) {
1046
1768
  for (const name of order) {
1047
- const variant = resolveColorForScheme(name, defs[name], ctx, true, false);
1048
- darkMap.set(name, variant);
1769
+ const existing = ctx.resolved.get(name);
1049
1770
  ctx.resolved.set(name, {
1050
- ...ctx.resolved.get(name),
1051
- dark: variant
1771
+ ...existing,
1772
+ [field]: source.get(name)
1052
1773
  });
1053
1774
  }
1054
- const darkHCMap = /* @__PURE__ */ new Map();
1055
- for (const name of order) ctx.resolved.set(name, {
1056
- ...ctx.resolved.get(name),
1057
- darkContrast: darkMap.get(name)
1058
- });
1775
+ }
1776
+ /**
1777
+ * After the four passes, surface chromatic contrast drift (§10): a color
1778
+ * resolved with a `base` + `contrast` may land slightly under the contrast
1779
+ * its tone implies because chromatic luminance drifts from the gray tone.
1780
+ */
1781
+ function verifyContrastDrift(order, defs, result) {
1059
1782
  for (const name of order) {
1060
- const variant = resolveColorForScheme(name, defs[name], ctx, true, true);
1061
- darkHCMap.set(name, variant);
1062
- ctx.resolved.set(name, {
1063
- ...ctx.resolved.get(name),
1064
- darkContrast: variant
1065
- });
1783
+ const def = defs[name];
1784
+ if (isShadowDef(def) || isMixDef(def)) continue;
1785
+ const regDef = def;
1786
+ if (regDef.contrast === void 0 || !regDef.base) continue;
1787
+ const color = result.get(name);
1788
+ const base = result.get(regDef.base);
1789
+ if (!color || !base) continue;
1790
+ for (const s of [
1791
+ {
1792
+ isDark: false,
1793
+ isHighContrast: false,
1794
+ field: "light"
1795
+ },
1796
+ {
1797
+ isDark: false,
1798
+ isHighContrast: true,
1799
+ field: "lightContrast"
1800
+ },
1801
+ {
1802
+ isDark: true,
1803
+ isHighContrast: false,
1804
+ field: "dark"
1805
+ },
1806
+ {
1807
+ isDark: true,
1808
+ isHighContrast: true,
1809
+ field: "darkContrast"
1810
+ }
1811
+ ]) {
1812
+ const spec = resolveContrastSpec(regDef.contrast, s.isHighContrast);
1813
+ const cVariant = color[s.field];
1814
+ const bVariant = base[s.field];
1815
+ const cOkhsl = toOkhslVariant(cVariant);
1816
+ const bOkhsl = toOkhslVariant(bVariant);
1817
+ const yC = metricLuminance(spec.metric, okhslToLinearSrgb(cOkhsl.h, cOkhsl.s, cOkhsl.l));
1818
+ const yB = metricLuminance(spec.metric, okhslToLinearSrgb(bOkhsl.h, bOkhsl.s, bOkhsl.l));
1819
+ warnContrastDrift(name, s.isDark, s.isHighContrast, spec, yC, yB);
1820
+ }
1066
1821
  }
1822
+ }
1823
+ function resolveAllColors(hue, saturation, defs, config, externalBases) {
1824
+ validateColorDefs(defs, externalBases);
1825
+ const order = topoSort(defs);
1826
+ const ctx = {
1827
+ hue,
1828
+ saturation,
1829
+ defs,
1830
+ resolved: /* @__PURE__ */ new Map(),
1831
+ config
1832
+ };
1833
+ if (externalBases) for (const [name, color] of externalBases) ctx.resolved.set(name, color);
1834
+ const lightMap = runPass(order, defs, ctx, false, false, "light");
1835
+ seedField(order, ctx, "lightContrast", lightMap);
1836
+ const lightHCMap = runPass(order, defs, ctx, false, true, "lightContrast");
1837
+ seedField(order, ctx, "dark", lightMap);
1838
+ seedField(order, ctx, "darkContrast", lightHCMap);
1839
+ const darkMap = runPass(order, defs, ctx, true, false, "dark");
1840
+ seedField(order, ctx, "darkContrast", darkMap);
1841
+ const darkHCMap = runPass(order, defs, ctx, true, true, "darkContrast");
1067
1842
  const result = /* @__PURE__ */ new Map();
1068
1843
  for (const name of order) result.set(name, {
1069
1844
  name,
@@ -1073,89 +1848,827 @@ function resolveAllColors(hue, saturation, defs) {
1073
1848
  darkContrast: darkHCMap.get(name),
1074
1849
  mode: defMode(defs[name])
1075
1850
  });
1076
- return result;
1851
+ verifyContrastDrift(order, defs, result);
1852
+ return result;
1853
+ }
1854
+
1855
+ //#endregion
1856
+ //#region src/formatters.ts
1857
+ /**
1858
+ * Output formatting for resolved color maps.
1859
+ *
1860
+ * Owns the CSS-string formatter dispatch table (`okhsl` / `rgb` / `hsl` /
1861
+ * `oklch`) and the four token-map shapes Glaze emits:
1862
+ * - `buildTokenMap` — Tasty style-to-state bindings (`#name` keys, state aliases).
1863
+ * - `buildFlatTokenMap` — `{ light, dark, ... }` per-variant maps.
1864
+ * - `buildJsonMap` — `{ name: { light, dark, ... } }` per-color JSON.
1865
+ * - `buildCssMap` — CSS custom property declaration strings per variant.
1866
+ */
1867
+ const formatters = {
1868
+ okhsl: formatOkhsl,
1869
+ rgb: formatRgb,
1870
+ hsl: formatHsl,
1871
+ oklch: formatOklch
1872
+ };
1873
+ function fmt(value, decimals) {
1874
+ return parseFloat(value.toFixed(decimals)).toString();
1875
+ }
1876
+ function formatVariant(v, format = "okhsl") {
1877
+ const { l } = variantToOkhsl(v);
1878
+ const base = formatters[format](v.h, v.s * 100, l * 100);
1879
+ if (v.alpha >= 1) return base;
1880
+ const closing = base.lastIndexOf(")");
1881
+ return `${base.slice(0, closing)} / ${fmt(v.alpha, 4)})`;
1882
+ }
1883
+ function resolveModes(override) {
1884
+ const cfg = getConfig();
1885
+ return {
1886
+ dark: override?.dark ?? cfg.modes.dark,
1887
+ highContrast: override?.highContrast ?? cfg.modes.highContrast
1888
+ };
1889
+ }
1890
+ function buildTokenMap(resolved, prefix, states, modes, format = "okhsl") {
1891
+ const tokens = {};
1892
+ for (const [name, color] of resolved) {
1893
+ const key = `#${prefix}${name}`;
1894
+ const entry = { "": formatVariant(color.light, format) };
1895
+ if (modes.dark) entry[states.dark] = formatVariant(color.dark, format);
1896
+ if (modes.highContrast) entry[states.highContrast] = formatVariant(color.lightContrast, format);
1897
+ if (modes.dark && modes.highContrast) entry[`${states.dark} & ${states.highContrast}`] = formatVariant(color.darkContrast, format);
1898
+ tokens[key] = entry;
1899
+ }
1900
+ return tokens;
1901
+ }
1902
+ function buildFlatTokenMap(resolved, prefix, modes, format = "okhsl") {
1903
+ const result = { light: {} };
1904
+ if (modes.dark) result.dark = {};
1905
+ if (modes.highContrast) result.lightContrast = {};
1906
+ if (modes.dark && modes.highContrast) result.darkContrast = {};
1907
+ for (const [name, color] of resolved) {
1908
+ const key = `${prefix}${name}`;
1909
+ result.light[key] = formatVariant(color.light, format);
1910
+ if (modes.dark) result.dark[key] = formatVariant(color.dark, format);
1911
+ if (modes.highContrast) result.lightContrast[key] = formatVariant(color.lightContrast, format);
1912
+ if (modes.dark && modes.highContrast) result.darkContrast[key] = formatVariant(color.darkContrast, format);
1913
+ }
1914
+ return result;
1915
+ }
1916
+ function buildJsonMap(resolved, modes, format = "okhsl") {
1917
+ const result = {};
1918
+ for (const [name, color] of resolved) {
1919
+ const entry = { light: formatVariant(color.light, format) };
1920
+ if (modes.dark) entry.dark = formatVariant(color.dark, format);
1921
+ if (modes.highContrast) entry.lightContrast = formatVariant(color.lightContrast, format);
1922
+ if (modes.dark && modes.highContrast) entry.darkContrast = formatVariant(color.darkContrast, format);
1923
+ result[name] = entry;
1924
+ }
1925
+ return result;
1926
+ }
1927
+ function buildCssMap(resolved, prefix, suffix, format) {
1928
+ const lines = {
1929
+ light: [],
1930
+ dark: [],
1931
+ lightContrast: [],
1932
+ darkContrast: []
1933
+ };
1934
+ for (const [name, color] of resolved) {
1935
+ const prop = `--${prefix}${name}${suffix}`;
1936
+ lines.light.push(`${prop}: ${formatVariant(color.light, format)};`);
1937
+ lines.dark.push(`${prop}: ${formatVariant(color.dark, format)};`);
1938
+ lines.lightContrast.push(`${prop}: ${formatVariant(color.lightContrast, format)};`);
1939
+ lines.darkContrast.push(`${prop}: ${formatVariant(color.darkContrast, format)};`);
1940
+ }
1941
+ return {
1942
+ light: lines.light.join("\n"),
1943
+ dark: lines.dark.join("\n"),
1944
+ lightContrast: lines.lightContrast.join("\n"),
1945
+ darkContrast: lines.darkContrast.join("\n")
1946
+ };
1947
+ }
1948
+
1949
+ //#endregion
1950
+ //#region src/color-token.ts
1951
+ /**
1952
+ * Standalone single-color tokens (`glaze.color()` / `glaze.colorFrom()`).
1953
+ *
1954
+ * Owns the value-shorthand parser (hex, `rgb()` / `hsl()` / `okhsl()` /
1955
+ * `okhst()` / `oklch()`, `{ r, g, b }`, `{ h, s, l }`, `{ h, s, t }`,
1956
+ * `{ l, c, h }`), the structured-input validator, the two factory paths
1957
+ * (value vs structured), and the JSON-safe export / rehydration round-trip.
1958
+ *
1959
+ * Standalone tokens snapshot the full effective config at create time
1960
+ * so later `configure()` calls do not retroactively change exported
1961
+ * tokens. The snapshot is built eagerly in
1962
+ * `buildValueFormConfigOverride()` / `buildStructuredConfigOverride()`.
1963
+ * The token's resolved variants are then memoized on first
1964
+ * `.resolve()` / `.token()` / ... call.
1965
+ */
1966
+ /** Internal name of the user-facing standalone color in the synthesized def map. */
1967
+ const STANDALONE_VALUE = "value";
1968
+ /** Internal name of the hidden static-anchor seed used for relative tone / contrast. */
1969
+ const STANDALONE_SEED = "seed";
1970
+ /** Internal name of an externally-resolved `GlazeColorToken` injected as a base reference. */
1971
+ const STANDALONE_BASE = "externalBase";
1972
+ /** Reserved internal names that user-supplied `name` must not collide with. */
1973
+ const RESERVED_STANDALONE_NAMES = new Set([
1974
+ STANDALONE_VALUE,
1975
+ STANDALONE_SEED,
1976
+ STANDALONE_BASE
1977
+ ]);
1978
+ /**
1979
+ * Build the per-token effective config override for a value-form color.
1980
+ *
1981
+ * Light window defaults to `false` (preserve input tone exactly).
1982
+ * All other fields snapshot from global at create time. User override
1983
+ * fields win over all defaults.
1984
+ */
1985
+ function buildValueFormConfigOverride(userOverride) {
1986
+ const cfg = getConfig();
1987
+ return {
1988
+ lightTone: userOverride?.lightTone !== void 0 ? userOverride.lightTone : false,
1989
+ darkTone: userOverride?.darkTone !== void 0 ? userOverride.darkTone : cfg.darkTone,
1990
+ darkDesaturation: userOverride?.darkDesaturation ?? cfg.darkDesaturation,
1991
+ saturationTaper: userOverride?.saturationTaper ?? cfg.saturationTaper,
1992
+ autoFlip: userOverride?.autoFlip ?? cfg.autoFlip,
1993
+ shadowTuning: userOverride?.shadowTuning ?? cfg.shadowTuning
1994
+ };
1995
+ }
1996
+ /**
1997
+ * Build the per-token effective config override for a structured-form color.
1998
+ *
1999
+ * Both light and dark windows snapshot from global at create time.
2000
+ * User override fields win.
2001
+ */
2002
+ function buildStructuredConfigOverride(userOverride) {
2003
+ const cfg = getConfig();
2004
+ return {
2005
+ lightTone: userOverride?.lightTone !== void 0 ? userOverride.lightTone : cfg.lightTone,
2006
+ darkTone: userOverride?.darkTone !== void 0 ? userOverride.darkTone : cfg.darkTone,
2007
+ darkDesaturation: userOverride?.darkDesaturation ?? cfg.darkDesaturation,
2008
+ saturationTaper: userOverride?.saturationTaper ?? cfg.saturationTaper,
2009
+ autoFlip: userOverride?.autoFlip ?? cfg.autoFlip,
2010
+ shadowTuning: userOverride?.shadowTuning ?? cfg.shadowTuning
2011
+ };
2012
+ }
2013
+ /**
2014
+ * Build the `GlazeConfigResolved` to pass to `resolveAllColors` from a
2015
+ * snapshot override. Uses `defaultConfig()` as the base so all required
2016
+ * fields are present; the snapshot fields win.
2017
+ */
2018
+ function resolvedConfigFromOverride(override) {
2019
+ return mergeConfig(defaultConfig(), override);
2020
+ }
2021
+ /**
2022
+ * Matches the CSS color functions Glaze itself emits (`rgb()`, `hsl()`,
2023
+ * `okhsl()`, `oklch()`) plus their legacy alpha aliases (`rgba()`, `hsla()`).
2024
+ *
2025
+ * Only bare numeric components are supported. Named colors (`red`),
2026
+ * relative-color syntax (`from <color> ...`), and angle units other
2027
+ * than bare degrees (`deg` is the only suffix tolerated by `parseFloat`)
2028
+ * are out of scope.
2029
+ */
2030
+ const COLOR_FN_RE = /^(rgba?|hsla?|okhsl|okhst|oklch)\(\s*([^)]*)\s*\)$/i;
2031
+ function parseNumberOrPercent(raw, percentScale) {
2032
+ if (raw.endsWith("%")) return parseFloat(raw) / 100 * percentScale;
2033
+ return parseFloat(raw);
2034
+ }
2035
+ /**
2036
+ * Split the body of a CSS color function into its components and detect
2037
+ * whether an alpha channel was present.
2038
+ *
2039
+ * Handles both modern slash syntax (`R G B / A` or `R, G, B / A`) and
2040
+ * legacy comma syntax (`R, G, B, A`). The alpha value itself is discarded
2041
+ * by the caller — standalone Glaze colors have no opacity field.
2042
+ */
2043
+ function splitColorBody(body) {
2044
+ const slashIdx = body.indexOf("/");
2045
+ if (slashIdx !== -1) return {
2046
+ components: body.slice(0, slashIdx).trim().split(/[\s,]+/).filter(Boolean),
2047
+ hadAlpha: body.slice(slashIdx + 1).trim().length > 0
2048
+ };
2049
+ const components = body.split(/[\s,]+/).filter(Boolean);
2050
+ if (components.length === 4) {
2051
+ components.pop();
2052
+ return {
2053
+ components,
2054
+ hadAlpha: true
2055
+ };
2056
+ }
2057
+ return {
2058
+ components,
2059
+ hadAlpha: false
2060
+ };
2061
+ }
2062
+ function warnDroppedAlpha(input) {
2063
+ console.warn(`glaze: alpha component dropped from "${input}" (standalone color has no opacity field).`);
2064
+ }
2065
+ function parseColorString(input) {
2066
+ if (input.startsWith("#")) {
2067
+ const parsed = parseHexAlpha(input);
2068
+ if (!parsed) throw new Error(`glaze: invalid hex color "${input}".`);
2069
+ if (parsed.alpha !== void 0) warnDroppedAlpha(input);
2070
+ const [h, s, l] = srgbToOkhsl(parsed.rgb);
2071
+ return {
2072
+ h,
2073
+ s,
2074
+ l
2075
+ };
2076
+ }
2077
+ const m = input.match(COLOR_FN_RE);
2078
+ if (!m) throw new Error(`glaze: unsupported color string "${input}".`);
2079
+ const fn = m[1].toLowerCase();
2080
+ const { components, hadAlpha } = splitColorBody(m[2].trim());
2081
+ if (hadAlpha) warnDroppedAlpha(input);
2082
+ if (components.length !== 3) throw new Error(`glaze: expected 3 components in "${input}".`);
2083
+ switch (fn) {
2084
+ case "rgb":
2085
+ case "rgba": {
2086
+ const [h, s, l] = srgbToOkhsl([
2087
+ parseNumberOrPercent(components[0], 255) / 255,
2088
+ parseNumberOrPercent(components[1], 255) / 255,
2089
+ parseNumberOrPercent(components[2], 255) / 255
2090
+ ]);
2091
+ return {
2092
+ h,
2093
+ s,
2094
+ l
2095
+ };
2096
+ }
2097
+ case "hsl":
2098
+ case "hsla": {
2099
+ const [oh, os, ol] = srgbToOkhsl(hslToSrgb(parseFloat(components[0]), parseNumberOrPercent(components[1], 1), parseNumberOrPercent(components[2], 1)));
2100
+ return {
2101
+ h: oh,
2102
+ s: os,
2103
+ l: ol
2104
+ };
2105
+ }
2106
+ case "okhsl": return {
2107
+ h: parseFloat(components[0]),
2108
+ s: parseNumberOrPercent(components[1], 1),
2109
+ l: parseNumberOrPercent(components[2], 1)
2110
+ };
2111
+ case "okhst": return okhstToOkhsl({
2112
+ h: parseFloat(components[0]),
2113
+ s: parseNumberOrPercent(components[1], 1),
2114
+ t: parseNumberOrPercent(components[2], 1)
2115
+ });
2116
+ case "oklch": {
2117
+ const L = parseNumberOrPercent(components[0], 1);
2118
+ const C = parseNumberOrPercent(components[1], .4);
2119
+ const hRad = parseFloat(components[2]) * Math.PI / 180;
2120
+ const [h, s, l] = oklabToOkhsl([
2121
+ L,
2122
+ C * Math.cos(hRad),
2123
+ C * Math.sin(hRad)
2124
+ ]);
2125
+ return {
2126
+ h,
2127
+ s,
2128
+ l
2129
+ };
2130
+ }
2131
+ }
2132
+ throw new Error(`glaze: unsupported color function "${fn}".`);
2133
+ }
2134
+ /**
2135
+ * Validate a user-supplied `OkhslColor`. Catches the common 0-100 vs 0-1
2136
+ * confusion (the structured form uses 0-100, OKHSL objects use 0-1).
2137
+ */
2138
+ function validateOkhslColor(value) {
2139
+ const { h, s, l } = value;
2140
+ if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) throw new Error("glaze.color: OkhslColor h/s/l must be finite numbers.");
2141
+ 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)?");
2142
+ }
2143
+ /** Validate a user-supplied `{ r, g, b }` object in 0–255. */
2144
+ function validateRgbColor(value) {
2145
+ for (const key of [
2146
+ "r",
2147
+ "g",
2148
+ "b"
2149
+ ]) {
2150
+ const n = value[key];
2151
+ 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}).`);
2152
+ }
2153
+ }
2154
+ /** Validate a user-supplied `{ l, c, h }` OKLCh object. */
2155
+ function validateOklchColor(value) {
2156
+ const { l, c, h } = value;
2157
+ if (!Number.isFinite(l) || !Number.isFinite(c) || !Number.isFinite(h)) throw new Error("glaze.color: OklchColor l/c/h must be finite numbers.");
2158
+ if (l > 1.5 || c > 1.5) throw new Error("glaze.color: OklchColor l/c must be in 0–1 range (matching oklch() strings).");
2159
+ }
2160
+ function oklchComponentsToOkhsl(l, c, hDeg) {
2161
+ const hRad = hDeg * Math.PI / 180;
2162
+ const [h, s, outL] = oklabToOkhsl([
2163
+ l,
2164
+ c * Math.cos(hRad),
2165
+ c * Math.sin(hRad)
2166
+ ]);
2167
+ return {
2168
+ h,
2169
+ s,
2170
+ l: outL
2171
+ };
2172
+ }
2173
+ function isRgbColorObject(value) {
2174
+ return "r" in value && "g" in value && "b" in value;
2175
+ }
2176
+ function isOklchColorObject(value) {
2177
+ return "c" in value && "l" in value && "h" in value;
2178
+ }
2179
+ function isOkhstColorObject(value) {
2180
+ return "t" in value && "h" in value && "s" in value;
2181
+ }
2182
+ /** Validate a user-supplied `{ h, s, t }` OKHST object (s/t in 0–1). */
2183
+ function validateOkhstColor(value) {
2184
+ const { h, s, t } = value;
2185
+ if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(t)) throw new Error("glaze.color: OkhstColor h/s/t must be finite numbers.");
2186
+ 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)?");
2187
+ }
2188
+ /**
2189
+ * Validate a user-supplied `opacity` override on `glaze.color()`.
2190
+ * Must be a finite number in `0..=1`.
2191
+ */
2192
+ function validateStandaloneOpacity(value) {
2193
+ if (!Number.isFinite(value) || value < 0 || value > 1) throw new Error(`glaze.color: opacity must be a finite number in 0–1 (got ${value}).`);
2194
+ }
2195
+ /**
2196
+ * Validate a structured `GlazeColorInput`. Range-checks the `hue` /
2197
+ * `saturation` / `tone` numerics (and any HC-pair second value)
2198
+ * before the resolver sees them so out-of-range or non-finite inputs
2199
+ * fail with a helpful, top-level error rather than producing a
2200
+ * NaN-laden token. `opacity` is checked here too so all input
2201
+ * validation lives in one place.
2202
+ */
2203
+ function validateStructuredInput(input) {
2204
+ if (!Number.isFinite(input.hue)) throw new Error(`glaze.color: structured hue must be a finite number (got ${input.hue}).`);
2205
+ 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}).`);
2206
+ const checkTone = (value, label) => {
2207
+ if (value === "max" || value === "min") return;
2208
+ 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)}).`);
2209
+ };
2210
+ if (Array.isArray(input.tone)) {
2211
+ checkTone(input.tone[0], "tone[normal]");
2212
+ checkTone(input.tone[1], "tone[hc]");
2213
+ } else checkTone(input.tone, "tone");
2214
+ if (input.saturationFactor !== void 0) {
2215
+ 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}).`);
2216
+ }
2217
+ if (input.opacity !== void 0) validateStandaloneOpacity(input.opacity);
2218
+ }
2219
+ /**
2220
+ * Validate a user-supplied `name` override. Rejects empty / whitespace-only
2221
+ * strings and names colliding with `glaze`'s reserved internal sentinels.
2222
+ */
2223
+ function validateStandaloneName(name) {
2224
+ 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.");
2225
+ if (RESERVED_STANDALONE_NAMES.has(name)) {
2226
+ const reserved = [...RESERVED_STANDALONE_NAMES].map((n) => `"${n}"`).join(", ");
2227
+ throw new Error(`glaze.color: name "${name}" is reserved (used internally). Reserved names are: ${reserved}. Pick a different name.`);
2228
+ }
2229
+ }
2230
+ /**
2231
+ * Extract an OKHSL color from any `GlazeColorValue` form. Also used by
2232
+ * `glaze.shadow()` so all shadow inputs (hex, color functions, OKHSL,
2233
+ * literal objects) go through one parser.
2234
+ */
2235
+ function extractOkhslFromValue(value) {
2236
+ if (typeof value === "string") return parseColorString(value);
2237
+ if (Array.isArray(value)) throw new Error("glaze.color: RGB tuple [r, g, b] is no longer supported — use { r, g, b } instead.");
2238
+ if (isRgbColorObject(value)) {
2239
+ validateRgbColor(value);
2240
+ const [h, s, l] = srgbToOkhsl([
2241
+ value.r / 255,
2242
+ value.g / 255,
2243
+ value.b / 255
2244
+ ]);
2245
+ return {
2246
+ h,
2247
+ s,
2248
+ l
2249
+ };
2250
+ }
2251
+ if (isOklchColorObject(value)) {
2252
+ validateOklchColor(value);
2253
+ return oklchComponentsToOkhsl(value.l, value.c, value.h);
2254
+ }
2255
+ if (isOkhstColorObject(value)) {
2256
+ validateOkhstColor(value);
2257
+ return okhstToOkhsl(value);
2258
+ }
2259
+ validateOkhslColor(value);
2260
+ return value;
2261
+ }
2262
+ /**
2263
+ * Build the `ColorMap` for a value-shorthand `glaze.color()` call.
2264
+ *
2265
+ * The user-facing color (`STANDALONE_VALUE`) defaults to `mode: 'auto'`
2266
+ * across every value-shorthand form.
2267
+ *
2268
+ * When the user requests `contrast` or relative `tone`, a hidden
2269
+ * `STANDALONE_SEED` def is synthesized at `mode: 'static'`. That keeps
2270
+ * the seed pinned to the literal user-provided color across all four
2271
+ * variants, so the contrast solver always anchors against it.
2272
+ */
2273
+ function buildStandaloneValueDefs(main, options) {
2274
+ const seedHue = typeof options?.hue === "number" ? options.hue : main.h;
2275
+ const seedSaturation = options?.saturation ?? main.s * 100;
2276
+ const relativeHue = typeof options?.hue === "string" ? options.hue : void 0;
2277
+ const toneOption = options?.tone;
2278
+ const hasExternalBase = options?.base !== void 0;
2279
+ const needsSeedAnchor = !hasExternalBase && (options?.contrast !== void 0 || toneOption !== void 0 && !isAbsoluteTone(toneOption));
2280
+ if (options?.opacity !== void 0) validateStandaloneOpacity(options.opacity);
2281
+ const userName = options?.name;
2282
+ if (userName !== void 0) validateStandaloneName(userName);
2283
+ const primary = userName ?? STANDALONE_VALUE;
2284
+ const seedTone = toTone(main.l);
2285
+ const valueDef = {
2286
+ hue: relativeHue,
2287
+ saturation: options?.saturationFactor,
2288
+ tone: toneOption ?? seedTone,
2289
+ contrast: options?.contrast,
2290
+ mode: options?.mode ?? "auto",
2291
+ flip: options?.flip,
2292
+ opacity: options?.opacity,
2293
+ base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
2294
+ };
2295
+ const defs = { [primary]: valueDef };
2296
+ if (needsSeedAnchor) defs[STANDALONE_SEED] = {
2297
+ hue: main.h,
2298
+ saturation: 1,
2299
+ tone: seedTone,
2300
+ mode: "static"
2301
+ };
2302
+ return {
2303
+ seedHue,
2304
+ seedSaturation,
2305
+ defs,
2306
+ primary
2307
+ };
2308
+ }
2309
+ function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveConfig, baseToken, exportData) {
2310
+ let cached;
2311
+ const resolveOnce = () => {
2312
+ if (cached) return cached;
2313
+ cached = resolveAllColors(seedHue, seedSaturation, defs, effectiveConfig, baseToken ? new Map([[STANDALONE_BASE, baseToken.resolve()]]) : void 0);
2314
+ return cached;
2315
+ };
2316
+ const resolveStates = (options) => {
2317
+ const cfg = getConfig();
2318
+ return {
2319
+ dark: options?.states?.dark ?? cfg.states.dark,
2320
+ highContrast: options?.states?.highContrast ?? cfg.states.highContrast
2321
+ };
2322
+ };
2323
+ const tokenLike = (options) => {
2324
+ return buildTokenMap(resolveOnce(), "", resolveStates(options), resolveModes(options?.modes), options?.format)[`#${primary}`];
2325
+ };
2326
+ return {
2327
+ resolve() {
2328
+ return resolveOnce().get(primary);
2329
+ },
2330
+ token: tokenLike,
2331
+ tasty: tokenLike,
2332
+ json(options) {
2333
+ return buildJsonMap(resolveOnce(), resolveModes(options?.modes), options?.format)[primary];
2334
+ },
2335
+ css(options) {
2336
+ return buildCssMap(new Map([[options.name, resolveOnce().get(primary)]]), "", options.suffix ?? "-color", options.format ?? "rgb");
2337
+ },
2338
+ export: exportData
2339
+ };
2340
+ }
2341
+ /**
2342
+ * When a value/`from` color links to a base that was created via the
2343
+ * structured form (with explicit `hue`/`saturation`/`tone`), resolve
2344
+ * that base with `lightTone: false` for the linking math so the
2345
+ * contrast/tone anchor matches the input tone — not the
2346
+ * windowed output. The original base token's `.resolve()` is unaffected.
2347
+ */
2348
+ function toLinkingBase(base) {
2349
+ if (!base) return void 0;
2350
+ const exp = base.export();
2351
+ if (exp.form !== "structured") return base;
2352
+ const linkingConfig = {
2353
+ ...exp.config ?? {},
2354
+ lightTone: false
2355
+ };
2356
+ return colorFromExport({
2357
+ ...exp,
2358
+ config: linkingConfig
2359
+ });
2360
+ }
2361
+ /**
2362
+ * Resolve `base` (which may be a token reference or a raw color value)
2363
+ * into a `GlazeColorToken`. Raw values are auto-wrapped via
2364
+ * `createColorTokenFromValue` so they pick up the same auto-invert
2365
+ * defaults as an explicit wrap. Returns `undefined` when no base is provided.
2366
+ */
2367
+ function resolveBaseToken(base) {
2368
+ if (base === void 0) return void 0;
2369
+ if (isGlazeColorToken(base)) return base;
2370
+ return createColorTokenFromValue(base, void 0, void 0);
2371
+ }
2372
+ /**
2373
+ * Discriminate a `GlazeColorToken` from a raw `GlazeColorValue`.
2374
+ */
2375
+ function isGlazeColorToken(candidate) {
2376
+ return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "resolve" in candidate && typeof candidate.resolve === "function";
2377
+ }
2378
+ function createColorToken(input, configOverride) {
2379
+ validateStructuredInput(input);
2380
+ const userName = input.name;
2381
+ if (userName !== void 0) validateStandaloneName(userName);
2382
+ const primary = userName ?? STANDALONE_VALUE;
2383
+ const baseToken = resolveBaseToken(input.base);
2384
+ const hasExternalBase = baseToken !== void 0;
2385
+ const needsSeedAnchor = !hasExternalBase && input.contrast !== void 0;
2386
+ const defs = { [primary]: {
2387
+ tone: input.tone,
2388
+ saturation: input.saturationFactor,
2389
+ mode: input.mode ?? "auto",
2390
+ flip: input.flip,
2391
+ contrast: input.contrast,
2392
+ opacity: input.opacity,
2393
+ base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
2394
+ } };
2395
+ if (needsSeedAnchor) {
2396
+ const seedTone = pairNormal(input.tone);
2397
+ defs[STANDALONE_SEED] = {
2398
+ tone: seedTone === "max" ? 100 : seedTone === "min" ? 0 : seedTone,
2399
+ saturation: 1,
2400
+ mode: "static"
2401
+ };
2402
+ }
2403
+ const effectiveConfigOverride = buildStructuredConfigOverride(configOverride);
2404
+ const effectiveConfig = resolvedConfigFromOverride(effectiveConfigOverride);
2405
+ const exportData = () => ({
2406
+ form: "structured",
2407
+ input: buildStructuredInputExport(input),
2408
+ config: effectiveConfigOverride
2409
+ });
2410
+ return createColorTokenFromDefs(input.hue, input.saturation, defs, primary, effectiveConfig, baseToken, exportData);
1077
2411
  }
1078
- const formatters = {
1079
- okhsl: formatOkhsl,
1080
- rgb: formatRgb,
1081
- hsl: formatHsl,
1082
- oklch: formatOklch
1083
- };
1084
- function fmt(value, decimals) {
1085
- return parseFloat(value.toFixed(decimals)).toString();
2412
+ function createColorTokenFromValue(value, options, configOverride) {
2413
+ const main = extractOkhslFromValue(value);
2414
+ const linkingBase = toLinkingBase(resolveBaseToken(options?.base));
2415
+ const { seedHue, seedSaturation, defs, primary } = buildStandaloneValueDefs(main, options);
2416
+ const effectiveConfigOverride = buildValueFormConfigOverride(configOverride);
2417
+ const effectiveConfig = resolvedConfigFromOverride(effectiveConfigOverride);
2418
+ const exportData = () => ({
2419
+ form: "value",
2420
+ input: value,
2421
+ ...options !== void 0 ? { overrides: buildOverridesExport(options) } : {},
2422
+ config: effectiveConfigOverride
2423
+ });
2424
+ return createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveConfig, linkingBase, exportData);
1086
2425
  }
1087
- function formatVariant(v, format = "okhsl") {
1088
- const base = formatters[format](v.h, v.s * 100, v.l * 100);
1089
- if (v.alpha >= 1) return base;
1090
- const closing = base.lastIndexOf(")");
1091
- return `${base.slice(0, closing)} / ${fmt(v.alpha, 4)})`;
2426
+ /**
2427
+ * Build a JSON-safe snapshot of `GlazeColorOverrides`. `base` is
2428
+ * recursively serialized when it was originally a token; raw values are
2429
+ * preserved as-is so `glaze.colorFrom(...)` round-trips them.
2430
+ */
2431
+ function buildOverridesExport(options) {
2432
+ const out = {};
2433
+ if (options.hue !== void 0) out.hue = options.hue;
2434
+ if (options.saturation !== void 0) out.saturation = options.saturation;
2435
+ if (options.tone !== void 0) out.tone = options.tone;
2436
+ if (options.saturationFactor !== void 0) out.saturationFactor = options.saturationFactor;
2437
+ if (options.mode !== void 0) out.mode = options.mode;
2438
+ if (options.flip !== void 0) out.flip = options.flip;
2439
+ if (options.contrast !== void 0) out.contrast = options.contrast;
2440
+ if (options.opacity !== void 0) out.opacity = options.opacity;
2441
+ if (options.name !== void 0) out.name = options.name;
2442
+ if (options.base !== void 0) out.base = isGlazeColorToken(options.base) ? options.base.export() : options.base;
2443
+ return out;
1092
2444
  }
1093
- function resolveModes(override) {
1094
- return {
1095
- dark: override?.dark ?? globalConfig.modes.dark,
1096
- highContrast: override?.highContrast ?? globalConfig.modes.highContrast
2445
+ function buildStructuredInputExport(input) {
2446
+ const out = {
2447
+ hue: input.hue,
2448
+ saturation: input.saturation,
2449
+ tone: input.tone
1097
2450
  };
2451
+ if (input.saturationFactor !== void 0) out.saturationFactor = input.saturationFactor;
2452
+ if (input.mode !== void 0) out.mode = input.mode;
2453
+ if (input.flip !== void 0) out.flip = input.flip;
2454
+ if (input.opacity !== void 0) out.opacity = input.opacity;
2455
+ if (input.contrast !== void 0) out.contrast = input.contrast;
2456
+ if (input.name !== void 0) out.name = input.name;
2457
+ if (input.base !== void 0) out.base = isGlazeColorToken(input.base) ? input.base.export() : input.base;
2458
+ return out;
1098
2459
  }
1099
- function buildTokenMap(resolved, prefix, states, modes, format = "okhsl") {
1100
- const tokens = {};
1101
- for (const [name, color] of resolved) {
1102
- const key = `#${prefix}${name}`;
1103
- const entry = { "": formatVariant(color.light, format) };
1104
- if (modes.dark) entry[states.dark] = formatVariant(color.dark, format);
1105
- if (modes.highContrast) entry[states.highContrast] = formatVariant(color.lightContrast, format);
1106
- if (modes.dark && modes.highContrast) entry[`${states.dark} & ${states.highContrast}`] = formatVariant(color.darkContrast, format);
1107
- tokens[key] = entry;
2460
+ /**
2461
+ * Discriminate a `GlazeColorTokenExport` from a raw `GlazeColorValue`.
2462
+ */
2463
+ function isExportedToken(candidate) {
2464
+ return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "form" in candidate && (candidate.form === "value" || candidate.form === "structured");
2465
+ }
2466
+ function rehydrateOverrides(data) {
2467
+ const out = {};
2468
+ if (data.hue !== void 0) out.hue = data.hue;
2469
+ if (data.saturation !== void 0) out.saturation = data.saturation;
2470
+ if (data.tone !== void 0) out.tone = data.tone;
2471
+ if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
2472
+ if (data.mode !== void 0) out.mode = data.mode;
2473
+ if (data.flip !== void 0) out.flip = data.flip;
2474
+ if (data.contrast !== void 0) out.contrast = data.contrast;
2475
+ if (data.opacity !== void 0) out.opacity = data.opacity;
2476
+ if (data.name !== void 0) out.name = data.name;
2477
+ if (data.base !== void 0) out.base = isExportedToken(data.base) ? colorFromExport(data.base) : data.base;
2478
+ return out;
2479
+ }
2480
+ function rehydrateStructuredInput(data) {
2481
+ const out = {
2482
+ hue: data.hue,
2483
+ saturation: data.saturation,
2484
+ tone: data.tone
2485
+ };
2486
+ if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
2487
+ if (data.mode !== void 0) out.mode = data.mode;
2488
+ if (data.flip !== void 0) out.flip = data.flip;
2489
+ if (data.opacity !== void 0) out.opacity = data.opacity;
2490
+ if (data.contrast !== void 0) out.contrast = data.contrast;
2491
+ if (data.name !== void 0) out.name = data.name;
2492
+ if (data.base !== void 0) out.base = isExportedToken(data.base) ? colorFromExport(data.base) : data.base;
2493
+ return out;
2494
+ }
2495
+ /**
2496
+ * Rehydrate a token from its `.export()` snapshot. Recursively rebuilds
2497
+ * any base dependency. Inverse of `GlazeColorToken.export()`.
2498
+ *
2499
+ * The stored `config` field contains the full effective config override
2500
+ * snapshotted at creation time, so the rehydrated token is deterministic
2501
+ * regardless of subsequent `glaze.configure()` calls.
2502
+ */
2503
+ function colorFromExport(data) {
2504
+ if (data === null || typeof data !== "object") throw new Error(`glaze.colorFrom: expected an object from token.export(), got ${data === null ? "null" : typeof data}.`);
2505
+ if (data.form !== "value" && data.form !== "structured") throw new Error(`glaze.colorFrom: invalid "form" field — expected "value" or "structured" (got ${JSON.stringify(data.form)}).`);
2506
+ if (data.input === void 0) throw new Error(`glaze.colorFrom: missing "input" field — expected the original ${data.form === "value" ? "GlazeColorValue" : "GlazeColorInput"}.`);
2507
+ if (data.form === "value") {
2508
+ const value = data.input;
2509
+ return createColorTokenFromValue(value, data.overrides ? rehydrateOverrides(data.overrides) : void 0, data.config);
1108
2510
  }
1109
- return tokens;
2511
+ return createColorToken(rehydrateStructuredInput(data.input), data.config);
1110
2512
  }
1111
- function buildFlatTokenMap(resolved, prefix, modes, format = "okhsl") {
1112
- const result = { light: {} };
1113
- if (modes.dark) result.dark = {};
1114
- if (modes.highContrast) result.lightContrast = {};
1115
- if (modes.dark && modes.highContrast) result.darkContrast = {};
1116
- for (const [name, color] of resolved) {
1117
- const key = `${prefix}${name}`;
1118
- result.light[key] = formatVariant(color.light, format);
1119
- if (modes.dark) result.dark[key] = formatVariant(color.dark, format);
1120
- if (modes.highContrast) result.lightContrast[key] = formatVariant(color.lightContrast, format);
1121
- if (modes.dark && modes.highContrast) result.darkContrast[key] = formatVariant(color.darkContrast, format);
2513
+
2514
+ //#endregion
2515
+ //#region src/palette.ts
2516
+ /**
2517
+ * Palette factory.
2518
+ *
2519
+ * Composes multiple themes into a single token namespace with optional
2520
+ * theme-name prefixes and a "primary theme" that also surfaces an
2521
+ * unprefixed copy of its tokens. All four export methods (`tokens` /
2522
+ * `tasty` / `json` / `css`) share a `buildPaletteOutput` driver that
2523
+ * handles validation, per-theme iteration, prefix resolution, collision
2524
+ * filtering, and primary duplication.
2525
+ */
2526
+ function resolvePrefix(options, themeName, defaultPrefix = false) {
2527
+ const prefix = options?.prefix ?? defaultPrefix;
2528
+ if (prefix === true) return `${themeName}-`;
2529
+ if (typeof prefix === "object" && prefix !== null) return prefix[themeName] ?? `${themeName}-`;
2530
+ return "";
2531
+ }
2532
+ function validatePrimaryTheme(primary, themes) {
2533
+ if (primary !== void 0 && !(primary in themes)) {
2534
+ const available = Object.keys(themes).join(", ");
2535
+ throw new Error(`glaze: primary theme "${primary}" not found in palette. Available: ${available}.`);
1122
2536
  }
1123
- return result;
1124
2537
  }
1125
- function buildJsonMap(resolved, modes, format = "okhsl") {
1126
- const result = {};
2538
+ /**
2539
+ * Resolve the effective primary for an export call.
2540
+ * `false` disables, a string overrides, `undefined` inherits from palette.
2541
+ */
2542
+ function resolveEffectivePrimary(exportPrimary, palettePrimary) {
2543
+ if (exportPrimary === false) return void 0;
2544
+ return exportPrimary ?? palettePrimary;
2545
+ }
2546
+ /**
2547
+ * Filter a resolved color map, skipping keys already in `seen`.
2548
+ * Warns on collision and keeps the first-written value (first-write-wins).
2549
+ * Returns a new map containing only non-colliding entries.
2550
+ */
2551
+ function filterCollisions(resolved, prefix, seen, themeName, isPrimary) {
2552
+ const filtered = /* @__PURE__ */ new Map();
2553
+ const label = isPrimary ? `${themeName} (primary)` : themeName;
1127
2554
  for (const [name, color] of resolved) {
1128
- const entry = { light: formatVariant(color.light, format) };
1129
- if (modes.dark) entry.dark = formatVariant(color.dark, format);
1130
- if (modes.highContrast) entry.lightContrast = formatVariant(color.lightContrast, format);
1131
- if (modes.dark && modes.highContrast) entry.darkContrast = formatVariant(color.darkContrast, format);
1132
- result[name] = entry;
2555
+ const key = `${prefix}${name}`;
2556
+ if (seen.has(key)) {
2557
+ console.warn(`glaze: token "${key}" from theme "${label}" collides with theme "${seen.get(key)}" — skipping.`);
2558
+ continue;
2559
+ }
2560
+ seen.set(key, label);
2561
+ filtered.set(name, color);
1133
2562
  }
1134
- return result;
2563
+ return filtered;
1135
2564
  }
1136
- function buildCssMap(resolved, prefix, suffix, format) {
1137
- const lines = {
1138
- light: [],
1139
- dark: [],
1140
- lightContrast: [],
1141
- darkContrast: []
1142
- };
1143
- for (const [name, color] of resolved) {
1144
- const prop = `--${prefix}${name}${suffix}`;
1145
- lines.light.push(`${prop}: ${formatVariant(color.light, format)};`);
1146
- lines.dark.push(`${prop}: ${formatVariant(color.dark, format)};`);
1147
- lines.lightContrast.push(`${prop}: ${formatVariant(color.lightContrast, format)};`);
1148
- lines.darkContrast.push(`${prop}: ${formatVariant(color.darkContrast, format)};`);
2565
+ /**
2566
+ * Shared per-theme driver for `tokens` / `tasty` / `css`. `json` skips
2567
+ * this because it doesn't do collision filtering or primary duplication.
2568
+ */
2569
+ function buildPaletteOutput(themes, paletteOptions, options, buildOne, merge, empty) {
2570
+ const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
2571
+ if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
2572
+ const acc = empty();
2573
+ const seen = /* @__PURE__ */ new Map();
2574
+ for (const [themeName, theme] of Object.entries(themes)) {
2575
+ const resolved = theme.resolve();
2576
+ const prefix = resolvePrefix(options, themeName, true);
2577
+ merge(acc, buildOne(filterCollisions(resolved, prefix, seen, themeName), prefix));
2578
+ if (themeName === effectivePrimary) merge(acc, buildOne(filterCollisions(resolved, "", seen, themeName, true), ""));
1149
2579
  }
2580
+ return acc;
2581
+ }
2582
+ function createPalette(themes, paletteOptions) {
2583
+ validatePrimaryTheme(paletteOptions?.primary, themes);
1150
2584
  return {
1151
- light: lines.light.join("\n"),
1152
- dark: lines.dark.join("\n"),
1153
- lightContrast: lines.lightContrast.join("\n"),
1154
- darkContrast: lines.darkContrast.join("\n")
2585
+ tokens(options) {
2586
+ const modes = resolveModes(options?.modes);
2587
+ return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix) => buildFlatTokenMap(filtered, prefix, modes, options?.format), (acc, part) => {
2588
+ for (const variant of Object.keys(part)) {
2589
+ if (!acc[variant]) acc[variant] = {};
2590
+ Object.assign(acc[variant], part[variant]);
2591
+ }
2592
+ }, () => ({}));
2593
+ },
2594
+ tasty(options) {
2595
+ const cfg = getConfig();
2596
+ const states = {
2597
+ dark: options?.states?.dark ?? cfg.states.dark,
2598
+ highContrast: options?.states?.highContrast ?? cfg.states.highContrast
2599
+ };
2600
+ const modes = resolveModes(options?.modes);
2601
+ return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix) => buildTokenMap(filtered, prefix, states, modes, options?.format), (acc, part) => Object.assign(acc, part), () => ({}));
2602
+ },
2603
+ json(options) {
2604
+ const modes = resolveModes(options?.modes);
2605
+ const result = {};
2606
+ for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes, options?.format);
2607
+ return result;
2608
+ },
2609
+ css(options) {
2610
+ const suffix = options?.suffix ?? "-color";
2611
+ const format = options?.format ?? "rgb";
2612
+ const lines = buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix) => buildCssMap(filtered, prefix, suffix, format), (acc, part) => {
2613
+ for (const key of [
2614
+ "light",
2615
+ "dark",
2616
+ "lightContrast",
2617
+ "darkContrast"
2618
+ ]) if (part[key]) acc[key].push(part[key]);
2619
+ }, () => ({
2620
+ light: [],
2621
+ dark: [],
2622
+ lightContrast: [],
2623
+ darkContrast: []
2624
+ }));
2625
+ return {
2626
+ light: lines.light.join("\n"),
2627
+ dark: lines.dark.join("\n"),
2628
+ lightContrast: lines.lightContrast.join("\n"),
2629
+ darkContrast: lines.darkContrast.join("\n")
2630
+ };
2631
+ }
1155
2632
  };
1156
2633
  }
1157
- function createTheme(hue, saturation, initialColors) {
2634
+
2635
+ //#endregion
2636
+ //#region src/theme.ts
2637
+ /**
2638
+ * Theme factory.
2639
+ *
2640
+ * Wraps a hue/saturation seed, a mutable `ColorMap`, and an optional
2641
+ * per-theme `GlazeConfigOverride`. Exposes `tokens()` / `tasty()` /
2642
+ * `json()` / `css()` / `resolve()` / `export()` / `extend()`.
2643
+ *
2644
+ * The per-theme config override is **merged over the live global config at
2645
+ * resolve time** so the theme still reacts to later `configure()` calls
2646
+ * for fields it didn't override. The merged config is memoized by
2647
+ * `configVersion` to avoid rebuilding it on every export call.
2648
+ */
2649
+ function createTheme(hue, saturation, initialColors, configOverride) {
1158
2650
  let colorDefs = initialColors ? { ...initialColors } : {};
2651
+ let cache = null;
2652
+ function getEffectiveConfig() {
2653
+ const version = getConfigVersion();
2654
+ if (cache && cache.version === version) return cache.effectiveConfig;
2655
+ return mergeConfig(getConfig(), configOverride);
2656
+ }
2657
+ function resolveCached() {
2658
+ const version = getConfigVersion();
2659
+ if (cache && cache.version === version) return cache.map;
2660
+ const effectiveConfig = mergeConfig(getConfig(), configOverride);
2661
+ const map = resolveAllColors(hue, saturation, colorDefs, effectiveConfig);
2662
+ cache = {
2663
+ map,
2664
+ version,
2665
+ effectiveConfig
2666
+ };
2667
+ return map;
2668
+ }
2669
+ function invalidate() {
2670
+ cache = null;
2671
+ }
1159
2672
  return {
1160
2673
  get hue() {
1161
2674
  return hue;
@@ -1168,14 +2681,17 @@ function createTheme(hue, saturation, initialColors) {
1168
2681
  ...colorDefs,
1169
2682
  ...defs
1170
2683
  };
2684
+ invalidate();
1171
2685
  },
1172
2686
  color(name, def) {
1173
2687
  if (def === void 0) return colorDefs[name];
1174
2688
  colorDefs[name] = def;
2689
+ invalidate();
1175
2690
  },
1176
2691
  remove(names) {
1177
2692
  const list = Array.isArray(names) ? names : [names];
1178
2693
  for (const name of list) delete colorDefs[name];
2694
+ invalidate();
1179
2695
  },
1180
2696
  has(name) {
1181
2697
  return name in colorDefs;
@@ -1185,217 +2701,191 @@ function createTheme(hue, saturation, initialColors) {
1185
2701
  },
1186
2702
  reset() {
1187
2703
  colorDefs = {};
2704
+ invalidate();
1188
2705
  },
1189
2706
  export() {
1190
- return {
2707
+ const out = {
1191
2708
  hue,
1192
2709
  saturation,
1193
2710
  colors: { ...colorDefs }
1194
2711
  };
2712
+ if (configOverride !== void 0) out.config = configOverride;
2713
+ return out;
1195
2714
  },
1196
2715
  extend(options) {
1197
- return createTheme(options.hue ?? hue, options.saturation ?? saturation, options.colors ? {
1198
- ...colorDefs,
2716
+ const newHue = options.hue ?? hue;
2717
+ const newSat = options.saturation ?? saturation;
2718
+ const inheritedColors = {};
2719
+ for (const [name, def] of Object.entries(colorDefs)) if (def.inherit !== false) inheritedColors[name] = def;
2720
+ return createTheme(newHue, newSat, options.colors ? {
2721
+ ...inheritedColors,
1199
2722
  ...options.colors
1200
- } : { ...colorDefs });
2723
+ } : { ...inheritedColors }, configOverride || options.config ? {
2724
+ ...configOverride ?? {},
2725
+ ...options.config ?? {}
2726
+ } : void 0);
1201
2727
  },
1202
2728
  resolve() {
1203
- return resolveAllColors(hue, saturation, colorDefs);
1204
- },
1205
- tokens(options) {
1206
- return buildFlatTokenMap(resolveAllColors(hue, saturation, colorDefs), "", resolveModes(options?.modes), options?.format);
1207
- },
1208
- tasty(options) {
1209
- return buildTokenMap(resolveAllColors(hue, saturation, colorDefs), "", {
1210
- dark: options?.states?.dark ?? globalConfig.states.dark,
1211
- highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
1212
- }, resolveModes(options?.modes), options?.format);
1213
- },
1214
- json(options) {
1215
- return buildJsonMap(resolveAllColors(hue, saturation, colorDefs), resolveModes(options?.modes), options?.format);
2729
+ return new Map(resolveCached());
1216
2730
  },
1217
- css(options) {
1218
- return buildCssMap(resolveAllColors(hue, saturation, colorDefs), "", options?.suffix ?? "-color", options?.format ?? "rgb");
1219
- }
1220
- };
1221
- }
1222
- function resolvePrefix(options, themeName) {
1223
- if (options?.prefix === true) return `${themeName}-`;
1224
- if (typeof options?.prefix === "object" && options.prefix !== null) return options.prefix[themeName] ?? `${themeName}-`;
1225
- return "";
1226
- }
1227
- function createPalette(themes) {
1228
- return {
1229
2731
  tokens(options) {
1230
2732
  const modes = resolveModes(options?.modes);
1231
- const allTokens = {};
1232
- for (const [themeName, theme] of Object.entries(themes)) {
1233
- const tokens = buildFlatTokenMap(theme.resolve(), resolvePrefix(options, themeName), modes, options?.format);
1234
- for (const variant of Object.keys(tokens)) {
1235
- if (!allTokens[variant]) allTokens[variant] = {};
1236
- Object.assign(allTokens[variant], tokens[variant]);
1237
- }
1238
- }
1239
- return allTokens;
2733
+ return buildFlatTokenMap(resolveCached(), "", modes, options?.format);
1240
2734
  },
1241
2735
  tasty(options) {
2736
+ const cfg = getEffectiveConfig();
1242
2737
  const states = {
1243
- dark: options?.states?.dark ?? globalConfig.states.dark,
1244
- highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
2738
+ dark: options?.states?.dark ?? cfg.states.dark,
2739
+ highContrast: options?.states?.highContrast ?? cfg.states.highContrast
1245
2740
  };
1246
2741
  const modes = resolveModes(options?.modes);
1247
- const allTokens = {};
1248
- for (const [themeName, theme] of Object.entries(themes)) {
1249
- const tokens = buildTokenMap(theme.resolve(), resolvePrefix(options, themeName), states, modes, options?.format);
1250
- Object.assign(allTokens, tokens);
1251
- }
1252
- return allTokens;
2742
+ return buildTokenMap(resolveCached(), "", states, modes, options?.format);
1253
2743
  },
1254
2744
  json(options) {
1255
2745
  const modes = resolveModes(options?.modes);
1256
- const result = {};
1257
- for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes, options?.format);
1258
- return result;
2746
+ return buildJsonMap(resolveCached(), modes, options?.format);
1259
2747
  },
1260
2748
  css(options) {
1261
- const suffix = options?.suffix ?? "-color";
1262
- const format = options?.format ?? "rgb";
1263
- const allLines = {
1264
- light: [],
1265
- dark: [],
1266
- lightContrast: [],
1267
- darkContrast: []
1268
- };
1269
- for (const [themeName, theme] of Object.entries(themes)) {
1270
- const css = buildCssMap(theme.resolve(), resolvePrefix(options, themeName), suffix, format);
1271
- for (const key of [
1272
- "light",
1273
- "dark",
1274
- "lightContrast",
1275
- "darkContrast"
1276
- ]) if (css[key]) allLines[key].push(css[key]);
1277
- }
1278
- return {
1279
- light: allLines.light.join("\n"),
1280
- dark: allLines.dark.join("\n"),
1281
- lightContrast: allLines.lightContrast.join("\n"),
1282
- darkContrast: allLines.darkContrast.join("\n")
1283
- };
1284
- }
1285
- };
1286
- }
1287
- function createColorToken(input) {
1288
- const defs = { __color__: {
1289
- lightness: input.lightness,
1290
- saturation: input.saturationFactor,
1291
- mode: input.mode
1292
- } };
1293
- return {
1294
- resolve() {
1295
- return resolveAllColors(input.hue, input.saturation, defs).get("__color__");
1296
- },
1297
- token(options) {
1298
- return buildTokenMap(resolveAllColors(input.hue, input.saturation, defs), "", {
1299
- dark: options?.states?.dark ?? globalConfig.states.dark,
1300
- highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
1301
- }, resolveModes(options?.modes), options?.format)["#__color__"];
1302
- },
1303
- tasty(options) {
1304
- return buildTokenMap(resolveAllColors(input.hue, input.saturation, defs), "", {
1305
- dark: options?.states?.dark ?? globalConfig.states.dark,
1306
- highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
1307
- }, resolveModes(options?.modes), options?.format)["#__color__"];
1308
- },
1309
- json(options) {
1310
- return buildJsonMap(resolveAllColors(input.hue, input.saturation, defs), resolveModes(options?.modes), options?.format)["__color__"];
2749
+ return buildCssMap(resolveCached(), "", options?.suffix ?? "-color", options?.format ?? "rgb");
1311
2750
  }
1312
2751
  };
1313
2752
  }
2753
+
2754
+ //#endregion
2755
+ //#region src/glaze.ts
2756
+ /**
2757
+ * Glaze — OKHST color theme generator.
2758
+ *
2759
+ * Public API entry. Wires `glaze()` and its attached static methods to
2760
+ * the focused modules in this folder:
2761
+ * - `theme.ts` — single-theme factory
2762
+ * - `palette.ts` — multi-theme composition
2763
+ * - `color-token.ts` — standalone single-color tokens (`glaze.color`)
2764
+ * - `shadow.ts` — standalone shadow factory (`glaze.shadow`)
2765
+ * - `formatters.ts` — variant → string (`glaze.format`)
2766
+ * - `config.ts` — global config singleton
2767
+ */
1314
2768
  /**
1315
2769
  * Create a single-hue glaze theme.
1316
2770
  *
2771
+ * An optional `config` override can be supplied to customize the resolve
2772
+ * behavior for this theme (tone windows, saturation taper, etc.). The
2773
+ * override is **merged over the live global config at resolve time** —
2774
+ * the theme still reacts to later `configure()` calls for fields it
2775
+ * didn't override.
2776
+ *
1317
2777
  * @example
1318
2778
  * ```ts
1319
- * const primary = glaze({ hue: 280, saturation: 80 });
1320
- * // or shorthand:
1321
2779
  * const primary = glaze(280, 80);
2780
+ * // or shorthand:
2781
+ * const primary = glaze({ hue: 280, saturation: 80 });
2782
+ * // with config override:
2783
+ * const raw = glaze(280, 80, { lightTone: false });
1322
2784
  * ```
1323
2785
  */
1324
- function glaze(hueOrOptions, saturation) {
1325
- if (typeof hueOrOptions === "number") return createTheme(hueOrOptions, saturation ?? 100);
1326
- return createTheme(hueOrOptions.hue, hueOrOptions.saturation);
2786
+ function glaze(hueOrOptions, saturation, config) {
2787
+ if (typeof hueOrOptions === "number") return createTheme(hueOrOptions, saturation ?? 100, void 0, config);
2788
+ return createTheme(hueOrOptions.hue, hueOrOptions.saturation, void 0, config);
1327
2789
  }
1328
- /**
1329
- * Configure global glaze settings.
1330
- */
1331
- glaze.configure = function configure(config) {
1332
- globalConfig = {
1333
- lightLightness: config.lightLightness ?? globalConfig.lightLightness,
1334
- darkLightness: config.darkLightness ?? globalConfig.darkLightness,
1335
- darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
1336
- states: {
1337
- dark: config.states?.dark ?? globalConfig.states.dark,
1338
- highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
1339
- },
1340
- modes: {
1341
- dark: config.modes?.dark ?? globalConfig.modes.dark,
1342
- highContrast: config.modes?.highContrast ?? globalConfig.modes.highContrast
1343
- },
1344
- shadowTuning: config.shadowTuning ?? globalConfig.shadowTuning
1345
- };
2790
+ /** Configure global glaze settings. */
2791
+ glaze.configure = function configure$1(config) {
2792
+ configure(config);
1346
2793
  };
1347
- /**
1348
- * Compose multiple themes into a palette.
1349
- */
1350
- glaze.palette = function palette(themes) {
1351
- return createPalette(themes);
2794
+ /** Compose multiple themes into a palette. */
2795
+ glaze.palette = function palette(themes, options) {
2796
+ return createPalette(themes, options);
1352
2797
  };
1353
- /**
1354
- * Create a theme from a serialized export.
1355
- */
2798
+ /** Create a theme from a serialized export. */
1356
2799
  glaze.from = function from(data) {
1357
- return createTheme(data.hue, data.saturation, data.colors);
2800
+ return createTheme(data.hue, data.saturation, data.colors, data.config);
1358
2801
  };
1359
2802
  /**
1360
2803
  * Create a standalone single-color token.
2804
+ *
2805
+ * **arg1 — the color** (four accepted shapes, discriminated by structure):
2806
+ *
2807
+ * | Shape | Example | Notes |
2808
+ * |---|---|---|
2809
+ * | Bare string | `'#26fcb2'`, `'rgb(38 252 178)'` | Hex or CSS color function (incl. `okhst()`) |
2810
+ * | Value object | `{ h: 152, s: 0.95, l: 0.74 }` | OKHSL, OKHST (`{h,s,t}`), `{r,g,b}`, `{l,c,h}` |
2811
+ * | `{ from, ...overrides }` | `{ from: '#fff', base: bg, contrast: 'AA' }` | Value + color overrides |
2812
+ * | Structured | `{ hue: 152, saturation: 95, tone: 74 }` | Full theme-style token |
2813
+ *
2814
+ * **arg2 — config override** (optional, all shapes):
2815
+ * Overrides the resolve-relevant global config fields for this token.
2816
+ * Fields that are omitted fall through to the live global config at
2817
+ * create time (and are snapshotted). Pass `false` for a tone window
2818
+ * to disable clamping entirely.
2819
+ *
2820
+ * ```ts
2821
+ * // Bare string — no overrides
2822
+ * glaze.color('#26fcb2')
2823
+ *
2824
+ * // From form — value + color overrides
2825
+ * glaze.color({ from: '#fff', base: bg, contrast: 'AA' })
2826
+ *
2827
+ * // Structured form — full theme-style token
2828
+ * glaze.color({ hue: 152, saturation: 95, tone: 74 })
2829
+ *
2830
+ * // Config override on any form
2831
+ * glaze.color('#26fcb2', { darkTone: false, autoFlip: false })
2832
+ * glaze.color({ from: '#fff', base: bg }, { saturationTaper: 0 })
2833
+ * ```
2834
+ *
2835
+ * Defaults: every form defaults to `mode: 'auto'`. Value-shorthand forms
2836
+ * (bare strings and value objects) preserve light tone exactly
2837
+ * (`lightTone: false` internally). Structured form snapshots both
2838
+ * tone windows from `globalConfig` at create time.
2839
+ *
2840
+ * Relative `tone: '+N'` and `contrast` anchor to the literal seed by
2841
+ * default; when `base` is set they anchor to the base's resolved variant
2842
+ * per scheme. Relative `hue: '+N'` always anchors to the seed, not the base.
1361
2843
  */
1362
- glaze.color = function color(input) {
1363
- return createColorToken(input);
2844
+ glaze.color = function color(input, config) {
2845
+ if (typeof input === "string") return createColorTokenFromValue(input, void 0, config);
2846
+ const obj = input;
2847
+ if ("from" in obj) {
2848
+ const { from, ...overrides } = input;
2849
+ return createColorTokenFromValue(from, overrides, config);
2850
+ }
2851
+ if ("hue" in obj) return createColorToken(input, config);
2852
+ return createColorTokenFromValue(input, void 0, config);
1364
2853
  };
1365
2854
  /**
1366
2855
  * Compute a shadow color from a bg/fg pair and intensity.
2856
+ *
2857
+ * Both `bg` and `fg` accept any `GlazeColorValue` form: hex (`#rgb` /
2858
+ * `#rrggbb` / `#rrggbbaa`), `rgb()` / `hsl()` / `okhsl()` / `oklch()`
2859
+ * strings, or `{ r, g, b }` / `{ h, s, l }` / `{ l, c, h }` objects.
1367
2860
  */
1368
2861
  glaze.shadow = function shadow(input) {
1369
- const bg = parseOkhslInput(input.bg);
1370
- const fg = input.fg ? parseOkhslInput(input.fg) : void 0;
1371
- const tuning = resolveShadowTuning(input.tuning);
1372
- return computeShadow({
2862
+ const bg = extractOkhslFromValue(input.bg);
2863
+ const fg = input.fg ? extractOkhslFromValue(input.fg) : void 0;
2864
+ const cfg = getConfig();
2865
+ const tuning = resolveShadowTuning(input.tuning, cfg.shadowTuning);
2866
+ const result = computeShadow({
1373
2867
  ...bg,
1374
2868
  alpha: 1
1375
2869
  }, fg ? {
1376
2870
  ...fg,
1377
2871
  alpha: 1
1378
2872
  } : void 0, input.intensity, tuning);
2873
+ const { h, s, t } = okhslToOkhst({
2874
+ h: result.h,
2875
+ s: result.s,
2876
+ l: result.l
2877
+ });
2878
+ return {
2879
+ h,
2880
+ s,
2881
+ t,
2882
+ alpha: result.alpha
2883
+ };
1379
2884
  };
1380
- /**
1381
- * Format a resolved color variant as a CSS string.
1382
- */
2885
+ /** Format a resolved color variant as a CSS string. */
1383
2886
  glaze.format = function format(variant, colorFormat) {
1384
2887
  return formatVariant(variant, colorFormat);
1385
2888
  };
1386
- function parseOkhslInput(input) {
1387
- if (typeof input === "string") {
1388
- const rgb = parseHex(input);
1389
- if (!rgb) throw new Error(`glaze: invalid hex color "${input}".`);
1390
- const [h, s, l] = srgbToOkhsl(rgb);
1391
- return {
1392
- h,
1393
- s,
1394
- l
1395
- };
1396
- }
1397
- return input;
1398
- }
1399
2889
  /**
1400
2890
  * Create a theme from a hex color string.
1401
2891
  * Extracts hue and saturation from the color.
@@ -1419,43 +2909,62 @@ glaze.fromRgb = function fromRgb(r, g, b) {
1419
2909
  return createTheme(h, s * 100);
1420
2910
  };
1421
2911
  /**
1422
- * Get the current global configuration (for testing/debugging).
2912
+ * Rehydrate a `glaze.color()` token from a `.export()` snapshot.
2913
+ *
2914
+ * The snapshot is a plain JSON-safe object containing the original
2915
+ * input value, overrides (with any `base` token recursively serialized),
2916
+ * and the effective config snapshot. The reconstructed token is identical
2917
+ * in behavior to the original at the time of export.
2918
+ *
2919
+ * @example
2920
+ * ```ts
2921
+ * const text = glaze.color({ from: '#1a1a1a', contrast: 'AA' });
2922
+ * const data = text.export(); // JSON-safe
2923
+ * localStorage.setItem('text', JSON.stringify(data));
2924
+ * // ...later...
2925
+ * const restored = glaze.colorFrom(JSON.parse(localStorage.getItem('text')!));
2926
+ * ```
1423
2927
  */
2928
+ glaze.colorFrom = function colorFrom(data) {
2929
+ return colorFromExport(data);
2930
+ };
2931
+ /** Get the current global configuration (for testing/debugging). */
1424
2932
  glaze.getConfig = function getConfig() {
1425
- return { ...globalConfig };
2933
+ return snapshotConfig();
1426
2934
  };
1427
- /**
1428
- * Reset global configuration to defaults.
1429
- */
1430
- glaze.resetConfig = function resetConfig() {
1431
- globalConfig = {
1432
- lightLightness: [10, 100],
1433
- darkLightness: [15, 95],
1434
- darkDesaturation: .1,
1435
- states: {
1436
- dark: "@dark",
1437
- highContrast: "@high-contrast"
1438
- },
1439
- modes: {
1440
- dark: true,
1441
- highContrast: false
1442
- }
1443
- };
2935
+ /** Reset global configuration to defaults. */
2936
+ glaze.resetConfig = function resetConfig$1() {
2937
+ resetConfig();
1444
2938
  };
1445
2939
 
1446
2940
  //#endregion
2941
+ exports.REF_EPS = REF_EPS;
2942
+ exports.apcaContrast = apcaContrast;
1447
2943
  exports.contrastRatioFromLuminance = contrastRatioFromLuminance;
1448
- exports.findLightnessForContrast = findLightnessForContrast;
2944
+ exports.findToneForContrast = findToneForContrast;
2945
+ exports.findValueForMixContrast = findValueForMixContrast;
1449
2946
  exports.formatHsl = formatHsl;
1450
2947
  exports.formatOkhsl = formatOkhsl;
1451
2948
  exports.formatOklch = formatOklch;
1452
2949
  exports.formatRgb = formatRgb;
2950
+ exports.fromTone = fromTone;
2951
+ exports.gamutClampedLuminance = gamutClampedLuminance;
1453
2952
  exports.glaze = glaze;
2953
+ exports.hslToSrgb = hslToSrgb;
1454
2954
  exports.okhslToLinearSrgb = okhslToLinearSrgb;
2955
+ exports.okhslToOkhst = okhslToOkhst;
1455
2956
  exports.okhslToOklab = okhslToOklab;
1456
2957
  exports.okhslToSrgb = okhslToSrgb;
2958
+ exports.okhstToOkhsl = okhstToOkhsl;
2959
+ exports.oklabToOkhsl = oklabToOkhsl;
1457
2960
  exports.parseHex = parseHex;
2961
+ exports.parseHexAlpha = parseHexAlpha;
1458
2962
  exports.relativeLuminanceFromLinearRgb = relativeLuminanceFromLinearRgb;
2963
+ exports.resolveContrastForMode = resolveContrastForMode;
1459
2964
  exports.resolveMinContrast = resolveMinContrast;
1460
2965
  exports.srgbToOkhsl = srgbToOkhsl;
2966
+ exports.toTone = toTone;
2967
+ exports.toneFromY = toneFromY;
2968
+ exports.variantToOkhsl = variantToOkhsl;
2969
+ exports.yFromTone = yFromTone;
1461
2970
  //# sourceMappingURL=index.cjs.map