@designtools/tokens 0.6.0 → 0.7.0

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.
@@ -413,7 +413,7 @@ function deriveWarningColor(primaryColor) {
413
413
  mode: "oklch",
414
414
  l: clamp(0.7 + pC * 0.3, 0.72, 0.82),
415
415
  c: clamp(pC * 1, 0.1, 0.17),
416
- h: 75
416
+ h: 60
417
417
  };
418
418
  return oklchToHex(warning);
419
419
  }
@@ -462,11 +462,12 @@ function getShade(palette, colorName, shade) {
462
462
  // src/bridge.ts
463
463
  var BRIDGE_OFFSETS = {
464
464
  light: {
465
- extended: { DEFAULT: 0, subdued: -5, highlight: 2 },
465
+ extended: { DEFAULT: 0, subdued: -5 },
466
466
  ring: -1
467
467
  },
468
468
  dark: {
469
- extended: { DEFAULT: 1, subdued: -3, highlight: -1 },
469
+ // subdued is a dark tint on a dark canvas (900 from a 500 seed), not a pale chip
470
+ extended: { DEFAULT: 1, subdued: 4 },
470
471
  ring: 2
471
472
  }
472
473
  };
@@ -479,15 +480,29 @@ function extendedBridgeShades(mode, seedShade, active, overrides) {
479
480
  const offsets = BRIDGE_OFFSETS[mode].extended;
480
481
  const at = (offset) => bridgeShade(offset, seedShade, active);
481
482
  const snap = (shade) => snapShade(shade, active);
483
+ const offsetSubdued = at(offsets.subdued);
484
+ const subdued = mode === "light" ? snap(Math.min(offsetSubdued, 100)) : snap(Math.max(offsetSubdued, 800));
482
485
  return {
483
486
  DEFAULT: overrides?.DEFAULT ? snap(overrides.DEFAULT) : at(offsets.DEFAULT),
484
- subdued: overrides?.subdued ? snap(overrides.subdued) : at(offsets.subdued),
485
- highlight: overrides?.highlight ? snap(overrides.highlight) : at(offsets.highlight)
487
+ subdued: overrides?.subdued ? snap(overrides.subdued) : subdued
486
488
  };
487
489
  }
488
490
  function ringBridgeShade(mode, seedShade, active) {
489
491
  return bridgeShade(BRIDGE_OFFSETS[mode].ring, seedShade, active);
490
492
  }
493
+ function accessibleStep(scale, start, mode, grounds, floor, active) {
494
+ const steps = active.filter((s) => scale[s] !== void 0);
495
+ const from = steps.indexOf(snapShade(start, active));
496
+ const direction = mode === "light" ? 1 : -1;
497
+ const worst = (step) => Math.min(...grounds.map((g) => getContrastRatio(scale[step], g)));
498
+ let best = steps[from];
499
+ for (let i = from; i >= 0 && i < steps.length; i += direction) {
500
+ const ratio = worst(steps[i]);
501
+ if (ratio >= floor) return steps[i];
502
+ if (ratio > worst(best)) best = steps[i];
503
+ }
504
+ return best;
505
+ }
491
506
 
492
507
  // src/semantic.ts
493
508
  function shadeKeys(shades) {
@@ -514,64 +529,92 @@ function getContrastThresholds(algorithm, target) {
514
529
  }
515
530
  }
516
531
  }
