@designtools/tokens 0.6.0 → 0.7.1

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,94 @@ 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 away = labelIsLight ? -shift : shift;
558
+ const room = away > 0 ? 1 - f.l : f.l;
559
+ const l = Math.min(1, Math.max(0, f.l + (room >= shift ? away : -away)));
560
+ return formatOklchInGamut(l, f.c ?? 0, f.h ?? 0, gamut);
561
+ }
562
+ function chromaOf(color) {
563
+ return parseToOklch(color).c;
564
+ }
565
+ function tintTarget(algorithm, target, mode) {
566
+ if (algorithm === "apca") return { "aa-large": 60, aa: 75, aaa: 90 }[target];
567
+ if (mode === "dark") return { "aa-large": 7, aa: 9.5, aaa: 11 }[target];
568
+ return { "aa-large": 4.5, aa: 7, aaa: 9 }[target];
569
+ }
570
+ function findBestForeground(views, white, black, algorithm, contrastTarget, colorName, activeShades, role = "fill", preference = "balanced", mode = "light", wcagFloor = true, pageGrounds = []) {
571
+ const { floor } = getContrastThresholds(algorithm, contrastTarget);
572
+ const wcagMin = algorithm === "apca" && wcagFloor ? getContrastThresholds("wcag", contrastTarget).floor : 0;
573
+ const getWcag = (fgOf) => Math.min(...views.map((view) => getContrastRatio(view.background, fgOf(view), view.gamut)));
519
574
  const getContrast = (fgOf) => Math.min(
520
575
  ...views.map(
521
576
  (view) => algorithm === "apca" ? getAPCAContrast(fgOf(view), view.background, view.gamut) : getContrastRatio(view.background, fgOf(view), view.gamut)
522
577
  )
523
578
  );
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" }) {
579
+ const ends = [
580
+ { value: white, ref: { type: "white" }, contrast: getContrast(() => white), wcag: wcagMin ? getWcag(() => white) : Infinity },
581
+ { value: black, ref: { type: "black" }, contrast: getContrast(() => black), wcag: wcagMin ? getWcag(() => black) : Infinity }
582
+ ].sort((a, b) => Number(b.wcag >= wcagMin) - Number(a.wcag >= wcagMin) || b.contrast - a.contrast);
583
+ const extreme = { value: ends[0].value, ref: ends[0].ref };
584
+ const best = ends[0].contrast;
585
+ const steps = (which, refName) => activeShades.map((shade) => ({
586
+ value: views[0][which][shade],
587
+ ref: { type: "shade", colorName: refName, shade },
588
+ contrast: getContrast((view) => view[which][shade]),
589
+ wcag: wcagMin ? getWcag((view) => view[which][shade]) : Infinity,
590
+ // the least chroma any view shows, so the sRGB and P3 tiers judge the hue alike and pick the same step
591
+ chroma: Math.min(...views.map((view) => chromaOf(view[which][shade]))),
592
+ own: which === "scale"
593
+ }));
594
+ const own = steps("scale", colorName);
595
+ if (role === "tint") {
596
+ const peak = Math.max(...own.map((c) => c.chroma));
597
+ const aim = tintTarget(algorithm, contrastTarget, mode);
598
+ const closest = (list) => [...list].sort((a, b) => Math.abs(a.contrast - aim) - Math.abs(b.contrast - aim) || b.contrast - a.contrast)[0];
599
+ const textFloor = getContrastThresholds("wcag", contrastTarget).floor;
600
+ const onPage = (c) => pageGrounds.every((g) => getContrastRatio(c.value, g) >= textFloor);
601
+ const allPassing = own.filter((c) => c.contrast >= floor && c.wcag >= wcagMin);
602
+ const onPagePassing = allPassing.filter(onPage);
603
+ const passing = onPagePassing.length ? onPagePassing : allPassing;
604
+ const enough = floor + (aim - floor) / 2;
605
+ const keepsHue = passing.filter((c) => c.chroma >= peak * TINT_HUE_SHARE && c.contrast >= enough);
606
+ const pick = closest(keepsHue) ?? closest(passing);
607
+ if (pick) return { value: pick.value, ref: pick.ref };
608
+ return extreme;
609
+ }
610
+ if (preference === "balanced" && best >= floor) {
611
+ const tints = [...own, ...steps("neutral", "neutral")].filter((c) => c.contrast >= floor && c.wcag >= wcagMin && c.contrast >= best * FILL_TINT_SHARE);
612
+ tints.sort((a, b) => a.contrast - b.contrast || Number(b.own) - Number(a.own));
613
+ if (tints[0]) return { value: tints[0].value, ref: tints[0].ref };
614
+ }
615
+ return extreme;
616
+ }
617
+ 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
618
  const activeShades = shadeKeys(shades);
