@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
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The smallest move along one axis that satisfies a predicate.
3
+ *
4
+ * Tries `step`, then `2 × step`, and so on up to `steps` increments, in each of
5
+ * `directions` at every size, and returns the first offset `accepts`.
6
+ */
7
+ export function smallestOffset(
8
+ step: number,
9
+ steps: number,
10
+ directions: readonly number[],
11
+ accepts: (offset: number) => boolean
12
+ ): number | undefined {
13
+ for (let index = 1; index <= steps; index += 1) {
14
+ for (const direction of directions) {
15
+ const offset = direction * step * index
16
+ if (accepts(offset)) return offset
17
+ }
18
+ }
19
+ return undefined
20
+ }
21
+
22
+ /** The size of an offset as a repair reports it: positive, at the catalogue's precision. */
23
+ export function offsetSize(offset: number): number {
24
+ return Number(Math.abs(offset).toFixed(4))
25
+ }
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Serializable projections for token maps that have stable roles but predate one
3
+ * of the design system's derived token families.
4
+ *
5
+ * The rules are data rather than a callback because the same projection can run
6
+ * against a provider's live variant and against a stored theme during the
7
+ * pre-paint script. Explicit destination tokens always win.
8
+ */
9
+ import { contrastRatio, parseColor } from './oklch.js'
10
+ import { repairToHex } from './render.js'
11
+
12
+ export type ThemeTokenContrastRule = Readonly<{
13
+ /** The custom property the generated foreground must read against. */
14
+ against: string
15
+ /** Minimum WCAG 2.1 contrast ratio for the generated value. */
16
+ minimum: number
17
+ }>
18
+
19
+ export type ThemeTokenProjectionRule = Readonly<{
20
+ /** Destination custom property; an explicit value is never replaced. */
21
+ target: string
22
+ /** Existing custom property to project when `target` is absent. */
23
+ source: string
24
+ /** Repair a generated destination when both source and surface are colours. */
25
+ contrast?: ThemeTokenContrastRule
26
+ }>
27
+
28
+ export type ThemeTokenProjection = readonly ThemeTokenProjectionRule[]
29
+
30
+ export type ThemeTokenProjectionFailure = Readonly<{
31
+ target: string
32
+ source: string
33
+ against: string
34
+ minimum: number
35
+ reason: 'unsupported_color' | 'contrast_unreachable'
36
+ }>
37
+
38
+ export type ThemeTokenProjectionResult = Readonly<{
39
+ tokens: Record<string, string>
40
+ failures: readonly ThemeTokenProjectionFailure[]
41
+ }>
42
+
43
+ const CUSTOM_PROPERTY = /^--[A-Za-z0-9_-]+$/
44
+
45
+ function validRule(rule: ThemeTokenProjectionRule): boolean {
46
+ if (!rule || typeof rule !== 'object' || Array.isArray(rule)) return false
47
+ const contrast = rule.contrast
48
+ return (
49
+ CUSTOM_PROPERTY.test(rule.target) &&
50
+ CUSTOM_PROPERTY.test(rule.source) &&
51
+ (contrast === undefined ||
52
+ (!!contrast &&
53
+ typeof contrast === 'object' &&
54
+ CUSTOM_PROPERTY.test(contrast.against) &&
55
+ Number.isFinite(contrast.minimum) &&
56
+ contrast.minimum > 1 &&
57
+ contrast.minimum <= 21))
58
+ )
59
+ }
60
+
61
+ function hasUnsupportedAlpha(value: string): boolean {
62
+ return /^oklch\([^)]*\/[^)]*\)$/i.test(value.trim())
63
+ }
64
+
65
+ function repairGeneratedValue(
66
+ value: string,
67
+ against: string,
68
+ minimum: number
69
+ ): { value: string; failure?: ThemeTokenProjectionFailure['reason'] } {
70
+ if (hasUnsupportedAlpha(value) || hasUnsupportedAlpha(against)) {
71
+ return { value, failure: 'unsupported_color' }
72
+ }
73
+ const foreground = parseColor(value)
74
+ const background = parseColor(against)
75
+ if (!foreground || !background) return { value, failure: 'unsupported_color' }
76
+ if (contrastRatio(foreground, background) >= minimum) return { value }
77
+
78
+ const repaired = repairToHex(foreground, background, minimum, 0.6)
79
+ return repaired ? { value: repaired } : { value, failure: 'contrast_unreachable' }
80
+ }
81
+
82
+ /**
83
+ * Add projected values for absent custom properties, leaving explicit values byte
84
+ * for byte intact. Rules run in order so one generated surface can anchor its text.
85
+ * Unknown or malformed rules and unparseable colors fail closed by skipping repair.
86
+ */
87
+ export function projectThemeTokensDetailed(
88
+ tokens: Readonly<Record<string, string>>,
89
+ projection: ThemeTokenProjection
90
+ ): ThemeTokenProjectionResult {
91
+ const projected = { ...tokens }
92
+ const failures: ThemeTokenProjectionFailure[] = []
93
+
94
+ for (const rule of Array.isArray(projection) ? projection : []) {
95
+ if (!validRule(rule) || Object.hasOwn(projected, rule.target)) continue
96
+ const value = projected[rule.source]
97
+ if (typeof value !== 'string') continue
98
+ if (!rule.contrast) {
99
+ projected[rule.target] = value
100
+ continue
101
+ }
102
+
103
+ const against = projected[rule.contrast.against] ?? ''
104
+ const result = repairGeneratedValue(value, against, rule.contrast.minimum)
105
+ projected[rule.target] = result.value
106
+ if (result.failure) {
107
+ failures.push({
108
+ target: rule.target,
109
+ source: rule.source,
110
+ against: rule.contrast.against,
111
+ minimum: rule.contrast.minimum,
112
+ reason: result.failure,
113
+ })
114
+ }
115
+ }
116
+
117
+ return { tokens: projected, failures }
118
+ }
119
+
120
+ /** Project tokens while discarding diagnostics for callers that only paint them. */
121
+ export function projectThemeTokens(
122
+ tokens: Readonly<Record<string, string>>,
123
+ projection: ThemeTokenProjection
124
+ ): Record<string, string> {
125
+ return projectThemeTokensDetailed(tokens, projection).tokens
126
+ }
package/src/validate.ts CHANGED
@@ -32,7 +32,7 @@ import type { AdeaTheme, AnsiKey, ThemeColorKey } from './schema.js'
32
32
  import { ANSI_KEYS, THEME_COLOR_KEYS } from './schema.js'