517
- function findBestForeground(views, white, black, algorithm, contrastTarget, colorName, activeShades) {
518
- const { floor, target } = getContrastThresholds(algorithm, contrastTarget);
532
+ var FILL_TINT_SHARE = 0.9;
533
+ var TINT_HUE_SHARE = 0.8;
534
+ var APCA_WHITE_MAX_Y = { "aa-large": 0.4564, aa: 0.314, aaa: 0.1946 };
535
+ var APCA_BLACK_MIN_Y = { "aa-large": 0.2831, aa: 0.4233, aaa: 0.5906 };
536
+ var WCAG_FLOOR = { "aa-large": 3, aa: 4.5, aaa: 7 };
537
+ function settleFill(scale, shade, target, active, views) {
538
+ const lum = (s) => views.map((v) => getContrastRatio(v.scale[s], "#000000", v.gamut) * 0.05 - 0.05);
539
+ const whiteMax = Math.min(1.05 / WCAG_FLOOR[target] - 0.05, APCA_WHITE_MAX_Y[target]);
540
+ const blackMin = Math.max(WCAG_FLOOR[target] * 0.05 - 0.05, APCA_BLACK_MIN_Y[target]);
541
+ const ok = (s) => lum(s).every((y2) => y2 <= whiteMax) || lum(s).every((y2) => y2 >= blackMin);
542
+ if (ok(shade)) return shade;
543
+ const y = Math.max(...lum(shade));
544
+ const darkerFirst = y - whiteMax <= 2 * (blackMin - y);
545
+ const at = active.indexOf(shade);
546
+ const darker = [at + 1, at + 2], lighter = [at - 1, at - 2];
547
+ for (const i of darkerFirst ? [...darker, ...lighter] : [...lighter, ...darker]) {
548
+ if (i >= 0 && i < active.length && scale[active[i]] && ok(active[i])) return active[i];
549
+ }
550
+ return shade;
551
+ }
552
+ var HOVER_SHIFT = 0.09;
553
+ var ACTIVE_SHIFT = 0.15;
554
+ function stateOf(fill, label, shift, gamut) {
555
+ const f = parseToOklch(fill);
556
+ const labelIsLight = getContrastRatio(label, "#000000") > getContrastRatio(label, "#ffffff");
557
+ const l = Math.min(1, Math.max(0, f.l + (labelIsLight ? -shift : shift)));
558
+ return formatOklchInGamut(l, f.c ?? 0, f.h ?? 0, gamut);
559
+ }
560
+ function chromaOf(color) {
561
+ return parseToOklch(color).c;
562
+ }
563
+ function tintTarget(algorithm, target, mode) {
564
+ if (algorithm === "apca") return { "aa-large": 60, aa: 75, aaa: 90 }[target];
565
+ if (mode === "dark") return { "aa-large": 7, aa: 9.5, aaa: 11 }[target];
566
+ return { "aa-large": 4.5, aa: 7, aaa: 9 }[target];
567
+ }
568
+ function findBestForeground(views, white, black, algorithm, contrastTarget, colorName, activeShades, role = "fill", preference = "balanced", mode = "light", wcagFloor = true, pageGrounds = []) {
569
+ const { floor } = getContrastThresholds(algorithm, contrastTarget);
570
+ const wcagMin = algorithm === "apca" && wcagFloor ? getContrastThresholds("wcag", contrastTarget).floor : 0;
571
+ const getWcag = (fgOf) => Math.min(...views.map((view) => getContrastRatio(view.background, fgOf(view), view.gamut)));
519
572
  const getContrast = (fgOf) => Math.min(
520
573
  ...views.map(
521
574
  (view) => algorithm === "apca" ? getAPCAContrast(fgOf(view), view.background, view.gamut) : getContrastRatio(view.background, fgOf(view), view.gamut)
522
575
  )
523
576
  );
524
- function searchScale(which, refColorName) {
525
- let bestPassing = null;
526
- let bestOverall = null;
527
- for (const shade of activeShades) {
528
- const c = getContrast((view) => view[which][shade]);
529
- if (c >= floor) {
530
- if (!bestPassing || Math.abs(c - target) < Math.abs(bestPassing.contrast - target)) {
531
- bestPassing = { shade, contrast: c };
532
- }
533
- }
534
- if (!bestOverall || c > bestOverall.contrast) {
535
- bestOverall = { shade, contrast: c };
536
- }
537
- }
538
- const pick = bestPassing ?? bestOverall;
539
- if (!pick) return null;
540
- return {
541
- value: views[0][which][pick.shade],
542
- ref: { type: "shade", colorName: refColorName, shade: pick.shade },
543
- contrast: pick.contrast,
544
- passes: pick === bestPassing
545
- };
546
- }
547
- const ownResult = searchScale("scale", colorName);
548
- if (ownResult?.passes) {
549
- return { value: ownResult.value, ref: ownResult.ref };
550
- }
551
- const neutralResult = searchScale("neutral", "neutral");
552
- if (neutralResult?.passes) {
553
- if (!ownResult?.passes) {
554
- return { value: neutralResult.value, ref: neutralResult.ref };
555
- }
556
- if (Math.abs(neutralResult.contrast - target) < Math.abs(ownResult.contrast - target)) {
557
- return { value: neutralResult.value, ref: neutralResult.ref };
558
- }
559
- return { value: ownResult.value, ref: ownResult.ref };
560
- }
561
- if (ownResult && (!neutralResult || ownResult.contrast >= neutralResult.contrast)) {
562
- return { value: ownResult.value, ref: ownResult.ref };
563
- }
564
- if (neutralResult) {
565
- return { value: neutralResult.value, ref: neutralResult.ref };
566
- }
567
- const whiteC = getContrast(() => white);
568
- const blackC = getContrast(() => black);
569
- if (whiteC >= blackC) return { value: white, ref: { type: "white" } };
570
- return { value: black, ref: { type: "black" } };
571
- }
572
- function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades, seedShade, gamuts = { own: "srgb" }) {
577
+ const ends = [
578
+ { value: white, ref: { type: "white" }, contrast: getContrast(() => white), wcag: wcagMin ? getWcag(() => white) : Infinity },
579
+ { value: black, ref: { type: "black" }, contrast: getContrast(() => black), wcag: wcagMin ? getWcag(() => black) : Infinity }
580
+ ].sort((a, b) => Number(b.wcag >= wcagMin) - Number(a.wcag >= wcagMin) || b.contrast - a.contrast);
581
+ const extreme = { value: ends[0].value, ref: ends[0].ref };
582
+ const best = ends[0].contrast;
583
+ const steps = (which, refName) => activeShades.map((shade) => ({
584
+ value: views[0][which][shade],
585
+ ref: { type: "shade", colorName: refName, shade },
586
+ contrast: getContrast((view) => view[which][shade]),
587
+ wcag: wcagMin ? getWcag((view) => view[which][shade]) : Infinity,
588
+ // the least chroma any view shows, so the sRGB and P3 tiers judge the hue alike and pick the same step
589
+ chroma: Math.min(...views.map((view) => chromaOf(view[which][shade]))),
590
+ own: which === "scale"
591
+ }));
592
+ const own = steps("scale", colorName);
593
+ if (role === "tint") {
594
+ const peak = Math.max(...own.map((c) => c.chroma));
595
+ const aim = tintTarget(algorithm, contrastTarget, mode);
596
+ const closest = (list) => [...list].sort((a, b) => Math.abs(a.contrast - aim) - Math.abs(b.contrast - aim) || b.contrast - a.contrast)[0];
597
+ const textFloor = getContrastThresholds("wcag", contrastTarget).floor;
598
+ const onPage = (c) => pageGrounds.every((g) => getContrastRatio(c.value, g) >= textFloor);
599
+ const allPassing = own.filter((c) => c.contrast >= floor && c.wcag >= wcagMin);
600
+ const onPagePassing = allPassing.filter(onPage);
601
+ const passing = onPagePassing.length ? onPagePassing : allPassing;
602
+ const enough = floor + (aim - floor) / 2;
603
+ const keepsHue = passing.filter((c) => c.chroma >= peak * TINT_HUE_SHARE && c.contrast >= enough);
604
+ const pick = closest(keepsHue) ?? closest(passing);
605
+ if (pick) return { value: pick.value, ref: pick.ref };
606
+ return extreme;
607
+ }
608
+ if (preference === "balanced" && best >= floor) {
609
+ const tints = [...own, ...steps("neutral", "neutral")].filter((c) => c.contrast >= floor && c.wcag >= wcagMin && c.contrast >= best * FILL_TINT_SHARE);
610
+ tints.sort((a, b) => a.contrast - b.contrast || Number(b.own) - Number(a.own));
611
+ if (tints[0]) return { value: tints[0].value, ref: tints[0].ref };
612
+ }
613
+ return extreme;
614
+ }
615
+ function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades, seedShade, gamuts = { own: "srgb" }, preference = "balanced", wcagFloor = true, pageGrounds = []) {
573
616
  const activeShades = shadeKeys(shades);
574
- const findFg = (bgShade) => {
617
+ const findFg = (bgShade, role, grounds = []) => {
575
618
  const views = [
576
619
  { background: scale[bgShade], scale, neutral, gamut: gamuts.own }
577
620
  ];
@@ -579,30 +622,36 @@ function createExtendedSemanticColor(scale, neutral, white, black, mode, contras
579
622
  const o = gamuts.other;
580
623
  views.push({ background: o.scale[bgShade], scale: o.scale, neutral: o.neutral, gamut: o.gamut });
581
624
  }
582
- return findBestForeground(views, white, black, contrastAlgorithm, contrastTarget, colorName, activeShades);
625
+ return findBestForeground(views, white, black, contrastAlgorithm, contrastTarget, colorName, activeShades, role, preference, mode, wcagFloor, grounds);
583
626
  };
584
- const { DEFAULT: defaultShade, subdued: subduedShade, highlight: highlightShade } = extendedBridgeShades(mode, seedShade, activeShades, overrides);
627
+ const { DEFAULT: offsetDefault, subdued: offsetSubdued } = extendedBridgeShades(mode, seedShade, activeShades, overrides);
628
+ const seedColour = parseToOklch(scale[bridgeShade(0, seedShade, activeShades)]);
629
+ const warm = mode === "light" && seedColour.c >= 0.05 && (seedColour.h ?? 0) >= 40 && (seedColour.h ?? 0) <= 115;
630
+ const subduedShade = warm && !overrides?.subdued ? snapShade(200, activeShades) : offsetSubdued;
631
+ const seedStep = bridgeShade(0, seedShade, activeShades);
632
+ const blackLabel = (shade) => getContrastRatio(scale[shade], black) > getContrastRatio(scale[shade], white);
633
+ const bridgeDefault = mode === "dark" && !overrides?.DEFAULT && offsetDefault !== seedStep && blackLabel(seedStep) ? seedStep : offsetDefault;
634
+ const fillViews = [{ scale, gamut: gamuts.own }, ...gamuts.other ? [{ scale: gamuts.other.scale, gamut: gamuts.other.gamut }] : []];
635
+ const defaultShade = colorName === "primary" || overrides?.DEFAULT ? bridgeDefault : settleFill(scale, bridgeDefault, contrastTarget, activeShades, fillViews);
585
636
  const DEFAULT = scale[defaultShade];
586
637
  const subdued = scale[subduedShade];
587
- const highlight = scale[highlightShade];
588
- const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(defaultShade);
589
- const sfgResult = overrides?.["on-subdued"] ? { value: scale[overrides["on-subdued"]], ref: { type: "shade", colorName, shade: overrides["on-subdued"] } } : findFg(subduedShade);
590
- const hfgResult = overrides?.["on-highlight"] ? { value: scale[overrides["on-highlight"]], ref: { type: "shade", colorName, shade: overrides["on-highlight"] } } : findFg(highlightShade);
638
+ const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(defaultShade, "fill");
639
+ const sfgResult = overrides?.["on-subdued"] ? { value: scale[overrides["on-subdued"]], ref: { type: "shade", colorName, shade: overrides["on-subdued"] } } : warm ? findFg(subduedShade, "fill") : findFg(subduedShade, "tint", ["success", "warning", "destructive"].includes(colorName) ? pageGrounds : []);
591
640
  return {
592
641
  DEFAULT,
593
642
  foreground: fgResult.value,
594
643
  subdued,
595
644
  "subdued-foreground": sfgResult.value,
596
- highlight,
597
- "highlight-foreground": hfgResult.value,
645
+ hover: stateOf(DEFAULT, fgResult.value, HOVER_SHIFT, gamuts.own),
646
+ active: stateOf(DEFAULT, fgResult.value, ACTIVE_SHIFT, gamuts.own),
598
647
  _refs: {
599
648
  foreground: fgResult.ref,
600
- "subdued-foreground": sfgResult.ref,
601
- "highlight-foreground": hfgResult.ref
602
- }
649
+ "subdued-foreground": sfgResult.ref
650
+ },
651
+ _shades: { DEFAULT: defaultShade, subdued: subduedShade }
603
652
  };
604
653
  }