574
- const findFg = (bgShade) => {
619
+ const findFg = (bgShade, role, grounds = []) => {
575
620
  const views = [
576
621
  { background: scale[bgShade], scale, neutral, gamut: gamuts.own }
577
622
  ];
@@ -579,30 +624,36 @@ function createExtendedSemanticColor(scale, neutral, white, black, mode, contras
579
624
  const o = gamuts.other;
580
625
  views.push({ background: o.scale[bgShade], scale: o.scale, neutral: o.neutral, gamut: o.gamut });
581
626
  }
582
- return findBestForeground(views, white, black, contrastAlgorithm, contrastTarget, colorName, activeShades);
627
+ return findBestForeground(views, white, black, contrastAlgorithm, contrastTarget, colorName, activeShades, role, preference, mode, wcagFloor, grounds);
583
628
  };
584
- const { DEFAULT: defaultShade, subdued: subduedShade, highlight: highlightShade } = extendedBridgeShades(mode, seedShade, activeShades, overrides);
629
+ const { DEFAULT: offsetDefault, subdued: offsetSubdued } = extendedBridgeShades(mode, seedShade, activeShades, overrides);
630
+ const seedColour = parseToOklch(scale[bridgeShade(0, seedShade, activeShades)]);
631
+ const warm = mode === "light" && seedColour.c >= 0.05 && (seedColour.h ?? 0) >= 40 && (seedColour.h ?? 0) <= 115;
632
+ const subduedShade = warm && !overrides?.subdued ? snapShade(200, activeShades) : offsetSubdued;
633
+ const seedStep = bridgeShade(0, seedShade, activeShades);
634
+ const blackLabel = (shade) => getContrastRatio(scale[shade], black) > getContrastRatio(scale[shade], white);
635
+ const bridgeDefault = mode === "dark" && !overrides?.DEFAULT && offsetDefault !== seedStep && blackLabel(seedStep) ? seedStep : offsetDefault;
636
+ const fillViews = [{ scale, gamut: gamuts.own }, ...gamuts.other ? [{ scale: gamuts.other.scale, gamut: gamuts.other.gamut }] : []];
637
+ const defaultShade = colorName === "primary" || overrides?.DEFAULT ? bridgeDefault : settleFill(scale, bridgeDefault, contrastTarget, activeShades, fillViews);
585
638
  const DEFAULT = scale[defaultShade];
586
639
  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);
640
+ const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(defaultShade, "fill");
641
+ 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
642
  return {
592
643
  DEFAULT,
593
644
  foreground: fgResult.value,
594
645
  subdued,
595
646
  "subdued-foreground": sfgResult.value,
596
- highlight,
597
- "highlight-foreground": hfgResult.value,
647
+ hover: stateOf(DEFAULT, fgResult.value, HOVER_SHIFT, gamuts.own),
648
+ active: stateOf(DEFAULT, fgResult.value, ACTIVE_SHIFT, gamuts.own),
598
649
  _refs: {
599
650
  foreground: fgResult.ref,
600
- "subdued-foreground": sfgResult.ref,
601
- "highlight-foreground": hfgResult.ref
602
- }
651
+ "subdued-foreground": sfgResult.ref
652
+ },
653
+ _shades: { DEFAULT: defaultShade, subdued: subduedShade }
603
654
  };
604
655
  }
605
- function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }) {
656
+ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }, pageGrounds = []) {
606
657
  const contrastAlgorithm = options?.contrastAlgorithm ?? "wcag";
607
658
  const contrastTarget = options?.contrastTarget ?? "aa";
608
659
  const shadeOverrides = options?.shadeOverrides ?? {};
@@ -632,19 +683,28 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
632
683
  }
633
684
  } : {}
