@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/oklch.ts CHANGED
@@ -23,6 +23,9 @@
23
23
  * the colour recognisably the one that was asked for.
24
24
  */
25
25
 
26
+ import { REPAIR_STEP } from './policy.js'
27
+ import { offsetSize, smallestOffset } from './step-search.js'
28
+
26
29
  /** A colour in OKLCH. `l` and `c` are unbounded here; use {@link inGamut} to test. */
27
30
  export type Oklch = {
28
31
  /** Perceptual lightness, nominally 0–1. */
@@ -83,8 +86,6 @@ export const oklchToOklab = ({ l, c, h }: Oklch): Oklab => {
83
86
 
84
87
  export const oklabToOklch = ({ l, a, b }: Oklab): Oklch => {
85
88
  const c = Math.hypot(a, b)
86
- // Hue is undefined for a neutral; report 0 rather than NaN so that a grey
87
- // survives a round trip and can still be interpolated.
88
89
  const h = c < 1e-6 ? 0 : ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360
89
90
  return { l, c, h }
90
91
  }
@@ -98,12 +99,6 @@ export function inGamut(color: Oklch, tolerance = 1e-4): boolean {
98
99
 
99
100
  /**
100
101
  * Brings a colour into sRGB by reducing chroma at constant lightness and hue.
101
- *
102
- * Reduction is a bisection because the sRGB boundary along a constant-hue line is
103
- * not a linear function of chroma — a fixed number of decrements either stops
104
- * short of the boundary (leaving a clipped colour) or overshoots it (desaturating
105
- * far more than necessary). 24 iterations resolve chroma to well under one
106
- * 8-bit-steps' worth of difference.
107
102
  */
108
103
  export function gamutMap(color: Oklch): Oklch {
109
104
  const lightness = Math.min(1, Math.max(0, color.l))
@@ -290,11 +285,6 @@ export function repairContrast(
290
285
  minimum: number,
291
286
  budget = 0.22
292
287
  ): ContrastRepair {
293
- // Measured in the canonical form, for the reason {@link canonical} gives: the caller
294
- // commits `formatOklch`'s rounded output, and a repair that converges on the exact
295
- // floor of an unrounded value ships a value that fails it. The lower a floor is, the
296
- // more this matters, and the scale starts at 1.2:1 — so a selection colour's whole
297
- // range is a few rounding steps wide.
298
288
  const initial = contrastRatio(canonical(foreground), background)
299
289
  if (initial >= minimum) {
300
290
  return { color: foreground, ratio: initial, delta: 0, satisfied: true }
@@ -306,30 +296,25 @@ export function repairContrast(
306
296
  // mid grey on a near-white canvas, and *lightening* it has 0.03 to work with while
307
297
  // darkening it has the whole range.
308
298
  const preferred = contrastDirection(foreground, background)
309
- const step = 0.002
310
- const maximumSteps = Math.floor(budget / step)
311
-
312
- for (let index = 1; index <= maximumSteps; index += 1) {
313
- for (const direction of [preferred, -preferred] as const) {
314
- const candidate: Oklch = { ...foreground, l: foreground.l + direction * step * index }
315
- if (candidate.l <= 0 || candidate.l >= 1) continue
316
- const ratio = contrastRatio(canonical(candidate), background)
317
- if (ratio >= minimum) {
318
- return {
319
- color: canonical(candidate),
320
- ratio,
321
- delta: Number((step * index).toFixed(4)),
322
- satisfied: true,
323
- }
324
- }
299
+ const shifted = (offset: number): Oklch => canonical({ ...foreground, l: foreground.l + offset })
300
+ const offset = smallestOffset(
301
+ REPAIR_STEP,
302
+ Math.floor(budget / REPAIR_STEP),
303
+ [preferred, -preferred],
304
+ (trial) => {
305
+ const l = foreground.l + trial
306
+ return l > 0 && l < 1 && contrastRatio(shifted(trial), background) >= minimum
325
307
  }
308
+ )
309
+ if (offset === undefined) {
310
+ return { color: foreground, ratio: initial, delta: 0, satisfied: false }
326
311
  }
327
-
312
+ const color = shifted(offset)
328
313
  return {
329
- color: foreground,
330
- ratio: initial,
331
- delta: 0,
332
- satisfied: false,
314
+ color,
315
+ ratio: contrastRatio(color, background),
316
+ delta: offsetSize(offset),
317
+ satisfied: true,
333
318
  }
334
319
  }
335
320
 
@@ -374,7 +359,7 @@ export function shiftLightness(color: Oklch, delta: number): Oklch {
374
359
  return { l, c: color.c * headroom, h: color.h }
375
360
  }
376
361
 
377
- /** Scales chroma, clamped to the maximum the hue can hold. */
362
+ /** Scales chroma, clamped to 0–0.4. */
378
363
  export function scaleChroma(color: Oklch, factor: number): Oklch {
379
364
  return { ...color, c: Math.max(0, Math.min(0.4, color.c * factor)) }
380
365
  }
package/src/policy.ts CHANGED
@@ -2,10 +2,9 @@
2
2
  * The catalogue's policy: the contrast floors, the repair budgets and the accent
3
3
  * preference.
4
4
  *
5
- * Kept apart from the normalizer, which applies them, so that the accent axis —
6
- * which the normalizer now consults to fit the interaction rungs around the primary
7
- * tint — can read the same numbers without the two modules importing each other.
8
- * `normalize.ts` re-exports everything here.
5
+ * Kept apart from the normalizer, which applies them, so that the accent axis,
6
+ * the validator and the source list read the same numbers without importing the
7
+ * normalizer.
9
8
  */
10
9
 
11
10
  import type { AnsiKey } from './schema.js'
@@ -70,10 +69,25 @@ export const CONTRAST_FLOORS = Object.freeze({
70
69
  export const REPAIR_BUDGET = Object.freeze({
71
70
  /** Body text: some palettes publish a foreground that fails on their own canvas. */
72
71
  text: 0.2,
72
+ textMuted: 0.3,
73
+ textSubtle: 0.3,
73
74
  status: 0.24,
74
75
  accent: 0.24,
76
+ accentForeground: 0.6,
77
+ selection: 0.5,
75
78
  })
76
79
 
80
+ /** The lightness increment every contrast repair searches in. */
81
+ export const REPAIR_STEP = 0.002
82
+
83
+ /**
84
+ * Whether a measured ratio clears a floor, allowing for floating-point noise in a
85
+ * ratio computed to land exactly on it.
86
+ */
87
+ export function clearsFloor(ratio: number, floor: number): boolean {
88
+ return ratio >= floor - 1e-9
89
+ }
90
+
77
91
  /** The accent preference order, best first. */
78
92
  export const ACCENT_PREFERENCE = [
79
93
  'blue',
package/src/render.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Colours as a screen draws them, and the choices made by measuring them.
3
+ *
4
+ * A browser stores three eight-bit sRGB channels and composites translucent fills in
5
+ * gamma-encoded sRGB, and an accessibility audit samples that result. The helpers
6
+ * here reproduce it, so a floor measured in this package is a floor on what is drawn.
7
+ */
8
+
9
+ import type { Oklch } from './oklch.js'
10
+ import { contrastRatio, hexToOklch, oklchToHex, parseColor, repairContrast } from './oklch.js'
11
+ import { clearsFloor } from './policy.js'
12
+
13
+ /** A colour string as hex, or the input unchanged when it does not parse. */
14
+ export function toHex(value: string): string {
15
+ const parsed = parseColor(value)
16
+ return parsed ? oklchToHex(parsed) : value
17
+ }
18
+
19
+ /** A colour string as the screen draws it: through eight-bit sRGB. */
20
+ export function renderedColor(value: string): Oklch | undefined {
21
+ const parsed = parseColor(value)
22
+ return parsed ? hexToOklch(oklchToHex(parsed)) : undefined
23
+ }
24
+
25
+ /**
26
+ * A translucent fill painted over an opaque one: `alpha` (0–1) of `front` over
27
+ * `back`, composited per eight-bit sRGB channel the way a browser paints
28
+ * `color-mix(in oklch, <front> N%, transparent)` and `--colour/90`.
29
+ */
30
+ export function compositeOver(front: string, back: string, alpha: number): Oklch | undefined {
31
+ const frontColor = parseColor(front)
32
+ const backColor = parseColor(back)
33
+ if (!frontColor || !backColor) return undefined
34
+ const frontHex = oklchToHex(frontColor)
35
+ const backHex = oklchToHex(backColor)
36
+ const channels = [1, 3, 5].map((offset) => {
37
+ const top = Number.parseInt(frontHex.slice(offset, offset + 2), 16)
38
+ const bottom = Number.parseInt(backHex.slice(offset, offset + 2), 16)
39
+ return Math.round(top * alpha + bottom * (1 - alpha))
40
+ .toString(16)
41
+ .padStart(2, '0')
42
+ })
43
+ return hexToOklch(`#${channels.join('')}`)
44
+ }
45
+
46
+ /** The candidate with the highest contrast against `target`; the earliest wins a tie. */
47
+ export function mostContrasting<T extends Oklch>(target: Oklch, candidates: readonly T[]): T {
48
+ let best = candidates[0] as T
49
+ let bestRatio = contrastRatio(best, target)
50
+ for (const candidate of candidates.slice(1)) {
51
+ const ratio = contrastRatio(candidate, target)
52
+ if (ratio > bestRatio) {
53
+ best = candidate
54
+ bestRatio = ratio
55
+ }
56
+ }
57
+ return best
58
+ }
59
+
60
+ /**
61
+ * The smallest lightness repair of `foreground` whose hex encoding clears `floor`
62
+ * against `background`, as hex, or `undefined` when `budget` cannot reach it.
63
+ *
64
+ * A value repaired to exactly the floor can round to an eight-bit colour just below
65
+ * it, so the requested floor is raised a thousandth at a time until the encoded value
66
+ * itself clears.
67
+ */
68
+ export function repairToHex(
69
+ foreground: Oklch,
70
+ background: Oklch,
71
+ floor: number,
72
+ budget: number
73
+ ): string | undefined {
74
+ for (let margin = 0; margin <= 0.05; margin += 0.001) {
75
+ const repaired = repairContrast(foreground, background, floor + margin, budget)
76
+ if (!repaired.satisfied) continue
77
+ const hex = oklchToHex(repaired.color)
78
+ const drawn = parseColor(hex)
79
+ if (drawn && clearsFloor(contrastRatio(drawn, background), floor)) return hex
80
+ }
81
+ return undefined
82
+ }