605
- function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }) {
654
+ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }, pageGrounds = []) {
606
655
  const contrastAlgorithm = options?.contrastAlgorithm ?? "wcag";
607
656
  const contrastTarget = options?.contrastTarget ?? "aa";
608
657
  const shadeOverrides = options?.shadeOverrides ?? {};
@@ -632,19 +681,26 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
632
681
  }
633
682
  } : {}
634
683
  });
635
- const muted = mode === "light" ? {
636
- DEFAULT: neutral[snapShade(25, activeShades)],
637
- foreground: neutral[snapShade(500, activeShades)]
638
- } : {
639
- DEFAULT: neutral[snapShade(800, activeShades)],
640
- foreground: neutral[snapShade(200, activeShades)]
684
+ const mutedBg = snapShade(mode === "light" ? 100 : 800, activeShades);
685
+ const mutedFg = accessibleStep(
686
+ neutral,
687
+ mode === "light" ? 500 : 200,
688
+ mode,
689
+ [neutral[mutedBg]],
690
+ contrastTarget === "aaa" ? 7 : 4.5,
691
+ activeShades
692
+ );
693
+ const muted = {
694
+ DEFAULT: neutral[mutedBg],
695
+ foreground: neutral[mutedFg],
696
+ _shades: { DEFAULT: mutedBg, foreground: mutedFg }
641
697
  };
642
698
  const tokens = {
643
- primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades, anchor("primary"), gamutsFor("primary")),
644
- neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades, anchor("neutral"), gamutsFor("neutral")),
645
- success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades, anchor("success"), gamutsFor("success")),
646
- destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades, anchor("destructive"), gamutsFor("destructive")),
647
- warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, anchor("warning"), gamutsFor("warning")),
699
+ primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades, anchor("primary"), gamutsFor("primary"), options?.foreground, options?.wcagFloor, pageGrounds),
700
+ neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades, anchor("neutral"), gamutsFor("neutral"), options?.foreground, options?.wcagFloor, pageGrounds),
701
+ success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades, anchor("success"), gamutsFor("success"), options?.foreground, options?.wcagFloor, pageGrounds),
702
+ destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades, anchor("destructive"), gamutsFor("destructive"), options?.foreground, options?.wcagFloor, pageGrounds),
703
+ warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, anchor("warning"), gamutsFor("warning"), options?.foreground, options?.wcagFloor, pageGrounds),
648
704
  muted