33
33
  import type { Oklch } from './oklch.js'
34
34
  import { contrastRatio, deltaEok, parseColor } from './oklch.js'
35
- import { CONTRAST_FLOORS } from './policy.js'
35
+ import { CONTRAST_FLOORS, clearsFloor } from './policy.js'
36
36
  import { STATUS_ROLES, statusForeground } from './derive.js'
37
37
 
38
38
  /** One measured pairing that failed. */
@@ -48,29 +48,26 @@ export interface ContrastFinding {
48
48
  }
49
49
 
50
50
  /**
51
- * The pairings a theme is required to satisfy.
52
- *
53
- * Declared as data rather than as a sequence of assertions so that the set is
54
- * reviewable in one place, and so a consumer can see exactly what is guaranteed.
55
- * `background` appears as the counterpart almost everywhere because a component's
56
- * default surface is the canvas; the two exceptions are the pairs drawn on a
57
- * raised surface or on the accent.
51
+ * One guaranteed pairing: `role` clears `minimum` against each of `against`.
58
52
  */
59
53
  export interface Pairing {
60
54
  role: ThemeColorKey | AnsiKey | 'accentForeground' | 'border' | 'borderMuted' | 'selection'
61
55
  against: readonly (ThemeColorKey | 'background' | 'accent')[]
62
56
  minimum: number
63
- /** Findings for pairings marked `advisory` are reported but do not fail. */
57
+ /**
58
+ * Marks a pairing as advisory. No built-in pairing sets it, and `validateTheme`
59
+ * reports its findings like any other.
60
+ */
64
61
  advisory?: boolean
65
62
  }
66
63
 
67
64
  /**
68
65
  * Every pairing the catalogue guarantees.
69
66
  *
70
- * `textSubtle` and `brightBlack` are held to 3:1 rather than 4.5:1 — see
71
- * `derive` and the schema's note on `textSubtle`. That is not a relaxation for
72
- * convenience: at 4.5:1 there is no visible difference between tertiary and
73
- * secondary text and the role stops earning its place in the schema.
67
+ * `textSubtle` is held to 3:1 rather than 4.5:1 — see the schema's note on it.
68
+ * That is not a relaxation for convenience: at 4.5:1 there is no visible
69
+ * difference between tertiary and secondary text and the role stops earning its
70
+ * place in the schema.
74
71
  */
