@adea-ai/themes 0.11.6 → 0.11.12

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.
Files changed (103) hide show
  1. package/README.md +46 -0
  2. package/dist/accents.d.ts +19 -19
  3. package/dist/accents.d.ts.map +1 -1
  4. package/dist/accents.js +45 -93
  5. package/dist/accents.js.map +1 -1
  6. package/dist/adapters/base24.d.ts +21 -12
  7. package/dist/adapters/base24.d.ts.map +1 -1
  8. package/dist/adapters/base24.js +47 -72
  9. package/dist/adapters/base24.js.map +1 -1
  10. package/dist/adapters/css-variables.d.ts +29 -0
  11. package/dist/adapters/css-variables.d.ts.map +1 -0
  12. package/dist/adapters/css-variables.js +56 -0
  13. package/dist/adapters/css-variables.js.map +1 -0
  14. package/dist/adapters/css.d.ts +2 -4
  15. package/dist/adapters/css.d.ts.map +1 -1
  16. package/dist/adapters/css.js +7 -39
  17. package/dist/adapters/css.js.map +1 -1
  18. package/dist/adapters/shadcn.d.ts +8 -4
  19. package/dist/adapters/shadcn.d.ts.map +1 -1
  20. package/dist/adapters/shadcn.js +38 -57
  21. package/dist/adapters/shadcn.js.map +1 -1
  22. package/dist/adapters/shiki.d.ts +2 -10
  23. package/dist/adapters/shiki.d.ts.map +1 -1
  24. package/dist/adapters/shiki.js +16 -29
  25. package/dist/adapters/shiki.js.map +1 -1
  26. package/dist/adapters/tailwind.d.ts +3 -3
  27. package/dist/adapters/tailwind.d.ts.map +1 -1
  28. package/dist/adapters/tailwind.js +7 -28
  29. package/dist/adapters/tailwind.js.map +1 -1
  30. package/dist/adapters/xterm.d.ts.map +1 -1
  31. package/dist/adapters/xterm.js +9 -21
  32. package/dist/adapters/xterm.js.map +1 -1
  33. package/dist/base24-synthesis.d.ts +31 -0
  34. package/dist/base24-synthesis.d.ts.map +1 -0
  35. package/dist/base24-synthesis.js +25 -0
  36. package/dist/base24-synthesis.js.map +1 -0
  37. package/dist/derive.d.ts +1 -1
  38. package/dist/derive.d.ts.map +1 -1
  39. package/dist/derive.js +11 -26
  40. package/dist/derive.js.map +1 -1
  41. package/dist/index.d.ts +6 -5
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +6 -5
  44. package/dist/index.js.map +1 -1
  45. package/dist/normalize.d.ts +0 -8
  46. package/dist/normalize.d.ts.map +1 -1
  47. package/dist/normalize.js +105 -232
  48. package/dist/normalize.js.map +1 -1
  49. package/dist/oklch.d.ts +4 -10
  50. package/dist/oklch.d.ts.map +1 -1
  51. package/dist/oklch.js +18 -38
  52. package/dist/oklch.js.map +1 -1
  53. package/dist/policy.d.ts +14 -4
  54. package/dist/policy.d.ts.map +1 -1
  55. package/dist/policy.js +16 -4
  56. package/dist/policy.js.map +1 -1
  57. package/dist/render.d.ts +30 -0
  58. package/dist/render.d.ts.map +1 -0
  59. package/dist/render.js +74 -0
  60. package/dist/render.js.map +1 -0
  61. package/dist/schema.d.ts +2 -1
  62. package/dist/schema.d.ts.map +1 -1
  63. package/dist/schema.js +2 -1
  64. package/dist/schema.js.map +1 -1
  65. package/dist/sources.d.ts +26 -33
  66. package/dist/sources.d.ts.map +1 -1
  67. package/dist/sources.js +136 -305
  68. package/dist/sources.js.map +1 -1
  69. package/dist/step-search.d.ts +10 -0
  70. package/dist/step-search.d.ts.map +1 -0
  71. package/dist/step-search.js +21 -0
  72. package/dist/step-search.js.map +1 -0
  73. package/dist/theme-token-projection-script.d.ts +2 -0
  74. package/dist/theme-token-projection-script.js +2 -0
  75. package/dist/theme-token-projection.d.ts +35 -0
  76. package/dist/theme-token-projection.d.ts.map +1 -0
  77. package/dist/theme-token-projection.js +79 -0
  78. package/dist/theme-token-projection.js.map +1 -0
  79. package/dist/validate.d.ts +12 -17
  80. package/dist/validate.d.ts.map +1 -1
  81. package/dist/validate.js +33 -55
  82. package/dist/validate.js.map +1 -1
  83. package/package.json +14 -4
  84. package/src/accents.ts +57 -96
  85. package/src/adapters/base24.ts +53 -75
  86. package/src/adapters/css-variables.ts +67 -0
  87. package/src/adapters/css.ts +9 -54
  88. package/src/adapters/shadcn.ts +34 -60
  89. package/src/adapters/shiki.ts +17 -30
  90. package/src/adapters/tailwind.ts +7 -37
  91. package/src/adapters/xterm.ts +9 -21
  92. package/src/base24-synthesis.ts +29 -0
  93. package/src/derive.ts +12 -27
  94. package/src/index.ts +6 -5
  95. package/src/normalize.ts +140 -267
  96. package/src/oklch.ts +23 -38
  97. package/src/policy.ts +18 -4
  98. package/src/render.ts +82 -0
  99. package/src/schema.ts +2 -1
  100. package/src/sources.ts +160 -330
  101. package/src/step-search.ts +25 -0
  102. package/src/theme-token-projection.ts +126 -0
  103. package/src/validate.ts +46 -66