649
705
  };
650
706
  const coreColors = [
@@ -660,7 +716,7 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
660
716
  "yellow"
661
717
  ];
662
718
  for (const [colorName, colorValue] of Object.entries(palette)) {
663
- if (!coreColors.includes(colorName) && typeof colorValue !== "string" && colorValue) {
719
+ if (!coreColors.includes(colorName) && !options?.decorative?.includes(colorName) && typeof colorValue !== "string" && colorValue) {
664
720
  tokens[colorName] = createExtendedSemanticColor(
665
721
  colorValue,
666
722
  neutral,
@@ -673,13 +729,23 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
673
729
  colorName,
674
730
  activeShades,
675
731
  anchor(colorName),
676
- gamutsFor(colorName)
732
+ gamutsFor(colorName),
733
+ options?.foreground,
734
+ options?.wcagFloor,
735
+ pageGrounds
677
736
  );
678
737
  }
679
738
  }
680
739
  return tokens;
681
740
  }
682
- function generateSurfaceTokens(palette, mode, overrides, shades) {
741
+ function textOn(bg, neutral, white, black, active) {
742
+ const light = getContrastRatio(bg, black) > getContrastRatio(bg, white);
743
+ if (!light) return { value: white, ref: { type: "white" } };
744
+ const step = accessibleStep(neutral, snapShade(800, active), "light", [bg], 7, active);
745
+ if (getContrastRatio(bg, neutral[step]) >= 4.5) return { value: neutral[step], ref: { type: "shade", shade: step } };
746
+ return { value: black, ref: { type: "black" } };
747
+ }
748
+ function generateSurfaceTokens(palette, mode, overrides, shades, colors, backgrounds) {
683
749
  const activeShades = shadeKeys(shades);
684
750
  const snap = (shade) => snapShade(shade, activeShades);
685
751
  const neutral = palette.neutral;
@@ -696,12 +762,22 @@ function generateSurfaceTokens(palette, mode, overrides, shades) {
696
762
  const val = snap(raw);
697
763
  return { value: neutral[val], ref: { type: "shade", shade: val } };
698
764
  }
699
- const canvas = resolve("canvas");
700
- const canvasFg = resolve("canvas-foreground");
701
- const surface = resolve("surface");
702
- const surfaceFg = resolve("surface-foreground");
703
- const overlay = resolve("overlay");
704
- const overlayFg = resolve("overlay-foreground");
765
+ const ground = (key) => {
766
+ const given = colors?.[key];
767
+ if (!given) return { bg: resolve(key), fg: resolve(`${key}-foreground`) };
768
+ const bg = { value: given, ref: { type: "value", value: given } };
769
+ return { bg, fg: overrides?.[`${key}-foreground`] ? resolve(`${key}-foreground`) : textOn(given, neutral, white, black, activeShades) };
770
+ };
771
+ const { bg: canvas, fg: canvasFg } = ground("canvas");
772
+ const { bg: surface, fg: surfaceFg } = ground("surface");
773
+ const { bg: overlay, fg: overlayFg } = ground("overlay");
774
+ const named = {};
775
+ for (const name of Object.keys(backgrounds ?? {}).sort()) {
776
+ const def = backgrounds[name];
777
+ const value = mode === "dark" ? def.dark ?? def.light : def.light;
778
+ const fg = textOn(value, neutral, white, black, activeShades);
779
+ named[name] = { DEFAULT: value, foreground: fg.value, _refs: { DEFAULT: { type: "value", value }, foreground: fg.ref } };
780
+ }
705
781
  return {
706
782
  canvas: canvas.value,
707
783
  "canvas-foreground": canvasFg.value,
@@ -716,10 +792,11 @@ function generateSurfaceTokens(palette, mode, overrides, shades) {
716
792
  "surface-foreground": surfaceFg.ref,
717
793
  overlay: overlay.ref,
718
794
  "overlay-foreground": overlayFg.ref
719
- }
795
+ },
796
+ ...Object.keys(named).length ? { backgrounds: named } : {}
720
797
  };
721
798
  }