634
685
  });
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)]
686
+ const canvas = pageGrounds[0];
687
+ const whitePage = mode === "light" && !!canvas && parseToOklch(canvas).l > parseToOklch(neutral[snapShade(25, activeShades)]).l + 5e-3;
688
+ const mutedBg = snapShade(mode === "light" ? whitePage ? 50 : 100 : 800, activeShades);
689
+ const mutedFg = accessibleStep(
690
+ neutral,
691
+ mode === "light" ? 500 : 200,
692
+ mode,
693
+ [neutral[mutedBg]],
694
+ contrastTarget === "aaa" ? 7 : 4.5,
695
+ activeShades
696
+ );
697
+ const muted = {
698
+ DEFAULT: neutral[mutedBg],
699
+ foreground: neutral[mutedFg],
700
+ _shades: { DEFAULT: mutedBg, foreground: mutedFg }
641
701
  };
642
702
  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")),
703
+ primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades, anchor("primary"), gamutsFor("primary"), options?.foreground, options?.wcagFloor, pageGrounds),
704
+ neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades, anchor("neutral"), gamutsFor("neutral"), options?.foreground, options?.wcagFloor, pageGrounds),
705
+ success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades, anchor("success"), gamutsFor("success"), options?.foreground, options?.wcagFloor, pageGrounds),
706
+ destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades, anchor("destructive"), gamutsFor("destructive"), options?.foreground, options?.wcagFloor, pageGrounds),
707
+ warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, anchor("warning"), gamutsFor("warning"), options?.foreground, options?.wcagFloor, pageGrounds),
648
708
  muted
649
709
  };
650
710
  const coreColors = [
@@ -660,7 +720,7 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
660
720
  "yellow"
661
721
  ];
662
722
  for (const [colorName, colorValue] of Object.entries(palette)) {
663
- if (!coreColors.includes(colorName) && typeof colorValue !== "string" && colorValue) {
723
+ if (!coreColors.includes(colorName) && !options?.decorative?.includes(colorName) && typeof colorValue !== "string" && colorValue) {
664
724
  tokens[colorName] = createExtendedSemanticColor(
665
725
  colorValue,
666
726
  neutral,
@@ -673,13 +733,23 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
673
733
  colorName,
674
734
  activeShades,
675
735
  anchor(colorName),
676
- gamutsFor(colorName)
736
+ gamutsFor(colorName),
737
+ options?.foreground,
738
+ options?.wcagFloor,
739
+ pageGrounds
677
740
  );
678
741
  }
679
742
  }
680
743
  return tokens;
681
744
  }
682
- function generateSurfaceTokens(palette, mode, overrides, shades) {
745
+ function textOn(bg, neutral, white, black, active) {
746
+ const light = getContrastRatio(bg, black) > getContrastRatio(bg, white);
747
+ if (!light) return { value: white, ref: { type: "white" } };
748
+ const step = accessibleStep(neutral, snapShade(800, active), "light", [bg], 7, active);
749
+ if (getContrastRatio(bg, neutral[step]) >= 4.5) return { value: neutral[step], ref: { type: "shade", shade: step } };
750
+ return { value: black, ref: { type: "black" } };
751
+ }
752
+ function generateSurfaceTokens(palette, mode, overrides, shades, colors, backgrounds) {
683
753
  const activeShades = shadeKeys(shades);
684
754
  const snap = (shade) => snapShade(shade, activeShades);
685
755
  const neutral = palette.neutral;
@@ -696,12 +766,22 @@ function generateSurfaceTokens(palette, mode, overrides, shades) {
696
766
  const val = snap(raw);
697
767
  return { value: neutral[val], ref: { type: "shade", shade: val } };
698
768
  }
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");
769
+ const ground = (key) => {
770
+ const given = colors?.[key];
771
+ if (!given) return { bg: resolve(key), fg: resolve(`${key}-foreground`) };
772
+ const bg = { value: given, ref: { type: "value", value: given } };
773
+ return { bg, fg: overrides?.[`${key}-foreground`] ? resolve(`${key}-foreground`) : textOn(given, neutral, white, black, activeShades) };
774
+ };
775
+ const { bg: canvas, fg: canvasFg } = ground("canvas");
776
+ const { bg: surface, fg: surfaceFg } = ground("surface");
777
+ const { bg: overlay, fg: overlayFg } = ground("overlay");
778
+ const named = {};
779
+ for (const name of Object.keys(backgrounds ?? {}).sort()) {
780
+ const def = backgrounds[name];
781
+ const value = mode === "dark" ? def.dark ?? def.light : def.light;
782
+ const fg = textOn(value, neutral, white, black, activeShades);
783
+ named[name] = { DEFAULT: value, foreground: fg.value, _refs: { DEFAULT: { type: "value", value }, foreground: fg.ref } };
784
+ }
705
785
  return {
706
786
  canvas: canvas.value,
707
787
  "canvas-foreground": canvasFg.value,
@@ -716,10 +796,11 @@ function generateSurfaceTokens(palette, mode, overrides, shades) {
716
796
  "surface-foreground": surfaceFg.ref,
717
797
  overlay: overlay.ref,
718
798
  "overlay-foreground": overlayFg.ref
719
- }
799
+ },
800
+ ...Object.keys(named).length ? { backgrounds: named } : {}
720
801
  };
721
802
  }
