@adea-ai/themes 0.11.7 → 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 (98) 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 +1 -7
  50. package/dist/oklch.d.ts.map +1 -1
  51. package/dist/oklch.js +15 -35
  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/sources.d.ts +26 -33
  62. package/dist/sources.d.ts.map +1 -1
  63. package/dist/sources.js +136 -305
  64. package/dist/sources.js.map +1 -1
  65. package/dist/step-search.d.ts +10 -0
  66. package/dist/step-search.d.ts.map +1 -0
  67. package/dist/step-search.js +21 -0
  68. package/dist/step-search.js.map +1 -0
  69. package/dist/theme-token-projection-script.d.ts +2 -0
  70. package/dist/theme-token-projection-script.js +2 -0
  71. package/dist/theme-token-projection.d.ts +35 -0
  72. package/dist/theme-token-projection.d.ts.map +1 -0
  73. package/dist/theme-token-projection.js +79 -0
  74. package/dist/theme-token-projection.js.map +1 -0
  75. package/dist/validate.d.ts +12 -17
  76. package/dist/validate.d.ts.map +1 -1
  77. package/dist/validate.js +17 -63
  78. package/dist/validate.js.map +1 -1
  79. package/package.json +13 -3
  80. package/src/accents.ts +57 -96
  81. package/src/adapters/base24.ts +53 -75
  82. package/src/adapters/css-variables.ts +67 -0
  83. package/src/adapters/css.ts +9 -54
  84. package/src/adapters/shadcn.ts +34 -60
  85. package/src/adapters/shiki.ts +17 -30
  86. package/src/adapters/tailwind.ts +7 -37
  87. package/src/adapters/xterm.ts +9 -21
  88. package/src/base24-synthesis.ts +29 -0
  89. package/src/derive.ts +12 -27
  90. package/src/index.ts +6 -5
  91. package/src/normalize.ts +140 -267
  92. package/src/oklch.ts +20 -35
  93. package/src/policy.ts +18 -4
  94. package/src/render.ts +82 -0
  95. package/src/sources.ts +160 -330
  96. package/src/step-search.ts +25 -0
  97. package/src/theme-token-projection.ts +126 -0
  98. package/src/validate.ts +31 -74
package/src/accents.ts CHANGED
@@ -11,14 +11,10 @@
11
11
  *
12
12
  * They are colour data with a light/dark pair per preset — structurally the same
13
13
  * thing as a theme's primary, and the last colour a consumer should have to author
14
- * itself. Keeping them in a consumer meant two things that must agree lived in two
15
- * places: the preset list, and the CSS blocks generated from it. They were written
16
- * by hand in both, and they drifted — the generated default moved its hover *toward*
17
- * the canvas while every hand-written accent moved *away*, so the default primary
18
- * button hovered backwards and picking any accent silently fixed it.
19
- *
20
- * Here the values and the derivation are one module, so a consumer emits both from
21
- * the same rule and cannot produce that disagreement.
14
+ * itself. With the values and the derivation in one module, a consumer emits the
15
+ * preset list and its CSS from the same rule, and the two cannot drift apart — as
16
+ * they did when a consumer hand-wrote both and its default primary hovered the
17
+ * opposite way to every other accent.
22
18
  *
23
19
  * ## The hover rule
24
20
  *
@@ -28,7 +24,7 @@
28
24
  * Two rules were tried here before this one, and both are worth recording because
29
25
  * each looks right until it is measured:
30
26
  *
31
- * - **Toward `--background`** — what the consumer used to do. Inverted in both
27
+ * - **Toward `--background`** — inverted in both
32
28
  * appearances: it moves the button toward the page it sits on, so a hovered button
33
29
  * recedes.
34
30
  * - **Toward `--foreground`** — correct in principle, since the foreground is
@@ -68,10 +64,10 @@
68
64
  import type { AdeaTheme, AnsiKey, ShadcnThemeProjection, ThemeAppearance } from './schema.js'
69
65
  import type { Oklch } from './oklch.js'
70
66
  import {
67
+ canonical,
68
+ contrastDirection,
71
69
  contrastRatio,
72
70
  formatOklch,
73
- hexToOklch,
74
- oklchToHex,
75
71
  parseColor,
76
72
  shiftLightness,
77
73
  } from './oklch.js'
@@ -80,7 +76,10 @@ import {
80
76
  CONTRAST_FLOORS,
81
77
  MINIMUM_ACCENT_CHROMA,
82
78
  REPAIR_BUDGET,
79
+ REPAIR_STEP,
83
80
  } from './policy.js'