722
- function generateUtilityTokens(palette, mode, overrides, shades, seedShade) {
799
+ function generateUtilityTokens(palette, mode, overrides, shades, seedShade, grounds) {
723
800
  const activeShades = shadeKeys(shades);
724
801
  const snap = (shade) => snapShade(shade, activeShades);
725
802
  const neutral = palette.neutral;
@@ -727,13 +804,23 @@ function generateUtilityTokens(palette, mode, overrides, shades, seedShade) {
727
804
  if (!neutral || !primary) {
728
805
  throw new Error("Required palettes not found");
729
806
  }
730
- const ring = ringBridgeShade(mode, seedShade, activeShades);
731
- const defaults = mode === "light" ? { border: snap(200), input: snap(200), ring, shadow: snap(950) } : { border: snap(800), input: snap(800), ring, shadow: snap(950) };
807
+ const NON_TEXT = 3;
808
+ const ring = grounds ? accessibleStep(primary, ringBridgeShade(mode, seedShade, activeShades), mode, grounds, NON_TEXT, activeShades) : ringBridgeShade(mode, seedShade, activeShades);
809
+ const inputStart = snap(mode === "light" ? 200 : 800);
810
+ const input = grounds ? accessibleStep(neutral, inputStart, mode, grounds, NON_TEXT, activeShades) : inputStart;
811
+ const defaults = mode === "light" ? { border: snap(200), input, ring, shadow: snap(950) } : { border: snap(800), input, ring, shadow: snap(950) };
812
+ const steps = {
813
+ border: overrides?.border ? snap(overrides.border) : defaults.border,
814
+ input: overrides?.input ? snap(overrides.input) : defaults.input,
815
+ ring: overrides?.ring ? snap(overrides.ring) : defaults.ring,
816
+ shadow: overrides?.shadow ? snap(overrides.shadow) : defaults.shadow
817
+ };
732
818
  return {
733
- border: neutral[overrides?.border ? snap(overrides.border) : defaults.border],
734
- input: neutral[overrides?.input ? snap(overrides.input) : defaults.input],
735
- ring: primary[overrides?.ring ? snap(overrides.ring) : defaults.ring],
736
- shadow: neutral[overrides?.shadow ? snap(overrides.shadow) : defaults.shadow]
819
+ border: neutral[steps.border],
820
+ input: neutral[steps.input],
821
+ ring: primary[steps.ring],
822
+ shadow: neutral[steps.shadow],
823
+ _steps: steps
737
824
  };
738
825
  }
739
826
  function checkSemanticContrast(tokens, gamut = "srgb") {
@@ -763,16 +850,6 @@ function checkSemanticContrast(tokens, gamut = "srgb") {
763
850
  passesAA: meetsWCAG_AA(subduedRatio),
764
851
  passesAAA: subduedRatio >= 7
765
852
  };
766
- const highlightRatio = getContrastRatio(
767
- extendedColor.highlight,
768
- extendedColor["highlight-foreground"],
769
- gamut
770
- );
771
- results[`${name}/highlight`] = {
772
- ratio: Math.round(highlightRatio * 100) / 100,
773
- passesAA: meetsWCAG_AA(highlightRatio),
774
- passesAAA: highlightRatio >= 7
775
- };
776
853
  }
777
854
  const mutedRatio = getContrastRatio(
778
855
  tokens.muted.DEFAULT,
@@ -803,11 +880,6 @@ function checkSemanticContrastAPCA(tokens, gamut = "srgb") {
803
880
  lcValue: Math.round(subduedLc * 10) / 10,
804
881
  level: classifyAPCALevel(subduedLc)
805
882
  };
806
- const highlightLc = getAPCAContrast(ext["highlight-foreground"], ext.highlight, gamut);
807
- results[`${name}/highlight`] = {
808
- lcValue: Math.round(highlightLc * 10) / 10,
809
- level: classifyAPCALevel(highlightLc)
810
- };
811
883
  }
812
884
  const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT, gamut);
813
885
  results["muted"] = {
@@ -972,26 +1044,35 @@ function generateTokens(brandColors, options) {
972
1044
  };
973
1045
  const primitives = generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "srgb" });
974
1046
  const wide = options?.gamut === "p3" ? generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "p3" }) : void 0;
1047
+ const grounds = (palette, mode) => {
1048
+ const surface = generateSurfaceTokens(palette, mode, options?.surfaceShadeOverrides?.[mode], options?.shades, options?.surfaceColors?.[mode], options?.backgrounds);
1049
+ const light = (c) => getContrastRatio(c, "#000000") > getContrastRatio(c, "#ffffff");
1050
+ const pageIsLight = light(surface.canvas);
1051
+ const named = Object.values(surface.backgrounds ?? {}).map((b) => b.DEFAULT).filter((c) => light(c) === pageIsLight);
1052
+ return [surface.canvas, surface.surface, surface.overlay, ...named];
1053
+ };
975
1054
  const colourTiers = (palette, gamuts) => ({
976
1055
  primitives: palette,
977
1056
  semantic: {
978
- light: generateSemanticTokens(palette, "light", options, gamuts),
979
- dark: generateSemanticTokens(palette, "dark", options, gamuts)
1057
+ light: generateSemanticTokens(palette, "light", options, gamuts, grounds(palette, "light")),
1058
+ dark: generateSemanticTokens(palette, "dark", options, gamuts, grounds(palette, "dark"))
980
1059
  },
981
1060
  surface: {
982
- light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades),
983
- dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades)
1061
+ light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades, options?.surfaceColors?.light, options?.backgrounds),
1062
+ dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades, options?.surfaceColors?.dark, options?.backgrounds)
984
1063
  },
