@adea-ai/themes 0.9.9 → 0.10.0

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/src/normalize.ts CHANGED
@@ -69,64 +69,18 @@ import {
69
69
  } from './oklch.js'
70
70
  import type { ContrastRepair } from './oklch.js'
71
71
  import { repairContrast } from './oklch.js'
72
+ import { ACCENT_SUBTLE_MINIMUM_ALPHA, primaryTintClears } from './accents.js'
73
+ import {
74
+ ACCENT_PREFERENCE,
75
+ CONTRAST_FLOORS,
76
+ MINIMUM_ACCENT_CHROMA,
77
+ REPAIR_BUDGET,
78
+ } from './policy.js'
72
79
 
73
- /**
74
- * The contrast floors.
75
- *
76
- * These are the catalogue's policy, and they are the reason an external palette
77
- * can be admitted at all: nothing enters the catalogue on taste. `text` sits at
78
- * the WCAG AA body-text floor; `border` is measured on the non-text scale, where
79
- * the requirement is perceptibility rather than legibility.
80
- */
81
- export const CONTRAST_FLOORS = Object.freeze({
82
- text: 4.5,
83
- textMuted: 4.5,
84
- /** Large or non-essential text, per WCAG 1.4.3. */
85
- textSubtle: 3,
86
- accent: 4.5,
87
- /**
88
- * The accent on a raised surface, where it is a fill rather than a label.
89
- *
90
- * The same two-tier reasoning as `statusRaised`: a button or a selected tab is a
91
- * user-interface component, which WCAG holds to 3:1, and the pairing that
92
- * actually carries text — the accent's own label — is measured separately at 4.5
93
- * against the accent itself. Holding the accent to 4.5 on every surface instead
94
- * made Everforest Light's every hue collapse into its body text, because that
95
- * palette's colours are all around 2:1 on its own canvas.
96
- */
97
- accentRaised: 3,
98
- accentForeground: 4.5,
99
- /**
100
- * Status colours drawn on the canvas, where they are read as small text.
101
- */
102
- status: 4.5,
103
- /**
104
- * Status colours drawn on a raised surface, where they are an indicator.
105
- *
106
- * The two-tier policy is deliberate and it is not a relaxation for convenience.
107
- * A status role on a card is nearly always a dot, an icon or a chip's own fill —
108
- * a graphical object, which WCAG holds to 3:1 — and it is that way because the
109
- * alternative does not exist: Ayu Light and Everforest Light are muted palettes
110
- * built for a near-white canvas and neither contains a yellow that is legible as
111
- * small text on one. Forcing 4.5:1 there required blending the warning colour
112
- * 94% of the way to the body text, which produces a warning that is the same
113
- * colour as the text beside it and is therefore not a warning at all.
114
- *
115
- * A component that renders small status *text* on a raised surface should put it
116
- * on the role's `-subtle` fill, which is a tint of the canvas and is measured at
117
- * the body floor.
118
- */
119
- statusRaised: 3,
120
- /** A divider is decorative; it must be seen, not read. */
121
- border: 1.15,
122
- borderMuted: 1.08,
123
- /** A raised surface must be distinguishable from the canvas it sits on. */
124
- surface: 1.03,
125
- /** A selection must be visible under the text it selects. */
126
- selection: 1.2,
127
- } as const)
80
+ // The catalogue's policy lives in its own module so the accent axis can read it
81
+ // without importing the normalizer; it is re-exported here, where it has always been.
82
+ export { ACCENT_PREFERENCE, CONTRAST_FLOORS, MINIMUM_ACCENT_CHROMA, REPAIR_BUDGET }
128
83
 
129
- /** How far the normalizer may move a colour's lightness before it gives up. */
130
84
  /**
131
85
  * How much a repair changed the palette's colour.
132
86
  *
@@ -141,21 +95,6 @@ function distortionOf(attempt: { repair: ContrastRepair; blended: boolean }): nu
141
95
  return attempt.blended ? attempt.repair.delta * 3 : attempt.repair.delta
142
96
  }
143
97
 
144
- export const REPAIR_BUDGET = Object.freeze({
145
- /** Body text: some palettes publish a foreground that fails on their own canvas. */
146
- text: 0.2,
147
- status: 0.24,
148
- accent: 0.24,
149
- })
150
-
151
- /** The accent preference order, best first. */
152
- export const ACCENT_PREFERENCE = [
153
- 'blue',
154
- 'magenta',
155
- 'cyan',
156
- 'green',
157
- ] as const satisfies readonly AnsiKey[]
158
-
159
98
  /**
160
99
  * How much stronger body text must be than the secondary rung.
161
100
  *
@@ -165,9 +104,6 @@ export const ACCENT_PREFERENCE = [
165
104
  */