81
+ import { compositeOver, mostContrasting, renderedColor } from './render.js'
82
+ import { smallestOffset } from './step-search.js'
84
83
 
85
84
  /** One accent preset: a named primary, per appearance. */
86
85
  export type AccentPreset = {
@@ -177,8 +176,7 @@ export function accentForeground(accent: string): string {
177
176
 
178
177
  const black: Oklch = { l: 0, c: 0, h: 0 }
179
178
  const white: Oklch = { l: 1, c: 0, h: 0 }
180
- const best = contrastRatio(black, fill) >= contrastRatio(white, fill) ? black : white
181
- return formatOklch(best)
179
+ return formatOklch(mostContrasting(fill, [black, white]))
182
180
  }
183
181
 
184
182
  /** The contrast the chosen label achieves, for validation and reporting. */
@@ -209,7 +207,7 @@ export const ACCENT_HOVER_STEP = 0.05
209
207
  * be written as a mix — see {@link ACCENT_HOVER_STEP}. That means it does **not**
210
208
  * follow `--primary` on its own, so a consumer that applies themes at runtime has to
211
209
  * write this token too; the alternative is a hover left over from the previous
212
- * theme's primary. `themeCssVariables` includes it for that reason.
210
+ * theme's primary. `shadcnVariables` includes it for that reason.
213
211
  *
214
212
  * The direction is away from the canvas: darker on a light theme, lighter on a dark
215
213
  * one. `shiftLightness` keeps chroma and hue and damps chroma near the extremes, so a
@@ -302,43 +300,12 @@ function offeredPrimaries(theme: TintedTheme): string[] {
302
300
  ]
303
301
  }
304
302
 
305
- /** A colour as the screen draws it: through eight-bit sRGB. */
306
- function rendered(value: string): Oklch | undefined {
307
- const parsed = parseColor(value)
308
- return parsed ? hexToOklch(oklchToHex(parsed)) : undefined
309
- }
310
-
311
- /**
312
- * A translucent fill painted over an opaque one, the way a browser paints
313
- * `color-mix(in oklch, <tint> N%, transparent)`: the mix keeps the tint's colour and
314
- * takes the share as alpha, and the alpha is composited in gamma-encoded sRGB.
315
- * Rounded to the eight-bit value that reaches the screen, which is the colour an
316
- * accessibility audit samples.
317
- */
318
- function compositeTint(tint: string, surface: string, alpha: number): Oklch | undefined {
319
- const tintColor = parseColor(tint)
320
- const surfaceColor = parseColor(surface)
321
- if (!tintColor || !surfaceColor) return undefined
322
- const front = oklchToHex(tintColor)
323
- const back = oklchToHex(surfaceColor)
324
- const channels = [1, 3, 5].map((offset) => {
325
- const top = Number.parseInt(front.slice(offset, offset + 2), 16)
326
- const bottom = Number.parseInt(back.slice(offset, offset + 2), 16)
327
- return Math.round(top * alpha + bottom * (1 - alpha))
328
- .toString(16)
329
- .padStart(2, '0')
330
- })
331
- return hexToOklch(`#${channels.join('')}`)
332
- }
333
-
334
303
  /**
335
304
  * Whether body text holds the floor on a surface under the primary tint.
336
305
  *
337
306
  * True when `text` and `foreground` both clear `CONTRAST_FLOORS.text` on the tint
338
307
  * at `alpha` percent composited over `surface`, for every primary the theme offers
339
- * and every one in `primaries`. At `alpha` 0 it is the bare surface. The
340
- * normalizer calls this to fit the hover rung, and {@link primarySubtleAlpha} to
341
- * fit the strength, so the two are measured by one rule.
308
+ * and every one in `primaries`. At `alpha` 0 it is the bare surface.
342
309
  */