985
1064
  utility: {
986
- light: generateUtilityTokens(palette, "light", options?.utilityShadeOverrides?.light, options?.shades, options?.seedShade),
987
- dark: generateUtilityTokens(palette, "dark", options?.utilityShadeOverrides?.dark, options?.shades, options?.seedShade)
1065
+ light: generateUtilityTokens(palette, "light", options?.utilityShadeOverrides?.light, options?.shades, options?.seedShade, grounds(palette, "light")),
1066
+ dark: generateUtilityTokens(palette, "dark", options?.utilityShadeOverrides?.dark, options?.shades, options?.seedShade, grounds(palette, "dark"))
988
1067
  }
989
1068
  });
1069
+ const tiers = colourTiers(primitives, {
1070
+ own: "srgb",
1071
+ ...wide ? { other: { gamut: "p3", palette: wide } } : {}
1072
+ });
990
1073
  return {
991
- ...colourTiers(primitives, {
992
- own: "srgb",
993
- ...wide ? { other: { gamut: "p3", palette: wide } } : {}
994
- }),
1074
+ ...tiers,
1075
+ pageContrast: { light: pageContrast(tiers, "light"), dark: pageContrast(tiers, "dark") },
995
1076
  spacing: {},
996
1077
  typography: defaultTypography,
997
1078
  radii: defaultRadii,
@@ -999,6 +1080,57 @@ function generateTokens(brandColors, options) {
999
1080
  ...wide ? { p3: colourTiers(wide, { own: "p3", other: { gamut: "srgb", palette: primitives } }) } : {}
1000
1081
  };
1001
1082
  }
1083
+ function pageContrast(tiers, mode) {
1084
+ const surface = tiers.surface[mode];
1085
+ const grounds = { canvas: surface.canvas, surface: surface.surface, overlay: surface.overlay };
1086
+ for (const [name, bg] of Object.entries(surface.backgrounds ?? {})) grounds[`surface-${name}`] = bg.DEFAULT;
1087
+ const colours = {};
1088
+ for (const [name, colour] of Object.entries(tiers.semantic[mode])) {
1089
+ colours[name === "muted" ? "muted-foreground" : name] = name === "muted" ? colour.foreground : colour.DEFAULT;
1090
+ }
1091
+ const out = {};
1092
+ for (const name of Object.keys(colours).sort()) {
1093
+ out[name] = {};
1094
+ for (const ground of Object.keys(grounds)) {
1095
+ out[name][ground] = Math.round(getContrastRatio(colours[name], grounds[ground]) * 100) / 100;
1096
+ }
1097
+ }
1098
+ return out;
1099
+ }
1100
+
1101
+ // src/usage.ts
1102
+ var TEXT_FLOOR = { "aa-large": 3, aa: 4.5, aaa: 7 };
1103
+ function usageReport(tokens, target = "aa") {
1104
+ const floors = { text: TEXT_FLOOR[target], shape: 3 };
1105
+ const verdict = (r) => r >= floors.text ? "text" : r >= floors.shape ? "shape" : "none";
1106
+ const mode = (m) => {
1107
+ const surface = tokens.surface[m];
1108
+ const grounds = { canvas: surface.canvas, surface: surface.surface, overlay: surface.overlay };
1109
+ for (const [name, bg] of Object.entries(surface.backgrounds ?? {})) grounds[`surface-${name}`] = bg.DEFAULT;
1110
+ const colours = {};
1111
+ for (const [name, colour] of Object.entries(tokens.semantic[m])) {
1112
+ if (name === "muted") {
1113
+ colours["muted-foreground"] = colour.foreground;
1114
+ continue;
1115
+ }
1116
+ colours[name] = colour.DEFAULT;
1117
+ colours[`${name}-subdued-foreground`] = colour["subdued-foreground"];
1118
+ }
1119
+ const out = {};
1120
+ for (const name of Object.keys(colours).sort()) {
1121
+ out[name] = {};
1122
+ for (const g of Object.keys(grounds).sort()) {
1123
+ const ratio = Math.round(getContrastRatio(colours[name], grounds[g]) * 100) / 100;
1124
+ out[name][g] = { ratio, use: verdict(ratio) };
1125
+ }
1126
+ }
1127
+ return out;
1128
+ };
1129
+ return { schema: "designtools.usage/1", target, floors, light: mode("light"), dark: mode("dark") };
1130
+ }
1131
+ function serialiseUsage(report) {
1132
+ return JSON.stringify(report, null, 2) + "\n";
1133
+ }
1002
1134
 
1003
1135
  // src/export.ts