75
72
  export const REQUIRED_PAIRINGS: readonly Pairing[] = Object.freeze([
76
73
  { role: 'text', against: ['background'], minimum: CONTRAST_FLOORS.text },
@@ -101,7 +98,7 @@ export const REQUIRED_PAIRINGS: readonly Pairing[] = Object.freeze([
101
98
  *
102
99
  * Two tiers, matching the normalizer: the body floor on the canvas, where a status
103
100
  * colour is read as small text, and the indicator floor on a raised surface. The
104
- * reasoning is in `normalize.CONTRAST_FLOORS.statusRaised`.
101
+ * reasoning is in `CONTRAST_FLOORS.statusRaised`.
105
102
  */
106
103
  export const STATUS_PAIRINGS: readonly Pairing[] = Object.freeze([
107
104
  ...STATUS_ROLES.map((role) => ({
@@ -131,10 +128,8 @@ export const STATUS_PAIRINGS: readonly Pairing[] = Object.freeze([
131
128
  *
132
129
  * - **Collapse** — a ramp whose roles resolve to the same colour, which means the
133
130
  * theme cannot express dim text at all.
134
- * - **Inversion** — an ANSI black lighter than its bright black on a dark theme, or
135
- * a light theme whose "white" is darker than its text. This is the defect the
136
- * normalizer's ramp logic was rewritten to avoid, and the one a palette with a
137
- * degenerate `base05`/`base06`/`base07` produces.
131
+ * - **Inversion** — an ANSI bright black no lighter than its black, in either
132
+ * appearance.
138
133
  */
139
134
  export interface AnsiInvariant {
140
135
  themeId: string
@@ -147,8 +142,6 @@ export function checkAnsiRamp(theme: AdeaTheme): AnsiInvariant[] {
147
142
  const findings: AnsiInvariant[] = []
148
143
  const black = parseColor(theme.ansi.black)
149
144
  const brightBlack = parseColor(theme.ansi.brightBlack)
150
- const white = parseColor(theme.ansi.white)
151
- const brightWhite = parseColor(theme.ansi.brightWhite)
152
145
 
153
146
  if (black && brightBlack) {
154
147
  if (deltaEok(black, brightBlack) < 0.02) {
@@ -175,15 +168,10 @@ export function checkAnsiRamp(theme: AdeaTheme): AnsiInvariant[] {
175
168
  // own light theme uses, sets white to a mid grey and bright white darker still,
176
169
  // and several palettes in the catalogue set the two to the same value. There is no
177
170
  // invariant there — unlike ANSI 0/8, whose ordering holds in both appearances.
178
- void white
179
- void brightWhite
180
-
181
171
  return findings
182
172
  }
183
173
 
184
174
  function resolve(theme: AdeaTheme, role: string): Oklch | undefined {
185
- if (role === 'accent') return parseColor(theme.colors.accent)
186
- if (role === 'background') return parseColor(theme.colors.background)
187
175
  if ((THEME_COLOR_KEYS as readonly string[]).includes(role)) {
188
176
  return parseColor(theme.colors[role as ThemeColorKey])
189
177
  }
@@ -195,6 +183,11 @@ function resolve(theme: AdeaTheme, role: string): Oklch | undefined {
195
183
  return undefined
196
184
  }
197
185
 
186
+ /** A finding about a theme's shape rather than a measured pairing. */
187
+ function structural(themeId: string, role: string, message: string): ContrastFinding {
188
+ return { themeId, role, against: [], ratio: 0, minimum: 0, message }
189
+ }
190
+
198
191
  /** Measures one pairing, returning a finding when it fails. */
199
192
  function measure(theme: AdeaTheme, pairing: Pairing): ContrastFinding | undefined {
200
193
  const subject = resolve(theme, pairing.role)
@@ -213,7 +206,7 @@ function measure(theme: AdeaTheme, pairing: Pairing): ContrastFinding | undefine
213
206
  const counterpart = resolve(theme, counterpartName)
214
207
  if (!counterpart) continue
215
208
  const ratio = contrastRatio(subject, counterpart)
216
- if (ratio < pairing.minimum - 1e-9) {
209
+ if (!clearsFloor(ratio, pairing.minimum)) {
217
210
  return {
218
211
  themeId: theme.id,
219
212
  role: pairing.role,
@@ -244,18 +237,9 @@ export function validateTheme(theme: AdeaTheme): ContrastFinding[] {
244
237
  if (finding) findings.push(finding)
245
238
  }
246
239
 
247
- // Some roles are not used in contrast pairings, but still leave the package
248
- // through adapters and must be valid colours at this public validation boundary.
249
240
  for (const role of [...THEME_COLOR_KEYS, ...ANSI_KEYS, 'cursor', 'selection']) {
250
241
  if (resolve(theme, role) || findings.some((finding) => finding.role === role)) continue
251
- findings.push({
252
- themeId: theme.id,
253
- role,
254
- against: [],
255
- ratio: 0,
256
- minimum: 0,
257
- message: `${role} is missing or is not a colour`,
258
- })
242
+ findings.push(structural(theme.id, role, `${role} is missing or is not a colour`))
259
243
  }
260
244
 
261
245
  // Canonical colours are measured as opaque, and adapters such as CSS emit the
@@ -271,38 +255,19 @@ export function validateTheme(theme: AdeaTheme): ContrastFinding[] {
271
255
  if (typeof value !== 'string' || !parseColor(value)) continue
272
256
  const alpha = /\/\s*([\d.]+)(%)?\s*\)$/i.exec(value.trim())
273
257
  if (!alpha || Number(alpha[1]) >= (alpha[2] ? 100 : 1)) continue
274
- findings.push({
275
- themeId: theme.id,
276
- role,
277
- against: [],
278
- ratio: 0,
279
- minimum: 0,
280
- message: `${role} must be opaque for contrast validation`,
281
- })
258
+ findings.push(structural(theme.id, role, `${role} must be opaque for contrast validation`))
282
259
  }
283
260
 
284
- // The greyscale ramp's failures are structural rather than contrast-based, so
285
- // they are reported as findings with no ratio.
286
261
  for (const invariant of checkAnsiRamp(theme)) {
287
- findings.push({
288
- themeId: invariant.themeId,
289
- role: invariant.role,
290
- against: [],
291
- ratio: 0,
292
- minimum: 0,
293
- message: invariant.message,
294
- })
262
+ findings.push(structural(invariant.themeId, invariant.role, invariant.message))
295
263
  }
296
264
 
297
- // The status foreground pairing cannot be declared as a table entry because it
298
- // depends on the theme: the correct colour is whichever extreme of the theme
299
- // measures better, so it is derived and then measured rather than named.
300
265
  for (const role of STATUS_ROLES) {
301
266
  const fill = parseColor(theme.colors[role])
302
267
  const chosen = parseColor(statusForeground(theme, role))
303
268
  if (!fill || !chosen) continue
304
269
  const ratio = contrastRatio(chosen, fill)
305
- if (ratio < CONTRAST_FLOORS.accentForeground - 1e-9) {
270
+ if (!clearsFloor(ratio, CONTRAST_FLOORS.accentForeground)) {
306
271
  findings.push({
307
272
  themeId: theme.id,
308
273
  role: `${role}-foreground`,
@@ -331,14 +296,7 @@ export function validateCatalogue(themes: readonly AdeaTheme[]): ContrastFinding
331
296
  const ids = new Set<string>()
332
297
  for (const theme of themes) {
333
298
  if (ids.has(theme.id)) {
334
- findings.push({
335
- themeId: theme.id,
336
- role: 'id',
337
- against: [],
338
- ratio: 0,
339
- minimum: 0,
340
- message: `duplicate theme id: ${theme.id}`,
341
- })
299
+ findings.push(structural(theme.id, 'id', `duplicate theme id: ${theme.id}`))
342
300
  }
343
301
  ids.add(theme.id)
344
302
  findings.push(...validateTheme(theme))
@@ -347,14 +305,13 @@ export function validateCatalogue(themes: readonly AdeaTheme[]): ContrastFinding
347
305
  const light = themes.filter((theme) => theme.appearance === 'light').length
348
306
  const dark = themes.filter((theme) => theme.appearance === 'dark').length
349
307
  if (light === 0 || dark === 0) {
350
- findings.push({
351
- themeId: 'catalogue',
352
- role: 'appearance',
353
- against: [],
354
- ratio: 0,
355
- minimum: 0,
356
- message: `catalogue covers ${light} light and ${dark} dark themes; both appearances must be represented`,
357
- })
308
+ findings.push(
309
+ structural(
310
+ 'catalogue',
311
+ 'appearance',
312
+ `catalogue covers ${light} light and ${dark} dark themes; both appearances must be represented`
313
+ )
314
+ )
358
315
  }
359
316
 
360
317
  return findings