343
310
  export function primaryTintClears(
344
311
  theme: TintedTheme,
@@ -346,25 +313,36 @@ export function primaryTintClears(
346
313
  alpha: number,
347
314
  primaries: readonly string[] = []
348
315
  ): boolean {
349
- const foregrounds = [...new Set([theme.colors.text, theme.colors.foreground])].map(rendered)
350
- if (foregrounds.some((value) => value === undefined)) return false
351
- const tints = alpha === 0 ? [surface] : [...new Set([...offeredPrimaries(theme), ...primaries])]
352
- return tints.every((tint) => {
353
- const fill = compositeTint(tint, surface, alpha / 100)
354
- return (
355
- fill !== undefined &&
356
- foregrounds.every((foreground) => contrastRatio(foreground!, fill) >= CONTRAST_FLOORS.text)
357
- )
358
- })
316
+ return tintCheck(theme, primaries)(surface, alpha)
359
317
  }
360
318
 
361
- const subtleAlphaCache = new WeakMap<object, { inputs: string; alpha: number }>()
319
+ /**
320
+ * {@link primaryTintClears} for one theme and set of primaries, measuring the
321
+ * foregrounds and the offered primaries once for every surface and alpha it is
322
+ * asked about. The normalizer uses it to fit the interaction rungs.
323
+ */
324
+ export function tintCheck(
325
+ theme: TintedTheme,
326
+ primaries: readonly string[] = []
327
+ ): (surface: string, alpha: number) => boolean {
328
+ const foregrounds = [...new Set([theme.colors.text, theme.colors.foreground])].map(renderedColor)
329
+ if (foregrounds.some((value) => value === undefined)) return () => false
330
+ const tints = [...new Set([...offeredPrimaries(theme), ...primaries])]
331
+ const legibleOn = (fill: Oklch | undefined): boolean =>
332
+ fill !== undefined &&
333
+ foregrounds.every((foreground) => contrastRatio(foreground!, fill) >= CONTRAST_FLOORS.text)
334
+
335
+ return (surface, alpha) =>
336
+ alpha === 0
337
+ ? legibleOn(renderedColor(surface))
338
+ : tints.every((tint) => legibleOn(compositeOver(tint, surface, alpha / 100)))
339
+ }
362
340
 
363
341
  /**
364
342
  * The tint strength a theme can carry, as a whole percentage.
365
343
  *
366
- * The appearance's {@link ACCENT_SUBTLE_ALPHA} ceiling, lowered one point at a time
367
- * until body text clears `CONTRAST_FLOORS.text` on the tint over every floored
344
+ * The appearance's {@link ACCENT_SUBTLE_ALPHA} ceiling, lowered to the highest whole
345
+ * percentage at which body text clears `CONTRAST_FLOORS.text` on the tint over every floored
368
346
  * surface, hover rung included, for every primary the theme offers. One value per
369
347
  * theme rather than one per accent, so switching between offered accents at runtime
370
348
  * needs no new tint: the expression follows `--primary` and the strength already
@@ -376,27 +354,12 @@ const subtleAlphaCache = new WeakMap<object, { inputs: string; alpha: number }>(
376
354
  * a product's own brand primary, say. The offered set is always included.
377
355
  */
378
356
  export function primarySubtleAlpha(theme: TintedTheme, primaries: readonly string[] = []): number {
379
- // Theme records are mutable; an identity-only cache can return a strength for
380
- // colours that are no longer present on the same object.
381
- const inputs = JSON.stringify([
382
- theme.appearance,
383
- theme.colors,
384
- theme.ansi,
385
- theme.shadcn,
386
- ACCENTS,
387
- primaries,
388
- ])
389
- const cached = subtleAlphaCache.get(theme)
390
- if (cached?.inputs === inputs) return cached.alpha
391
-
392
357
  const surfaces = tintSurfaces(theme)
393
- const clears = (alpha: number): boolean =>
394
- surfaces.every((surface) => primaryTintClears(theme, surface, alpha, primaries))
358
+ const check = tintCheck(theme, primaries)
359
+ const clears = (alpha: number): boolean => surfaces.every((surface) => check(surface, alpha))
395
360
 
396
361
  let alpha = ACCENT_SUBTLE_ALPHA[theme.appearance]
397
362
  while (alpha > 0 && !clears(alpha)) alpha -= 1
398
-
399
- subtleAlphaCache.set(theme, { inputs, alpha })
400
363
  return alpha
401
364
  }
402
365
 
@@ -412,10 +375,10 @@ export function primarySubtleAlpha(theme: TintedTheme, primaries: readonly strin
412
375
  * it as `primaries` to have the strength measured for it as well.
413
376
  *
414
377
  * Passing only an **appearance** gives the {@link ACCENT_SUBTLE_ALPHA} ceiling for
415
- * that appearance, unmeasured. That is the old behaviour and it is not legible on
416
- * every theme — One Dark's body text on its own primary's tint over a card measures
417
- * 4.34:1 at the dark ceiling — so it is kept for a consumer without a theme object
418
- * in hand, not as the default to reach for.
378
+ * that appearance, unmeasured. It is not legible on every theme — One Dark's body
379
+ * text on its own primary's tint over a card measures 4.34:1 at the dark ceiling —
380
+ * so it is kept for a consumer without a theme object in hand, not as the default
381
+ * to reach for.
419
382
  */
420
383
  export function primarySubtleCss(
421
384
  target: ThemeAppearance | TintedTheme,
@@ -451,8 +414,6 @@ export function primarySubtleCss(
451
414
  */
452
415
  export type ThemeAccentSlot = (typeof ACCENT_PREFERENCE)[number]
453
416
 
454
- const REPAIR_STEP = 0.002
455
-
456
417
  /**
457
418
  * A slot's offerable value on one theme: the palette's own colour when it already
458
419
  * clears the accent floors on that theme's canvas, otherwise the catalogue's
@@ -471,27 +432,27 @@ function offeredSlotValue(
471
432
  if (!value || !background) return undefined
472
433
  if (value.c < MINIMUM_ACCENT_CHROMA) return undefined
473
434
 
474
- // Measured in the canonical form the offer commits, so a repair cannot
475
- // converge on a value that rounding then pushes back below the floor.
476
435
  const clearsFloors = (candidate: Oklch): boolean => {
477
- const canonical = parseColor(formatOklch(candidate))
478
- if (!canonical) return false
436
+ const committed = canonical(candidate)
479
437
  return (
480
- contrastRatio(canonical, background) >= CONTRAST_FLOORS.accentRaised &&
481
- accentForegroundContrast(formatOklch(canonical)) >= CONTRAST_FLOORS.accentForeground
438
+ contrastRatio(committed, background) >= CONTRAST_FLOORS.accentRaised &&
439
+ accentForegroundContrast(formatOklch(committed)) >= CONTRAST_FLOORS.accentForeground
482
440
  )
483
441
  }
484
442
 
485
443
  if (clearsFloors(value)) return original
486
444
 
487
- const direction = value.l >= background.l ? 1 : -1
488
- const maximumSteps = Math.floor(REPAIR_BUDGET.accent / REPAIR_STEP)
489
- for (let index = 1; index <= maximumSteps; index += 1) {
490
- const candidate = { ...value, l: value.l + direction * REPAIR_STEP * index }
491
- if (candidate.l <= 0 || candidate.l >= 1) break
492
- if (clearsFloors(candidate)) return formatOklch(candidate)
493
- }
494
- return undefined
445
+ const shifted = (offset: number): Oklch => ({ ...value, l: value.l + offset })
446
+ const offset = smallestOffset(
447
+ REPAIR_STEP,
448
+ Math.floor(REPAIR_BUDGET.accent / REPAIR_STEP),
449
+ [contrastDirection(value, background)],
450
+ (trial) => {
451
+ const candidate = shifted(trial)
452
+ return candidate.l > 0 && candidate.l < 1 && clearsFloors(candidate)
453
+ }
454
+ )
455
+ return offset === undefined ? undefined : formatOklch(shifted(offset))
495
456
  }
496
457
 
497
458
  /**
@@ -21,15 +21,21 @@
21
21
  * `base09` (orange) and `base0F` (brown) have no counterpart in the canonical
22
22
  * schema: Adea has no "constant" or "deprecated" role, because a UI does not. On
23
23
  * export they are re-derived from `red` and `yellow` by a fixed rotation, and
24
- * {@link AdeaThemeRecord} carries the vendored schemes separately for callers who
25
- * need the originals byte-for-byte. Round-tripping is therefore lossless for every
24
+ * `getBase24Scheme` returns the vendored schemes for callers who need the
25
+ * originals byte-for-byte. Round-tripping is therefore lossless for every
26
26
  * role Adea models, and explicitly approximate for the two it does not — rather
27
27
  * than silently lossy, which is what an unnamed slot would have been.
28
28
  */
29
29
 
30
30
  import type { AdeaAnsi, AdeaTheme, AdeaThemeColors } from '../schema.js'
31
31
  import type { Oklch } from '../oklch.js'
32
- import { formatOklch, parseColor } from '../oklch.js'
32
+ import { formatOklch, parseColor, shiftLightness } from '../oklch.js'
33
+ import {
34
+ PAST_CANVAS_STEPS,
35
+ SYNTHESISED_HUES,
36
+ pastCanvasDirection,
37
+ rotateHue,
38
+ } from '../base24-synthesis.js'
33
39
 
34
40
  /** The twenty-four Base24 slots, in the order the specification lists them. */
35
41
  export const BASE24_SLOTS = [
@@ -76,9 +82,8 @@ export interface Base24Scheme {
76
82
  /**
77
83
  * What each slot means, in the specification's own terms.
78
84
  *
79
- * Kept as data because the normalizer reads it rather than hard-coding slot names
80
- * in its logic, and because it is the documentation a reader needs to check that
81
- * the mapping is right.
85
+ * The documentation a reader needs to check the mappings below against the
86
+ * specification.
82
87
  */
83
88
  export const BASE24_SLOT_MEANING: Readonly<Record<Base24Slot, string>> = Object.freeze({
84
89
  base00: 'Background',
@@ -108,11 +113,11 @@ export const BASE24_SLOT_MEANING: Readonly<Record<Base24Slot, string>> = Object.
108
113
  })
109
114
 
110
115
  /**
111
- * Base24 slot → ANSI role.
116
+ * ANSI role → Base24 slot.
112
117
  *
113
- * `black`, `white` and `brightBlack` are absent because Base24 does not name them:
114
- * the background and foreground slots do double duty. The normalizer decides those
115
- * three; see its `ANSI_FROM_BASE24` note for why the choice is the one it is.
118
+ * `black`, `brightBlack`, `white` and `brightWhite` are absent because Base24 does
119
+ * not name them: the background and foreground slots do double duty. The
120
+ * normalizer decides those four; see its `buildAnsiRamp` note for why.
116
121
  */
117
122
  export const BASE24_TO_ANSI = Object.freeze({
118
123
  red: 'base08',
@@ -139,9 +144,11 @@ export const ANSI_TO_BASE24 = Object.freeze(
139
144
  /**
140
145
  * How each semantic role is written back to a Base24 slot on export.
141
146
  *
142
- * The choices are not arbitrary. `base01` receives `black` rather than the
143
- * background because an ANSI black that equals the background is invisible as
144
- * foreground text — which is precisely the defect the import side documents. The
147
+ * The accent and status roles are absent because their slots belong to the ANSI
148
+ * hues: `base08`–`base0E` are written from `BASE24_TO_ANSI`, the colours a terminal
149
+ * reads, not from the repaired UI roles. `base01` receives `black` rather than a
150
+ * role because an ANSI black that equals the background is invisible as foreground
151
+ * text — the defect the import side documents. The
145
152
  * surface ladder has no Base24 slot at all, so it is deliberately absent here:
146
153
  * Base24 cannot express it, and pretending otherwise by packing four rungs into
147
154
  * `base01`/`base02` would make the export lie about what it preserves.
@@ -159,12 +166,12 @@ export const THEME_TO_BASE24: Readonly<Record<keyof AdeaThemeColors, Base24Slot
159
166
  text: 'base06',
160
167
  textMuted: 'base04',
161
168
  textSubtle: 'base03',
162
- accent: 'base0D',
169
+ accent: undefined,
163
170
  accentForeground: undefined,
164
- success: 'base0B',
165
- warning: 'base0A',
166
- error: 'base08',
167
- info: 'base0C',
171
+ success: undefined,
172
+ warning: undefined,
173
+ error: undefined,
174
+ info: undefined,
168
175
  })
169
176
 
170
177
  /** Thrown when a palette is missing a slot or holds an unparseable colour. */
@@ -195,18 +202,6 @@ export function parseBase24Palette(palette: Base24Palette): Record<Base24Slot, O
195
202
  return parsed
196
203
  }
197
204
 
198
- /**
199
- * Rotates a hue by a fixed number of degrees.
200
- *
201
- * Used only for the two Base24 slots Adea has no role for. A rotation is chosen
202
- * over an interpolation toward grey because both source colours are already
203
- * saturated, and a rotation keeps saturation while moving the hue far enough to
204
- * read as a distinct token.
205
- */
206
- function rotateHue(color: Oklch, degrees: number): Oklch {
207
- return { ...color, h: (color.h + degrees + 360) % 360 }
208
- }
209
-
210
205
  /**
211
206
  * Writes a theme back to Base24.
212
207
  *
@@ -220,44 +215,28 @@ export function toBase24(
220
215
  options: { author?: string; name?: string } = {}
221
216
  ): Base24Scheme {
222
217
  const background = requireColor(theme.colors.background, 'background')
223
- const red = requireColor(theme.ansi.red, 'ansi.red')
224
- const yellow = requireColor(theme.ansi.yellow, 'ansi.yellow')
225
- const text = requireColor(theme.colors.text, 'text')
226
-
227
- const palette: Record<Base24Slot, string> = {
228
- base00: theme.colors.background,
218
+ const fixed: Partial<Record<Base24Slot, string>> = {
229
219
  base01: theme.ansi.black,
230
220
  base02: theme.selection,
231
- base03: theme.colors.textSubtle,
232
- base04: theme.colors.textMuted,
233
- base05: theme.colors.foreground,
234
- base06: theme.colors.text,
235
221
  base07: theme.ansi.brightWhite,
236
- base08: theme.ansi.red,
237
- // Synthesised: Base24's constants/orange slot and its deprecated/brown slot
238
- // have no Adea role. See the module header.
239
- base09: formatOklch(rotateHue(red, -18)),
240
- base0A: theme.ansi.yellow,
241
- base0B: theme.ansi.green,
242
- base0C: theme.ansi.cyan,
243
- base0D: theme.ansi.blue,
244
- base0E: theme.ansi.magenta,
245
- base0F: formatOklch(rotateHue(yellow, 22)),
246
- base10: formatOklch({ ...background, l: Math.max(0, background.l - 0.03) }),
247
- base11: formatOklch({ ...background, l: Math.max(0, background.l - 0.06) }),
248
- base12: theme.ansi.brightRed,
249
- base13: theme.ansi.brightYellow,
250
- base14: theme.ansi.brightGreen,
251
- base15: theme.ansi.brightCyan,
252
- base16: theme.ansi.brightBlue,
253
- base17: theme.ansi.brightMagenta,
254
222
  }
255
-
256
- // `base05` is the default foreground and the caret; a palette whose text role
257
- // differs from its ANSI white means the two disagree about which is the body
258
- // colour. The foreground role wins, because every contrast floor is defined
259
- // against it.
260
- void text
223
+ for (const [role, slot] of Object.entries(THEME_TO_BASE24)) {
224
+ if (slot) fixed[slot] = theme.colors[role as keyof AdeaThemeColors]
225
+ }
226
+ const direction = pastCanvasDirection(theme.appearance)
227
+ for (const [slot, step] of Object.entries(PAST_CANVAS_STEPS)) {
228
+ fixed[slot as Base24Slot] = formatOklch(shiftLightness(background, direction * step))
229
+ }
230
+ for (const [role, slot] of Object.entries(BASE24_TO_ANSI)) {
231
+ fixed[slot] = theme.ansi[role as keyof typeof BASE24_TO_ANSI]
232
+ }
233
+ for (const [slot, { from, degrees }] of Object.entries(SYNTHESISED_HUES)) {
234
+ const source = requireColor(theme.ansi[from], `ansi.${from}`)
235
+ fixed[slot as Base24Slot] = formatOklch(rotateHue(source, degrees))
236
+ }
237
+ const palette = Object.fromEntries(
238
+ BASE24_SLOTS.map((slot) => [slot, fixed[slot] as string])
239
+ ) as Record<Base24Slot, string>
261
240
 
262
241
  return {
263
242
  system: 'base24',
@@ -287,15 +266,6 @@ export function formatBase24Scheme(scheme: Base24Scheme): string {
287
266
  return `${lines.join('\n')}\n`
288
267
  }
289
268
 
290
- /**
291
- * Reads the YAML dialect Base24 schemes are published in.
292
- *
293
- * Hand-written rather than pulled from a YAML library: the format is a fixed set
294
- * of scalar keys, the package has no runtime dependencies and no build-time
295
- * dependencies beyond the TypeScript compiler, and a general parser would accept
296
- * documents this bridge cannot actually honour. Anything outside the specification
297
- * is rejected loudly instead of being parsed into a half-scheme.
298
- */
299
269
  /**
300
270
  * A line with its trailing `#` comment removed.
301
271
  *
@@ -306,7 +276,7 @@ export function formatBase24Scheme(scheme: Base24Scheme): string {
306
276
  * `js/polynomial-redos`, which is how it was found.
307
277
  *
308
278
  * A `#` is a comment marker only when whitespace precedes it and it is outside a
309
- * quoted scalar. A leading `#` is skipped later by the `separator === -1` check.
279
+ * quoted scalar.
310
280
  */
311
281
  function stripTrailingComment(line: string): string {
312
282
  const separator = line.indexOf(':')
@@ -331,6 +301,14 @@ function stripTrailingComment(line: string): string {
331
301
  return line
332
302
  }
333
303
 
304
+ /**
305
+ * Reads the YAML dialect Base24 schemes are published in.
306
+ *
307
+ * Hand-written rather than pulled from a YAML library: the format is a fixed set
308
+ * of scalar keys, the package has no runtime dependencies, and a general parser would accept
309
+ * documents this bridge cannot actually honour. Anything outside the specification
310
+ * is rejected loudly instead of being parsed into a half-scheme.
311
+ */
334
312
  export function parseBase24Scheme(source: string): Base24Scheme {
335
313
  const scalars = new Map<string, string>()
336
314
  const palette = new Map<string, string>()
@@ -346,7 +324,7 @@ export function parseBase24Scheme(source: string): Base24Scheme {
346
324
  const separator = line.indexOf(':')
347
325
  if (separator === -1) continue
348
326
 
349
- const key = (indented ? line.slice(0, separator) : line.slice(0, separator)).trim()
327
+ const key = line.slice(0, separator).trim()
350
328
  const rawValue = line.slice(separator + 1).trim()
351
329
  let value = rawValue.replace(/^["']|["']$/g, '')
352
330
  if (rawValue.startsWith('"') && rawValue.endsWith('"')) {
@@ -0,0 +1,67 @@
1
+ /**
2
+ * The custom properties the CSS adapter writes, named once.
3
+ *
4
+ * `adapters/css.ts` writes their values and `adapters/tailwind.ts` points utilities at
5
+ * them, so a variable added here becomes both a property and a utility.
6
+ */
7
+
8
+ import type { AdeaTheme } from '../schema.js'
9
+ import { ANSI_KEYS, THEME_COLOR_KEYS } from '../schema.js'
10
+ import { CHART_SERIES, STATUS_ROLES, chartSeries, statusForeground, tint } from '../derive.js'
11
+
12
+ /** camelCase to kebab-case, the way the property names read in CSS. */
13
+ export function kebab(name: string): string {
14
+ return name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
15
+ }
16
+
17
+ /** The full property name for an unprefixed variable. */
18
+ export function variableName(name: string, prefix: string): string {
19
+ return prefix ? `--${prefix}-${name}` : `--${name}`
20
+ }
21
+
22
+ export type CssVariable = {
23
+ /** The unprefixed name, e.g. `surface-elevated`. */
24
+ name: string
25
+ value: (theme: AdeaTheme) => string
26
+ }
27
+
28
+ /** The theme's own roles, plus its cursor and selection. */
29
+ export const CANONICAL_VARIABLES: readonly CssVariable[] = Object.freeze([
30
+ ...THEME_COLOR_KEYS.map((key) => ({
31
+ name: kebab(key),
32
+ value: (theme: AdeaTheme) => theme.colors[key],
33
+ })),
34
+ { name: 'cursor', value: (theme: AdeaTheme) => theme.cursor },
35
+ { name: 'selection', value: (theme: AdeaTheme) => theme.selection },
36
+ ])
37
+
38
+ /**
39
+ * Variables computed from the roles: the ANSI colours, the chart series, the status
40
+ * fills and the text drawn on them, and the sunken surface.
41
+ *
42
+ * A status fill is the role at low strength against the canvas; its foreground is the
43
+ * theme's legible extreme for that fill, not the body text — see
44
+ * `derive.statusForeground`. The sunken surface is the one step below the canvas that
45
+ * a well or a code block sits in.
46
+ */
47
+ export const DERIVED_VARIABLES: readonly CssVariable[] = Object.freeze([
48
+ ...ANSI_KEYS.map((key) => ({
49
+ name: `ansi-${kebab(key)}`,
50
+ value: (theme: AdeaTheme) => theme.ansi[key],
51
+ })),
52
+ ...CHART_SERIES.map((_, index) => ({
53
+ name: `chart-${index + 1}`,
54
+ value: (theme: AdeaTheme) => chartSeries(theme)[index] as string,
55
+ })),
56
+ ...STATUS_ROLES.flatMap((role) => [
57
+ {
58
+ name: `${role}-subtle`,
59
+ value: (theme: AdeaTheme) => tint(theme.colors[role], theme.colors.background),
60
+ },
61
+ { name: `${role}-foreground`, value: (theme: AdeaTheme) => statusForeground(theme, role) },
62
+ ]),
63
+ {
64
+ name: 'surface-sunken',
65
+ value: (theme: AdeaTheme) => tint(theme.colors.text, theme.colors.background, 0.03),
66
+ },
67
+ ])
@@ -17,10 +17,8 @@
17
17
  * ## What is derived rather than stored
18
18
  *
19
19
  * A handful of variables have no role in the canonical schema and are computed:
20
- * the chart series (see `syntax.ts`), the status tints used behind status text, and
21
- * the ANSI variables. They are derived on every call rather than cached, because
22
- * they are pure functions of the theme and caching them would put a second copy of
23
- * the truth in the module.
20
+ * the chart series (see `derive.ts`), the status tints used behind status text, and
21
+ * the ANSI variables.
24
22
  *
25
23
  * ## Why `oklch()` strings and not hex
26
24
  *
@@ -32,8 +30,7 @@
32
30
  */
33
31
 
34
32
  import type { AdeaTheme, AdeaThemeColors } from '../schema.js'
35
- import { THEME_COLOR_KEYS, ANSI_KEYS } from '../schema.js'
36
- import { STATUS_ROLES, chartSeries, statusForeground, tint } from '../derive.js'
33
+ import { CANONICAL_VARIABLES, DERIVED_VARIABLES, kebab, variableName } from './css-variables.js'
37
34
 
38
35
  /** How the variables are named. */
39
36
  export interface CssOptions {
@@ -49,15 +46,6 @@ export interface CssOptions {
49
46
 
50
47
  const DEFAULT_PREFIX = 'adea'
51
48
 
52
- function variableName(name: string, prefix: string): string {
53
- return prefix ? `--${prefix}-${name}` : `--${name}`
54
- }
55
-
56
- /** camelCase to kebab-case, the way the property names read in CSS. */
57
- function kebab(name: string): string {
58
- return name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
59
- }
60
-
61
49
  /**
62
50
  * The literal roles, plus the derived ones.
63
51
  *
@@ -69,46 +57,13 @@ export function themeCssVariables(
69
57
  options: Omit<CssOptions, 'selector' | 'indent'> = {}
70
58
  ): Record<string, string> {
71
59
  const prefix = options.prefix ?? DEFAULT_PREFIX
72
- const includeDerived = options.includeDerived ?? true
73
- const variables: Record<string, string> = {}
74
-
75
- for (const key of THEME_COLOR_KEYS) {
76
- variables[variableName(kebab(key), prefix)] = theme.colors[key]
77
- }
78
-
79
- variables[variableName('cursor', prefix)] = theme.cursor
80
- variables[variableName('selection', prefix)] = theme.selection
81
-
82
- if (!includeDerived) return variables
83
-
84
- for (const key of ANSI_KEYS) {
85
- variables[variableName(`ansi-${kebab(key)}`, prefix)] = theme.ansi[key]
86
- }
87
-
88
- chartSeries(theme).forEach((value, index) => {
89
- variables[variableName(`chart-${index + 1}`, prefix)] = value
90
- })
91
-
92
- // Status fills and the text that goes on them. The fill is the role at low
93
- // strength against the canvas; the foreground is the theme's legible extreme for
94
- // that fill, not the theme's body text — see `derive.statusForeground`.
95
- for (const role of STATUS_ROLES) {
96
- variables[variableName(`${role}-subtle`, prefix)] = tint(
97
- theme.colors[role],
98
- theme.colors.background
99
- )
100
- variables[variableName(`${role}-foreground`, prefix)] = statusForeground(theme, role)
101
- }
102
-
103
- // A dimmer rule for tables and inner separators, and the surface one step below
104
- // the canvas that a well or a code block sits in.
105
- variables[variableName('surface-sunken', prefix)] = tint(
106
- theme.colors.text,
107
- theme.colors.background,
108
- 0.03
60
+ const variables =
61
+ options.includeDerived === false
62
+ ? CANONICAL_VARIABLES
63
+ : [...CANONICAL_VARIABLES, ...DERIVED_VARIABLES]
64
+ return Object.fromEntries(
65
+ variables.map(({ name, value }) => [variableName(name, prefix), value(theme)])
109
66
  )
110
-
111
- return variables
112
67
  }
113
68
 
114
69
  /** A CSS rule for one theme. */