@adea-ai/themes 0.11.9 → 0.11.13
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.
- package/dist/accents.d.ts +19 -19
- package/dist/accents.d.ts.map +1 -1
- package/dist/accents.js +45 -93
- package/dist/accents.js.map +1 -1
- package/dist/adapters/base24.d.ts +21 -12
- package/dist/adapters/base24.d.ts.map +1 -1
- package/dist/adapters/base24.js +47 -72
- package/dist/adapters/base24.js.map +1 -1
- package/dist/adapters/css-variables.d.ts +29 -0
- package/dist/adapters/css-variables.d.ts.map +1 -0
- package/dist/adapters/css-variables.js +56 -0
- package/dist/adapters/css-variables.js.map +1 -0
- package/dist/adapters/css.d.ts +2 -4
- package/dist/adapters/css.d.ts.map +1 -1
- package/dist/adapters/css.js +7 -39
- package/dist/adapters/css.js.map +1 -1
- package/dist/adapters/shadcn.d.ts +8 -4
- package/dist/adapters/shadcn.d.ts.map +1 -1
- package/dist/adapters/shadcn.js +38 -57
- package/dist/adapters/shadcn.js.map +1 -1
- package/dist/adapters/shiki.d.ts +2 -10
- package/dist/adapters/shiki.d.ts.map +1 -1
- package/dist/adapters/shiki.js +16 -29
- package/dist/adapters/shiki.js.map +1 -1
- package/dist/adapters/tailwind.d.ts +3 -3
- package/dist/adapters/tailwind.d.ts.map +1 -1
- package/dist/adapters/tailwind.js +7 -28
- package/dist/adapters/tailwind.js.map +1 -1
- package/dist/adapters/xterm.d.ts.map +1 -1
- package/dist/adapters/xterm.js +9 -21
- package/dist/adapters/xterm.js.map +1 -1
- package/dist/base24-synthesis.d.ts +31 -0
- package/dist/base24-synthesis.d.ts.map +1 -0
- package/dist/base24-synthesis.js +25 -0
- package/dist/base24-synthesis.js.map +1 -0
- package/dist/derive.d.ts +1 -1
- package/dist/derive.d.ts.map +1 -1
- package/dist/derive.js +11 -26
- package/dist/derive.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/normalize.d.ts +0 -8
- package/dist/normalize.d.ts.map +1 -1
- package/dist/normalize.js +105 -232
- package/dist/normalize.js.map +1 -1
- package/dist/oklch.d.ts +1 -7
- package/dist/oklch.d.ts.map +1 -1
- package/dist/oklch.js +15 -35
- package/dist/oklch.js.map +1 -1
- package/dist/policy.d.ts +14 -4
- package/dist/policy.d.ts.map +1 -1
- package/dist/policy.js +16 -4
- package/dist/policy.js.map +1 -1
- package/dist/render.d.ts +30 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +74 -0
- package/dist/render.js.map +1 -0
- package/dist/sources.d.ts +26 -33
- package/dist/sources.d.ts.map +1 -1
- package/dist/sources.js +136 -305
- package/dist/sources.js.map +1 -1
- package/dist/step-search.d.ts +10 -0
- package/dist/step-search.d.ts.map +1 -0
- package/dist/step-search.js +21 -0
- package/dist/step-search.js.map +1 -0
- package/dist/theme-token-projection-script.js +1 -1
- package/dist/theme-token-projection.d.ts +34 -3
- package/dist/theme-token-projection.d.ts.map +1 -1
- package/dist/theme-token-projection.js +78 -2
- package/dist/theme-token-projection.js.map +1 -1
- package/dist/validate.d.ts +12 -17
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +17 -63
- package/dist/validate.js.map +1 -1
- package/package.json +1 -1
- package/src/accents.ts +57 -96
- package/src/adapters/base24.ts +53 -75
- package/src/adapters/css-variables.ts +67 -0
- package/src/adapters/css.ts +9 -54
- package/src/adapters/shadcn.ts +34 -60
- package/src/adapters/shiki.ts +17 -30
- package/src/adapters/tailwind.ts +7 -37
- package/src/adapters/xterm.ts +9 -21
- package/src/base24-synthesis.ts +29 -0
- package/src/derive.ts +12 -27
- package/src/index.ts +6 -5
- package/src/normalize.ts +140 -267
- package/src/oklch.ts +20 -35
- package/src/policy.ts +18 -4
- package/src/render.ts +82 -0
- package/src/sources.ts +160 -330
- package/src/step-search.ts +25 -0
- package/src/theme-token-projection.ts +126 -12
- package/src/validate.ts +31 -74
- package/dist/theme-token-projection-core.d.ts +0 -35
- package/dist/theme-token-projection-core.d.ts.map +0 -1
- package/dist/theme-token-projection-core.js +0 -84
- package/dist/theme-token-projection-core.js.map +0 -1
- package/src/theme-token-projection-core.ts +0 -131
|
@@ -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
|
+
}
|
|
@@ -1,12 +1,126 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
*
|
|
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
|
-
/**
|
|
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`
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
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 `
|
|
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
|
|
135
|
-
*
|
|
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
|
|
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
|
|
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
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
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
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export type ThemeTokenContrastRule = Readonly<{
|
|
2
|
-
/** The custom property the generated foreground must read against. */
|
|
3
|
-
against: string;
|
|
4
|
-
/** Minimum WCAG 2.1 contrast ratio for the generated value. */
|
|
5
|
-
minimum: number;
|
|
6
|
-
}>;
|
|
7
|
-
export type ThemeTokenProjectionRule = Readonly<{
|
|
8
|
-
/** Destination custom property; an explicit value is never replaced. */
|
|
9
|
-
target: string;
|
|
10
|
-
/** Existing custom property to project when `target` is absent. */
|
|
11
|
-
source: string;
|
|
12
|
-
/** Repair a generated destination when both source and surface are colours. */
|
|
13
|
-
contrast?: ThemeTokenContrastRule;
|
|
14
|
-
}>;
|
|
15
|
-
export type ThemeTokenProjection = readonly ThemeTokenProjectionRule[];
|
|
16
|
-
export type ThemeTokenProjectionFailure = Readonly<{
|
|
17
|
-
target: string;
|
|
18
|
-
source: string;
|
|
19
|
-
against: string;
|
|
20
|
-
minimum: number;
|
|
21
|
-
reason: 'unsupported_color' | 'contrast_unreachable';
|
|
22
|
-
}>;
|
|
23
|
-
export type ThemeTokenProjectionResult = Readonly<{
|
|
24
|
-
tokens: Record<string, string>;
|
|
25
|
-
failures: readonly ThemeTokenProjectionFailure[];
|
|
26
|
-
}>;
|
|
27
|
-
/**
|
|
28
|
-
* Add projected values for absent custom properties, leaving explicit values byte
|
|
29
|
-
* for byte intact. Rules run in order so one generated surface can anchor its text.
|
|
30
|
-
* Unknown or malformed rules and unparseable colors fail closed by skipping repair.
|
|
31
|
-
*/
|
|
32
|
-
export declare function projectThemeTokensDetailed(tokens: Readonly<Record<string, string>>, projection: ThemeTokenProjection): ThemeTokenProjectionResult;
|
|
33
|
-
/** Project tokens while discarding diagnostics for callers that only paint them. */
|
|
34
|
-
export declare function projectThemeTokensCore(tokens: Readonly<Record<string, string>>, projection: ThemeTokenProjection): Record<string, string>;
|
|
35
|
-
//# sourceMappingURL=theme-token-projection-core.d.ts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oB,GAC/B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAExB"}
|
|
@@ -1,84 +0,0 @@
|
|
|
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, oklchToHex, parseColor, repairContrast } from './oklch.js';
|
|
10
|
-
const CUSTOM_PROPERTY = /^--[A-Za-z0-9_-]+$/;
|
|
11
|
-
function validRule(rule) {
|
|
12
|
-
if (!rule || typeof rule !== 'object' || Array.isArray(rule))
|
|
13
|
-
return false;
|
|
14
|
-
const contrast = rule.contrast;
|
|
15
|
-
return (CUSTOM_PROPERTY.test(rule.target) &&
|
|
16
|
-
CUSTOM_PROPERTY.test(rule.source) &&
|
|
17
|
-
(contrast === undefined ||
|
|
18
|
-
(!!contrast &&
|
|
19
|
-
typeof contrast === 'object' &&
|
|
20
|
-
CUSTOM_PROPERTY.test(contrast.against) &&
|
|
21
|
-
Number.isFinite(contrast.minimum) &&
|
|
22
|
-
contrast.minimum > 1 &&
|
|
23
|
-
contrast.minimum <= 21)));
|
|
24
|
-
}
|
|
25
|
-
function hasUnsupportedAlpha(value) {
|
|
26
|
-
return /^oklch\([^)]*\/[^)]*\)$/i.test(value.trim());
|
|
27
|
-
}
|
|
28
|
-
function repairGeneratedValue(value, against, minimum) {
|
|
29
|
-
if (hasUnsupportedAlpha(value) || hasUnsupportedAlpha(against)) {
|
|
30
|
-
return { value, failure: 'unsupported_color' };
|
|
31
|
-
}
|
|
32
|
-
const foreground = parseColor(value);
|
|
33
|
-
const background = parseColor(against);
|
|
34
|
-
if (!foreground || !background)
|
|
35
|
-
return { value, failure: 'unsupported_color' };
|
|
36
|
-
if (contrastRatio(foreground, background) >= minimum)
|
|
37
|
-
return { value };
|
|
38
|
-
const repaired = repairContrast(foreground, background, minimum, 0.6);
|
|
39
|
-
if (!repaired.satisfied)
|
|
40
|
-
return { value, failure: 'contrast_unreachable' };
|
|
41
|
-
const hex = oklchToHex(repaired.color);
|
|
42
|
-
const rounded = parseColor(hex);
|
|
43
|
-
return rounded && contrastRatio(rounded, background) >= minimum
|
|
44
|
-
? { value: hex }
|
|
45
|
-
: { value, failure: 'contrast_unreachable' };
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Add projected values for absent custom properties, leaving explicit values byte
|
|
49
|
-
* for byte intact. Rules run in order so one generated surface can anchor its text.
|
|
50
|
-
* Unknown or malformed rules and unparseable colors fail closed by skipping repair.
|
|
51
|
-
*/
|
|
52
|
-
export function projectThemeTokensDetailed(tokens, projection) {
|
|
53
|
-
const projected = { ...tokens };
|
|
54
|
-
const failures = [];
|
|
55
|
-
for (const rule of Array.isArray(projection) ? projection : []) {
|
|
56
|
-
if (!validRule(rule) || Object.hasOwn(projected, rule.target))
|
|
57
|
-
continue;
|
|
58
|
-
const value = projected[rule.source];
|
|
59
|
-
if (typeof value !== 'string')
|
|
60
|
-
continue;
|
|
61
|
-
if (!rule.contrast) {
|
|
62
|
-
projected[rule.target] = value;
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
const against = projected[rule.contrast.against] ?? '';
|
|
66
|
-
const result = repairGeneratedValue(value, against, rule.contrast.minimum);
|
|
67
|
-
projected[rule.target] = result.value;
|
|
68
|
-
if (result.failure) {
|
|
69
|
-
failures.push({
|
|
70
|
-
target: rule.target,
|
|
71
|
-
source: rule.source,
|
|
72
|
-
against: rule.contrast.against,
|
|
73
|
-
minimum: rule.contrast.minimum,
|
|
74
|
-
reason: result.failure,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return { tokens: projected, failures };
|
|
79
|
-
}
|
|
80
|
-
/** Project tokens while discarding diagnostics for callers that only paint them. */
|
|
81
|
-
export function projectThemeTokensCore(tokens, projection) {
|
|
82
|
-
return projectThemeTokensDetailed(tokens, projection).tokens;
|
|
83
|
-
}
|
|
84
|
-
//# sourceMappingURL=theme-token-projection-core.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"theme-token-projection-core.js","sourceRoot":"","sources":["../src/theme-token-projection-core.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAiClF,MAAM,eAAe,GAAG,oBAAoB,CAAA;AAE5C,SAAS,SAAS,CAAC,IAA8B;IAC/C,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAA;IAC1E,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAA;IAC9B,OAAO,CACL,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;QACjC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;QACjC,CAAC,QAAQ,KAAK,SAAS;YACrB,CAAC,CAAC,CAAC,QAAQ;gBACT,OAAO,QAAQ,KAAK,QAAQ;gBAC5B,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;gBACtC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC;gBACjC,QAAQ,CAAC,OAAO,GAAG,CAAC;gBACpB,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,CAC7B,CAAA;AACH,CAAC;AAED,SAAS,mBAAmB,CAAC,KAAa;IACxC,OAAO,0BAA0B,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAA;AACtD,CAAC;AAED,SAAS,oBAAoB,CAC3B,KAAa,EACb,OAAe,EACf,OAAe;IAEf,IAAI,mBAAmB,CAAC,K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@@
|
|
|
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, oklchToHex, parseColor, repairContrast } from './oklch.js'
|
|
10
|
-
|
|
11
|
-
export type ThemeTokenContrastRule = Readonly<{
|
|
12
|
-
/** The custom property the generated foreground must read against. */
|
|
13
|
-
against: string
|
|
14
|
-
/** Minimum WCAG 2.1 contrast ratio for the generated value. */
|
|
15
|
-
minimum: number
|
|
16
|
-
}>
|
|
17
|
-
|
|
18
|
-
export type ThemeTokenProjectionRule = Readonly<{
|
|
19
|
-
/** Destination custom property; an explicit value is never replaced. */
|
|
20
|
-
target: string
|
|
21
|
-
/** Existing custom property to project when `target` is absent. */
|
|
22
|
-
source: string
|
|
23
|
-
/** Repair a generated destination when both source and surface are colours. */
|
|
24
|
-
contrast?: ThemeTokenContrastRule
|
|
25
|
-
}>
|
|
26
|
-
|
|
27
|
-
export type ThemeTokenProjection = readonly ThemeTokenProjectionRule[]
|
|
28
|
-
|
|
29
|
-
export type ThemeTokenProjectionFailure = Readonly<{
|
|
30
|
-
target: string
|
|
31
|
-
source: string
|
|
32
|
-
against: string
|
|
33
|
-
minimum: number
|
|
34
|
-
reason: 'unsupported_color' | 'contrast_unreachable'
|
|
35
|
-
}>
|
|
36
|
-
|
|
37
|
-
export type ThemeTokenProjectionResult = Readonly<{
|
|
38
|
-
tokens: Record<string, string>
|
|
39
|
-
failures: readonly ThemeTokenProjectionFailure[]
|
|
40
|
-
}>
|
|
41
|
-
|
|
42
|
-
const CUSTOM_PROPERTY = /^--[A-Za-z0-9_-]+$/
|
|
43
|
-
|
|
44
|
-
function validRule(rule: ThemeTokenProjectionRule): boolean {
|
|
45
|
-
if (!rule || typeof rule !== 'object' || Array.isArray(rule)) return false
|
|
46
|
-
const contrast = rule.contrast
|
|
47
|
-
return (
|
|
48
|
-
CUSTOM_PROPERTY.test(rule.target) &&
|
|
49
|
-
CUSTOM_PROPERTY.test(rule.source) &&
|
|
50
|
-
(contrast === undefined ||
|
|
51
|
-
(!!contrast &&
|
|
52
|
-
typeof contrast === 'object' &&
|
|
53
|
-
CUSTOM_PROPERTY.test(contrast.against) &&
|
|
54
|
-
Number.isFinite(contrast.minimum) &&
|
|
55
|
-
contrast.minimum > 1 &&
|
|
56
|
-
contrast.minimum <= 21))
|
|
57
|
-
)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function hasUnsupportedAlpha(value: string): boolean {
|
|
61
|
-
return /^oklch\([^)]*\/[^)]*\)$/i.test(value.trim())
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function repairGeneratedValue(
|
|
65
|
-
value: string,
|
|
66
|
-
against: string,
|
|
67
|
-
minimum: number
|
|
68
|
-
): { value: string; failure?: ThemeTokenProjectionFailure['reason'] } {
|
|
69
|
-
if (hasUnsupportedAlpha(value) || hasUnsupportedAlpha(against)) {
|
|
70
|
-
return { value, failure: 'unsupported_color' }
|
|
71
|
-
}
|
|
72
|
-
const foreground = parseColor(value)
|
|
73
|
-
const background = parseColor(against)
|
|
74
|
-
if (!foreground || !background) return { value, failure: 'unsupported_color' }
|
|
75
|
-
if (contrastRatio(foreground, background) >= minimum) return { value }
|
|
76
|
-
|
|
77
|
-
const repaired = repairContrast(foreground, background, minimum, 0.6)
|
|
78
|
-
if (!repaired.satisfied) return { value, failure: 'contrast_unreachable' }
|
|
79
|
-
|
|
80
|
-
const hex = oklchToHex(repaired.color)
|
|
81
|
-
const rounded = parseColor(hex)
|
|
82
|
-
return rounded && contrastRatio(rounded, background) >= minimum
|
|
83
|
-
? { value: hex }
|
|
84
|
-
: { value, failure: 'contrast_unreachable' }
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Add projected values for absent custom properties, leaving explicit values byte
|
|
89
|
-
* for byte intact. Rules run in order so one generated surface can anchor its text.
|
|
90
|
-
* Unknown or malformed rules and unparseable colors fail closed by skipping repair.
|
|
91
|
-
*/
|
|
92
|
-
export function projectThemeTokensDetailed(
|
|
93
|
-
tokens: Readonly<Record<string, string>>,
|
|
94
|
-
projection: ThemeTokenProjection
|
|
95
|
-
): ThemeTokenProjectionResult {
|
|
96
|
-
const projected = { ...tokens }
|
|
97
|
-
const failures: ThemeTokenProjectionFailure[] = []
|
|
98
|
-
|
|
99
|
-
for (const rule of Array.isArray(projection) ? projection : []) {
|
|
100
|
-
if (!validRule(rule) || Object.hasOwn(projected, rule.target)) continue
|
|
101
|
-
const value = projected[rule.source]
|
|
102
|
-
if (typeof value !== 'string') continue
|
|
103
|
-
if (!rule.contrast) {
|
|
104
|
-
projected[rule.target] = value
|
|
105
|
-
continue
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const against = projected[rule.contrast.against] ?? ''
|
|
109
|
-
const result = repairGeneratedValue(value, against, rule.contrast.minimum)
|
|
110
|
-
projected[rule.target] = result.value
|
|
111
|
-
if (result.failure) {
|
|
112
|
-
failures.push({
|
|
113
|
-
target: rule.target,
|
|
114
|
-
source: rule.source,
|
|
115
|
-
against: rule.contrast.against,
|
|
116
|
-
minimum: rule.contrast.minimum,
|
|
117
|
-
reason: result.failure,
|
|
118
|
-
})
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return { tokens: projected, failures }
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** Project tokens while discarding diagnostics for callers that only paint them. */
|
|
126
|
-
export function projectThemeTokensCore(
|
|
127
|
-
tokens: Readonly<Record<string, string>>,
|
|
128
|
-
projection: ThemeTokenProjection
|
|
129
|
-
): Record<string, string> {
|
|
130
|
-
return projectThemeTokensDetailed(tokens, projection).tokens
|
|
131
|
-
}
|