@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/docs/okhst.mdx ADDED
@@ -0,0 +1,692 @@
1
+ ---
2
+ title: Tone and OKHST
3
+ description: The contrast-uniform tone axis Glaze authors every color on, its math and guarantees, and how a tone becomes a rendered color in each scheme.
4
+ ---
5
+
6
+ import { Preview } from '@tenphi/cookbook/components';
7
+ import Example from '../docs-site/components/Example.astro';
8
+ import ToneRamp from '../docs-site/components/ToneRamp.tsx';
9
+ import ToneDrift from '../docs-site/components/okhst/ToneDrift.tsx';
10
+ import SchemeRamp from '../docs-site/components/okhst/SchemeRamp.tsx';
11
+ import ToneLadder from '../docs-site/components/okhst/ToneLadder.tsx';
12
+ import FloorCompare from '../docs-site/components/okhst/FloorCompare.tsx';
13
+ import { rampExplorer } from '../docs-site/components/okhst/ramp-explorer.ts';
14
+
15
+ export const ramp = rampExplorer();
16
+
17
+ Glaze places every color in [OKHST](https://github.com/tenphi/okhst): OKHSL's
18
+ hue and saturation, with the lightness axis replaced by **tone** — a 0–100
19
+ axis shaped so that equal tone steps give equal WCAG contrast. This page is the
20
+ reference for the color model as Glaze applies it: the transfer math, what tone
21
+ does and does not guarantee, and how an authored tone becomes a rendered color
22
+ in each scheme. The [OKHST repository](https://github.com/tenphi/okhst) is the
23
+ canonical specification of the color space itself: transfer functions,
24
+ derivation, and invariants.
25
+
26
+ To build a palette on this model, continue to
27
+ [Designing a palette](./methodology.mdx). Every option and utility named here is
28
+ listed in the [API reference](./reference/index.md).
29
+
30
+ ## OKHSL with a tone axis
31
+
32
+ OKHST keeps OKHSL's hue (`h`, 0–360) and saturation (`s`) verbatim and swaps
33
+ the third coordinate:
34
+
35
+ | Space | Coordinates | Third coordinate |
36
+ | ----- | ----------- | ----------------------------------------------------- |
37
+ | OKHSL | `h, s, l` | `l`: perceptual lightness (toe-adjusted OKLab `L`) |
38
+ | OKHST | `h, s, t` | `t`: tone, a normalized logarithm of luminance |
39
+
40
+ You author tone on a 0–100 scale (`tone: 97`). Resolved variants and the
41
+ `{ h, s, t }` input object carry it on 0–1, like saturation (hue stays 0–360).
42
+
43
+ OKHSL lightness is perceptually even, but equal lightness steps produce uneven
44
+ contrast: the ratio between neighbors changes along the axis. Tone is shaped so
45
+ equal steps produce equal WCAG contrast. Two things follow: a ramp authored in
46
+ tone is a contrast-even ladder for free, and dark-mode inversion is a single
47
+ subtraction (`100 - t`) instead of a fitted curve.
48
+
49
+ ### Equal tone steps, equal contrast
50
+
51
+ <ToneRamp
52
+ saturation={0}
53
+ caption="Eleven neutral steps, evenly spaced in tone. The row underneath is the WCAG ratio between neighbors: 1.36:1 at every step."
54
+ />
55
+
56
+ <ToneRamp
57
+ saturation={0}
58
+ axis="lightness"
59
+ caption="The same eleven steps evenly spaced in OKHSL lightness. They look even, but the ratio between neighbors runs from 1.16:1 at the dark end up to 1.46:1 and back down to 1.29:1."
60
+ />
61
+
62
+ ### Ramp explorer
63
+
64
+ Build a ramp for any hue and saturation, spaced in tone or in OKHSL lightness.
65
+ A gray tone ramp keeps the same ratio between every pair of neighbors. A
66
+ saturated one stays close: hue 280 at saturation 80 runs from 1.32:1 to 1.38:1
67
+ in tone, against 1.14:1 to 1.46:1 in lightness (see
68
+ [chromatic drift](#uniform-in-lightness-not-luminance)). Open **Values** to copy
69
+ each step as hex and `oklch()`.
70
+
71
+ <Preview title="Try it: a ramp in tone or in lightness" {...ramp} />
72
+
73
+ ## The tone transfer
74
+
75
+ For a gray (`s = 0`) at OKHSL lightness `l`, luminance has a closed form through
76
+ the OKHSL toe and the OKLab cube:
77
+
78
+ ```text
79
+ Y = toeInv(l)^3 // OKLab L = toeInv(l), cubed
80
+ l = toe(cbrt(Y)) // exact inverse
81
+ ```
82
+
83
+ Tone is a normalized natural logarithm of `Y`, offset by a small `eps`:
84
+
85
+ ```text
86
+ D = ln(1 + eps) - ln(eps)
87
+
88
+ toneFromY(Y, eps) = 100 * (ln(Y + eps) - ln(eps)) / D
89
+ yFromTone(T, eps) = exp(T / 100 * D + ln(eps)) - eps
90
+ ```
91
+
92
+ `toTone(l)` and `fromTone(t)` compose these with the gray luminance above, so
93
+ they convert between OKHSL lightness (0–1) and tone (0–100) directly. The two
94
+ directions are exact analytic inverses: a round trip is lossless to
95
+ floating-point precision. `toneFromY(0)` is 0 and `toneFromY(1)` is 100 for any
96
+ `eps`, so tone is always a clean 0–100 scale.
97
+
98
+ ### Why `eps` is 0.05
99
+
100
+ WCAG 2 contrast is `(Y_hi + 0.05) / (Y_lo + 0.05)`. With `eps = 0.05`, tone is a
101
+ normalized `ln(Y + 0.05)`, so a fixed tone difference is a fixed ratio of
102
+ `Y + 0.05` — a fixed WCAG contrast — wherever it sits on the scale. Since
103
+ `ln(1.05) - ln(0.05) = ln(21)`:
104
+
105
+ ```text
106
+ ratio(T2, T1) = (Y2 + 0.05) / (Y1 + 0.05)
107
+ = exp((T2 - T1) / 100 * ln(21))
108
+ = 21 ^ ((T2 - T1) / 100)
109
+ ```
110
+
111
+ The whole scale is 21:1, black to white, and every 10 tones multiply contrast by
112
+ `21 ^ 0.1`, about 1.36. For neutrals:
113
+
114
+ | Tone | Contrast vs. black |
115
+ | ---: | -----------------: |
116
+ | 10 | 1.36:1 |
117
+ | 20 | 1.84:1 |
118
+ | 30 | 2.49:1 |
119
+ | 40 | 3.38:1 |
120
+ | 50 | 4.58:1 |
121
+ | 60 | 6.21:1 |
122
+ | 70 | 8.42:1 |
123
+ | 80 | 11.42:1 |
124
+ | 90 | 15.49:1 |
125
+ | 100 | 21:1 |
126
+
127
+ Inverted, the tone distance between two neutrals for a target ratio `R` is
128
+ `100 * ln(R) / ln(21)`:
129
+
130
+ | Ratio | Tone distance |
131
+ | ---------------------- | ------------: |
132
+ | 3:1 (`'AA-large'`) | 36.08 |
133
+ | 4.5:1 (`'AA'`) | 49.40 |
134
+ | 7:1 (`'AAA'`) | 63.92 |
135
+
136
+ These distances are exact for neutrals only;
137
+ [Same tone, same lightness](#same-tone-same-lightness) explains why.
138
+
139
+ ### Tone above 100
140
+
141
+ The transfer has no ceiling: `toneFromY` keeps counting past SDR white
142
+ (`Y = 1`), so tone runs on past 100.
143
+
144
+ | Luminance `Y` | Stops above white | Tone |
145
+ | ------------: | ----------------: | -----: |
146
+ | 1 | 0 | 100 |
147
+ | 2 | 1 | 121.98 |
148
+ | 4 | 2 | 144.34 |
149
+
150
+ Each stop, a doubling of `Y`, adds about 22 tones. The scale is still a
151
+ logarithm of `Y + 0.05`, so equal tone steps past 100 are still equal ratios of
152
+ `Y + 0.05`: 12 tones past white (`Y` 1.4631, tone 112) is the same ratio as a
153
+ 12-tone step anywhere else. Contrast does not carry over. WCAG and APCA are
154
+ defined only up to SDR white, so a tone past 100 has no contrast meaning, and
155
+ the contrast solver never searches there.
156
+
157
+ Only [HDR glows](./hdr.mdx) go past 100, in an HDR value kept next to the SDR
158
+ one; a resolved variant's own `t` stays within 0–1.
159
+
160
+ ## Same tone, same lightness
161
+
162
+ `okhstToOkhsl({ h, s, t })` passes `h` and `s` through unchanged and sets
163
+ `l = fromTone(100 * t)`. `fromTone` depends on the tone (and `eps`) alone, and OKHSL's
164
+ `l → L` map has no hue or saturation term: `h` and `s` enter only the chroma
165
+ math. That gives OKHST its core invariant:
166
+
167
+ > A given tone yields the same OKHSL lightness for every hue and saturation.
168
+
169
+ OKHST inherits OKHSL's gamut and reversibility exactly: every `(h, s, t)` is
170
+ realizable and round-trips.
171
+
172
+ ### Uniform in lightness, not luminance
173
+
174
+ The invariant is about lightness. Equal tone gives equal WCAG and APCA contrast
175
+ only for grays: a saturated red and a saturated green at the same tone share an
176
+ OKHSL lightness but differ in real luminance `Y`, because hue, saturation, and
177
+ gamut mapping all change the final sRGB values. This **chromatic drift** is the
178
+ one approximation in the model.
179
+
180
+ <ToneDrift
181
+ rows={[
182
+ { hue: 0, saturation: 0, label: 'Neutral' },
183
+ { hue: 25, saturation: 100 },
184
+ { hue: 25, saturation: 50 },
185
+ { hue: 110, saturation: 100 },
186
+ { hue: 150, saturation: 100 },
187
+ { hue: 300, saturation: 100 },
188
+ ]}
189
+ caption="Each column is drawn at the authored tone above it. The label in each cell is its measured tone: the tone of the gray with the same luminance. Neutrals match exactly; chromatic colors drift, and less so at lower saturation."
190
+ />
191
+
192
+ At full saturation, the rows above read up to 5.6 tones darker than their tone
193
+ (hue 300, purple) and 4.1 darker (hue 25, red), up to 2.7 tones lighter
194
+ (hue 150, green), and within a tone for yellow (hue 110). Across all hues the
195
+ extremes are 5.6 tones darker, near hue 300, and 2.9 lighter, near hue 143.
196
+ Drift shrinks with saturation: hue 25 at tone 50 reads 3.9 tones dark at full
197
+ saturation and 1.6 at half.
198
+
199
+ That splits the work between two tools:
200
+
201
+ - Use **tone deltas** for visual spacing within ramps and between related UI
202
+ colors. The spacing stays even and ordered for every hue.
203
+ - Use **`contrast`** when a rendered color must meet a measured WCAG or APCA
204
+ floor. The solver is the one deliberate exception to the invariant: it moves a
205
+ color's tone per hue and saturation until the measured floor is met. See
206
+ [Contrast floors](#contrast-floors-and-verification).
207
+
208
+ ## Tone windows
209
+
210
+ Each scheme renders authored tone into a **tone window**, configured by
211
+ `lightTone` and `darkTone`:
212
+
213
+ ```ts
214
+ import { glaze } from '@tenphi/glaze';
215
+
216
+ glaze.configure({
217
+ lightTone: [10, 100],
218
+ darkTone: [15, 95],
219
+ });
220
+ ```
221
+
222
+ The window's `lo` and `hi` are **OKHSL-lightness** boundaries (0–100). Glaze
223
+ converts them to tone, positions the authored tone linearly in that tone
224
+ interval, converts the result back to lightness with the window's `eps`, and
225
+ stores it as canonical tone:
226
+
227
+ ```text
228
+ loT = toTone(lo / 100, eps)
229
+ hiT = toTone(hi / 100, eps)
230
+ l = fromTone(loT + (T / 100) * (hiT - loT), eps)
231
+ t = toTone(l) / 100 // canonical tone (0–1), REF_EPS
232
+ ```
233
+
234
+ A window takes three forms:
235
+
236
+ | Form | Meaning |
237
+ | ----------------- | ------------------------------------------------------------------------------- |
238
+ | `[lo, hi]` | Boundaries at the reference `eps`. The common form. |
239
+ | `{ lo, hi, eps }` | Boundaries plus a render curvature for this scheme. Advanced. |
240
+ | `false` | The full range `[0, 100]` at the reference `eps`: no boundaries, same tone curve. |
241
+
242
+ Endpoints clamp to 0–100. A value that is none of the three forms (`null`
243
+ included), a window with `lo` above `hi` or a non-finite endpoint, and an `eps`
244
+ outside `(0, 1e6]` all throw instead of quietly inverting or breaking the
245
+ scale. The `eps` cap is numerical: the transfer divides by
246
+ `ln(1 + eps) - ln(eps)`, which loses precision as `eps` grows and cancels to 0
247
+ (`NaN` tones) from about 1e15, while past about 1e3 the curve is already linear
248
+ in luminance. The rules are in
249
+ [Configuration](./reference/configuration.md#tone-windows).
250
+
251
+ ### Calibrated defaults
252
+
253
+ | | `lightTone` | `darkTone` |
254
+ | ---------- | ----------: | ---------: |
255
+ | `lo` | 10 | 15 |
256
+ | `hi` | 100 | 95 |
257
+ | `eps` | 0.05 | 0.05 |
258
+ | Tone range | 4.95–100 | 9.25–95.9 |
259
+
260
+ The floors keep mapped tones off pure black in both schemes (OKHSL lightness 10
261
+ in light, 15 in dark), and the dark ceiling keeps them short of white at
262
+ lightness 95. `eps` is pinned to the reference 0.05 so the axis stays
263
+ WCAG-uniform inside the window. Because the remap is linear in tone, it scales
264
+ distances between absolute tones uniformly: about 95% of the authored distance
265
+ in light and 86.6% in dark.
266
+
267
+ <SchemeRamp
268
+ caption="Authored tones 0 to 100 (mode 'auto') resolved in every scheme. Each label is the tone Glaze stores. Light compresses into 4.95–100; dark inverts, then compresses into 9.25–95.9; high contrast uses the full range."
269
+ />
270
+
271
+ ### High contrast uses the full range
272
+
273
+ High-contrast variants run the same math with the window forced to the full
274
+ range `[0, 100]`, keeping the window's `eps`. There is no separate high-contrast
275
+ curve and no fitted dark curve.
276
+
277
+ ### Manual contrast level
278
+
279
+ A [manual contrast level](./reference/contrast.md#manual-contrast-level) generalizes that bypass
280
+ continuously. With `contrastLevel: L` and `f = L / 100`, each boundary moves
281
+ toward the full range instead of jumping to it:
282
+
283
+ ```text
284
+ lo(f) = lo + (0 - lo) * f
285
+ hi(f) = hi + (100 - hi) * f
286
+ ```
287
+
288
+ The light default walks `[10, 100] → [0, 100]` and the dark default
289
+ `[15, 95] → [0, 100]`; at level 50 they are `[5, 100]` (tone 1.63–100) and
290
+ `[7.5, 97.5]` (tone 3.15–97.97). The render `eps` is not interpolated, and a
291
+ `false` window is already the full range, so it is level-invariant.
292
+
293
+ The continuous form applies to the normal variants only. High-contrast variants
294
+ are already at the full range, so the level does not widen them further: the
295
+ bypass is what keeps the high-contrast tier independent of the level. The level
296
+ itself is covered in
297
+ [Light, dark, and high contrast](./schemes.mdx#manual-contrast-level).
298
+
299
+ ## Reference and render epsilon
300
+
301
+ Two `eps` values play separate roles:
302
+
303
+ - **Reference eps** — `REF_EPS = 0.05`, fixed. It defines the OKHST color
304
+ space and Glaze's canonical tone. `okhst()` input, `{ h, s, t }` input, the
305
+ stored `ResolvedColorVariant.t`, tone deltas, and the contrast solver all use
306
+ it, which keeps tone comparable between schemes.
307
+ - **Render eps** — the `eps` of an object tone window. A per-scheme curvature
308
+ control that only changes how authored tone maps through that scheme's
309
+ window. It defaults to the reference value, so by default the two coincide.
310
+
311
+ When a window's `eps` differs from the reference, Glaze maps with it to land a
312
+ final OKHSL lightness, then stores `toTone(l, REF_EPS)`, so deltas and contrast
313
+ calculations stay on the reference scale. For example,
314
+ `lightTone: { lo: 10, hi: 100, eps: 0.2 }` stores authored tone 50 at 63.89
315
+ instead of 52.47: mid tones bend lighter. Most palettes should keep the default.
316
+
317
+ ## The scheme pipeline
318
+
319
+ A regular color's tone goes through one pipeline per scheme:
320
+
321
+ ```text
322
+ authored tone T (0–100)
323
+ -> adaptation mode
324
+ auto + dark invert: T' = 100 - T
325
+ light / fixed keep: T' = T
326
+ static identity; skip the window
327
+ -> remap into the scheme's tone window (full range for high contrast)
328
+ -> render curvature (window eps) -> OKHSL lightness l
329
+ -> store canonical tone t = toTone(l, REF_EPS) / 100
330
+ // variant { h, s, t, alpha }, t on 0–1
331
+ -> at the rendering edge only:
332
+ fromTone(100 * t) -> l -> sRGB / luminance
333
+ optional: a contrast floor searches tone and overrides t
334
+ ```
335
+
336
+ ### Adaptation modes
337
+
338
+ The first step is the color's `mode`. Use `auto` for surfaces and foregrounds
339
+ that should trade places between light and dark, `fixed` for brand fills and
340
+ inverse surfaces that should stay on the same side of the scale, and `static`
341
+ for colors that must not adapt.
342
+ [Light, dark, and high contrast](./schemes.mdx#adaptation-modes) has the full
343
+ table and shows each mode side by side.
344
+
345
+ **Dark tone inversion is not `autoFlip`.** Inversion is ordinary scheme
346
+ adaptation, controlled by `mode`. `autoFlip` solves a different, local problem:
347
+ it may mirror a tone delta that overshoots `[0, 100]` to the other side of its
348
+ base, or let the contrast solver try the opposite side of a base.
349
+
350
+ ### Hue and saturation pass through
351
+
352
+ Tone is the only channel OKHST adapts across schemes; `h` and `s` are authoring
353
+ inputs that pass through the tone transfer untouched. The one automatic
354
+ adjustment is `darkDesaturation`, which dark schemes apply to every color except
355
+ `static` ones. A theme, or a single color, can seed a different hue and
356
+ saturation for the dark schemes with `darkHue` / `darkSaturation`; those pick the
357
+ `h` and `s` that enter the pipeline and never change how tone is transferred.
358
+ An authored dark saturation replaces the `darkDesaturation` step instead of
359
+ stacking with it: a color's own `darkSaturation` always does, and the theme's
360
+ seed does for every color that reads it. A `from` color with no `saturation` of
361
+ its own starts from the literal's saturation rather than the seed, so it keeps
362
+ the step. `static` colors ignore all of it. The rules are in
363
+ [Light, dark, and high contrast](./schemes.mdx#dark-seed) and
364
+ [Color definitions](./reference/color-definitions.md#dark-seed-darkhue--darksaturation).
365
+
366
+ ## Tone values
367
+
368
+ A color's `tone` takes one of three kinds of value. The full syntax, including
369
+ `[normal, highContrast]` pairs, is in
370
+ [Color definitions](./reference/color-definitions.md#tone-values).
371
+
372
+ ```ts
373
+ import { glaze } from '@tenphi/glaze';
374
+
375
+ const theme = glaze(280, 30);
376
+
377
+ theme.colors({
378
+ surface: { tone: 97 }, // absolute
379
+ border: { base: 'surface', tone: '-8' }, // tone delta
380
+ deep: { tone: 'min' }, // extreme
381
+ ink: { base: 'surface', tone: 'min' }, // extreme anchored to a base
382
+ });
383
+ ```
384
+
385
+ ### Absolute tone
386
+
387
+ A number places a color on the 0–100 scale independently of every other color.
388
+ Each scheme maps it through the pipeline above, so the stored tone is not the
389
+ authored one: `surface: { tone: 97 }` is stored as 97.15 in light and, inverted
390
+ to 3 and mapped into the dark window, as 11.85 in dark. A dependent color (one
391
+ with a `base`) can still take an absolute tone; it is then mapped on its own and
392
+ the base only matters to a `contrast` floor.
393
+
394
+ ### Tone deltas
395
+
396
+ A signed string is a **tone delta**: the signed difference between a dependent
397
+ color and its base.
398
+
399
+ ```ts
400
+ border: { base: 'surface', tone: '-8' }
401
+ ```
402
+
403
+ How the delta travels depends on the dependent color's `mode`:
404
+
405
+ - **`fixed` and `static` in every scheme, and `auto` in the light schemes:**
406
+ the delta is applied to the base's resolved tone in that scheme, sign
407
+ preserved. It is an exact tone step, so for neutrals it is an exact contrast
408
+ step.
409
+ - **`auto` in the dark schemes:** the delta is added to the base's **anchor**
410
+ — where the base sits on the authored scale, the tone its own dark variant is
411
+ the inversion of — and the result is inverted and mapped through the dark
412
+ window like an absolute tone. Order and direction carry over — text that
413
+ sits below its surface in light sits above it in dark.
414
+
415
+ The anchor depends on how the base got its tone, and the same rules hold in
416
+ both dark passes:
417
+
418
+ - An `auto` base placed by a tone (a number, a `from` color, an extreme with
419
+ no base) anchors at its authored tone, and an `auto` tone delta at its base's
420
+ anchor plus the delta, so a chain steps evenly. An `auto` color with no
421
+ `tone` or `contrast` of its own anchors at its base's anchor.
422
+ - Any other base, whose dark tone is not the inversion of an authored one — a
423
+ `fixed` or `static` color, an `auto` contrast-solved color, an `auto` extreme
424
+ on a base, a mix — anchors at its own dark tone read back through the dark
425
+ window (the window remap run backwards, then un-inverted), so a dependent
426
+ follows where the base lands in dark.
427
+
428
+ On every base, the step is then the dark window's scale times the delta at the
429
+ reference dark `eps`, 86.6% of the authored delta at the default windows. A
430
+ tone-50 base steps `'+4'` to −3.5 and `'-4'` to +3.5, and a `'-4'` hover on an
431
+ AAA-solved button steps +3.5 too. A custom dark `eps` bends the dark window's
432
+ curve, so a dark delta lands where the same authored tone would instead of at a
433
+ fixed scale. In dark high contrast the window is the full range, so at the
434
+ reference dark `eps` the step is exact, only mirrored, wherever it fits on the
435
+ scale (limits below). The anchor is the authored position, not the base's light
436
+ high-contrast tone, so a custom light `eps`, which moves that tone (lighter
437
+ above the reference 0.05, darker below), doesn't change the step away from the
438
+ ends.
439
+
440
+ Order carries over only when the base inverts too. An `auto` delta on a
441
+ `fixed` or `static` base inverts on its own while the base does not (it keeps
442
+ its side, or, as a `fixed` delta, rides its own base into dark), so it mirrors
443
+ to the other side of the base in dark: a `'+30'` label on a `fixed` tone-52
444
+ fill sits 30 tones above it in light and 26.0 below it in dark. Make such a
445
+ dependent `fixed` as well when it should keep its side.
446
+
447
+ If `base + delta` overshoots `[0, 100]`, `autoFlip` (on by default) mirrors the
448
+ delta to the other side of the base; if the mirror overshoots too, or `autoFlip`
449
+ is off, the delta clamps on its authored side. Under `auto` the flip is decided
450
+ on the light tones, and dark mirrors the side light took. Dark then steps on
451
+ the authored scale, which adds two limits there:
452
+
453
+ - A step that fits from the base's light tone can still run past 0 or 100
454
+ from the anchor. In dark it then stops at the dark window's edge. The light
455
+ window lifts authored tone 0 to 4.95, so an `auto` base placed by a tone
456
+ meets this at the bottom: `'-4'` steps about +1.7 on a tone-2 base and not
457
+ at all on a tone-0 one. A base anchored at its own dark tone can meet it at
458
+ either end. In `glaze(280, 40)`, `contrast: 12` text on a tone-97 surface
459
+ sits at 15.6 in light but anchors at 2.7, so a `'-4'` hover on it steps
460
+ +2.3 in dark, not +3.5.
461
+ - An anchor past the dark window — a base a contrast floor or an extreme pushed
462
+ beyond its edge, or a `fixed` delta riding its base there — is stepped from
463
+ where it sits. A step back toward the window moves by the full scaled delta;
464
+ one further out stops at the window edge or the base, whichever is further
465
+ out.
466
+
467
+ High contrast's windows are the full range, and at the reference light `eps`
468
+ the light one is the identity, so an `auto` base placed by a tone flips the
469
+ same way in both high-contrast schemes and its step fits. The first limit
470
+ still applies there under a custom light `eps`, and to a base anchored at its
471
+ own dark tone.
472
+
473
+ ### Extremes: `'max'` and `'min'`
474
+
475
+ `'max'` is author tone 100 and `'min'` is author tone 0. They select the
476
+ corresponding end of the scheme without a magic number.
477
+
478
+ - **Without a base,** an extreme flows through the pipeline like any absolute
479
+ tone: it lands on the window edge and, under `auto`, inverts in dark — `'min'`
480
+ is the darkest color in light and the lightest in dark. Use `fixed` to keep the
481
+ same end without inverting, or `static` to pin one tone everywhere.
482
+ - **With a base,** the dark scheme takes a different path. The windows are
483
+ asymmetric, so running the extreme through the dark window would squeeze the
484
+ span between it and its base and cost contrast. Instead Glaze measures the
485
+ tone shift the light scheme produced between the base and the extreme, then
486
+ replays it against the base's resolved dark tone: mirrored under `auto` (both
487
+ ends invert), same-signed under `fixed`. Only `[0, 100]` clamping applies, so
488
+ the color may render past the dark window's edge — on purpose, since you asked
489
+ for the extreme. When the shift does not fit, the color pins at tone 100 or 0.
490
+ `static` colors are unaffected. High-contrast variants follow the same rule,
491
+ but their window is already the full range, so the replay matches the plain
492
+ mapping unless the base itself sits asymmetrically across schemes. With a
493
+ `contrast` floor, an anchored extreme's starting tone is not clamped back
494
+ into the scheme window.
495
+
496
+ ```ts
497
+ const neutral = glaze(0, 0);
498
+
499
+ neutral.colors({
500
+ bg: { tone: 60 },
501
+ fg: { base: 'bg', tone: 'max' },
502
+ });
503
+ // light: bg 62.0, fg 100.0 (shift +38.0, 3.18:1)
504
+ // dark: bg 43.9, fg 5.9 (shift -38.0, 3.18:1)
505
+ ```
506
+
507
+ A root `tone: 'max'` in the same theme lands at the dark window's edge,
508
+ tone 9.25, and drops to 2.87:1 against `bg` in dark.
509
+
510
+ ### All three, light to dark
511
+
512
+ <Example
513
+ hue={280}
514
+ saturation={30}
515
+ colors={{
516
+ surface: { tone: 97 },
517
+ border: { base: 'surface', tone: '-8' },
518
+ text: { base: 'surface', tone: '-60' },
519
+ deep: { tone: 'min' },
520
+ ink: { base: 'surface', tone: 'min' },
521
+ }}
522
+ visual="none"
523
+ />
524
+
525
+ <ToneLadder
526
+ hue={280}
527
+ saturation={30}
528
+ colors={{
529
+ surface: { tone: 97 },
530
+ border: { base: 'surface', tone: '-8' },
531
+ text: { base: 'surface', tone: '-60' },
532
+ deep: { tone: 'min' },
533
+ ink: { base: 'surface', tone: 'min' },
534
+ }}
535
+ base="surface"
536
+ schemes={['light', 'dark', 'lightContrast', 'darkContrast']}
537
+ caption="Each chip shows the stored tone, the tone difference from surface, and the measured WCAG ratio against it. In dark, the deltas shrink with the window (−8 becomes +6.9, −60 becomes +52.0), deep stops at the window edge, and ink replays its full light span past it, clamped at 100. In high contrast the window is the full range and the deltas are exact."
538
+ />
539
+
540
+ The `text` color above has no contrast floor, so its ratio follows its tone
541
+ step: 6.29:1 in light and 4.85:1 in dark. Add `contrast` when the measured
542
+ ratio matters.
543
+
544
+ ## Contrast floors and verification
545
+
546
+ Tone is the contrast solver's search axis because it is contrast-uniform: the
547
+ search converges quickly and WCAG gets a closed-form first probe. But Glaze
548
+ never assumes a chromatic color has the luminance of the gray at its tone. It
549
+ measures the real color.
550
+
551
+ `contrast` takes one spec with a pluggable metric: a WCAG ratio (a bare number,
552
+ a preset such as `'AA'`, or `{ wcag }`) or an APCA Lc target (`{ apca }`).
553
+ Forms, presets, roles, and high-contrast escalation are covered in
554
+ [Contrast solving](./contrast.mdx) and
555
+ [Contrast and high contrast](./reference/contrast.md).
556
+
557
+ ### How a floor is solved
558
+
559
+ For a color with `base` and `contrast`, in each scheme Glaze:
560
+
561
+ 1. resolves the base and the color's preferred tone for that scheme, clamped
562
+ into the scheme's tone window (anchored extremes excepted);
563
+ 2. measures the requested WCAG ratio or APCA Lc at the preferred tone, using the
564
+ color's real hue and saturation — if it already clears the floor with a
565
+ small margin (×1.01 for a WCAG ratio, +0.5 Lc for APCA, to absorb rounding
566
+ in the output formats), the tone is kept (`contrast` is a floor, not a
567
+ target); a preferred tone that clears the floor but not the margin is also
568
+ kept, and counts as met, when no searched side meets the floor (white text
569
+ at tone 100 has nowhere further to go);
570
+ 3. otherwise searches tone. For WCAG the search first measures the gray tone
571
+ whose luminance hits the ratio exactly — `Y = R * (Y_base + 0.05) - 0.05`
572
+ on the lighter side, `(Y_base + 0.05) / R - 0.05` on the darker — which
573
+ only narrows where the binary search against the measured chromatic
574
+ luminance looks, so a chromatic color still lands on its own crossing. For
575
+ APCA it binary-searches against the Lc target directly. The search covers
576
+ the full 0–100 range, so a demanding floor can push a color past the
577
+ window, and it aims for the same margin past the target;
578
+ 4. warns when the target is physically unreachable in the scheme.
579
+
580
+ ```ts
581
+ import { glaze } from '@tenphi/glaze';
582
+
583
+ const theme = glaze(150, 90);
584
+
585
+ theme.colors({
586
+ surface: { tone: 97, saturation: 0 },
587
+ // 49.4 tones below the surface: exactly 4.5:1 for a neutral
588
+ 'step-text': { base: 'surface', tone: '-49.4' },
589
+ // Same starting tone, but the solver measures the real color
590
+ 'floor-text': { base: 'surface', tone: '-49.4', contrast: 'AA' },
591
+ });
592
+ ```
593
+
594
+ <FloorCompare
595
+ hues={[25, 110, 150, 265, 320]}
596
+ caption="Light scheme. Each row is one hue at the neutral AA distance below a neutral surface, without and with contrast: 'AA'. Hues that read lighter than their tone fall short and are moved darker; hues that read darker already pass and keep their tone. The neutral is nudged just past 4.5:1 by the search overshoot."
597
+ />
598
+
599
+ ### APCA
600
+
601
+ `apcaContrast(yText, yBg)` implements APCA-W3 0.1.9 (simplified SAPC): a
602
+ soft clamp for low luminances and separate exponents for normal and reverse
603
+ polarity, returning a signed Lc whose magnitude the solver compares with the
604
+ target. Its inputs are APCA **screen luminances** over the gamma-encoded sRGB
605
+ channels, not WCAG relative luminance:
606
+
607
+ ```text
608
+ Ys = 0.2126 * R^2.4 + 0.7152 * G^2.4 + 0.0722 * B^2.4
609
+ ```
610
+
611
+ The soft-clamp constants are calibrated against `Ys`, so the solver feeds APCA
612
+ that basis. The function it uses is exported as
613
+ [`apcaLuminanceFromLinearRgb`](./reference/utilities.md#luminance-and-contrast),
614
+ so `apcaContrast` over its output reproduces the solver's own Lc. The
615
+ implementation omits APCA's font-size lookup that maps an Lc to
616
+ a usable text size.
617
+
618
+ ### The drift check
619
+
620
+ After all four scheme passes, Glaze verifies every color that has a `base` and
621
+ a `contrast` floor. It re-measures the color against its base in each scheme, in
622
+ the floor's own metric (APCA screen luminance for APCA, WCAG luminance for
623
+ WCAG) and with each variant's own `pastel` flag, and emits a `console.warn` when
624
+ the result drifts below the target by more than a small slack: 2% of a WCAG
625
+ ratio, or 1.5 Lc. It skips a scheme where the solver couldn't reach the floor,
626
+ which already has the solver's "cannot meet" warning with the same measured
627
+ value, and an achromatic color (saturation 0), whose luminance is exactly its
628
+ tone's. Because the solver already measures the real chromatic color, a floor
629
+ it meets passes this check, so in practice the advisory stays silent; it
630
+ remains as a safety net for the chromatic approximation. Warnings are
631
+ deduplicated through a 256-key session cache that starts over when full. Both
632
+ warnings are listed in [Configuration](./reference/configuration.md#warnings).
633
+
634
+ ## Input and output
635
+
636
+ Glaze parses OKHST wherever it takes a color value — `glaze.color()`, a color
637
+ definition's `from`, and the `bg` / `fg` of the standalone `glaze.shadow()` /
638
+ `glaze.glow()`:
639
+
640
+ ```ts
641
+ import { glaze } from '@tenphi/glaze';
642
+
643
+ glaze.color('okhst(152 95% 70%)');
644
+ glaze.color({ h: 152, s: 0.95, t: 0.7 });
645
+ ```
646
+
647
+ Both value forms reproduce the color exactly in the light scheme. The
648
+ structured form `glaze.color({ hue: 152, saturation: 95, tone: 70 })` is
649
+ different: its `tone` is an authored tone on the 0–100 scale, mapped through the
650
+ light window like a theme color, so it renders at tone 71.48. See
651
+ [Color tokens](./reference/color-tokens.md#glazecolorinput).
652
+
653
+ There is no native CSS `okhst()` function, so native exports use `oklch`,
654
+ `rgb`, or `hsl`. [Tasty](https://tasty.style) can consume Glaze's custom
655
+ serialization, so `tasty()` accepts `format: 'okhst'`, as does `glaze.format()`
656
+ for a single variant. `tokens()`, `json()`, `css()`, and `tailwind()` throw and
657
+ name a native format instead; the DTCG exports pick a color space of their own.
658
+
659
+ ```ts
660
+ const theme = glaze(280, 30);
661
+
662
+ theme.colors({ surface: { tone: 97 } });
663
+ theme.tasty({ format: 'okhst' });
664
+ // {
665
+ // '#surface': {
666
+ // '': 'okhst(280 30% 97.15%)',
667
+ // '@media(prefers-color-scheme: dark)': 'okhst(280 27% 11.85%)',
668
+ // },
669
+ // }
670
+ ```
671
+
672
+ The color formats are listed in
673
+ [Output formats](./reference/output.md#color-formats); `toTone`, `fromTone`,
674
+ `toneFromY`, `yFromTone`, `okhstToOkhsl`, `okhslToOkhst`, `variantToOkhsl`, and
675
+ `REF_EPS` are exported for your own math (see
676
+ [Utilities](./reference/utilities.md#okhst-tone-utilities)).
677
+
678
+ ## Bringing existing colors into Glaze
679
+
680
+ When you move raw CSS colors or design tokens into Glaze, preserve the source
681
+ colors first: `glaze.fromHex()` and `glaze.fromRgb()` seed a theme from a
682
+ color, `glaze.color()` takes any color value, and a color definition's `from`
683
+ places a literal color inside a theme. Then replace one-off values with root
684
+ tones, tone deltas, and contrast floors as the relationships become clear.
685
+
686
+ To carry an OKHSL lightness `L` (0–100) over as a tone, convert it with
687
+ `toTone(L / 100)`; the two are not the same number. That tone is still an
688
+ authored tone, so the light window remaps it and it renders slightly lighter:
689
+ `L` 20 becomes tone 14.36 and renders at lightness 23.8. To keep a source color
690
+ exact in light, place it with `from`, which skips the light window, or turn the
691
+ window off with `lightTone: false`. The full workflow is in
692
+ [Migration and upgrading](./migration.md#migrating-from-an-existing-color-system).