1004
1136
  function formatColor(color, format) {
@@ -1048,7 +1180,7 @@ function getExtendedMappings(mode, colorName, overrides, active, anchors) {
1048
1180
  return extendedBridgeShades(mode, seedShade, active, overrides?.[colorName]?.[mode]);
1049
1181
  }
1050
1182
  var MUTED_SHADE_MAPPINGS = {
1051
- light: { DEFAULT: 25, foreground: 500 },
1183
+ light: { DEFAULT: 100, foreground: 500 },
1052
1184
  dark: { DEFAULT: 800, foreground: 200 }
1053
1185
  };
1054
1186
  var UTILITY_SHADE_MAPPINGS = {
@@ -1076,7 +1208,7 @@ function refToVar(ref) {
1076
1208
  return `var(--color-${ref.colorName}-${ref.shade})`;
1077
1209
  }
1078
1210
  }
1079
- function surfaceRefToVar(ref) {
1211
+ function surfaceRefToVar(ref, colorFormat) {
1080
1212
  switch (ref.type) {
1081
1213
  case "white":
1082
1214
  return "var(--color-white)";
@@ -1084,25 +1216,25 @@ function surfaceRefToVar(ref) {
1084
1216
  return "var(--color-black)";
1085
1217
  case "shade":
1086
1218
  return `var(--color-neutral-${ref.shade})`;
1219
+ // a background passed in as a colour is not on any ramp, so it is written as it is
1220
+ case "value":
1221
+ return formatColor(ref.value, colorFormat);
1087
1222
  }
1088
1223
  }
1089
1224
  function emitSurfaceVars(surface, colorFormat, indent) {
1090
1225
  const lines = [];
1091
- const keys = [
1092
- "canvas",
1093
- "canvas-foreground",
1094
- "surface",
1095
- "surface-foreground",
1096
- "overlay",
1097
- "overlay-foreground"
1098
- ];
1226
+ const keys = ["canvas", "canvas-foreground", "surface", "surface-foreground", "overlay", "overlay-foreground"];
1099
1227
  for (const key of keys) {
1100
1228
  if (surface._refs) {
1101
- lines.push(`${indent}--${key}: ${surfaceRefToVar(surface._refs[key])};`);
1229
+ lines.push(`${indent}--${key}: ${surfaceRefToVar(surface._refs[key], colorFormat)};`);
1102
1230
  } else {
1103
1231
  lines.push(`${indent}--${key}: ${formatColor(surface[key], colorFormat)};`);
1104
1232
  }
1105
1233
  }
1234
+ for (const [name, bg] of Object.entries(surface.backgrounds ?? {})) {
1235
+ lines.push(`${indent}--surface-${name}: ${surfaceRefToVar(bg._refs.DEFAULT, colorFormat)};`);
1236
+ lines.push(`${indent}--surface-${name}-foreground: ${surfaceRefToVar(bg._refs.foreground, colorFormat)};`);
1237
+ }
1106
1238
  return lines;
1107
1239
  }
1108
1240
  function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors) {
@@ -1111,36 +1243,41 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
1111
1243
  for (const name of extendedSemanticNames) {
1112
1244
  if (!inFamily(name)) continue;
1113
1245
  const color = tokens.semantic[mode][name];
1114
- const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1246
+ const mappings = color._shades ?? getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1115
1247
  lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
1116
1248
  lines.push(`${indent}--${name}-foreground: ${color._refs ? refToVar(color._refs.foreground) : formatColor(color.foreground, colorFormat)};`);
1117
1249
  lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
1118
1250
  lines.push(`${indent}--${name}-subdued-foreground: ${color._refs ? refToVar(color._refs["subdued-foreground"]) : formatColor(color["subdued-foreground"], colorFormat)};`);
1119
- lines.push(`${indent}--${name}-highlight: var(--color-${name}-${mappings.highlight});`);
1120
- lines.push(`${indent}--${name}-highlight-foreground: ${color._refs ? refToVar(color._refs["highlight-foreground"]) : formatColor(color["highlight-foreground"], colorFormat)};`);
1251
+ lines.push(`${indent}--${name}-hover: ${formatColor(color.hover, colorFormat)};`);
1252
+ lines.push(`${indent}--${name}-active: ${formatColor(color.active, colorFormat)};`);
1121
1253
  }
1122
1254
  for (const [name, color] of Object.entries(tokens.semantic[mode])) {
1123
1255
  if (!extendedSemanticNames.includes(name) && name !== "muted" && inFamily(name)) {
1124
1256
  const ext = color;
1125
- const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1257
+ const mappings = ext._shades ?? getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1126
1258
  lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
1127
1259
  lines.push(`${indent}--${name}-foreground: ${ext._refs ? refToVar(ext._refs.foreground) : formatColor(ext.foreground, colorFormat)};`);
1128
1260
  lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
1129
1261
  lines.push(`${indent}--${name}-subdued-foreground: ${ext._refs ? refToVar(ext._refs["subdued-foreground"]) : formatColor(ext["subdued-foreground"], colorFormat)};`);
1130
- lines.push(`${indent}--${name}-highlight: var(--color-${name}-${mappings.highlight});`);
1131
- lines.push(`${indent}--${name}-highlight-foreground: ${ext._refs ? refToVar(ext._refs["highlight-foreground"]) : formatColor(ext["highlight-foreground"], colorFormat)};`);
1262
+ lines.push(`${indent}--${name}-hover: ${formatColor(ext.hover, colorFormat)};`);
1263
+ lines.push(`${indent}--${name}-active: ${formatColor(ext.active, colorFormat)};`);
1132
1264
  }
1133
1265
  }
1134
1266
  if (!scoped || inFamily("neutral")) {
1135
- const muted = MUTED_SHADE_MAPPINGS[mode];
1267
+ const muted = tokens.semantic[mode].muted._shades ?? MUTED_SHADE_MAPPINGS[mode];
1136
1268
  lines.push(`${indent}--muted: var(--color-neutral-${snapShade(muted.DEFAULT, active)});`);
1137
1269
  lines.push(`${indent}--muted-foreground: var(--color-neutral-${snapShade(muted.foreground, active)});`);
1138
1270
  }
1139
1271
  return lines;
1140
1272
  }
1141
- function emitUtilityVars(mode, indent, active, seedShade) {
1273
+ function emitUtilityVars(mode, indent, active, seedShade, steps) {
1142
1274
  const fixed = UTILITY_SHADE_MAPPINGS[mode];
1143
- const refs = [
1275
+ const refs = steps ? [
1276
+ ["border", `neutral-${steps.border}`],
1277
+ ["input", `neutral-${steps.input}`],
1278
+ ["ring", `primary-${steps.ring}`],
1279
+ ["shadow", `neutral-${steps.shadow}`]
1280
+ ] : [
1144
1281
  ["border", fixed.border],
1145
1282
  ["input", fixed.input],
1146
1283
  ["ring", `primary-${ringBridgeShade(mode, seedShade, active)}`],
@@ -1172,7 +1309,7 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
1172
1309
  }
1173
1310
  if (want.has("utility") && !scoped) {
1174
1311
  if (withComments) body.push(`${indent}/* Utility */`);
1175
- body.push(...emitUtilityVars(mode, indent, active, seedShade));
1312
+ body.push(...emitUtilityVars(mode, indent, active, seedShade, tokens.utility[mode]._steps));
1176
1313
  if (withComments) body.push("");
1177
1314
  }
1178
1315
  if (want.has("semantic")) {
@@ -1270,8 +1407,8 @@ function semanticAliasNames(tokens, sections, families) {
1270
1407
  `${n}-foreground`,
1271
1408
  `${n}-subdued`,
1272
1409
  `${n}-subdued-foreground`,
1273
- `${n}-highlight`,
1274
- `${n}-highlight-foreground`
1410
+ `${n}-hover`,
1411
+ `${n}-active`
1275
1412
  );
1276
1413
  };
1277
1414
  if (want.has("semantic")) {
@@ -1290,6 +1427,9 @@ function semanticAliasNames(tokens, sections, families) {
1290
1427
  "overlay",
1291
1428
  "overlay-foreground"
1292
1429
  );
1430
+ for (const name of Object.keys(tokens.surface.light.backgrounds ?? {})) {
1431
+ names.push(`surface-${name}`, `surface-${name}-foreground`);
1432
+ }
1293
1433
  }
1294
1434
  if (want.has("utility") && !scoped) {
1295
1435
  names.push("border", "input", "ring", "shadow");
@@ -1315,7 +1455,7 @@ function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases,
1315
1455
  }
1316
1456
  if (want.has("utility") && !scoped) {
1317
1457
  lines.push(`${indent}/* Utility */`);
1318
- lines.push(...emitUtilityVars(mode, indent, active, seedShade));
1458
+ lines.push(...emitUtilityVars(mode, indent, active, seedShade, tokens.utility[mode]._steps));
1319
1459
  lines.push("");
1320
1460
  }
1321
1461
  if (want.has("semantic")) {
@@ -1404,8 +1544,8 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
1404
1544
  foreground: `var(--${name}-foreground)`,
1405
1545
  subdued: `var(--${name}-subdued)`,
1406
1546
  "subdued-foreground": `var(--${name}-subdued-foreground)`,
1407
- highlight: `var(--${name}-highlight)`,
1408
- "highlight-foreground": `var(--${name}-highlight-foreground)`
1547
+ hover: `var(--${name}-hover)`,
1548
+ active: `var(--${name}-active)`
1409
1549
  };
1410
1550
  if (primitiveScale && typeof primitiveScale !== "string") {
1411
1551
  for (const [shade, color] of Object.entries(primitiveScale)) {
@@ -1422,8 +1562,8 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
1422
1562
  foreground: `var(--${name}-foreground)`,
1423
1563
  subdued: `var(--${name}-subdued)`,
1424
1564
  "subdued-foreground": `var(--${name}-subdued-foreground)`,
1425
- highlight: `var(--${name}-highlight)`,
1426
- "highlight-foreground": `var(--${name}-highlight-foreground)`
1565
+ hover: `var(--${name}-hover)`,
1566
+ active: `var(--${name}-active)`
1427
1567
  };
1428
1568
  if (primitiveScale && typeof primitiveScale !== "string") {
1429
1569
  for (const [shade, color] of Object.entries(primitiveScale)) {
@@ -1487,10 +1627,8 @@ function exportToDtcg(tokens) {
1487
1627
  "subdued-foreground": {
1488
1628
  $value: formatColorAs(color["subdued-foreground"], "hex")
1489
1629
  },
1490
- highlight: { $value: formatColorAs(color.highlight, "hex") },
1491
- "highlight-foreground": {
1492
- $value: formatColorAs(color["highlight-foreground"], "hex")
1493
- }
1630
+ hover: { $value: formatColorAs(color.hover, "hex") },
1631
+ active: { $value: formatColorAs(color.active, "hex") }
1494
1632
  };
1495
1633
  }
1496
1634
  for (const [colorName, color] of Object.entries(tokens.semantic[mode])) {
@@ -1504,10 +1642,8 @@ function exportToDtcg(tokens) {
1504
1642
  "subdued-foreground": {
1505
1643
  $value: formatColorAs(ext["subdued-foreground"], "hex")
1506
1644
  },
1507
- highlight: { $value: formatColorAs(ext.highlight, "hex") },
1508
- "highlight-foreground": {
1509
- $value: formatColorAs(ext["highlight-foreground"], "hex")
1510
- }
1645
+ hover: { $value: formatColorAs(ext.hover, "hex") },
1646
+ active: { $value: formatColorAs(ext.active, "hex") }
1511
1647
  };
1512
1648
  }
1513
1649
  }
@@ -1827,6 +1963,7 @@ export {
1827
1963
  familySeedShade,
1828
1964
  generatePrimitivePalette,
1829
1965
  getShade,
1966
+ bridgeShade,
1830
1967
  extendedBridgeShades,
1831
1968
  ringBridgeShade,
1832
1969
  generateSemanticTokens,
@@ -1838,6 +1975,8 @@ export {
1838
1975
  checkRampChroma,
1839
1976
  computeAccessibilityScore,
1840
1977
  generateTokens,
1978
+ usageReport,
1979
+ serialiseUsage,
1841
1980
  WIDE_GAMUT_QUERY,
1842
1981
  exportToCss,
1843
1982
  overrideContract,