722
- function generateUtilityTokens(palette, mode, overrides, shades, seedShade) {
803
+ function generateUtilityTokens(palette, mode, overrides, shades, seedShade, grounds) {
723
804
  const activeShades = shadeKeys(shades);
724
805
  const snap = (shade) => snapShade(shade, activeShades);
725
806
  const neutral = palette.neutral;
@@ -727,13 +808,23 @@ function generateUtilityTokens(palette, mode, overrides, shades, seedShade) {
727
808
  if (!neutral || !primary) {
728
809
  throw new Error("Required palettes not found");
729
810
  }
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) };
811
+ const NON_TEXT = 3;
812
+ const ring = grounds ? accessibleStep(primary, ringBridgeShade(mode, seedShade, activeShades), mode, grounds, NON_TEXT, activeShades) : ringBridgeShade(mode, seedShade, activeShades);
813
+ const inputStart = snap(mode === "light" ? 200 : 800);
814
+ const input = grounds ? accessibleStep(neutral, inputStart, mode, grounds, NON_TEXT, activeShades) : inputStart;
815
+ const defaults = mode === "light" ? { border: snap(200), input, ring, shadow: snap(950) } : { border: snap(800), input, ring, shadow: snap(950) };
816
+ const steps = {
817
+ border: overrides?.border ? snap(overrides.border) : defaults.border,
818
+ input: overrides?.input ? snap(overrides.input) : defaults.input,
819
+ ring: overrides?.ring ? snap(overrides.ring) : defaults.ring,
820
+ shadow: overrides?.shadow ? snap(overrides.shadow) : defaults.shadow
821
+ };
732
822
  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]
823
+ border: neutral[steps.border],
824
+ input: neutral[steps.input],
825
+ ring: primary[steps.ring],
826
+ shadow: neutral[steps.shadow],
827
+ _steps: steps
737
828
  };
738
829
  }
739
830
  function checkSemanticContrast(tokens, gamut = "srgb") {
@@ -763,16 +854,6 @@ function checkSemanticContrast(tokens, gamut = "srgb") {
763
854
  passesAA: meetsWCAG_AA(subduedRatio),
764
855
  passesAAA: subduedRatio >= 7
765
856
  };
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
857
  }
777
858
  const mutedRatio = getContrastRatio(
778
859
  tokens.muted.DEFAULT,
@@ -803,11 +884,6 @@ function checkSemanticContrastAPCA(tokens, gamut = "srgb") {
803
884
  lcValue: Math.round(subduedLc * 10) / 10,
804
885
  level: classifyAPCALevel(subduedLc)
805
886
  };
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
887
  }
812
888
  const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT, gamut);
813
889
  results["muted"] = {
@@ -972,26 +1048,35 @@ function generateTokens(brandColors, options) {
972
1048
  };
973
1049
  const primitives = generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "srgb" });
974
1050
  const wide = options?.gamut === "p3" ? generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "p3" }) : void 0;
