@tenphi/glaze 2.0.2 → 3.0.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/AGENTS.md +69 -32
- package/README.md +45 -25
- package/dist/index.cjs +3091 -1318
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +564 -119
- package/dist/index.d.mts +564 -119
- package/dist/index.mjs +3087 -1319
- package/dist/index.mjs.map +1 -1
- package/docs/contrast.mdx +751 -0
- package/docs/exporting.mdx +635 -0
- package/docs/getting-started.mdx +487 -0
- package/docs/hdr.mdx +523 -0
- package/docs/methodology.mdx +976 -0
- package/docs/migration.md +476 -285
- package/docs/mixes.mdx +348 -0
- package/docs/okhst.mdx +692 -0
- package/docs/reference/color-definitions.md +552 -0
- package/docs/reference/color-tokens.md +421 -0
- package/docs/reference/configuration.md +527 -0
- package/docs/reference/contrast.md +277 -0
- package/docs/reference/index.md +225 -0
- package/docs/reference/output.md +714 -0
- package/docs/reference/palette.md +435 -0
- package/docs/reference/theme.md +332 -0
- package/docs/reference/utilities.md +646 -0
- package/docs/schemes.mdx +636 -0
- package/docs/shadows-and-glows.mdx +519 -0
- package/package.json +4 -7
- package/docs/api.md +0 -2021
- package/docs/methodology.md +0 -686
- package/docs/okhst.md +0 -207
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Contrast solving
|
|
3
|
+
description: Give a color a WCAG or APCA floor against its base and Glaze finds the tone that meets it in every scheme.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Callout } from '@tenphi/cookbook/components';
|
|
7
|
+
import ContrastExample from '../docs-site/components/contrast/ContrastExample.astro';
|
|
8
|
+
import TokenExample from '../docs-site/components/contrast/TokenExample.astro';
|
|
9
|
+
|
|
10
|
+
Tone deltas space colors evenly; a **contrast floor** guarantees that a pair is
|
|
11
|
+
readable. Give a color a `base` and a `contrast` spec, and Glaze measures the
|
|
12
|
+
pair in light, dark, and both high-contrast schemes, then moves the color's
|
|
13
|
+
tone only when the measurement falls short.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
theme.colors({
|
|
17
|
+
surface: { tone: 97 },
|
|
18
|
+
'surface-text': {
|
|
19
|
+
base: 'surface',
|
|
20
|
+
tone: '-60',
|
|
21
|
+
contrast: 'AA',
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The floor is a minimum, not a target: a color that already clears it, with a
|
|
27
|
+
small margin, keeps its authored tone. Use tone for the relationship you want
|
|
28
|
+
and `contrast` as the safety net under it. The tone can be a delta from the
|
|
29
|
+
base or an absolute tone: a `base` with an absolute `tone` is a valid way to
|
|
30
|
+
get a contrast guarantee without a tone relationship.
|
|
31
|
+
|
|
32
|
+
## Floors at a glance
|
|
33
|
+
|
|
34
|
+
Each text color below asks for a soft tone, `'-20'` from the surface, and lets
|
|
35
|
+
its floor decide how far to move. The numbers are measured from the resolved
|
|
36
|
+
colors in the scheme you are viewing; switch the site's appearance to see the
|
|
37
|
+
other three.
|
|
38
|
+
|
|
39
|
+
<ContrastExample
|
|
40
|
+
hue={280}
|
|
41
|
+
saturation={80}
|
|
42
|
+
colors={{
|
|
43
|
+
surface: { tone: 97, saturation: 0.1 },
|
|
44
|
+
'aa-text': { base: 'surface', tone: '-20', saturation: 0.4, contrast: 'AA' },
|
|
45
|
+
'aaa-text': { base: 'surface', tone: '-20', saturation: 0.4, contrast: 'AAA' },
|
|
46
|
+
'content-text': {
|
|
47
|
+
base: 'surface',
|
|
48
|
+
tone: '-20',
|
|
49
|
+
saturation: 0.4,
|
|
50
|
+
contrast: { apca: 'content' },
|
|
51
|
+
},
|
|
52
|
+
'body-text': {
|
|
53
|
+
base: 'surface',
|
|
54
|
+
tone: '-20',
|
|
55
|
+
saturation: 0.4,
|
|
56
|
+
contrast: { apca: 'body' },
|
|
57
|
+
},
|
|
58
|
+
'preferred-text': {
|
|
59
|
+
base: 'surface',
|
|
60
|
+
tone: '-20',
|
|
61
|
+
saturation: 0.4,
|
|
62
|
+
contrast: { apca: 'preferred' },
|
|
63
|
+
},
|
|
64
|
+
}}
|
|
65
|
+
rows={['aa-text', 'aaa-text', 'content-text', 'body-text', 'preferred-text']}
|
|
66
|
+
caption="Each row is measured against surface in the active scheme. The first number is the floor's own metric; tone is the color's resolved tone (0–100)."
|
|
67
|
+
/>
|
|
68
|
+
|
|
69
|
+
Compare the WCAG and APCA rows across light and dark: the two metrics disagree
|
|
70
|
+
most about light text on a dark background, which is why the choice of metric
|
|
71
|
+
matters.
|
|
72
|
+
|
|
73
|
+
## WCAG and APCA
|
|
74
|
+
|
|
75
|
+
Glaze solves two metrics. Pick one per color.
|
|
76
|
+
|
|
77
|
+
### WCAG 2 ratios
|
|
78
|
+
|
|
79
|
+
The WCAG 2 contrast ratio compares the relative luminance of two colors on a
|
|
80
|
+
scale from `1:1` to `21:1`. It is symmetric: swapping text and background gives
|
|
81
|
+
the same ratio. A bare number or a preset name means WCAG.
|
|
82
|
+
|
|
83
|
+
| Preset | Ratio | Use |
|
|
84
|
+
| ------------- | ----- | ---------------------------------------- |
|
|
85
|
+
| `'AA-large'` | 3 | Large or bold text (WCAG SC 1.4.3) |
|
|
86
|
+
| `'AA'` | 4.5 | Body text (WCAG SC 1.4.3) |
|
|
87
|
+
| `'AAA-large'` | 4.5 | Large or bold text, enhanced (SC 1.4.6) |
|
|
88
|
+
| `'AAA'` | 7 | Body text, enhanced (SC 1.4.6) |
|
|
89
|
+
|
|
90
|
+
### APCA lightness contrast
|
|
91
|
+
|
|
92
|
+
APCA (the Accessible Perceptual Contrast Algorithm) reports a lightness
|
|
93
|
+
contrast, Lc, from 0 to about 106. It models how text actually reads, so it is
|
|
94
|
+
**polarity-aware**: dark text on a light background and light text on a dark
|
|
95
|
+
background score differently for the same pair of colors. Glaze takes the
|
|
96
|
+
magnitude and needs to know which color is the text; see
|
|
97
|
+
[Roles and APCA polarity](#roles-and-apca-polarity).
|
|
98
|
+
|
|
99
|
+
| Preset | Lc | Use |
|
|
100
|
+
| ------------- | --- | ----------------------------------------------------- |
|
|
101
|
+
| `'preferred'` | 90 | Preferred body and column text |
|
|
102
|
+
| `'body'` | 75 | Minimum for body and column text |
|
|
103
|
+
| `'content'` | 60 | Readable non-body content (about WCAG AA) |
|
|
104
|
+
| `'large'` | 45 | Large or bold headlines, fine icons and outlines |
|
|
105
|
+
| `'non-text'` | 30 | Solid icons and controls, placeholder or disabled text |
|
|
106
|
+
| `'min'` | 15 | Dividers and decoration; APCA's point of invisibility |
|
|
107
|
+
|
|
108
|
+
The presets are APCA's Bronze Simple Mode conformance levels and do not depend
|
|
109
|
+
on the role. You can also pass a raw Lc number.
|
|
110
|
+
|
|
111
|
+
### Which one to use
|
|
112
|
+
|
|
113
|
+
- Prefer **APCA presets** for text, icons, and other content when perceptual
|
|
114
|
+
readability is the goal: `contrast: { apca: 'content' }`, or with an explicit
|
|
115
|
+
high-contrast step, `contrast: { apca: ['content', 'body'] }`.
|
|
116
|
+
- Use **WCAG** when compliance, policy, or a migration requires a WCAG ratio:
|
|
117
|
+
`contrast: 4.5`, `contrast: 'AAA'`, `contrast: { wcag: [4.5, 7] }`.
|
|
118
|
+
- Add a floor only where readability or recognizability needs a measured
|
|
119
|
+
guarantee. Low-stakes relationships — surface ladders, hover steps, subtle
|
|
120
|
+
fills — read better as tone deltas than as fake contrast floors.
|
|
121
|
+
|
|
122
|
+
## Writing a contrast spec
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
type ContrastPreset = 'AA' | 'AAA' | 'AA-large' | 'AAA-large';
|
|
126
|
+
type ApcaPreset =
|
|
127
|
+
| 'preferred'
|
|
128
|
+
| 'body'
|
|
129
|
+
| 'content'
|
|
130
|
+
| 'large'
|
|
131
|
+
| 'non-text'
|
|
132
|
+
| 'min';
|
|
133
|
+
type HCPair<T> = T | [T, T];
|
|
134
|
+
|
|
135
|
+
type ContrastSpec =
|
|
136
|
+
| number // WCAG ratio
|
|
137
|
+
| ContrastPreset // WCAG preset
|
|
138
|
+
| { wcag: HCPair<number | ContrastPreset> }
|
|
139
|
+
| { apca: HCPair<number | ApcaPreset> };
|
|
140
|
+
|
|
141
|
+
// On a regular or mix color:
|
|
142
|
+
// contrast?: HCPair<ContrastSpec>;
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`HCPair<T>` is either one value for every scheme or a
|
|
146
|
+
`[normal, highContrast]` pair. The pair can wrap the whole spec or sit inside
|
|
147
|
+
the metric:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
contrast: 4.5; // WCAG 4.5
|
|
151
|
+
contrast: 'AAA'; // WCAG 7
|
|
152
|
+
contrast: { wcag: 6 }; // WCAG 6
|
|
153
|
+
contrast: [4.5, 7]; // WCAG 4.5 normal / 7 high contrast
|
|
154
|
+
contrast: { wcag: [4.5, 7] }; // the same pair, inside the metric
|
|
155
|
+
contrast: { apca: 60 }; // Lc 60 normal / 75 high contrast (automatic)
|
|
156
|
+
contrast: { apca: 'content' }; // the same, by preset
|
|
157
|
+
contrast: { apca: [45, 60] }; // Lc 45 / 60 high contrast (explicit)
|
|
158
|
+
contrast: { apca: ['content', 'body'] }; // Lc 60 / 75 (explicit)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
A WCAG number below 1 is treated as 1, and a negative APCA number uses its
|
|
162
|
+
magnitude.
|
|
163
|
+
|
|
164
|
+
Two definitions are rejected before anything resolves:
|
|
165
|
+
|
|
166
|
+
- `contrast` without `base` on a theme color — there is nothing to measure
|
|
167
|
+
against:
|
|
168
|
+
`glaze: color "text" has "contrast" without "base".`
|
|
169
|
+
- A pair that switches metric, such as `[4.5, { apca: 75 }]`. A ratio and an Lc
|
|
170
|
+
are different scales, so Glaze cannot pick between them or interpolate them.
|
|
171
|
+
The error starts with
|
|
172
|
+
`glaze: color "text" has a "contrast" pair that switches metric (wcag → apca).`
|
|
173
|
+
and suggests the fix: use one metric for both entries, e.g.
|
|
174
|
+
`{ wcag: [normal, highContrast] }` (here `{ wcag: [4.5, 7] }`).
|
|
175
|
+
|
|
176
|
+
Every form, preset, and escalation rule is listed in the API reference under
|
|
177
|
+
[Contrast and high contrast](./reference/contrast.md).
|
|
178
|
+
|
|
179
|
+
## How the solver finds a tone
|
|
180
|
+
|
|
181
|
+
For each scheme, Glaze resolves the base first, then the dependent color:
|
|
182
|
+
|
|
183
|
+
1. **Preferred tone.** The color's authored tone is placed for this scheme and
|
|
184
|
+
clamped into the scheme's [tone window](./schemes.mdx#tone-windows). An
|
|
185
|
+
absolute tone is mapped through the scheme. A tone delta is applied to the
|
|
186
|
+
base's tone in that scheme, except in dark under the default
|
|
187
|
+
`mode: 'auto'`. There the delta steps from where the base sits on the
|
|
188
|
+
authored scale: its authored tone, or, for a `fixed` or `static` base, an
|
|
189
|
+
`auto` contrast-solved base, a mix, or an `auto` `'max'` / `'min'` on a
|
|
190
|
+
base, its dark tone read back through the dark window. The result is
|
|
191
|
+
inverted and remapped like an absolute tone. At the default dark window the
|
|
192
|
+
dark step is about 0.87× the delta, mirrored, on every base: a `'-4'` hover
|
|
193
|
+
on an AAA button steps +3.5 in dark. High contrast keeps the step exact.
|
|
194
|
+
[How relative tone adapts](./schemes.mdx#how-relative-tone-adapts) covers
|
|
195
|
+
fixed bases, and [Overshooting deltas](./schemes.mdx#overshooting-deltas)
|
|
196
|
+
the clamps near the ends of the range. With no `tone`, the color starts at
|
|
197
|
+
the base's own tone.
|
|
198
|
+
2. **Keep it if it passes.** The pair is measured with the chosen metric. If
|
|
199
|
+
it clears the floor with a small safety margin, the preferred tone is the
|
|
200
|
+
result. A preferred tone that clears the floor but not the margin is kept
|
|
201
|
+
too when no search can beat it: white text at tone 100 has nowhere further
|
|
202
|
+
to go.
|
|
203
|
+
3. **Search one side.** Otherwise the solver searches on the side the authored
|
|
204
|
+
tone points to: darker when the preferred tone is below the base, lighter
|
|
205
|
+
when it is above. A color with no tone of its own starts on whichever side
|
|
206
|
+
reaches higher contrast. A tie starts darker: black and white score the
|
|
207
|
+
same WCAG ratio against a gray at tone 50. A binary search along tone
|
|
208
|
+
measures the real, chromatic color, and the passing tone nearest the
|
|
209
|
+
preferred one wins. For WCAG the search first measures the gray tone that
|
|
210
|
+
gives exactly the ratio. Tone is contrast-uniform for neutrals, so that
|
|
211
|
+
tone is closed-form. It only narrows the search and never stops it, so a
|
|
212
|
+
saturated color lands on its own crossing, not the gray's.
|
|
213
|
+
4. **Cross over if needed.** With [`autoFlip`](#autoflip-crossing-to-the-other-side)
|
|
214
|
+
on (the default), the opposite side is searched too.
|
|
215
|
+
|
|
216
|
+
The solver aims slightly above the target — 1% over a WCAG ratio, 0.5 Lc over
|
|
217
|
+
an APCA target — so rounding in the output formats cannot push the final color
|
|
218
|
+
under the floor. Measured values therefore sit at or just above the floor:
|
|
219
|
+
`muted-text` below lands at 4.55:1 for AA in light and dark.
|
|
220
|
+
|
|
221
|
+
<ContrastExample
|
|
222
|
+
hue={280}
|
|
223
|
+
saturation={80}
|
|
224
|
+
colors={{
|
|
225
|
+
surface: { tone: 97, saturation: 0.1 },
|
|
226
|
+
'muted-text': {
|
|
227
|
+
base: 'surface',
|
|
228
|
+
tone: '-30',
|
|
229
|
+
saturation: 0.4,
|
|
230
|
+
contrast: 'AA',
|
|
231
|
+
},
|
|
232
|
+
'strong-text': {
|
|
233
|
+
base: 'surface',
|
|
234
|
+
tone: '-85',
|
|
235
|
+
saturation: 0.4,
|
|
236
|
+
contrast: 'AA',
|
|
237
|
+
},
|
|
238
|
+
}}
|
|
239
|
+
rows={['muted-text', 'strong-text']}
|
|
240
|
+
compare="unsolved"
|
|
241
|
+
caption="“Without the floor” is the same theme with contrast removed. muted-text is moved only as far as AA requires; strong-text already clears it and keeps its authored tone."
|
|
242
|
+
/>
|
|
243
|
+
|
|
244
|
+
The preferred tone is clamped into the scheme's tone window, but the search
|
|
245
|
+
covers the full `0–100` tone range. A demanding floor can therefore push a color
|
|
246
|
+
past the window edge — for example, dark-scheme text that needs a very high Lc.
|
|
247
|
+
|
|
248
|
+
<Callout kind="note">
|
|
249
|
+
Tone is exactly contrast-uniform only for neutrals. Because the solver measures
|
|
250
|
+
the actual chromatic color, a saturated color still meets its floor; it just
|
|
251
|
+
ends at a different tone than a gray would. See
|
|
252
|
+
[Tone and OKHST](./okhst.mdx) for the model behind this.
|
|
253
|
+
</Callout>
|
|
254
|
+
|
|
255
|
+
## autoFlip: crossing to the other side
|
|
256
|
+
|
|
257
|
+
Sometimes the authored side cannot reach the floor at all — a light label on a
|
|
258
|
+
mid-tone button, for instance. `autoFlip` decides what happens then:
|
|
259
|
+
|
|
260
|
+
- **On (default):** the solver also searches the opposite side of the base and
|
|
261
|
+
takes the passing tone nearest the preferred one. The light label becomes a
|
|
262
|
+
dark one. When both sides are equally near — a neutral color with no tone of
|
|
263
|
+
its own sits exactly between them — it keeps the side it searched first.
|
|
264
|
+
- **Off:** only the authored side is searched. When it falls short, the color
|
|
265
|
+
pins to that side's extreme (tone `0` or `100`) and Glaze warns.
|
|
266
|
+
|
|
267
|
+
<ContrastExample
|
|
268
|
+
hue={280}
|
|
269
|
+
saturation={80}
|
|
270
|
+
colors={{
|
|
271
|
+
button: { tone: 68, saturation: 0.6 },
|
|
272
|
+
'button-label': { base: 'button', tone: '+25', contrast: 'AA' },
|
|
273
|
+
'button-label-pinned': {
|
|
274
|
+
base: 'button',
|
|
275
|
+
tone: '+25',
|
|
276
|
+
contrast: 'AA',
|
|
277
|
+
autoFlip: false,
|
|
278
|
+
},
|
|
279
|
+
}}
|
|
280
|
+
rows={['button-label', 'button-label-pinned']}
|
|
281
|
+
scheme="light"
|
|
282
|
+
sample="Publish"
|
|
283
|
+
caption="Light scheme. Both labels are authored lighter than the button. With autoFlip the first crosses to the darker side to meet AA; the pinned one stays on the lighter side and misses it. In dark the scheme inverts the authored direction, so the two swap sides."
|
|
284
|
+
/>
|
|
285
|
+
|
|
286
|
+
The same switch governs **tone-delta overshoot**. When `base + delta` falls
|
|
287
|
+
outside `0–100`, `autoFlip` mirrors the delta to the other side of the base
|
|
288
|
+
(`'+30'` on a tone-90 surface becomes `'-30'`) instead of clamping at the
|
|
289
|
+
boundary. If the mirrored tone is also out of range, the original delta is
|
|
290
|
+
kept and clamped on the authored side.
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
theme.colors({
|
|
294
|
+
surface: { tone: 90 },
|
|
295
|
+
// '+30' overshoots 100: mirrored to '-30'
|
|
296
|
+
raised: { base: 'surface', tone: '+30' },
|
|
297
|
+
// clamped at 100 instead
|
|
298
|
+
'raised-pinned': {
|
|
299
|
+
base: 'surface',
|
|
300
|
+
tone: '+30',
|
|
301
|
+
autoFlip: false,
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Set it per color, per theme, or globally:
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
// one color
|
|
310
|
+
theme.colors({
|
|
311
|
+
'fill-label': { base: 'fill', contrast: 'AA', autoFlip: false },
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// one theme
|
|
315
|
+
const strict = glaze(280, 80, { autoFlip: false });
|
|
316
|
+
|
|
317
|
+
// every theme and token
|
|
318
|
+
glaze.configure({ autoFlip: false });
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
Use `autoFlip: false` when a color must stay on its side of the base — a
|
|
322
|
+
tone delta that should never cross over, or an unmet floor you would rather
|
|
323
|
+
see pinned to one extreme. A mix color has no per-color field; it follows the
|
|
324
|
+
theme's (or global) `autoFlip`.
|
|
325
|
+
|
|
326
|
+
`autoFlip` is unrelated to dark-mode inversion. A color swapping ends between
|
|
327
|
+
light and dark is its [adaptation mode](./schemes.mdx#adaptation-modes) at
|
|
328
|
+
work; `autoFlip` only reverses a delta or a contrast search around the base.
|
|
329
|
+
|
|
330
|
+
When both sides pass, "nearest wins" can pick a different side as the target
|
|
331
|
+
grows. Under a [manual contrast level](#manual-contrast-level), Glaze therefore
|
|
332
|
+
decides the preferred side once per half of the slider and switches it only at
|
|
333
|
+
level 50. A preferred side that stops reaching the floor partway along still
|
|
334
|
+
falls back to the other side, at whatever level that happens: in
|
|
335
|
+
`glaze(280, 80)`, a `'-5'` label with `contrast: 'AA-large'` on a tone-40
|
|
336
|
+
button crosses to the lighter side at level 23 in light.
|
|
337
|
+
|
|
338
|
+
## Roles and APCA polarity
|
|
339
|
+
|
|
340
|
+
APCA is asymmetric — the Lc of `a` on `b` differs from `b` on `a` — so Glaze
|
|
341
|
+
needs to know which of the pair is the foreground. A color's **role** decides
|
|
342
|
+
it. WCAG is symmetric and ignores roles.
|
|
343
|
+
|
|
344
|
+
| Role (polarity) | Name keywords |
|
|
345
|
+
| ------------------------ | ----------------------------------------------------------------- |
|
|
346
|
+
| `'text'` (foreground) | `text`, `fg`, `foreground`, `content`, `ink`, `label`, `stroke` |
|
|
347
|
+
| `'border'` (foreground) | `border`, `divider`, `outline`, `separator`, `hairline`, `rule` |
|
|
348
|
+
| `'surface'` (background) | `surface`, `bg`, `background`, `fill`, `canvas`, `paper`, `layer` |
|
|
349
|
+
|
|
350
|
+
A foreground color — text, icons, and other content, or a border, divider, or
|
|
351
|
+
outline — is measured as text over its base. A surface color — a background or
|
|
352
|
+
fill — is measured as the background, with its base as the text on top.
|
|
353
|
+
|
|
354
|
+
Below, two colors share one definition. The only difference is the name:
|
|
355
|
+
`accent-text` resolves as text and `accent-fill` as a surface, so the solver
|
|
356
|
+
measures them in opposite directions and lands on different tones.
|
|
357
|
+
|
|
358
|
+
<ContrastExample
|
|
359
|
+
hue={280}
|
|
360
|
+
saturation={80}
|
|
361
|
+
colors={{
|
|
362
|
+
surface: { tone: 97, saturation: 0.1 },
|
|
363
|
+
'accent-text': { base: 'surface', tone: '-30', contrast: { apca: 'body' } },
|
|
364
|
+
'accent-fill': { base: 'surface', tone: '-30', contrast: { apca: 'body' } },
|
|
365
|
+
}}
|
|
366
|
+
rows={['accent-text', 'accent-fill']}
|
|
367
|
+
scheme="light"
|
|
368
|
+
showRole
|
|
369
|
+
caption="Light scheme, where the difference is largest. accent-text is drawn as text on surface; accent-fill as a fill with surface on top. Lc is measured in the direction each role implies; roles swapped is the Lc the same pair would score used the other way round."
|
|
370
|
+
/>
|
|
371
|
+
|
|
372
|
+
### How a role is chosen
|
|
373
|
+
|
|
374
|
+
For each color, the first rule that applies wins:
|
|
375
|
+
|
|
376
|
+
1. An explicit `role` (a role name or any keyword above as an alias).
|
|
377
|
+
2. Inference from the color's name, when `inferRole` is on (the default). The
|
|
378
|
+
name is split on non-alphanumerics and camelCase humps, and the **last**
|
|
379
|
+
recognized keyword wins: `button-text` is text, `input-bg` is a surface,
|
|
380
|
+
`card-outline` is a border.
|
|
381
|
+
3. The opposite of the base's role: a color on a surface is text; a color on
|
|
382
|
+
text or a border is a surface.
|
|
383
|
+
4. `'text'` — the base is treated as the background.
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
const theme = glaze(280, 60);
|
|
387
|
+
|
|
388
|
+
theme.colors({
|
|
389
|
+
surface: { tone: 90 },
|
|
390
|
+
// role 'text', from the name
|
|
391
|
+
text: { base: 'surface', contrast: { apca: 'content' } },
|
|
392
|
+
// role 'border', from the name
|
|
393
|
+
border: { base: 'surface', tone: '-10' },
|
|
394
|
+
// a fill with no keyword in its name would resolve as text
|
|
395
|
+
// (rule 3), so set the role explicitly
|
|
396
|
+
badge: {
|
|
397
|
+
base: 'surface',
|
|
398
|
+
contrast: { apca: 'content' },
|
|
399
|
+
role: 'surface',
|
|
400
|
+
},
|
|
401
|
+
});
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
<Callout kind="tip" title="Name the pair">
|
|
405
|
+
A root color with no keyword in its name resolves as `'text'` (rule 4), so a
|
|
406
|
+
dependent of it that also has no keyword resolves as `'surface'` (rule 3). If
|
|
407
|
+
`brand` is a fill and `on-brand` is its label, name them `brand-fill` /
|
|
408
|
+
`brand-label`, or set `role` explicitly.
|
|
409
|
+
</Callout>
|
|
410
|
+
|
|
411
|
+
Turn name inference off with `glaze.configure({ inferRole: false })` or per
|
|
412
|
+
theme with `glaze(hue, saturation, { inferRole: false })`; rules 1, 3, and 4
|
|
413
|
+
still apply. Shadows and glows always resolve as surfaces. Mix colors take the
|
|
414
|
+
same chain and the same `role` field. Good token names do double duty: they
|
|
415
|
+
describe the palette and give APCA the right polarity without any `role`
|
|
416
|
+
fields. Standalone `glaze.color()` tokens are the exception: their `name` is a
|
|
417
|
+
label only, and they default to text
|
|
418
|
+
([Pairing standalone colors](#pairing-standalone-colors)).
|
|
419
|
+
|
|
420
|
+
## High contrast
|
|
421
|
+
|
|
422
|
+
High-contrast variants (`lightContrast`, `darkContrast`) escalate the floor
|
|
423
|
+
automatically, so most colors need no high-contrast entry at all:
|
|
424
|
+
|
|
425
|
+
| Authored floor | Normal | High contrast |
|
|
426
|
+
| ---------------------------------- | ------ | -------------------------------- |
|
|
427
|
+
| `'AA'` | 4.5 | 7 (`'AAA'`) |
|
|
428
|
+
| `'AA-large'` | 3 | 4.5 (`'AAA-large'`) |
|
|
429
|
+
| `'AAA'`, `'AAA-large'` | as is | unchanged — already the top tier |
|
|
430
|
+
| a bare WCAG number | as is | unchanged — no successor tier |
|
|
431
|
+
| any APCA target | Lc | Lc + 15, capped at 106 |
|
|
432
|
+
| an explicit pair, outer or inner | first | second — no escalation |
|
|
433
|
+
|
|
434
|
+
The WCAG step follows the spec's own Enhanced level (SC 1.4.3 → SC 1.4.6). The
|
|
435
|
+
APCA step is the Lc analog of AAA over AA. An explicit high-contrast value, in
|
|
436
|
+
either the outer `contrast` pair or the inner `wcag` / `apca` pair, replaces the
|
|
437
|
+
automatic step. Use one to hold a ceiling: for large or bold text, where APCA
|
|
438
|
+
caps useful contrast at Lc 90 to avoid glare, `{ apca: ['preferred', 'preferred'] }`
|
|
439
|
+
keeps high contrast at Lc 90 instead of boosting it past the cap.
|
|
440
|
+
|
|
441
|
+
The automatic step can also outrun what a surface allows. An escalated
|
|
442
|
+
`'preferred'` becomes Lc 105, which dark text reaches only on a surface within
|
|
443
|
+
half a tone of white. On an off-white or tinted surface it pins to black and
|
|
444
|
+
warns in `lightContrast` — the `preferred-text` row in
|
|
445
|
+
[Floors at a glance](#floors-at-a-glance) does this. Give it an explicit,
|
|
446
|
+
reachable high-contrast entry there, such as `{ apca: [90, 95] }`.
|
|
447
|
+
|
|
448
|
+
High-contrast variants also ignore the [tone windows](./schemes.mdx#tone-windows): both the
|
|
449
|
+
placement and the search use the full `0–100` range, which gives the escalated
|
|
450
|
+
floors room to land.
|
|
451
|
+
|
|
452
|
+
<ContrastExample
|
|
453
|
+
hue={280}
|
|
454
|
+
saturation={80}
|
|
455
|
+
colors={{
|
|
456
|
+
surface: { tone: 97, saturation: 0.1 },
|
|
457
|
+
'aa-text': { base: 'surface', tone: '-20', saturation: 0.4, contrast: 'AA' },
|
|
458
|
+
'large-text': {
|
|
459
|
+
base: 'surface',
|
|
460
|
+
tone: '-20',
|
|
461
|
+
saturation: 0.4,
|
|
462
|
+
contrast: 'AA-large',
|
|
463
|
+
},
|
|
464
|
+
'aaa-text': { base: 'surface', tone: '-20', saturation: 0.4, contrast: 'AAA' },
|
|
465
|
+
'ratio-text': { base: 'surface', tone: '-20', saturation: 0.4, contrast: 4.5 },
|
|
466
|
+
'content-text': {
|
|
467
|
+
base: 'surface',
|
|
468
|
+
tone: '-20',
|
|
469
|
+
saturation: 0.4,
|
|
470
|
+
contrast: { apca: 'content' },
|
|
471
|
+
},
|
|
472
|
+
'explicit-text': {
|
|
473
|
+
base: 'surface',
|
|
474
|
+
tone: '-20',
|
|
475
|
+
saturation: 0.4,
|
|
476
|
+
contrast: { apca: ['content', 'content'] },
|
|
477
|
+
},
|
|
478
|
+
}}
|
|
479
|
+
rows={[
|
|
480
|
+
'aa-text',
|
|
481
|
+
'large-text',
|
|
482
|
+
'aaa-text',
|
|
483
|
+
'ratio-text',
|
|
484
|
+
'content-text',
|
|
485
|
+
'explicit-text',
|
|
486
|
+
]}
|
|
487
|
+
compare="hc"
|
|
488
|
+
caption="Normal and high-contrast variants of each color, in your current light or dark setting. The floor shown is the one the solver used for that variant."
|
|
489
|
+
/>
|
|
490
|
+
|
|
491
|
+
Any other value that takes an `HCPair` — `tone`, shadow and glow `intensity`,
|
|
492
|
+
mix `value` — can tighten the high-contrast variant the same way; see
|
|
493
|
+
[High-contrast pairs](./schemes.mdx#high-contrast-pairs).
|
|
494
|
+
|
|
495
|
+
### Manual contrast level
|
|
496
|
+
|
|
497
|
+
With [`contrastLevel`](./reference/contrast.md#manual-contrast-level) set to a number, the normal
|
|
498
|
+
variants solve for a target between the normal and high-contrast floors. Both
|
|
499
|
+
ends are resolved with the rules above first, so `'AA'` ramps from 4.5 at
|
|
500
|
+
level `0` to 7 at level `100`, and an explicit pair ramps between its two
|
|
501
|
+
entries. A pair that switches metric has no midpoint, which is one reason
|
|
502
|
+
validation rejects it. Under a level, the side of the base a color prefers is
|
|
503
|
+
decided once per half of the ramp and switches only at level 50, so
|
|
504
|
+
nearest-wins can't swap it back and forth. A preferred side that can no longer
|
|
505
|
+
reach the floor still falls back to the other side, at any level. The
|
|
506
|
+
high-contrast variants are not affected by the level. The concept is covered in
|
|
507
|
+
[Light, dark, and high contrast](./schemes.mdx#manual-contrast-level); the full
|
|
508
|
+
rules are in
|
|
509
|
+
[Contrast and high contrast](./reference/contrast.md#manual-contrast-level).
|
|
510
|
+
|
|
511
|
+
## When a floor can't be met
|
|
512
|
+
|
|
513
|
+
Some floors are physically out of reach. A mid-gray badge has no tone of text
|
|
514
|
+
that reaches 7:1 — not black, not white. When neither side passes, the color
|
|
515
|
+
pins to the extreme of the side it searched first, and Glaze warns.
|
|
516
|
+
|
|
517
|
+
<ContrastExample
|
|
518
|
+
hue={0}
|
|
519
|
+
saturation={0}
|
|
520
|
+
colors={{
|
|
521
|
+
badge: { tone: 50 },
|
|
522
|
+
'badge-text': { base: 'badge', contrast: 'AAA' },
|
|
523
|
+
'badge-text-light': { base: 'badge', tone: '+10', contrast: 'AAA' },
|
|
524
|
+
}}
|
|
525
|
+
rows={['badge-text', 'badge-text-light']}
|
|
526
|
+
scheme="light"
|
|
527
|
+
caption="Light scheme. Neither color can reach 7:1 on a mid-gray badge. badge-text has no tone of its own, so it starts on the stronger side; badge-text-light keeps its authored direction and pins to that side's extreme. In dark the inverted '+10' points darker, so both pin to black."
|
|
528
|
+
/>
|
|
529
|
+
|
|
530
|
+
The side matters: the solver pins to the side it searched first, even when the
|
|
531
|
+
other extreme would get closer. A color with no tone of its own starts on the
|
|
532
|
+
stronger side; a color with an authored direction keeps that direction.
|
|
533
|
+
|
|
534
|
+
The warning names the color, the scheme, the target, and what was reached:
|
|
535
|
+
|
|
536
|
+
```text
|
|
537
|
+
glaze: color "badge-text" cannot meet WCAG 7.00 in light scheme
|
|
538
|
+
(got 4.94).
|
|
539
|
+
Try widening the tone window, lowering the contrast target, or picking a
|
|
540
|
+
base color further from this color's tone.
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
It fires when the result is more than 2% under a WCAG target or more than
|
|
544
|
+
1.5 Lc under an APCA target. Each distinct warning prints once per session (the
|
|
545
|
+
dedupe cache holds 256 entries and starts over when full), so a dev server that
|
|
546
|
+
re-resolves a theme on every change does not flood the console. For a
|
|
547
|
+
standalone `glaze.color()`, pass `name` so the warning says which token it is.
|
|
548
|
+
|
|
549
|
+
To fix one:
|
|
550
|
+
|
|
551
|
+
- **Lower the target**, or give the high-contrast variant an explicit, reachable
|
|
552
|
+
entry: `contrast: ['AA', 'AA']` instead of letting AA escalate to AAA.
|
|
553
|
+
- **Move the base** further from the middle of the tone range.
|
|
554
|
+
- **Widen the tone window** when the normal scheme is clamping the base.
|
|
555
|
+
High-contrast variants already use the full range.
|
|
556
|
+
|
|
557
|
+
A mix with a floor it can't reach warns too, with its own message; see
|
|
558
|
+
[Contrast on mixes](#contrast-on-mixes).
|
|
559
|
+
|
|
560
|
+
### Chromatic drift advisory
|
|
561
|
+
|
|
562
|
+
After every scheme is resolved, Glaze re-measures each regular color that has a
|
|
563
|
+
floor against its base in that metric and emits an advisory when the measured value
|
|
564
|
+
is under the target by the same margin:
|
|
565
|
+
|
|
566
|
+
```text
|
|
567
|
+
glaze: color "link" drifts below WCAG 4.50 in light scheme
|
|
568
|
+
(measured <ratio>).
|
|
569
|
+
Chromatic luminance differs from the gray tone; nudge the tone or
|
|
570
|
+
saturation if the floor matters.
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
It is a safety net for the gap between tone and a chromatic color's real
|
|
574
|
+
luminance. It never repeats a "cannot meet" warning: a scheme where the solver
|
|
575
|
+
couldn't reach the floor is skipped, and so is an achromatic color (saturation
|
|
576
|
+
0), whose tone is exact. Because the solver itself measures the chromatic color,
|
|
577
|
+
a floor it meets passes this check too, so you should not normally see this
|
|
578
|
+
advisory at all. The model behind it is in
|
|
579
|
+
[Tone and OKHST](./okhst.mdx#the-drift-check).
|
|
580
|
+
|
|
581
|
+
### Contrast and opacity
|
|
582
|
+
|
|
583
|
+
A color with both `contrast` and `opacity` resolves, but Glaze warns:
|
|
584
|
+
|
|
585
|
+
```text
|
|
586
|
+
glaze: color "text" has both "contrast" and "opacity".
|
|
587
|
+
Opacity makes perceived tone unpredictable.
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
The floor is solved for the opaque color; what the reader sees depends on what
|
|
591
|
+
shows through. For a translucent color with a floor, use a
|
|
592
|
+
[transparent mix](./mixes.mdx#transparent-mix), whose solver models the
|
|
593
|
+
browser's compositing.
|
|
594
|
+
|
|
595
|
+
## Literal colors with a floor
|
|
596
|
+
|
|
597
|
+
A [`from`](./reference/color-definitions.md#from) color reproduces an exact value in
|
|
598
|
+
the light, normal-contrast scheme. A floor on it is still a floor: a value that
|
|
599
|
+
already clears it is emitted untouched, and one that misses moves only as far as
|
|
600
|
+
the floor requires. Dark and high contrast adapt as usual, and the floor is
|
|
601
|
+
solved in each of them.
|
|
602
|
+
|
|
603
|
+
<ContrastExample
|
|
604
|
+
hue={280}
|
|
605
|
+
saturation={80}
|
|
606
|
+
colors={{
|
|
607
|
+
surface: { tone: 100, saturation: 0.12 },
|
|
608
|
+
brand: { from: '#2f5bff', base: 'surface', contrast: 3 },
|
|
609
|
+
sky: { from: '#7cc8ff', base: 'surface', contrast: 3 },
|
|
610
|
+
}}
|
|
611
|
+
rows={['brand', 'sky']}
|
|
612
|
+
compare="unsolved"
|
|
613
|
+
sample={{ brand: 'Brand', sky: 'Sky' }}
|
|
614
|
+
caption="In light, brand already clears 3:1 on white and keeps its exact value; sky does not, so it darkens just enough."
|
|
615
|
+
/>
|
|
616
|
+
|
|
617
|
+
A `from` color works as a base like any other color. Here the label is solved
|
|
618
|
+
against whatever the brand resolves to in each scheme:
|
|
619
|
+
|
|
620
|
+
```ts
|
|
621
|
+
theme.colors({
|
|
622
|
+
surface: { tone: 100, saturation: 0.12 },
|
|
623
|
+
brand: { from: '#2f5bff', base: 'surface', contrast: 3 },
|
|
624
|
+
'brand-label': {
|
|
625
|
+
base: 'brand',
|
|
626
|
+
tone: 100,
|
|
627
|
+
contrast: 'AA-large',
|
|
628
|
+
},
|
|
629
|
+
});
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
## Pairing standalone colors
|
|
633
|
+
|
|
634
|
+
[`glaze.color()`](./reference/color-tokens.md) tokens use the same solver. Give
|
|
635
|
+
a token a `base` — another token, or a raw color value that Glaze wraps with
|
|
636
|
+
`glaze.color(value)` — and its `contrast` is solved against the base's resolved
|
|
637
|
+
variant in every scheme.
|
|
638
|
+
|
|
639
|
+
<TokenExample
|
|
640
|
+
base="#1a1a2e"
|
|
641
|
+
color={{ from: '#ffffff', contrast: 'AA' }}
|
|
642
|
+
caption="White text paired with a dark navy token, measured in the active scheme. In dark, the navy base inverts and the text follows it."
|
|
643
|
+
/>
|
|
644
|
+
|
|
645
|
+
With a `base`:
|
|
646
|
+
|
|
647
|
+
- `contrast` is solved per scheme against the base's `light`, `dark`,
|
|
648
|
+
`lightContrast`, and `darkContrast` variants: what the base token paints,
|
|
649
|
+
under its own config.
|
|
650
|
+
- A tone delta (`tone: '+N'` / `'-N'`) is measured from the base, exactly in
|
|
651
|
+
light and high contrast. Under the default `mode: 'auto'`, the dark variant
|
|
652
|
+
steps from where the base token sits on its own authored scale, read under
|
|
653
|
+
its own `lightTone`; for a `fixed` or `static` base, or an `auto` base
|
|
654
|
+
placed by a contrast floor or an extreme, that is its own dark tone read
|
|
655
|
+
back through the dark window, as in a theme. The step flips direction and
|
|
656
|
+
comes out about 0.87× the delta at the default dark window, the same as a
|
|
657
|
+
theme color
|
|
658
|
+
([How relative tone adapts](./schemes.mdx#how-relative-tone-adapts)). A
|
|
659
|
+
base whose dark window differs from the token's (its own `darkTone`, or
|
|
660
|
+
another `contrastLevel`) is read back through the token's dark window, so a
|
|
661
|
+
`'+0'` lands on it in dark. A relative `hue` still anchors to the token's
|
|
662
|
+
own seed value.
|
|
663
|
+
- Pairs chain: a paired token can be the base of the next one.
|
|
664
|
+
|
|
665
|
+
```ts
|
|
666
|
+
const page = glaze.color('#ffffff');
|
|
667
|
+
const button = glaze.color({
|
|
668
|
+
from: '#1e40af',
|
|
669
|
+
base: page,
|
|
670
|
+
contrast: 3,
|
|
671
|
+
name: 'button-fill',
|
|
672
|
+
});
|
|
673
|
+
const label = glaze.color({
|
|
674
|
+
from: '#ffffff',
|
|
675
|
+
base: button,
|
|
676
|
+
contrast: 'AA',
|
|
677
|
+
name: 'button-label',
|
|
678
|
+
});
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
In light all three keep their exact values, since each already clears its floor.
|
|
682
|
+
In dark they invert, and the label is solved against the inverted button.
|
|
683
|
+
|
|
684
|
+
Without a `base`, a token's `contrast` (and a tone delta) is measured
|
|
685
|
+
against the token's own literal value, held fixed in every scheme.
|
|
686
|
+
|
|
687
|
+
Either way the token is the foreground: APCA measures it as text on its anchor,
|
|
688
|
+
the base or its own literal value. Set `role: 'surface'` for a token that is a
|
|
689
|
+
fill with its anchor drawn on top. A token's `name` never changes the role —
|
|
690
|
+
`button-fill` above is a label for warnings, and its WCAG floor ignores roles
|
|
691
|
+
anyway. Base resolution timing, structured-base linking, and the `name` label
|
|
692
|
+
are covered in [Color tokens](./reference/color-tokens.md#pairing-colors) and
|
|
693
|
+
[Roles on tokens](./reference/color-tokens.md#roles-on-tokens).
|
|
694
|
+
|
|
695
|
+
## Contrast on mixes
|
|
696
|
+
|
|
697
|
+
Mix colors take the same `contrast` field. Instead of a tone, the solver adjusts
|
|
698
|
+
the mix ratio of an opaque mix or the opacity of a transparent one, against the
|
|
699
|
+
mix's `base`:
|
|
700
|
+
|
|
701
|
+
```ts
|
|
702
|
+
theme.colors({
|
|
703
|
+
tint: {
|
|
704
|
+
type: 'mix',
|
|
705
|
+
base: 'surface',
|
|
706
|
+
target: 'accent',
|
|
707
|
+
value: 10,
|
|
708
|
+
contrast: 'AA',
|
|
709
|
+
},
|
|
710
|
+
overlay: {
|
|
711
|
+
type: 'mix',
|
|
712
|
+
base: 'surface',
|
|
713
|
+
target: 'accent',
|
|
714
|
+
value: 5,
|
|
715
|
+
blend: 'transparent',
|
|
716
|
+
contrast: 3,
|
|
717
|
+
},
|
|
718
|
+
});
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
Both `value` and `contrast` accept `[normal, highContrast]` pairs. When even the
|
|
722
|
+
pure target can't meet the floor, the mix settles at the pure target. A `value`
|
|
723
|
+
of 100 is the pure target already. It is kept whenever it clears the floor,
|
|
724
|
+
even inside the solver's safety margin. When it misses, there is no room
|
|
725
|
+
above, so the mix falls back to the pure base, and a transparent mix ends at
|
|
726
|
+
alpha 0. That includes a high-contrast entry of 100, as in `value: [80, 100]`.
|
|
727
|
+
Either way Glaze warns, with the same margins and dedupe as a regular color.
|
|
728
|
+
Here `accent-wash`, a 10% mix toward a tone-55 `accent` on a tone-97 `surface`
|
|
729
|
+
in `glaze(280, 80)`, can't reach AA:
|
|
730
|
+
|
|
731
|
+
```text
|
|
732
|
+
glaze: mix "accent-wash" cannot meet WCAG 4.50 against its base in
|
|
733
|
+
light scheme (got 3.54).
|
|
734
|
+
Even the pure target falls short; try lowering the contrast target or
|
|
735
|
+
mixing toward a color further from the base.
|
|
736
|
+
```
|
|
737
|
+
|
|
738
|
+
A tone window has no say in a mix, so the fix is a target further from the base
|
|
739
|
+
or a lower floor. The drift advisory skips mixes. See
|
|
740
|
+
[Contrast on mixes](./mixes.mdx#contrast-on-mixes).
|
|
741
|
+
|
|
742
|
+
## Using the solver directly
|
|
743
|
+
|
|
744
|
+
The solver is exported for tools that work outside a theme:
|
|
745
|
+
`findToneForContrast`, `findValueForMixContrast`, `resolveContrastForMode`,
|
|
746
|
+
`resolveContrastForLevel`, `resolveMinContrast`, `resolveApcaTarget`,
|
|
747
|
+
`apcaContrast`, and the constants `APCA_PRESETS`, `APCA_HC_ENHANCEMENT` (15),
|
|
748
|
+
and `APCA_MAX_LC` (106). The role helpers — `normalizeRole`,
|
|
749
|
+
`inferRoleFromName`, `roleToPolarity`, `oppositeRole` — are exported too. Their
|
|
750
|
+
signatures and options are in the API reference under
|
|
751
|
+
[Utilities](./reference/utilities.md#contrast-solver).
|