166
105
  const MINIMUM_LADDER_GAP = 0.4
167
106
 
168
- /** The smallest chroma a colour needs before it reads as "a colour" rather than grey. */
169
- export const MINIMUM_ACCENT_CHROMA = 0.035
170
-
171
107
  /** A theme's source description, as the catalogue declares it. */
172
108
  export interface ThemeSourceSpec {
173
109
  id: string
@@ -883,9 +819,11 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
883
819
  /**
884
820
  * Every surface a foreground role can be drawn on.
885
821
  *
886
- * The interaction rungs are excluded: text is not placed on a hover fill without
887
- * the component also choosing a foreground for it, and holding every role to the
888
- * furthest rung would compress the whole palette for a case that does not occur.
822
+ * The interaction rungs are excluded: holding every role to the furthest rung would
823
+ * compress the whole palette for a case that does not occur. Body text *is* drawn
824
+ * on them — a hovered or pressed row keeps its label — so once the text and the
825
+ * accent are settled the rungs are fitted to the text instead, in
826
+ * {@link fitInteractionRungs}.
889
827
  */
890
828
  const surfaces = [background, surface, surfaceElevated]
891
829
 
@@ -1185,6 +1123,8 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
1185
1123
  }
1186
1124
  }
1187
1125
 
1126
+ fitInteractionRungs(source, { appearance, colors, ansi }, background, direction, findings)
1127
+
1188
1128
  return {
1189
1129
  record: {
1190
1130
  id: source.id,
@@ -1210,6 +1150,92 @@ export function normalizeTheme(source: ThemeSourceSpec): NormalizedTheme {
1210
1150
  }
1211
1151
  }
1212
1152
 
1153
+ /** The resolution the interaction rungs are fitted at. */
1154
+ const RUNG_FIT_STEP = 0.001
1155
+
1156
+ /** Every fitting step from `from` down to `to`, inclusive, largest first. */
1157
+ function stepsBetween(from: number, to: number): number[] {
1158
+ const count = Math.round((from - to) / RUNG_FIT_STEP)
1159
+ return Array.from({ length: count + 1 }, (_, index) => from - index * RUNG_FIT_STEP)
1160
+ }
1161
+
1162
+ /**
1163
+ * Fits the hover and active rungs so body text stays legible on them.
1164
+ *
1165
+ * The ladder's fixed steps are what make a hover read the same in every theme, and
1166
+ * most themes keep them. But a hovered or pressed row is still a row of text, which
1167
+ * WCAG holds to the body floor like any other, and a palette whose body text sits
1168
+ * close to that floor — One Dark's foreground is 6.6:1 on its own canvas — cannot
1169
+ * afford the full step: its hover rung measured 4.45:1 and its active rung 3.86:1.
1170
+ *
1171
+ * So each rung keeps its fixed step unless body text fails on it, and then gives up
1172
+ * only as much as it must, one thousandth of lightness at a time, in the same
1173
+ * direction away from the canvas. Only lightness moves: the rung keeps the chroma
1174
+ * and hue its fixed step gave it, and the text and the canvas are untouched. The hover rung also reserves room for the primary tint at
1175
+ * `ACCENT_SUBTLE_MINIMUM_ALPHA`, because a selected row that is hovered paints the
1176
+ * tint over it; without the reservation the tint would have to fall to nothing.
1177
+ *
1178
+ * The rungs never fall below the elevated rung, so the ladder still ascends, and the
1179
+ * active rung stays beyond the hover rung wherever any step there still holds the
1180
+ * floor. A rung that cannot be fitted above the elevated rung is reported as over
1181
+ * budget, and the validator fails the theme. An authored rung is left as written.
1182
+ */
1183
+ function fitInteractionRungs(
1184
+ source: ThemeSourceSpec,
1185
+ theme: { appearance: ThemeAppearance; colors: AdeaThemeColors; ansi: AdeaAnsi },
1186
+ background: Oklch,
1187
+ direction: 1 | -1,
1188
+ findings: NormalizationFinding[]
1189
+ ): void {
1190
+ const fit = (
1191
+ role: 'surfaceHover' | 'surfaceActive',
1192
+ from: number,
1193
+ floor: number,
1194
+ holds: (rung: string) => boolean
1195
+ ): number => {
1196
+ if (source.colors?.[role] !== undefined) return from
1197
+ // Lightness only: the rung keeps the chroma and hue its fixed step gave it.
1198
+ const original = surfaceRung(background, from, direction)
1199
+ const rungAt = (step: number): string =>
1200
+ formatOklch({ ...original, l: background.l + step * direction })
1201
+ const step = stepsBetween(from, floor).find((candidate) => holds(rungAt(candidate)))
1202
+ if (step === undefined) {
1203
+ findings.push({
1204
+ themeId: source.id,
1205
+ role,
1206
+ kind: 'budget-exceeded',
1207
+ message: `body text cannot clear ${CONTRAST_FLOORS.text}:1 on the ${role} rung at any step above the elevated rung`,
1208
+ })
1209
+ return from
1210
+ }
1211
+ if (from - step > RUNG_FIT_STEP / 2) {
1212
+ theme.colors[role] = rungAt(step)
1213
+ findings.push({
1214
+ themeId: source.id,
1215
+ role,
1216
+ kind: 'repaired',
1217
+ message: `step shrunk from ${from.toFixed(3)} to ${step.toFixed(3)} so body text holds ${CONTRAST_FLOORS.text}:1 on the rung${role === 'surfaceHover' ? ` and under a ${ACCENT_SUBTLE_MINIMUM_ALPHA}% primary tint` : ''}`,
1218
+ })
1219
+ }
1220
+ return step
1221
+ }
1222
+
1223
+ const hover = fit(
1224
+ 'surfaceHover',
1225
+ SURFACE_STEPS.surfaceHover,
1226
+ SURFACE_STEPS.surfaceElevated,
1227
+ (rung) =>
1228
+ primaryTintClears(theme, rung, 0) &&
1229
+ primaryTintClears(theme, rung, ACCENT_SUBTLE_MINIMUM_ALPHA)
1230
+ )
1231
+
1232
+ // Beyond the hover rung where any step there holds the floor; level with it, which
1233
+ // does hold, where none does.
1234
+ fit('surfaceActive', SURFACE_STEPS.surfaceActive, hover, (rung) =>
1235
+ primaryTintClears(theme, rung, 0)
1236
+ )
1237
+ }
1238
+
1213
1239
  /** Parses an authored value and re-serialises it in the catalogue's notation. */
1214
1240
  function authoredColor(themeId: string, role: string, value: string): string {
1215
1241
  const parsed = parseColor(value)
package/src/policy.ts ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * The catalogue's policy: the contrast floors, the repair budgets and the accent
3
+ * preference.
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.
9
+ */
10
+
11
+ import type { AnsiKey } from './schema.js'
12
+
13
+ /**
14
+ * The contrast floors.
15
+ *
16
+ * These are the catalogue's policy, and they are the reason an external palette
17
+ * can be admitted at all: nothing enters the catalogue on taste. `text` sits at
18
+ * the WCAG AA body-text floor; `border` is measured on the non-text scale, where
19
+ * the requirement is perceptibility rather than legibility.
20
+ */
21
+ export const CONTRAST_FLOORS = Object.freeze({
22
+ text: 4.5,
23
+ textMuted: 4.5,
24
+ /** Large or non-essential text, per WCAG 1.4.3. */
25
+ textSubtle: 3,
26
+ accent: 4.5,
27
+ /**
28
+ * The accent on a raised surface, where it is a fill rather than a label.
29
+ *
30
+ * The same two-tier reasoning as `statusRaised`: a button or a selected tab is a
31
+ * user-interface component, which WCAG holds to 3:1, and the pairing that
32
+ * actually carries text — the accent's own label — is measured separately at 4.5
33
+ * against the accent itself. Holding the accent to 4.5 on every surface instead
34
+ * made Everforest Light's every hue collapse into its body text, because that
35
+ * palette's colours are all around 2:1 on its own canvas.
36
+ */
37
+ accentRaised: 3,
38
+ accentForeground: 4.5,
39
+ /**
40
+ * Status colours drawn on the canvas, where they are read as small text.
41
+ */
42
+ status: 4.5,
43
+ /**
44
+ * Status colours drawn on a raised surface, where they are an indicator.
45
+ *
46
+ * The two-tier policy is deliberate and it is not a relaxation for convenience.
47
+ * A status role on a card is nearly always a dot, an icon or a chip's own fill —
48
+ * a graphical object, which WCAG holds to 3:1 — and it is that way because the
49
+ * alternative does not exist: Ayu Light and Everforest Light are muted palettes
50
+ * built for a near-white canvas and neither contains a yellow that is legible as
51
+ * small text on one. Forcing 4.5:1 there required blending the warning colour
52
+ * 94% of the way to the body text, which produces a warning that is the same
53
+ * colour as the text beside it and is therefore not a warning at all.
54
+ *
55
+ * A component that renders small status *text* on a raised surface should put it
56
+ * on the role's `-subtle` fill, which is a tint of the canvas and is measured at
57
+ * the body floor.
58
+ */
59
+ statusRaised: 3,
60
+ /** A divider is decorative; it must be seen, not read. */
61
+ border: 1.15,
62
+ borderMuted: 1.08,
63
+ /** A raised surface must be distinguishable from the canvas it sits on. */
64
+ surface: 1.03,
65
+ /** A selection must be visible under the text it selects. */
66
+ selection: 1.2,
67
+ } as const)
68
+
69
+ /** How far the normalizer may move a colour's lightness before it gives up. */
70
+ export const REPAIR_BUDGET = Object.freeze({
71
+ /** Body text: some palettes publish a foreground that fails on their own canvas. */
72
+ text: 0.2,
73
+ status: 0.24,
74
+ accent: 0.24,
75
+ })
76
+
77
+ /** The accent preference order, best first. */
78
+ export const ACCENT_PREFERENCE = [
79
+ 'blue',
80
+ 'magenta',
81
+ 'cyan',
82
+ 'green',
83
+ ] as const satisfies readonly AnsiKey[]
84
+
85
+ /** The smallest chroma a colour needs before it reads as "a colour" rather than grey. */
86
+ export const MINIMUM_ACCENT_CHROMA = 0.035
package/src/validate.ts CHANGED
@@ -15,10 +15,11 @@
15
15
  * than a legibility one.
16
16
  *
17
17
  * Not measured: a colour against itself, and the surface ladder against anything
18
- * but the canvas. A rung's job is to be distinguishable from the canvas, not to
19
- * carry text — components put text on `background`, and the ladder is decoration on
20
- * top of it. Measuring the ladder against every foreground would produce failures
21
- * no theme could pass and no component would care about.
18
+ * but the canvas and body text. A rung's job is to be distinguishable from the
19
+ * canvas, and measuring it against every foreground would produce failures no theme
20
+ * could pass and no component would care about. Body text is the exception: it is
21
+ * drawn on every rung, the hover and active rungs included, because a hovered or
22
+ * pressed row keeps its label.
22
23
  *
23
24
  * ## Why the validator is exported
24
25
  *
@@ -77,6 +78,10 @@ export const REQUIRED_PAIRINGS: readonly Pairing[] = Object.freeze([
77
78
  { role: 'textMuted', against: ['background'], minimum: CONTRAST_FLOORS.textMuted },
78
79
  { role: 'text', against: ['surface'], minimum: CONTRAST_FLOORS.text },
79
80
  { role: 'text', against: ['surfaceElevated'], minimum: CONTRAST_FLOORS.text },
81
+ // A hovered or pressed row keeps its label, so the interaction rungs carry the
82
+ // body floor too. The normalizer fits the rungs to it; see `fitInteractionRungs`.
83
+ { role: 'text', against: ['surfaceHover'], minimum: CONTRAST_FLOORS.text },
84
+ { role: 'text', against: ['surfaceActive'], minimum: CONTRAST_FLOORS.text },
80
85
  { role: 'textMuted', against: ['surface'], minimum: CONTRAST_FLOORS.textMuted },
81
86
  { role: 'textSubtle', against: ['background'], minimum: CONTRAST_FLOORS.textSubtle },
82
87
  { role: 'accent', against: ['background'], minimum: CONTRAST_FLOORS.accent },