1051
+ const grounds = (palette, mode) => {
1052
+ const surface = generateSurfaceTokens(palette, mode, options?.surfaceShadeOverrides?.[mode], options?.shades, options?.surfaceColors?.[mode], options?.backgrounds);
1053
+ const light = (c) => getContrastRatio(c, "#000000") > getContrastRatio(c, "#ffffff");
1054
+ const pageIsLight = light(surface.canvas);
1055
+ const named = Object.values(surface.backgrounds ?? {}).map((b) => b.DEFAULT).filter((c) => light(c) === pageIsLight);
1056
+ return [surface.canvas, surface.surface, surface.overlay, ...named];
1057
+ };
975
1058
  const colourTiers = (palette, gamuts) => ({
976
1059
  primitives: palette,
977
1060
  semantic: {
978
- light: generateSemanticTokens(palette, "light", options, gamuts),
979
- dark: generateSemanticTokens(palette, "dark", options, gamuts)
1061
+ light: generateSemanticTokens(palette, "light", options, gamuts, grounds(palette, "light")),
1062
+ dark: generateSemanticTokens(palette, "dark", options, gamuts, grounds(palette, "dark"))
980
1063
  },
981
1064
  surface: {
982
- light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades),
983
- dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades)
1065
+ light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades, options?.surfaceColors?.light, options?.backgrounds),
1066
+ dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades, options?.surfaceColors?.dark, options?.backgrounds)
984
1067
  },
985
1068
  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)
1069
+ light: generateUtilityTokens(palette, "light", options?.utilityShadeOverrides?.light, options?.shades, options?.seedShade, grounds(palette, "light")),
1070
+ dark: generateUtilityTokens(palette, "dark", options?.utilityShadeOverrides?.dark, options?.shades, options?.seedShade, grounds(palette, "dark"))
988
1071
  }
989
1072
  });
1073
+ const tiers = colourTiers(primitives, {
1074
+ own: "srgb",
1075
+ ...wide ? { other: { gamut: "p3", palette: wide } } : {}
1076
+ });
990
1077
  return {
991
- ...colourTiers(primitives, {
992
- own: "srgb",
993
- ...wide ? { other: { gamut: "p3", palette: wide } } : {}
994
- }),
1078
+ ...tiers,
1079
+ pageContrast: { light: pageContrast(tiers, "light"), dark: pageContrast(tiers, "dark") },
995
1080
  spacing: {},
996
1081
  typography: defaultTypography,
997
1082
  radii: defaultRadii,
@@ -999,6 +1084,57 @@ function generateTokens(brandColors, options) {
999
1084
  ...wide ? { p3: colourTiers(wide, { own: "p3", other: { gamut: "srgb", palette: primitives } }) } : {}
1000
1085
  };
1001
1086
  }
1087
+ function pageContrast(tiers, mode) {
1088
+ const surface = tiers.surface[mode];
1089
+ const grounds = { canvas: surface.canvas, surface: surface.surface, overlay: surface.overlay };
1090
+ for (const [name, bg] of Object.entries(surface.backgrounds ?? {})) grounds[`surface-${name}`] = bg.DEFAULT;
1091
+ const colours = {};
1092
+ for (const [name, colour] of Object.entries(tiers.semantic[mode])) {
1093
+ colours[name === "muted" ? "muted-foreground" : name] = name === "muted" ? colour.foreground : colour.DEFAULT;
1094
+ }
1095
+ const out = {};
1096
+ for (const name of Object.keys(colours).sort()) {
1097
+ out[name] = {};
1098
+ for (const ground of Object.keys(grounds)) {
1099
+ out[name][ground] = Math.round(getContrastRatio(colours[name], grounds[ground]) * 100) / 100;
1100
+ }
1101
+ }
1102
+ return out;
1103
+ }
1104
+
1105
+ // src/usage.ts
1106
+ var TEXT_FLOOR = { "aa-large": 3, aa: 4.5, aaa: 7 };
1107
+ function usageReport(tokens, target = "aa") {
1108
+ const floors = { text: TEXT_FLOOR[target], shape: 3 };
1109
+ const verdict = (r) => r >= floors.text ? "text" : r >= floors.shape ? "shape" : "none";
1110
+ const mode = (m) => {
1111
+ const surface = tokens.surface[m];
1112
+ const grounds = { canvas: surface.canvas, surface: surface.surface, overlay: surface.overlay };
1113
+ for (const [name, bg] of Object.entries(surface.backgrounds ?? {})) grounds[`surface-${name}`] = bg.DEFAULT;
1114
+ const colours = {};
1115
+ for (const [name, colour] of Object.entries(tokens.semantic[m])) {
1116
+ if (name === "muted") {
1117
+ colours["muted-foreground"] = colour.foreground;
1118
+ continue;
1119
+ }
1120
+ colours[name] = colour.DEFAULT;
1121
+ colours[`${name}-subdued-foreground`] = colour["subdued-foreground"];
1122
+ }
1123
+ const out = {};
1124
+ for (const name of Object.keys(colours).sort()) {
1125
+ out[name] = {};
1126
+ for (const g of Object.keys(grounds).sort()) {
1127
+ const ratio = Math.round(getContrastRatio(colours[name], grounds[g]) * 100) / 100;
1128
+ out[name][g] = { ratio, use: verdict(ratio) };
1129
+ }
1130
+ }
1131
+ return out;
1132
+ };
1133
+ return { schema: "designtools.usage/1", target, floors, light: mode("light"), dark: mode("dark") };
1134
+ }
1135
+ function serialiseUsage(report) {
1136
+ return JSON.stringify(report, null, 2) + "\n";
1137
+ }
1002
1138
 