package/src/normalize.ts CHANGED
@@ -54,9 +54,10 @@ import type {
54
54
  ThemeAppearance,
55
55
  ThemeProvenance,
56
56
  } from './schema.js'
57
+ import { ANSI_KEYS } from './schema.js'
57
58
  import type { Base24Scheme, Base24Slot } from './adapters/base24.js'
58
- import { parseBase24Palette } from './adapters/base24.js'
59
- import type { Oklch } from './oklch.js'
59
+ import { BASE24_TO_ANSI, parseBase24Palette } from './adapters/base24.js'
60
+ import type { ContrastRepair, Oklch } from './oklch.js'
60
61
  import {
61
62
  canonical,
62
63
  contrastRatio,
@@ -65,21 +66,19 @@ import {
65
66
  formatOklch,
66
67
  mix,
67
68
  parseColor,
69
+ repairContrast,
68
70
  shiftLightness,
69
71
  } from './oklch.js'
70
- import type { ContrastRepair } from './oklch.js'
71
- import { repairContrast } from './oklch.js'
72
- import { ACCENT_SUBTLE_MINIMUM_ALPHA, primaryTintClears } from './accents.js'
72
+ import { ACCENT_SUBTLE_MINIMUM_ALPHA, tintCheck } from './accents.js'
73
73
  import {
74
74
  ACCENT_PREFERENCE,
75
75
  CONTRAST_FLOORS,
76
76
  MINIMUM_ACCENT_CHROMA,
77
77
  REPAIR_BUDGET,
78
+ REPAIR_STEP,
78
79
  } from './policy.js'
79
-
80
- // The catalogue's policy lives in its own module so the accent axis can read it
81
- // without importing the normalizer; it is re-exported here, where it has always been.
82
- export { ACCENT_PREFERENCE, CONTRAST_FLOORS, MINIMUM_ACCENT_CHROMA, REPAIR_BUDGET }
80
+ import { mostContrasting } from './render.js'
81
+ import { offsetSize, smallestOffset } from './step-search.js'
83
82
 
84
83
  /**
85
84
  * How much a repair changed the palette's colour.
@@ -194,11 +193,6 @@ function ladderDirection(background: Oklch): 1 | -1 {
194
193
  return background.l <= 0.5 ? 1 : -1
195
194
  }
196
195
 
197
- /** True when the declared variant and the measured canvas agree. */
198
- export function appearanceMatchesCanvas(appearance: ThemeAppearance, background: Oklch): boolean {
199
- return (background.l <= 0.5 ? 'dark' : 'light') === appearance
200
- }
201
-
202
196
  /**
203
197
  * Builds one rung of the surface ladder.
204
198
  *
@@ -214,11 +208,14 @@ function surfaceRung(background: Oklch, step: number, direction: 1 | -1): Oklch
214
208
  return { ...raised, c: Math.min(0.06, raised.c * chromaScale) }
215
209
  }
216
210
 
211
+ /** The ANSI roles Base24 does not name, which {@link buildAnsiRamp} assigns. */
212
+ type RampRole = Exclude<AnsiKey, keyof typeof BASE24_TO_ANSI>
213
+
217
214
  /**
218
215
  * Assigns the greyscale ANSI roles.
219
216
  *
220
- * Base24 does not name `black`, `white` or `brightBlack` — the background and
221
- * foreground slots do double duty — so the mapping is Adea's, and two things about
217
+ * Base24 does not name `black`, `brightBlack`, `white` or `brightWhite` — the
218
+ * background and foreground slots do double duty — so the mapping is Adea's, and two things about
222
219
  * it are not obvious.
223
220
  *
224
221
  * **It is appearance-aware, because the ANSI greyscale ramp means opposite things
@@ -235,15 +232,14 @@ function surfaceRung(background: Oklch, step: number, direction: 1 | -1): Oklch
235
232
  * and `base11`. Where a palette leaves those darker than its canvas, the foreground
236
233
  * ramp is used instead.
237
234
  *
238
- * Candidates are paired with `base01` and de-duplicated first, because light
239
- * schemes routinely set `base05`, `base06` and `base07` to the same value — without
235
+ * Candidates are de-duplicated first, because light schemes routinely set `base05`, `base06` and `base07` to the same value — without
240
236
  * de-duplication the ramp collapses to three members and the roles collide.
241
237
  */
242
238
  function buildAnsiRamp(
243
239
  palette: Record<Base24Slot, Oklch>,
244
240
  background: Oklch,
245
241
  appearance: ThemeAppearance
246
- ): { black: Oklch; white: Oklch; brightBlack: Oklch; brightWhite: Oklch } {
242
+ ): Record<RampRole, Oklch> {
247
243
  // Which slots are *foregrounds* depends on the appearance, because Base24's
248
244
  // background slots do double duty. `base01` is the "lighter background" — a
249
245
  // status-bar fill — so on a dark scheme it is a usable mid grey and on a light
@@ -272,8 +268,6 @@ function buildAnsiRamp(
272
268
  for (const candidate of candidates) {
273
269
  if (distinct.every((kept) => deltaEok(kept, candidate) > 0.02)) distinct.push(candidate)
274
270
  }
275
- // Strongest first: the ramp is ranked by measured contrast, not by slot number,
276
- // because a slot's contrast depends on which appearance the palette is.
277
271
  distinct.sort((a, b) => contrastRatio(b, background) - contrastRatio(a, background))
278
272
 
279
273
  const strongest = distinct[0] ?? palette.base05
@@ -377,38 +371,13 @@ function chooseAccent(
377
371
  for (const role of order) {
378
372
  const candidate = ansi[role]
379
373
  if (!candidate || candidate.c < MINIMUM_ACCENT_CHROMA) continue
380
- // Two tiers, as for the status roles: the link floor on the canvas, then the
381
- // component floor on a raised surface, applied in sequence.
382
- const onCanvas = repairRole(
374
+ const attempt = repairTwoTier(
383
375
  candidate,
384
- [background],
376
+ surfaces,
385
377
  anchor,
386
- CONTRAST_FLOORS.accent,
378
+ { canvas: CONTRAST_FLOORS.accent, raised: CONTRAST_FLOORS.accentRaised },
387
379
  REPAIR_BUDGET.accent
388
380
  )
389
- if (!onCanvas.repair.satisfied) {
390
- if (!fallback || onCanvas.repair.ratio > fallback.repair.ratio) {
391
- fallback = { role, repair: onCanvas.repair }
392
- }
393
- continue
394
- }
395
-
396
- const onRaised = repairRole(
397
- onCanvas.repair.color,
398
- surfaces.slice(1),
399
- anchor,
400
- CONTRAST_FLOORS.accentRaised,
401
- REPAIR_BUDGET.accent
402
- )
403
- const attempt = {
404
- repair: {
405
- color: onRaised.repair.color,
406
- ratio: onRaised.repair.ratio,
407
- delta: onCanvas.repair.delta + onRaised.repair.delta,
408
- satisfied: onRaised.repair.satisfied,
409
- },
410
- blended: onCanvas.blended || onRaised.blended,
411
- }
412
381
 
413
382
  if (attempt.repair.satisfied) {
414
383
  satisfying.push({
@@ -419,8 +388,6 @@ function chooseAccent(
419
388
  })
420
389
  continue
421
390
  }
422
- // Track the best attempt so a theme that cannot satisfy the floor still gets
423
- // its most legible candidate rather than an arbitrary one.
424
391
  if (!fallback || attempt.repair.ratio > fallback.repair.ratio) {
425
392
  fallback = { role, repair: attempt.repair }
426
393
  }
@@ -454,7 +421,7 @@ function chooseAccent(
454
421
  : `the declared accent slot ${preferred} could not clear the floor; used ${chosen.role} instead`,
455
422
  })
456
423
  }
457
- if (chosen.repair.delta > 0.01) {
424
+ if (chosen.repair.delta > 0) {
458
425
  findings.push({
459
426
  themeId,
460
427
  role: 'accent',
@@ -486,7 +453,8 @@ function chooseAccent(
486
453
  * Not derived from the appearance, which is the tempting shortcut and is wrong:
487
454
  * Catppuccin Mocha's blue accent is light enough that black is the legible label,
488
455
  * while Nord's is dark enough that white is. The pick is whichever of the
489
- * palette's own two extremes measures better, repaired if neither clears the floor.
456
+ * palette's canvas and foreground slots measures better, repaired if none clears
457
+ * the floor.
490
458
  */
491
459
  function chooseAccentForeground(
492
460
  themeId: string,
@@ -494,19 +462,18 @@ function chooseAccentForeground(
494
462
  palette: Record<Base24Slot, Oklch>
495
463
  ): { value: Oklch; findings: NormalizationFinding[] } {
496
464
  const findings: NormalizationFinding[] = []
497
- const candidates = [palette.base00, palette.base07, palette.base05, palette.base06]
498
-
499
- let best = candidates[0] as Oklch
500
- let bestRatio = contrastRatio(best, accent)
501
- for (const candidate of candidates.slice(1)) {
502
- const ratio = contrastRatio(candidate, accent)
503
- if (ratio > bestRatio) {
504
- best = candidate
505
- bestRatio = ratio
506
- }
507
- }
508
-
509
- const repair = repairContrast(best, accent, CONTRAST_FLOORS.accentForeground, 0.6)
465
+ const best = mostContrasting(accent, [
466
+ palette.base00,
467
+ palette.base07,
468
+ palette.base05,
469
+ palette.base06,
470
+ ])
471
+ const repair = repairContrast(
472
+ best,
473
+ accent,
474
+ CONTRAST_FLOORS.accentForeground,
475
+ REPAIR_BUDGET.accentForeground
476
+ )
510
477
  if (!repair.satisfied) {
511
478
  findings.push({
512
479
  themeId,
@@ -528,11 +495,6 @@ function chooseAccentForeground(
528
495
  * as likely to appear inside a card or a popover as on the canvas, and those rungs
529
496
  * are deliberately moved away from the canvas, so a colour tuned to exactly 4.5:1
530
497
  * on the background lands at roughly 4.0:1 on a card.
531
- *
532
- * The surfaces of one theme all sit on the same side of the canvas, so the surface
533
- * with the *lowest* current ratio is the furthest from the colour, and moving away
534
- * from it moves away from all of them. The worst surface is recomputed on every
535
- * step rather than chosen once, because a colour can cross between them.
536
498
  */
537
499
  function repairAcrossSurfaces(
538
500
  color: Oklch,
@@ -540,8 +502,6 @@ function repairAcrossSurfaces(
540
502
  minimum: number,
541
503
  budget: number
542
504
  ): ContrastRepair {
543
- // Measured in the canonical form the catalogue commits, so a repair cannot
544
- // converge on a value that rounding then pushes back below the floor.
545
505
  const worst = (candidate: Oklch): { surface: Oklch; ratio: number } => {
546
506
  const rounded = canonical(candidate)
547
507
  let result = { surface: surfaces[0] as Oklch, ratio: Number.POSITIVE_INFINITY }
@@ -557,25 +517,26 @@ function repairAcrossSurfaces(
557
517
  return { color, ratio: initial.ratio, delta: 0, satisfied: true }
558
518
  }
559
519
 
560
- const direction = contrastDirection(color, initial.surface)
561
- const step = 0.002
562
- const maximumSteps = Math.floor(budget / step)
563
-
564
- for (let index = 1; index <= maximumSteps; index += 1) {
565
- const candidate: Oklch = { ...color, l: color.l + direction * step * index }
566
- if (candidate.l <= 0 || candidate.l >= 1) break
567
- const measured = worst(candidate)
568
- if (measured.ratio >= minimum) {
569
- return {
570
- color: candidate,
571
- ratio: measured.ratio,
572
- delta: Number((step * index).toFixed(4)),
573
- satisfied: true,
574
- }
520
+ const step = REPAIR_STEP
521
+ const shifted = (offset: number): Oklch => ({ ...color, l: color.l + offset })
522
+ const offset = smallestOffset(
523
+ step,
524
+ Math.floor(budget / step),
525
+ [contrastDirection(color, initial.surface)],
526
+ (trial) => {
527
+ const candidate = shifted(trial)
528
+ return candidate.l > 0 && candidate.l < 1 && worst(candidate).ratio >= minimum
575
529
  }
576
- }
530
+ )
531
+ if (offset === undefined) return { color, ratio: initial.ratio, delta: 0, satisfied: false }
577
532
 
578
- return { color, ratio: initial.ratio, delta: 0, satisfied: false }
533
+ const repaired = shifted(offset)
534
+ return {
535
+ color: repaired,
536
+ ratio: worst(repaired).ratio,
537
+ delta: offsetSize(offset),
538
+ satisfied: true,
539
+ }
579
540
  }
580
541
 
581
542
  /**
@@ -594,9 +555,8 @@ function repairAcrossSurfaces(
594
555
  * The result is a muted, deeper version of the accent — which is what Everforest's
595
556
  * light themes actually look like.
596
557
  *
597
- * The blend is bound by the foreground, so it always terminates; and when even a
598
- * full blend cannot clear the floor, the theme is reported rather than shipped with
599
- * an invented colour.
558
+ * When even a full blend cannot clear the floor, the theme is reported rather than
559
+ * shipped with an invented colour.
600
560
  */
601
561
  function repairByBlending(
602
562
  color: Oklch,
@@ -613,8 +573,6 @@ function repairByBlending(
613
573
  return { color, ratio: worstRatio(color), delta: 0, satisfied: false }
614
574
  }
615
575
 
616
- // The smallest blend that works: 20 iterations resolve the amount far below the
617
- // precision the catalogue is committed at.
618
576
  for (let iteration = 0; iteration < 20; iteration += 1) {
619
577
  const mid = (low + high) / 2
620
578
  if (worstRatio(mix(color, anchor, mid)) >= minimum) high = mid
@@ -653,6 +611,38 @@ function repairRole(
653
611
  return { repair: lightness, blended: false }
654
612
  }
655
613
 
614
+ /**
615
+ * The repair chain applied in two tiers: the `canvas` floor against the background,
616
+ * then the `raised` floor against the raised rungs, starting from the first result.
617
+ */
618
+ function repairTwoTier(
619
+ color: Oklch,
620
+ surfaces: readonly Oklch[],
621
+ anchor: Oklch,
622
+ floors: { canvas: number; raised: number },
623
+ budget: number
624
+ ): { repair: ContrastRepair; blended: boolean } {
625
+ const onCanvas = repairRole(color, surfaces.slice(0, 1), anchor, floors.canvas, budget)
626
+ if (!onCanvas.repair.satisfied) return onCanvas
627
+
628
+ const onRaised = repairRole(
629
+ onCanvas.repair.color,
630
+ surfaces.slice(1),
631
+ anchor,
632
+ floors.raised,
633
+ budget
634
+ )
635
+ return {
636
+ repair: {
637
+ color: onRaised.repair.color,
638
+ ratio: onRaised.repair.ratio,
639
+ delta: onCanvas.repair.delta + onRaised.repair.delta,
640
+ satisfied: onRaised.repair.satisfied,
641
+ },
642
+ blended: onCanvas.blended || onRaised.blended,
643
+ }
644
+ }
645
+
656
646
  /**
657
647
  * The Base24 slots that carry a hue rather than a grey.
658
648
  *
@@ -695,8 +685,7 @@ const NORMAL_HUE_SLOTS = ['base08', 'base0A', 'base0B', 'base0C', 'base0D', 'bas
695
685
  * Moving the group by one step instead transposes the whole set: every hue keeps its
696
686
  * angle and chroma exactly, every relative lightness is preserved, and the palette
697
687
  * stays internally consistent. The step is the smallest one that brings every *normal*
698
- * hue to the floor, measured in the canonical form the catalogue commits so that
699
- * rounding cannot push one back under.
688
+ * hue to the floor.
700
689
  */
701
690
  function transposeHues(
702
691
  themeId: string,
@@ -718,19 +707,11 @@ function transposeHues(
718
707
  // Both directions are tried, nearest first, as in `repairContrast`: a dark canvas
719
708
  // needs its hues lightened and a light canvas needs them darkened, and a palette
720
709
  // moved to the other kind of canvas is the whole point of the operation.
721
- const step = 0.002
722
- let chosen: number | undefined
723
- for (let index = 1; index <= Math.floor(0.45 / step) && chosen === undefined; index += 1) {
724
- for (const direction of [1, -1] as const) {
725
- const candidate = step * index * direction
726
- const probe = shiftLightness(palette[NORMAL_HUE_SLOTS[0]], candidate)
727
- if (probe.l <= 0.02 || probe.l >= 0.98) continue
728
- if (worst(candidate) >= floor) {
729
- chosen = candidate
730
- break
731
- }
732
- }
733
- }
710
+ const step = REPAIR_STEP
711
+ const chosen = smallestOffset(step, Math.floor(0.45 / step), [1, -1], (offset) => {
712
+ const probe = shiftLightness(palette[NORMAL_HUE_SLOTS[0]], offset)
713
+ return probe.l > 0.02 && probe.l < 0.98 && worst(offset) >= floor
714
+ })
734
715
 
735
716
  if (chosen === undefined) {
736
717
  findings.push({
@@ -750,7 +731,7 @@ function transposeHues(
750
731
  })
751
732
 
752
733
  return Object.fromEntries(
753
- CHROMATIC_SLOTS.map((slot) => [slot, shiftLightness(palette[slot], chosen as number)])
734
+ CHROMATIC_SLOTS.map((slot) => [slot, shiftLightness(palette[slot], chosen)])
754
735
  ) as Partial<Record<Base24Slot, Oklch>>
755
736
  }
756
737
 
@@ -770,10 +751,6 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
770
751
  const findings: NormalizationFinding[] = []
771
752
  const vendored = parseBase24Palette(source.scheme.palette)
772
753
 
773
- // Corrections are applied to the palette before anything derives from it, so the
774
- // surface ladder, the accent and every floor are computed from the value that
775
- // will actually ship. Correcting the output instead would leave the ladder built
776
- // around a background that is not the background.
777
754
  const palette: Record<Base24Slot, Oklch> = { ...vendored }
778
755
  for (const [slot, value] of Object.entries(source.palette ?? {})) {
779
756
  const parsed = parseColor(value as string)
@@ -787,9 +764,6 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
787
764
  })
788
765
  }
789
766
 
790
- // The hue transpose, applied at the same stage as the corrections and for the same
791
- // reason: the semantic roles derive from these slots, so transposing afterwards
792
- // would leave `error` built from the untransposed red and the two disagreeing.
793
767
  if (source.hueTranspose) {
794
768
  const transposed = transposeHues(source.id, palette, source.hueTranspose.floor, findings)
795
769
  for (const [slot, value] of Object.entries(transposed)) {
@@ -798,8 +772,8 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
798
772
  }
799
773
 
800
774
  const background = palette.base00
801
-
802
- const appearance: ThemeAppearance = ladderDirection(background) === 1 ? 'dark' : 'light'
775
+ const direction = ladderDirection(background)
776
+ const appearance: ThemeAppearance = direction === 1 ? 'dark' : 'light'
803
777
  if (appearance !== source.appearance) {
804
778
  findings.push({
805
779
  themeId: source.id,
@@ -809,8 +783,6 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
809
783
  })
810
784
  }
811
785
 
812
- const direction = ladderDirection(background)
813
-
814
786
  const surface = surfaceRung(background, SURFACE_STEPS.surface, direction)
815
787
  const surfaceElevated = surfaceRung(background, SURFACE_STEPS.surfaceElevated, direction)
816
788
  const surfaceHover = surfaceRung(background, SURFACE_STEPS.surfaceHover, direction)
@@ -853,8 +825,6 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
853
825
  info: '',
854
826
  }
855
827
 
856
- // Body text first: several palettes publish a foreground that fails on their own
857
- // background, and every other role's floors are measured against the canvas.
858
828
  const textFloor = source.textFloor ?? CONTRAST_FLOORS.text
859
829
  const textRepair = repairAcrossSurfaces(palette.base05, surfaces, textFloor, REPAIR_BUDGET.text)
860
830
  const text = emit(source.id, 'text', textRepair, textFloor)
@@ -869,7 +839,7 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
869
839
  mix(textRepair.color, background, 0.32),
870
840
  surfaces,
871
841
  CONTRAST_FLOORS.textMuted,
872
- 0.3
842
+ REPAIR_BUDGET.textMuted
873
843
  )
874
844
  const muted = emit(source.id, 'textMuted', mutedRepair, CONTRAST_FLOORS.textMuted)
875
845
  colors.textMuted = muted.value
@@ -893,7 +863,7 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
893
863
  palette.base03,
894
864
  surfaces,
895
865
  CONTRAST_FLOORS.textSubtle,
896
- 0.3
866
+ REPAIR_BUDGET.textSubtle
897
867
  )
898
868
  const paletteSubtleContrast = contrastRatio(fromPalette.color, background)
899
869
 
@@ -904,7 +874,7 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
904
874
  mix(textRepair.color, background, 0.55),
905
875
  surfaces,
906
876
  CONTRAST_FLOORS.textSubtle,
907
- 0.3
877
+ REPAIR_BUDGET.textSubtle
908
878
  )
909
879
 
910
880
  const subtle = emit(source.id, 'textSubtle', subtleRepair, CONTRAST_FLOORS.textSubtle)
@@ -919,20 +889,6 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
919
889
  })
920
890
  }
921
891
 
922
- /**
923
- * The status roles, each with fallback candidates.
924
- *
925
- * A palette's yellow is chosen to be *a* yellow, not to be legible as text — Ayu
926
- * Light's `#ffcc66` measures 1.9:1 on its own near-white canvas, and no amount of
927
- * lightness repair fixes that without turning it into olive, which is no longer
928
- * Ayu. So each status role has candidates and takes the first that clears the
929
- * floor: warnings fall back to the palette's orange slot, which is Base24's
930
- * constants colour and in practice the warning colour this palette already uses;
931
- * reds and greens fall back to their bright variants.
932
- *
933
- * The order is not "brightest first" — on a light canvas the bright variants are
934
- * worse, so the declared ANSI colour is always tried before its bright sibling.
935
- */
936
892
  /*
937
893
  * Body text gets enough headroom above the secondary rung to stay the stronger of the
938
894
  * two.
@@ -966,6 +922,20 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
966
922
  }
967
923
  }
968
924
 
925
+ /**
926
+ * The status roles, each with fallback candidates.
927
+ *
928
+ * A palette's yellow is chosen to be *a* yellow, not to be legible as text — Ayu
929
+ * Light's `#ffcc66` measures 1.9:1 on its own near-white canvas, and no amount of
930
+ * lightness repair fixes that without turning it into olive, which is no longer
931
+ * Ayu. So each status role has candidates and takes the first that clears the
932
+ * floor: warnings fall back to the palette's orange slot, which is Base24's
933
+ * constants colour and in practice the warning colour this palette already uses;
934
+ * reds and greens fall back to their bright variants.
935
+ *
936
+ * The order is not "brightest first" — on a light canvas the bright variants are
937
+ * worse, so the declared ANSI colour is always tried before its bright sibling.
938
+ */
969
939
  const statusCandidates = {
970
940
  error: [palette.base08, palette.base12],
971
941
  success: [palette.base0B, palette.base14],
@@ -978,42 +948,17 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
978
948
  let chosen: { repair: ContrastRepair; blended: boolean } | undefined
979
949
 
980
950
  for (const candidate of candidates) {
981
- // Two tiers, applied in sequence: the body floor on the canvas, then the
982
- // indicator floor on the raised surfaces. Both push the colour the same way,
983
- // so applying the second to the result of the first cannot undo it.
984
- //
985
951
  // The blend anchor is the *repaired* text colour rather than the palette's
986
952
  // raw foreground: when a palette's own body text fails the floor on a raised
987
953
  // surface, blending toward the raw value inherits that failure and the blend
988
954
  // has no valid endpoint.
989
- const onCanvas = repairRole(
955
+ const combined = repairTwoTier(
990
956
  candidate,
991
- [background],
992
- textRepair.color,
993
- CONTRAST_FLOORS.status,
994
- REPAIR_BUDGET.status
995
- )
996
- if (!onCanvas.repair.satisfied) {
997
- if (!chosen || onCanvas.repair.ratio > chosen.repair.ratio) chosen = onCanvas
998
- continue
999
- }
1000
-
1001
- const onRaised = repairRole(
1002
- onCanvas.repair.color,
1003
- surfaces.slice(1),
957
+ surfaces,
1004
958
  textRepair.color,
1005
- CONTRAST_FLOORS.statusRaised,
959
+ { canvas: CONTRAST_FLOORS.status, raised: CONTRAST_FLOORS.statusRaised },
1006
960
  REPAIR_BUDGET.status
1007
961
  )
1008
- const combined: { repair: ContrastRepair; blended: boolean } = {
1009
- repair: {
1010
- color: onRaised.repair.color,
1011
- ratio: onRaised.repair.ratio,
1012
- delta: onCanvas.repair.delta + onRaised.repair.delta,
1013
- satisfied: onRaised.repair.satisfied,
1014
- },
1015
- blended: onCanvas.blended || onRaised.blended,
1016
- }
1017
962
 
1018
963
  if (combined.repair.satisfied) {
1019
964
  chosen = combined
@@ -1043,40 +988,20 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
1043
988
 
1044
989
  const ramp = buildAnsiRamp(palette, background, appearance)
1045
990
 
1046
- const ansi: AdeaAnsi = {
1047
- black: formatOklch(ramp.black),
1048
- red: formatOklch(palette.base08),
1049
- green: formatOklch(palette.base0B),
1050
- yellow: formatOklch(palette.base0A),
1051
- blue: formatOklch(palette.base0D),
1052
- magenta: formatOklch(palette.base0E),
1053
- cyan: formatOklch(palette.base0C),
1054
- white: formatOklch(ramp.white),
1055
- brightBlack: formatOklch(ramp.brightBlack),
1056
- brightRed: formatOklch(palette.base12),
1057
- brightGreen: formatOklch(palette.base14),
1058
- brightYellow: formatOklch(palette.base13),
1059
- brightBlue: formatOklch(palette.base16),
1060
- brightMagenta: formatOklch(palette.base17),
1061
- brightCyan: formatOklch(palette.base15),
1062
- brightWhite: formatOklch(ramp.brightWhite),
1063
- }
1064
-
991
+ const slotOf: Partial<Record<AnsiKey, Base24Slot>> = BASE24_TO_ANSI
1065
992
  const ansiColors = Object.fromEntries(
1066
- Object.entries(ansi).map(([key]) => [key, palette[ansiToSlot(key, palette, ramp)]])
993
+ ANSI_KEYS.map((key) => {
994
+ const slot = slotOf[key]
995
+ return [key, slot ? palette[slot] : ramp[key as RampRole]]
996
+ })
1067
997
  ) as Record<AnsiKey, Oklch>
998
+ const ansi = Object.fromEntries(
999
+ ANSI_KEYS.map((key) => [key, formatOklch(ansiColors[key])])
1000
+ ) as unknown as AdeaAnsi
1068
1001
 
1069
1002
  const accent = chooseAccent(source.id, ansiColors, surfaces, source.accentSlot, textRepair.color)
1070
1003
  findings.push(...accent.findings)
1071
1004
  colors.accent = formatOklch(accent.repair.color)
1072
- if (accent.repair.delta > 0) {
1073
- findings.push({
1074
- themeId: source.id,
1075
- role: 'accent',
1076
- kind: 'repaired',
1077
- message: `lightness moved ${accent.repair.delta.toFixed(3)} to reach ${CONTRAST_FLOORS.accent}:1 against the canvas`,
1078
- })
1079
- }
1080
1005
 
1081
1006
  const accentForeground = chooseAccentForeground(source.id, accent.repair.color, palette)
1082
1007
  colors.accentForeground = formatOklch(accentForeground.value)
@@ -1087,8 +1012,8 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
1087
1012
  * applies to it either way.
1088
1013
  *
1089
1014
  * A composition can carry its structure donor's selection explicitly, and taking that
1090
- * on trust is how Adea Light shipped a selection measuring 1.11:1 against its own
1091
- * canvas — Nord's is deliberately subtle, and subtle is not the same as invisible. An
1015
+ * on trust once shipped an Adea Light selection measuring 1.11:1 against its own
1016
+ * canvas — a donor's deliberately subtle selection is not the same as visible. An
1092
1017
  * invisible selection is a functional defect rather than a matter of taste: the user
1093
1018
  * cannot see what they have selected. So the declared value is used as given and still
1094
1019
  * measured, and moving it is reported.
@@ -1103,15 +1028,13 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
1103
1028
  declaredSelection ?? (palette.base02 as Oklch),
1104
1029
  background,
1105
1030
  CONTRAST_FLOORS.selection,
1106
- 0.5
1031
+ REPAIR_BUDGET.selection
1107
1032
  )
1108
1033
  const selection = emit(source.id, 'selection', selectionRepair, CONTRAST_FLOORS.selection)
1109
1034
  if (selection.finding) findings.push(selection.finding)
1110
1035
 
1111
- // Authored overrides, applied last so they win over derivation. They go through
1112
- // the same conversion as every derived value: the catalogue's contract is that a
1113
- // role is an `oklch()` string, and an override written as hex in the source list
1114
- // must not be the one place that contract leaks.
1036
+ // Authored overrides go through the same conversion as every derived value: a
1037
+ // role is an `oklch()` string even when the source list writes it as hex.
1115
1038
  if (source.colors) {
1116
1039
  for (const [role, value] of Object.entries(source.colors)) {
1117
1040
  colors[role as keyof AdeaThemeColors] = authoredColor(source.id, role, value as string)
@@ -1135,8 +1058,6 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
1135
1058
  cursor: source.cursor
1136
1059
  ? authoredColor(source.id, 'cursor', source.cursor)
1137
1060
  : formatOklch(palette.base05),
1138
- // The repaired value, not the declared one: `selectionRepair` above started from
1139
- // whatever the source declared.
1140
1061
  selection: selection.value,
1141
1062
  family: source.family,
1142
1063
  familyLabel: source.familyLabel,
@@ -1153,12 +1074,6 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
1153
1074
  /** The resolution the interaction rungs are fitted at. */
1154
1075
  const RUNG_FIT_STEP = 0.001
1155
1076
 
1156
- /** Every fitting step from `from` down to `to`, inclusive, largest first. */
1157
- function stepsBetween(from: number, to: number): number[] {
1158
- const count = Math.round((from - to) / RUNG_FIT_STEP)
1159
- return Array.from({ length: count + 1 }, (_, index) => from - index * RUNG_FIT_STEP)
1160
- }
1161
-
1162
1077
  /**
1163
1078
  * Fits the hover and active rungs so body text stays legible on them.
1164
1079
  *
@@ -1169,9 +1084,8 @@ function stepsBetween(from: number, to: number): number[] {
1169
1084
  * afford the full step: its hover rung measured 4.45:1 and its active rung 3.86:1.
1170
1085
  *
1171
1086
  * So each rung keeps its fixed step unless body text fails on it, and then gives up
1172
- * only as much as it must, one thousandth of lightness at a time, in the same
1173
- * direction away from the canvas. Only lightness moves: the rung keeps the chroma
1174
- * and hue its fixed step gave it, and the text and the canvas are untouched. The hover rung also reserves room for the primary tint at
1087
+ * only as much lightness as it must; the text and the canvas are untouched. The
1088
+ * hover rung also reserves room for the primary tint at
1175
1089
  * `ACCENT_SUBTLE_MINIMUM_ALPHA`, because a selected row that is hovered paints the
1176
1090
  * tint over it; without the reservation the tint would have to fall to nothing.
1177
1091
  *
@@ -1194,12 +1108,15 @@ function fitInteractionRungs(
1194
1108
  holds: (rung: string) => boolean
1195
1109
  ): number => {
1196
1110
  if (source.colors?.[role] !== undefined) return from
1197
- // Lightness only: the rung keeps the chroma and hue its fixed step gave it.
1198
1111
  const original = surfaceRung(background, from, direction)
1199
1112
  const rungAt = (step: number): string =>
1200
1113
  formatOklch({ ...original, l: background.l + step * direction })
1201
- const step = stepsBetween(from, floor).find((candidate) => holds(rungAt(candidate)))
1202
- if (step === undefined) {
1114
+ const shrink = holds(rungAt(from))
1115
+ ? 0
1116
+ : smallestOffset(RUNG_FIT_STEP, Math.round((from - floor) / RUNG_FIT_STEP), [-1], (offset) =>
1117
+ holds(rungAt(from + offset))
1118
+ )
1119
+ if (shrink === undefined) {
1203
1120
  findings.push({
1204
1121
  themeId: source.id,
1205
1122
  role,
@@ -1208,7 +1125,8 @@ function fitInteractionRungs(
1208
1125
  })
1209
1126
  return from
1210
1127
  }
1211
- if (from - step > RUNG_FIT_STEP / 2) {
1128
+ const step = from + shrink
1129
+ if (shrink !== 0) {
1212
1130
  theme.colors[role] = rungAt(step)
1213
1131
  findings.push({
1214
1132
  themeId: source.id,
@@ -1220,20 +1138,15 @@ function fitInteractionRungs(
1220
1138
  return step
1221
1139
  }
1222
1140
 
1141
+ const clears = tintCheck(theme)
1223
1142
  const hover = fit(
1224
1143
  'surfaceHover',
1225
1144
  SURFACE_STEPS.surfaceHover,
1226
1145
  SURFACE_STEPS.surfaceElevated,
1227
- (rung) =>
1228
- primaryTintClears(theme, rung, 0) &&
1229
- primaryTintClears(theme, rung, ACCENT_SUBTLE_MINIMUM_ALPHA)
1146
+ (rung) => clears(rung, 0) && clears(rung, ACCENT_SUBTLE_MINIMUM_ALPHA)
1230
1147
  )
1231
1148
 
1232
- // Beyond the hover rung where any step there holds the floor; level with it, which
1233
- // does hold, where none does.
1234
- fit('surfaceActive', SURFACE_STEPS.surfaceActive, hover, (rung) =>
1235
- primaryTintClears(theme, rung, 0)
1236
- )
1149
+ fit('surfaceActive', SURFACE_STEPS.surfaceActive, hover, (rung) => clears(rung, 0))
1237
1150
  }
1238
1151
 
1239
1152
  /** Parses an authored value and re-serialises it in the catalogue's notation. */
@@ -1242,43 +1155,3 @@ function authoredColor(themeId: string, role: string, value: string): string {
1242
1155
  if (!parsed) throw new Error(`${themeId}: ${role} is not a colour: ${value}`)
1243
1156
  return formatOklch(parsed)
1244
1157
  }
1245
-
1246
- /** Maps an ANSI role back to the Base24 slot it was read from. */
1247
- function ansiToSlot(
1248
- role: string,
1249
- palette: Record<Base24Slot, Oklch>,
1250
- ramp: { black: Oklch; white: Oklch; brightBlack: Oklch; brightWhite: Oklch }
1251
- ): Base24Slot {
1252
- const direct: Record<string, Base24Slot> = {
1253
- red: 'base08',
1254
- green: 'base0B',
1255
- yellow: 'base0A',
1256
- blue: 'base0D',
1257
- magenta: 'base0E',
1258
- cyan: 'base0C',
1259
- brightRed: 'base12',
1260
- brightYellow: 'base13',
1261
- brightGreen: 'base14',
1262
- brightCyan: 'base15',
1263
- brightBlue: 'base16',
1264
- brightMagenta: 'base17',
1265
- }
1266
- const slot = direct[role]
1267
- if (slot) return slot
1268
-
1269
- const rampSlots: Record<string, Oklch> = {
1270
- black: ramp.black,
1271
- white: ramp.white,
1272
- brightBlack: ramp.brightBlack,
1273
- brightWhite: ramp.brightWhite,
1274
- }
1275
- const target = rampSlots[role]
1276
- if (!target) return 'base05'
1277
- const match = (Object.keys(palette) as Base24Slot[]).find(
1278
- (candidate) => deltaEok(palette[candidate], target) < 1e-9
1279
- )
1280
- return match ?? 'base05'
1281
- }
1282
-
1283
- /** Re-exported so the normalizer's callers do not need a second import. */
1284
- export { contrastDirection }