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