1003
1139
  // src/export.ts
1004
1140
  function formatColor(color, format) {
@@ -1048,7 +1184,7 @@ function getExtendedMappings(mode, colorName, overrides, active, anchors) {
1048
1184
  return extendedBridgeShades(mode, seedShade, active, overrides?.[colorName]?.[mode]);
1049
1185
  }
1050
1186
  var MUTED_SHADE_MAPPINGS = {
1051
- light: { DEFAULT: 25, foreground: 500 },
1187
+ light: { DEFAULT: 100, foreground: 500 },
1052
1188
  dark: { DEFAULT: 800, foreground: 200 }
1053
1189
  };
1054
1190
  var UTILITY_SHADE_MAPPINGS = {
@@ -1076,7 +1212,7 @@ function refToVar(ref) {
1076
1212
  return `var(--color-${ref.colorName}-${ref.shade})`;
1077
1213
  }
1078
1214
  }
1079
- function surfaceRefToVar(ref) {
1215
+ function surfaceRefToVar(ref, colorFormat) {
1080
1216
  switch (ref.type) {
1081
1217
  case "white":
1082
1218
  return "var(--color-white)";
@@ -1084,25 +1220,25 @@ function surfaceRefToVar(ref) {
1084
1220
  return "var(--color-black)";
1085
1221
  case "shade":
1086
1222
  return `var(--color-neutral-${ref.shade})`;
1223
+ // a background passed in as a colour is not on any ramp, so it is written as it is
1224
+ case "value":
1225
+ return formatColor(ref.value, colorFormat);
1087
1226
  }
1088
1227
  }
1089
1228
  function emitSurfaceVars(surface, colorFormat, indent) {
1090
1229
  const lines = [];
1091
- const keys = [
1092
- "canvas",
1093
- "canvas-foreground",
1094
- "surface",
1095
- "surface-foreground",
1096
- "overlay",
1097
- "overlay-foreground"
1098
- ];
1230
+ const keys = ["canvas", "canvas-foreground", "surface", "surface-foreground", "overlay", "overlay-foreground"];
1099
1231
  for (const key of keys) {
1100
1232
  if (surface._refs) {
1101
- lines.push(`${indent}--${key}: ${surfaceRefToVar(surface._refs[key])};`);
1233
+ lines.push(`${indent}--${key}: ${surfaceRefToVar(surface._refs[key], colorFormat)};`);
1102
1234
  } else {
1103
1235
  lines.push(`${indent}--${key}: ${formatColor(surface[key], colorFormat)};`);
1104
1236
  }
1105
1237
  }
1238
+ for (const [name, bg] of Object.entries(surface.backgrounds ?? {})) {
1239
+ lines.push(`${indent}--surface-${name}: ${surfaceRefToVar(bg._refs.DEFAULT, colorFormat)};`);
1240
+ lines.push(`${indent}--surface-${name}-foreground: ${surfaceRefToVar(bg._refs.foreground, colorFormat)};`);
1241
+ }
1106
1242
  return lines;
1107
1243
  }
1108
1244
  function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors) {
@@ -1111,36 +1247,41 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
1111
1247
  for (const name of extendedSemanticNames) {
1112
1248
  if (!inFamily(name)) continue;
1113
1249
  const color = tokens.semantic[mode][name];
1114
- const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1250
+ const mappings = color._shades ?? getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1115
1251
  lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
1116
1252
  lines.push(`${indent}--${name}-foreground: ${color._refs ? refToVar(color._refs.foreground) : formatColor(color.foreground, colorFormat)};`);
1117
1253
  lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
1118
1254
  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)};`);
1255
+ lines.push(`${indent}--${name}-hover: ${formatColor(color.hover, colorFormat)};`);
1256
+ lines.push(`${indent}--${name}-active: ${formatColor(color.active, colorFormat)};`);
1121
1257
  }
1122
1258
  for (const [name, color] of Object.entries(tokens.semantic[mode])) {
1123
1259
  if (!extendedSemanticNames.includes(name) && name !== "muted" && inFamily(name)) {
1124
1260
  const ext = color;
1125
- const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1261
+ const mappings = ext._shades ?? getExtendedMappings(mode, name, shadeOverrides, active, anchors);
1126
1262
  lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
1127
1263
  lines.push(`${indent}--${name}-foreground: ${ext._refs ? refToVar(ext._refs.foreground) : formatColor(ext.foreground, colorFormat)};`);
1128
1264
  lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
1129
1265
  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)};`);
1266
+ lines.push(`${indent}--${name}-hover: ${formatColor(ext.hover, colorFormat)};`);
1267
+ lines.push(`${indent}--${name}-active: ${formatColor(ext.active, colorFormat)};`);
1132
1268
  }
1133
1269
  }
1134
1270
  if (!scoped || inFamily("neutral")) {
1135
- const muted = MUTED_SHADE_MAPPINGS[mode];
1271
+ const muted = tokens.semantic[mode].muted._shades ?? MUTED_SHADE_MAPPINGS[mode];
1136
1272
  lines.push(`${indent}--muted: var(--color-neutral-${snapShade(muted.DEFAULT, active)});`);
1137
1273
  lines.push(`${indent}--muted-foreground: var(--color-neutral-${snapShade(muted.foreground, active)});`);
1138
1274
  }
1139
1275
  return lines;
1140
1276
  }
1141
- function emitUtilityVars(mode, indent, active, seedShade) {
1277
+ function emitUtilityVars(mode, indent, active, seedShade, steps) {
1142
1278
  const fixed = UTILITY_SHADE_MAPPINGS[mode];
1143
- const refs = [
1279
+ const refs = steps ? [
1280
+ ["border", `neutral-${steps.border}`],
1281
+ ["input", `neutral-${steps.input}`],
1282
+ ["ring", `primary-${steps.ring}`],
1283
+ ["shadow", `neutral-${steps.shadow}`]
1284
+ ] : [
1144
1285
  ["border", fixed.border],
1145
1286
  ["input", fixed.input],
1146
1287
  ["ring", `primary-${ringBridgeShade(mode, seedShade, active)}`],
@@ -1172,7 +1313,7 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
1172
1313
  }
1173
1314
  if (want.has("utility") && !scoped) {
1174
1315
  if (withComments) body.push(`${indent}/* Utility */`);
1175
- body.push(...emitUtilityVars(mode, indent, active, seedShade));
1316
+ body.push(...emitUtilityVars(mode, indent, active, seedShade, tokens.utility[mode]._steps));
1176
1317
  if (withComments) body.push("");
1177
1318
  }
1178
1319
  if (want.has("semantic")) {
@@ -1270,8 +1411,8 @@ function semanticAliasNames(tokens, sections, families) {
1270
1411
  `${n}-foreground`,
1271
1412
  `${n}-subdued`,
1272
1413
  `${n}-subdued-foreground`,
1273
- `${n}-highlight`,
1274
- `${n}-highlight-foreground`
1414
+ `${n}-hover`,
1415
+ `${n}-active`
1275
1416
  );
1276
1417
  };
1277
1418
  if (want.has("semantic")) {
@@ -1290,6 +1431,9 @@ function semanticAliasNames(tokens, sections, families) {
1290
1431
  "overlay",
1291
1432
  "overlay-foreground"
1292
1433
  );
1434
+ for (const name of Object.keys(tokens.surface.light.backgrounds ?? {})) {
1435
+ names.push(`surface-${name}`, `surface-${name}-foreground`);
1436
+ }
1293
1437
  }
1294
1438
  if (want.has("utility") && !scoped) {
1295
1439
  names.push("border", "input", "ring", "shadow");
@@ -1315,7 +1459,7 @@ function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases,
1315
1459
  }
1316
1460
  if (want.has("utility") && !scoped) {
1317
1461
  lines.push(`${indent}/* Utility */`);
1318
- lines.push(...emitUtilityVars(mode, indent, active, seedShade));
1462
+ lines.push(...emitUtilityVars(mode, indent, active, seedShade, tokens.utility[mode]._steps));
1319
1463
  lines.push("");
1320
1464
  }
1321
1465
  if (want.has("semantic")) {
@@ -1404,8 +1548,8 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
1404
1548
  foreground: `var(--${name}-foreground)`,
1405
1549
  subdued: `var(--${name}-subdued)`,
1406
1550
  "subdued-foreground": `var(--${name}-subdued-foreground)`,
1407
- highlight: `var(--${name}-highlight)`,
1408
- "highlight-foreground": `var(--${name}-highlight-foreground)`
1551
+ hover: `var(--${name}-hover)`,
1552
+ active: `var(--${name}-active)`
1409
1553
  };
1410
1554
  if (primitiveScale && typeof primitiveScale !== "string") {
1411
1555
  for (const [shade, color] of Object.entries(primitiveScale)) {
@@ -1422,8 +1566,8 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
1422
1566
  foreground: `var(--${name}-foreground)`,
1423
1567
  subdued: `var(--${name}-subdued)`,
1424
1568
  "subdued-foreground": `var(--${name}-subdued-foreground)`,
1425
- highlight: `var(--${name}-highlight)`,
1426
- "highlight-foreground": `var(--${name}-highlight-foreground)`
1569
+ hover: `var(--${name}-hover)`,
1570
+ active: `var(--${name}-active)`
1427
1571
  };
1428
1572
  if (primitiveScale && typeof primitiveScale !== "string") {
1429
1573
  for (const [shade, color] of Object.entries(primitiveScale)) {
@@ -1487,10 +1631,8 @@ function exportToDtcg(tokens) {
1487
1631
  "subdued-foreground": {
1488
1632
  $value: formatColorAs(color["subdued-foreground"], "hex")
1489
1633
  },
1490
- highlight: { $value: formatColorAs(color.highlight, "hex") },
1491
- "highlight-foreground": {
1492
- $value: formatColorAs(color["highlight-foreground"], "hex")
1493
- }
1634
+ hover: { $value: formatColorAs(color.hover, "hex") },
1635
+ active: { $value: formatColorAs(color.active, "hex") }
1494
1636
  };
1495
1637
  }
1496
1638
  for (const [colorName, color] of Object.entries(tokens.semantic[mode])) {
@@ -1504,10 +1646,8 @@ function exportToDtcg(tokens) {
1504
1646
  "subdued-foreground": {
1505
1647
  $value: formatColorAs(ext["subdued-foreground"], "hex")
1506
1648
  },
1507
- highlight: { $value: formatColorAs(ext.highlight, "hex") },
1508
- "highlight-foreground": {
1509
- $value: formatColorAs(ext["highlight-foreground"], "hex")
1510
- }
1649
+ hover: { $value: formatColorAs(ext.hover, "hex") },
1650
+ active: { $value: formatColorAs(ext.active, "hex") }
1511
1651
  };
1512
1652
  }
1513
1653
  }
@@ -1827,6 +1967,7 @@ export {
1827
1967
  familySeedShade,
1828
1968
  generatePrimitivePalette,
1829
1969
  getShade,
1970
+ bridgeShade,
1830
1971
  extendedBridgeShades,
1831
1972
  ringBridgeShade,
1832
1973
  generateSemanticTokens,
@@ -1838,6 +1979,8 @@ export {
1838
1979
  checkRampChroma,
1839
1980
  computeAccessibilityScore,
1840
1981
  generateTokens,
1982
+ usageReport,
1983
+ serialiseUsage,
1841
1984
  WIDE_GAMUT_QUERY,
1842
1985
  exportToCss,
1843
1986
  overrideContract,