@tenphi/glaze 0.0.0-snapshot.07e5d83

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/README.md ADDED
@@ -0,0 +1,1084 @@
1
+ <p align="center">
2
+ <img src="assets/glaze.svg" width="128" height="128" alt="Glaze logo">
3
+ </p>
4
+
5
+ <h1 align="center">Glaze</h1>
6
+
7
+ <p align="center">
8
+ OKHSL-based color theme generator with WCAG contrast solving
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/@tenphi/glaze"><img src="https://img.shields.io/npm/v/@tenphi/glaze.svg" alt="npm version"></a>
13
+ <a href="https://github.com/tenphi/glaze/actions/workflows/ci.yml"><img src="https://github.com/tenphi/glaze/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
14
+ <a href="https://github.com/tenphi/glaze/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@tenphi/glaze.svg" alt="license"></a>
15
+ </p>
16
+
17
+ ---
18
+
19
+ Glaze generates robust **light**, **dark**, and **high-contrast** color schemes from a single hue/saturation seed. It preserves WCAG contrast ratios for UI color pairs via explicit dependency declarations — no hidden role math, no magic multipliers.
20
+
21
+ ## Features
22
+
23
+ - **OKHSL color space** — perceptually uniform hue and saturation
24
+ - **WCAG 2 contrast solving** — automatic lightness adjustment to meet AA/AAA targets
25
+ - **Mix colors** — blend two colors with OKHSL or sRGB interpolation, opaque or transparent, with optional contrast solving
26
+ - **Shadow colors** — OKHSL-native shadow computation with automatic alpha, fg/bg tinting, and per-scheme adaptation
27
+ - **Light + Dark + High-Contrast** — all schemes from one definition
28
+ - **Per-color hue override** — absolute or relative hue shifts within a theme
29
+ - **Multi-format output** — `okhsl`, `rgb`, `hsl`, `oklch` with modern CSS space syntax
30
+ - **CSS custom properties export** — ready-to-use `--var: value;` declarations per scheme
31
+ - **Import/Export** — serialize and restore theme configurations
32
+ - **Create from hex/RGB** — start from an existing brand color
33
+ - **Zero dependencies** — pure math, runs anywhere (Node.js, browser, edge)
34
+ - **Tree-shakeable ESM + CJS** — dual-format package
35
+ - **TypeScript-first** — full type definitions included
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ pnpm add @tenphi/glaze
41
+ ```
42
+
43
+ ```bash
44
+ npm install @tenphi/glaze
45
+ ```
46
+
47
+ ```bash
48
+ yarn add @tenphi/glaze
49
+ ```
50
+
51
+ ## Quick Start
52
+
53
+ ```ts
54
+ import { glaze } from '@tenphi/glaze';
55
+
56
+ // Create a theme from a hue (0–360) and saturation (0–100)
57
+ const primary = glaze(280, 80);
58
+
59
+ // Define colors with explicit lightness and contrast relationships
60
+ primary.colors({
61
+ surface: { lightness: 97, saturation: 0.75 },
62
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
63
+ border: { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' },
64
+ 'accent-fill': { lightness: 52, mode: 'fixed' },
65
+ 'accent-text': { base: 'accent-fill', lightness: '+48', contrast: 'AA', mode: 'fixed' },
66
+ });
67
+
68
+ // Create status themes by rotating the hue
69
+ const danger = primary.extend({ hue: 23 });
70
+ const success = primary.extend({ hue: 157 });
71
+
72
+ // Compose into a palette and export
73
+ const palette = glaze.palette({ primary, danger, success });
74
+ const tokens = palette.tokens({ prefix: true });
75
+ // → { light: { 'primary-surface': 'okhsl(...)', ... }, dark: { 'primary-surface': 'okhsl(...)', ... } }
76
+ ```
77
+
78
+ ## Core Concepts
79
+
80
+ ### One Theme = One Hue Family
81
+
82
+ A single `glaze` theme is tied to one hue/saturation seed. Status colors (danger, success, warning) are derived via `extend`, which inherits all color definitions and replaces the seed.
83
+
84
+ Individual colors can override the hue via the `hue` prop (see [Per-Color Hue Override](#per-color-hue-override)), but the primary purpose of a theme is to scope colors with the same hue.
85
+
86
+ ### Color Definitions
87
+
88
+ Every color is defined explicitly. No implicit roles — every value is stated.
89
+
90
+ #### Root Colors (explicit position)
91
+
92
+ ```ts
93
+ primary.colors({
94
+ surface: { lightness: 97, saturation: 0.75 },
95
+ border: { lightness: 90, saturation: 0.20 },
96
+ });
97
+ ```
98
+
99
+ - `lightness` — lightness in the light scheme (0–100)
100
+ - `saturation` — saturation factor applied to the seed saturation (0–1, default: `1`)
101
+
102
+ #### Dependent Colors (relative to base)
103
+
104
+ ```ts
105
+ primary.colors({
106
+ surface: { lightness: 97, saturation: 0.75 },
107
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
108
+ });
109
+ ```
110
+
111
+ - `base` — name of another color in the same theme
112
+ - `lightness` — position of this color (see [Lightness Values](#lightness-values))
113
+ - `contrast` — ensures the WCAG contrast ratio meets a target floor against the base
114
+
115
+ ### Lightness Values
116
+
117
+ The `lightness` prop accepts two forms:
118
+
119
+ | Form | Example | Meaning |
120
+ |---|---|---|
121
+ | Number (absolute) | `lightness: 45` | Absolute lightness 0–100 |
122
+ | String (relative) | `lightness: '-52'` | Relative to base color's lightness |
123
+
124
+ **Absolute lightness** on a dependent color (with `base`) positions the color independently. In dark mode, it is dark-mapped on its own. The `contrast` WCAG solver acts as a safety net.
125
+
126
+ **Relative lightness** applies a signed delta to the base color's resolved lightness. In dark mode with `auto` adaptation, the sign flips automatically.
127
+
128
+ ```ts
129
+ // Relative: 97 - 52 = 45 in light mode
130
+ 'text': { base: 'surface', lightness: '-52' }
131
+
132
+ // Absolute: lightness 45 in light mode, dark-mapped independently
133
+ 'text': { base: 'surface', lightness: 45 }
134
+ ```
135
+
136
+ A dependent color with `base` but no `lightness` inherits the base's lightness (equivalent to a delta of 0).
137
+
138
+ ### Per-Color Hue Override
139
+
140
+ Individual colors can override the theme's hue. The `hue` prop accepts:
141
+
142
+ | Form | Example | Meaning |
143
+ |---|---|---|
144
+ | Number (absolute) | `hue: 120` | Absolute hue 0–360 |
145
+ | String (relative) | `hue: '+20'` | Relative to the **theme seed** hue |
146
+
147
+ **Important:** Relative hue is always relative to the **theme seed hue**, not to a base color's hue.
148
+
149
+ ```ts
150
+ const theme = glaze(280, 80);
151
+ theme.colors({
152
+ surface: { lightness: 97 },
153
+ // Gradient end — slight hue shift from seed (280 + 20 = 300)
154
+ gradientEnd: { lightness: 90, hue: '+20' },
155
+ // Entirely different hue
156
+ warning: { lightness: 60, hue: 40 },
157
+ });
158
+ ```
159
+
160
+ ### contrast (WCAG Floor)
161
+
162
+ Ensures the WCAG contrast ratio meets a target floor. Accepts a numeric ratio or a preset string:
163
+
164
+ ```ts
165
+ type MinContrast = number | 'AA' | 'AAA' | 'AA-large' | 'AAA-large';
166
+ ```
167
+
168
+ | Preset | Ratio |
169
+ |---|---|
170
+ | `'AA'` | 4.5 |
171
+ | `'AAA'` | 7 |
172
+ | `'AA-large'` | 3 |
173
+ | `'AAA-large'` | 4.5 |
174
+
175
+ You can also pass any numeric ratio directly (e.g., `contrast: 4.5`, `contrast: 7`, `contrast: 11`).
176
+
177
+ The constraint is applied independently for each scheme. If the `lightness` already satisfies the floor, it's kept. Otherwise, the solver adjusts lightness until the target is met.
178
+
179
+ ### High-Contrast via Array Values
180
+
181
+ `lightness` and `contrast` accept a `[normal, high-contrast]` pair:
182
+
183
+ ```ts
184
+ 'border': { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' }
185
+ // ↑ ↑
186
+ // normal high-contrast
187
+ ```
188
+
189
+ A single value applies to both modes. All control is local and explicit.
190
+
191
+ ```ts
192
+ 'text': { base: 'surface', lightness: '-52', contrast: 'AAA' }
193
+ 'border': { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' }
194
+ 'muted': { base: 'surface', lightness: ['-35', '-50'], contrast: ['AA-large', 'AA'] }
195
+ ```
196
+
197
+ ## Theme Color Management
198
+
199
+ ### Adding Colors
200
+
201
+ `.colors(defs)` performs an **additive merge** — it adds new colors and overwrites existing ones by name, but does not remove other colors:
202
+
203
+ ```ts
204
+ const theme = glaze(280, 80);
205
+ theme.colors({ surface: { lightness: 97 } });
206
+ theme.colors({ text: { lightness: 30 } });
207
+ // Both 'surface' and 'text' are now defined
208
+ ```
209
+
210
+ ### Single Color Getter/Setter
211
+
212
+ `.color(name)` returns the definition, `.color(name, def)` sets it:
213
+
214
+ ```ts
215
+ theme.color('surface', { lightness: 97, saturation: 0.75 }); // set
216
+ const def = theme.color('surface'); // get → { lightness: 97, saturation: 0.75 }
217
+ ```
218
+
219
+ ### Removing Colors
220
+
221
+ `.remove(name)` or `.remove([name1, name2])` deletes color definitions:
222
+
223
+ ```ts
224
+ theme.remove('surface');
225
+ theme.remove(['text', 'border']);
226
+ ```
227
+
228
+ ### Introspection
229
+
230
+ ```ts
231
+ theme.has('surface'); // → true/false
232
+ theme.list(); // → ['surface', 'text', 'border', ...]
233
+ ```
234
+
235
+ ### Clearing All Colors
236
+
237
+ ```ts
238
+ theme.reset(); // removes all color definitions
239
+ ```
240
+
241
+ ## Import / Export
242
+
243
+ Serialize a theme's configuration (hue, saturation, color definitions) to a plain JSON-safe object, and restore it later:
244
+
245
+ ```ts
246
+ // Export
247
+ const snapshot = theme.export();
248
+ // → { hue: 280, saturation: 80, colors: { surface: { lightness: 97, saturation: 0.75 }, ... } }
249
+
250
+ const jsonString = JSON.stringify(snapshot);
251
+
252
+ // Import
253
+ const restored = glaze.from(JSON.parse(jsonString));
254
+ // restored is a fully functional GlazeTheme
255
+ ```
256
+
257
+ The export contains only the configuration — not resolved color values. Resolved values are recomputed on demand.
258
+
259
+ ## Standalone Color Token
260
+
261
+ Create a single color token without a full theme:
262
+
263
+ ```ts
264
+ const accent = glaze.color({ hue: 280, saturation: 80, lightness: 52, mode: 'fixed' });
265
+
266
+ accent.resolve(); // → ResolvedColor with light/dark/lightContrast/darkContrast
267
+ accent.token(); // → { '': 'okhsl(...)', '@dark': 'okhsl(...)' } (tasty format)
268
+ accent.tasty(); // → { '': 'okhsl(...)', '@dark': 'okhsl(...)' } (same as token)
269
+ accent.json(); // → { light: 'okhsl(...)', dark: 'okhsl(...)' }
270
+ ```
271
+
272
+ Standalone colors are always root colors (no `base`/`contrast`).
273
+
274
+ ## From Existing Colors
275
+
276
+ Create a theme from an existing brand color by extracting its OKHSL hue and saturation:
277
+
278
+ ```ts
279
+ // From hex
280
+ const brand = glaze.fromHex('#7a4dbf');
281
+
282
+ // From RGB (0–255)
283
+ const brand = glaze.fromRgb(122, 77, 191);
284
+ ```
285
+
286
+ The resulting theme has the extracted hue and saturation. Add colors as usual:
287
+
288
+ ```ts
289
+ brand.colors({
290
+ surface: { lightness: 97, saturation: 0.75 },
291
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
292
+ });
293
+ ```
294
+
295
+ ## Shadow Colors
296
+
297
+ Shadow colors are colors with computed alpha. Instead of a parallel shadow system, they extend the existing color pipeline. All math is done natively in OKHSL.
298
+
299
+ ### Defining Shadow Colors
300
+
301
+ Shadow colors use `type: 'shadow'` and reference a `bg` (background) color and optionally an `fg` (foreground) color for tinting and intensity modulation:
302
+
303
+ ```ts
304
+ theme.colors({
305
+ surface: { lightness: 95 },
306
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
307
+
308
+ 'shadow-sm': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 5 },
309
+ 'shadow-md': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 10 },
310
+ 'shadow-lg': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 20 },
311
+ });
312
+ ```
313
+
314
+ Shadow colors are included in all output methods (`tokens()`, `tasty()`, `css()`, `json()`) alongside regular colors:
315
+
316
+ ```ts
317
+ theme.tokens({ format: 'oklch' });
318
+ // light: { 'shadow-md': 'oklch(0.15 0.009 282 / 0.1)', ... }
319
+ // dark: { 'shadow-md': 'oklch(0.06 0.004 0 / 0.49)', ... }
320
+ ```
321
+
322
+ ### How Shadows Work
323
+
324
+ The shadow algorithm computes a dark, low-saturation pigment color and an alpha value that produces the desired visual intensity:
325
+
326
+ 1. **Contrast weight** — when `fg` is provided, shadow strength scales with `|l_bg - l_fg|`. Dark text on a light background produces a strong shadow; near-background-lightness elements produce barely visible shadows.
327
+ 2. **Pigment color** — hue blended between fg and bg, low saturation, dark lightness.
328
+ 3. **Alpha** — computed via a `tanh` curve that saturates smoothly toward `alphaMax` (default 0.6), ensuring well-separated shadow levels even on dark backgrounds.
329
+
330
+ ### Achromatic Shadows
331
+
332
+ Omit `fg` for a pure achromatic shadow at full user-specified intensity:
333
+
334
+ ```ts
335
+ theme.colors({
336
+ 'drop-shadow': { type: 'shadow', bg: 'surface', intensity: 12 },
337
+ });
338
+ ```
339
+
340
+ ### High-Contrast Intensity
341
+
342
+ `intensity` supports `[normal, highContrast]` pairs:
343
+
344
+ ```ts
345
+ theme.colors({
346
+ 'shadow-card': { type: 'shadow', bg: 'surface', fg: 'text', intensity: [10, 20] },
347
+ });
348
+ ```
349
+
350
+ ### Fixed Opacity (Regular Colors)
351
+
352
+ For a simple fixed-alpha color (no shadow algorithm), use `opacity` on a regular color:
353
+
354
+ ```ts
355
+ theme.colors({
356
+ overlay: { lightness: 0, opacity: 0.5 },
357
+ });
358
+ // → 'oklch(0 0 0 / 0.5)'
359
+ ```
360
+
361
+ ### Shadow Tuning
362
+
363
+ Fine-tune shadow behavior per-color or globally:
364
+
365
+ ```ts
366
+ // Per-color tuning
367
+ theme.colors({
368
+ 'shadow-soft': {
369
+ type: 'shadow', bg: 'surface', intensity: 10,
370
+ tuning: { alphaMax: 0.3, saturationFactor: 0.1 },
371
+ },
372
+ });
373
+
374
+ // Global tuning
375
+ glaze.configure({
376
+ shadowTuning: { alphaMax: 0.5, bgHueBlend: 0.3 },
377
+ });
378
+ ```
379
+
380
+ Available tuning parameters:
381
+
382
+ | Parameter | Default | Description |
383
+ |---|---|---|
384
+ | `saturationFactor` | 0.18 | Fraction of fg saturation kept in pigment |
385
+ | `maxSaturation` | 0.25 | Upper clamp on pigment saturation |
386
+ | `lightnessFactor` | 0.25 | Multiplier for bg lightness to pigment lightness |
387
+ | `lightnessBounds` | [0.05, 0.20] | Clamp range for pigment lightness |
388
+ | `minGapTarget` | 0.05 | Target minimum gap between pigment and bg lightness |
389
+ | `alphaMax` | 0.6 | Asymptotic maximum alpha |
390
+ | `bgHueBlend` | 0.2 | Blend weight pulling pigment hue toward bg hue |
391
+
392
+ ### Standalone Shadow Computation
393
+
394
+ Compute a shadow outside of a theme:
395
+
396
+ ```ts
397
+ const v = glaze.shadow({
398
+ bg: '#f0eef5',
399
+ fg: '#1a1a2e',
400
+ intensity: 10,
401
+ });
402
+ // → { h: 280, s: 0.14, l: 0.2, alpha: 0.1 }
403
+
404
+ const css = glaze.format(v, 'oklch');
405
+ // → 'oklch(0.15 0.014 280 / 0.1)'
406
+ ```
407
+
408
+ ### Consuming in CSS
409
+
410
+ ```css
411
+ .card {
412
+ box-shadow: 0 2px 6px var(--shadow-sm-color),
413
+ 0 8px 24px var(--shadow-md-color);
414
+ }
415
+ ```
416
+
417
+ ## Mix Colors
418
+
419
+ Mix colors blend two existing colors together. Use them for hover overlays, tints, shades, and any derived color that sits between two reference colors.
420
+
421
+ ### Opaque Mix
422
+
423
+ Produces a solid color by interpolating between `base` and `target`:
424
+
425
+ ```ts
426
+ theme.colors({
427
+ surface: { lightness: 95 },
428
+ accent: { lightness: 30 },
429
+
430
+ // 30% of the way from surface toward accent
431
+ tint: { type: 'mix', base: 'surface', target: 'accent', value: 30 },
432
+ });
433
+ ```
434
+
435
+ - `value` — mix ratio 0–100 (0 = pure base, 100 = pure target)
436
+ - The result is a fully opaque color (alpha = 1)
437
+ - Adapts to light/dark/HC schemes automatically via the resolved base and target
438
+
439
+ ### Transparent Mix
440
+
441
+ Produces the target color with a controlled opacity — useful for hover overlays:
442
+
443
+ ```ts
444
+ theme.colors({
445
+ surface: { lightness: 95 },
446
+ black: { lightness: 0, saturation: 0 },
447
+
448
+ hover: {
449
+ type: 'mix',
450
+ base: 'surface',
451
+ target: 'black',
452
+ value: 8,
453
+ blend: 'transparent',
454
+ },
455
+ });
456
+ // hover → target color (black) with alpha = 0.08
457
+ ```
458
+
459
+ The output color has `h`, `s`, `l` from the target and `alpha = value / 100`.
460
+
461
+ ### Blend Space
462
+
463
+ By default, opaque mixing interpolates in OKHSL (perceptually uniform, consistent with Glaze's model). Use `space: 'srgb'` for linear sRGB interpolation, which matches browser compositing:
464
+
465
+ ```ts
466
+ theme.colors({
467
+ surface: { lightness: 95 },
468
+ accent: { lightness: 30 },
469
+
470
+ // sRGB blend — matches what the browser would render
471
+ hover: { type: 'mix', base: 'surface', target: 'accent', value: 20, space: 'srgb' },
472
+ });
473
+ ```
474
+
475
+ | Space | Behavior | Best for |
476
+ |---|---|---|
477
+ | `'okhsl'` (default) | Perceptually uniform OKHSL interpolation | Design token derivation |
478
+ | `'srgb'` | Linear sRGB channel interpolation | Matching browser compositing |
479
+
480
+ The `space` option only affects opaque blending. Transparent blending always composites in linear sRGB (matching browser alpha compositing).
481
+
482
+ ### Contrast Solving
483
+
484
+ Mix colors support the same `contrast` prop as regular colors. The solver adjusts the mix ratio (opaque) or opacity (transparent) to meet the WCAG target:
485
+
486
+ ```ts
487
+ theme.colors({
488
+ surface: { lightness: 95 },
489
+ accent: { lightness: 30 },
490
+
491
+ // Ensure the mixed color has at least AA contrast against surface
492
+ tint: {
493
+ type: 'mix',
494
+ base: 'surface',
495
+ target: 'accent',
496
+ value: 10,
497
+ contrast: 'AA',
498
+ },
499
+
500
+ // Ensure the transparent overlay has at least 3:1 contrast
501
+ overlay: {
502
+ type: 'mix',
503
+ base: 'surface',
504
+ target: 'accent',
505
+ value: 5,
506
+ blend: 'transparent',
507
+ contrast: 3,
508
+ },
509
+ });
510
+ ```
511
+
512
+ ### High-Contrast Pairs
513
+
514
+ Both `value` and `contrast` support `[normal, highContrast]` pairs:
515
+
516
+ ```ts
517
+ theme.colors({
518
+ surface: { lightness: 95 },
519
+ accent: { lightness: 30 },
520
+
521
+ tint: {
522
+ type: 'mix',
523
+ base: 'surface',
524
+ target: 'accent',
525
+ value: [20, 40], // stronger mix in high-contrast mode
526
+ contrast: [3, 'AAA'], // stricter contrast in high-contrast mode
527
+ },
528
+ });
529
+ ```
530
+
531
+ ### Achromatic Colors
532
+
533
+ When mixing with achromatic colors (saturation near zero, e.g., white or black) in `okhsl` space, the hue comes from whichever color has saturation. This prevents meaningless hue artifacts and matches CSS `color-mix()` "missing component" behavior. For purely achromatic mixes, prefer `space: 'srgb'` where hue is irrelevant.
534
+
535
+ ### Mix Chaining
536
+
537
+ Mix colors can reference other mix colors, enabling multi-step derivations:
538
+
539
+ ```ts
540
+ theme.colors({
541
+ white: { lightness: 100, saturation: 0 },
542
+ black: { lightness: 0, saturation: 0 },
543
+ gray: { type: 'mix', base: 'white', target: 'black', value: 50, space: 'srgb' },
544
+ lightGray: { type: 'mix', base: 'white', target: 'gray', value: 50, space: 'srgb' },
545
+ });
546
+ ```
547
+
548
+ Mix colors cannot reference shadow colors (same restriction as regular dependent colors).
549
+
550
+ ## Output Formats
551
+
552
+ Control the color format in exports with the `format` option:
553
+
554
+ ```ts
555
+ // Default: OKHSL
556
+ theme.tokens(); // → 'okhsl(280 60% 97%)'
557
+
558
+ // RGB (modern space syntax, rounded integers)
559
+ theme.tokens({ format: 'rgb' }); // → 'rgb(244 240 250)'
560
+
561
+ // HSL (modern space syntax)
562
+ theme.tokens({ format: 'hsl' }); // → 'hsl(270.5 45.2% 95.8%)'
563
+
564
+ // OKLCH
565
+ theme.tokens({ format: 'oklch' }); // → 'oklch(0.965 0.0123 280)'
566
+ ```
567
+
568
+ The `format` option works on all export methods: `theme.tokens()`, `theme.tasty()`, `theme.json()`, `theme.css()`, `palette.tokens()`, `palette.tasty()`, `palette.json()`, `palette.css()`, and standalone `glaze.color().token()` / `.tasty()` / `.json()`.
569
+
570
+ Colors with `alpha < 1` (shadow colors, or regular colors with `opacity`) include an alpha component:
571
+
572
+ ```ts
573
+ // → 'oklch(0.15 0.009 282 / 0.1)'
574
+ // → 'rgb(34 28 42 / 0.1)'
575
+ ```
576
+
577
+ Available formats:
578
+
579
+ | Format | Output (alpha = 1) | Output (alpha < 1) | Notes |
580
+ |---|---|---|---|
581
+ | `'okhsl'` (default) | `okhsl(H S% L%)` | `okhsl(H S% L% / A)` | Native format, not a CSS function |
582
+ | `'rgb'` | `rgb(R G B)` | `rgb(R G B / A)` | Rounded integers, space syntax |
583
+ | `'hsl'` | `hsl(H S% L%)` | `hsl(H S% L% / A)` | Modern space syntax |
584
+ | `'oklch'` | `oklch(L C H)` | `oklch(L C H / A)` | OKLab-based LCH |
585
+
586
+ All numeric output strips trailing zeros for cleaner CSS (e.g., `95` not `95.0`).
587
+
588
+ ## Adaptation Modes
589
+
590
+ Modes control how colors adapt across schemes:
591
+
592
+ | Mode | Behavior |
593
+ |---|---|
594
+ | `'auto'` (default) | Full adaptation. Light ↔ dark inversion. High-contrast boost. |
595
+ | `'fixed'` | Color stays recognizable. Only safety corrections. For brand buttons, CTAs. |
596
+ | `'static'` | No adaptation. Same value in every scheme. |
597
+
598
+ ### How Relative Lightness Adapts
599
+
600
+ **`auto` mode** — relative lightness sign flips in dark scheme:
601
+
602
+ ```ts
603
+ // Light: surface L=97, text lightness='-52' → L=45 (dark text on light bg)
604
+ // Dark: surface inverts to L≈14, sign flips → L=14+52=66
605
+ // contrast solver may push further (light text on dark bg)
606
+ ```
607
+
608
+ **`fixed` mode** — lightness is mapped (not inverted), relative sign preserved:
609
+
610
+ ```ts
611
+ // Light: accent-fill L=52, accent-text lightness='+48' → L=100 (white on brand)
612
+ // Dark: accent-fill maps to L≈51.6, sign preserved → L≈99.6
613
+ ```
614
+
615
+ **`static` mode** — no adaptation, same value in every scheme.
616
+
617
+ ## Light Scheme Mapping
618
+
619
+ ### Lightness
620
+
621
+ Root color lightness is mapped linearly within the configured `lightLightness` window:
622
+
623
+ ```ts
624
+ const [lo, hi] = lightLightness; // default: [10, 100]
625
+ const mappedL = (lightness * (hi - lo)) / 100 + lo;
626
+ ```
627
+
628
+ Both `auto` and `fixed` modes use the same linear formula. `static` mode bypasses the mapping entirely.
629
+
630
+ | Color | Raw L | Mapped L (default [10, 100]) |
631
+ |---|---|---|
632
+ | surface (L=97) | 97 | 97.3 |
633
+ | accent-fill (L=52) | 52 | 56.8 |
634
+ | near-black (L=0) | 0 | 10 |
635
+
636
+ ## Dark Scheme Mapping
637
+
638
+ ### Lightness
639
+
640
+ **`auto`** — inverted within the configured window:
641
+
642
+ ```ts
643
+ const [lo, hi] = darkLightness; // default: [15, 95]
644
+ const invertedL = ((100 - lightness) * (hi - lo)) / 100 + lo;
645
+ ```
646
+
647
+ **`fixed`** — mapped without inversion:
648
+
649
+ ```ts
650
+ const mappedL = (lightness * (hi - lo)) / 100 + lo;
651
+ ```
652
+
653
+ | Color | Light L | Auto (inverted) | Fixed (mapped) |
654
+ |---|---|---|---|
655
+ | surface (L=97) | 97 | 17.4 | 92.6 |
656
+ | accent-fill (L=52) | 52 | 53.4 | 56.6 |
657
+ | accent-text (L=100) | 100 | 15 | 95 |
658
+
659
+ ### Saturation
660
+
661
+ `darkDesaturation` reduces saturation for all colors in dark scheme:
662
+
663
+ ```ts
664
+ S_dark = S_light * (1 - darkDesaturation) // default: 0.1
665
+ ```
666
+
667
+ ## Inherited Themes (`extend`)
668
+
669
+ `extend` creates a new theme inheriting all color definitions, replacing the hue and/or saturation seed:
670
+
671
+ ```ts
672
+ const primary = glaze(280, 80);
673
+ primary.colors({ /* ... */ });
674
+
675
+ const danger = primary.extend({ hue: 23 });
676
+ const success = primary.extend({ hue: 157 });
677
+ const warning = primary.extend({ hue: 84 });
678
+ ```
679
+
680
+ Override individual colors (additive merge):
681
+
682
+ ```ts
683
+ const danger = primary.extend({
684
+ hue: 23,
685
+ colors: { 'accent-fill': { lightness: 48, mode: 'fixed' } },
686
+ });
687
+ ```
688
+
689
+ ## Palette Composition
690
+
691
+ Combine multiple themes into a single palette:
692
+
693
+ ```ts
694
+ const palette = glaze.palette({ primary, danger, success, warning });
695
+ ```
696
+
697
+ ### Token Export
698
+
699
+ Tokens are grouped by scheme variant, with plain color names as keys:
700
+
701
+ ```ts
702
+ const tokens = palette.tokens({ prefix: true });
703
+ // → {
704
+ // light: { 'primary-surface': 'okhsl(...)', 'danger-surface': 'okhsl(...)' },
705
+ // dark: { 'primary-surface': 'okhsl(...)', 'danger-surface': 'okhsl(...)' },
706
+ // }
707
+ ```
708
+
709
+ Custom prefix mapping:
710
+
711
+ ```ts
712
+ palette.tokens({ prefix: { primary: 'brand-', danger: 'error-' } });
713
+ ```
714
+
715
+ ### Tasty Export (for [Tasty](https://cube-ui-kit.vercel.app/?path=/docs/tasty-documentation--docs) style system)
716
+
717
+ The `tasty()` method exports tokens in the [Tasty](https://cube-ui-kit.vercel.app/?path=/docs/tasty-documentation--docs) style-to-state binding format — `#name` color token keys with state aliases (`''`, `@dark`, etc.):
718
+
719
+ ```ts
720
+ const tastyTokens = palette.tasty({ prefix: true });
721
+ // → {
722
+ // '#primary-surface': { '': 'okhsl(...)', '@dark': 'okhsl(...)' },
723
+ // '#danger-surface': { '': 'okhsl(...)', '@dark': 'okhsl(...)' },
724
+ // }
725
+ ```
726
+
727
+ Apply as global styles to make color tokens available app-wide:
728
+
729
+ ```ts
730
+ import { useGlobalStyles } from '@cube-dev/ui-kit';
731
+
732
+ // In your root component
733
+ useGlobalStyles('body', tastyTokens);
734
+ ```
735
+
736
+ For zero-runtime builds, use `tastyStatic` to generate the CSS at build time:
737
+
738
+ ```ts
739
+ import { tastyStatic } from '@cube-dev/ui-kit';
740
+
741
+ tastyStatic('body', tastyTokens);
742
+ ```
743
+
744
+ Alternatively, register as a recipe via `configure()`:
745
+
746
+ ```ts
747
+ import { configure, tasty } from '@cube-dev/ui-kit';
748
+
749
+ configure({
750
+ recipes: {
751
+ 'all-themes': tastyTokens,
752
+ },
753
+ });
754
+
755
+ const Page = tasty({
756
+ styles: {
757
+ recipe: 'all-themes',
758
+ fill: '#primary-surface',
759
+ color: '#primary-text',
760
+ },
761
+ });
762
+ ```
763
+
764
+ Or spread directly into component styles:
765
+
766
+ ```ts
767
+ const Card = tasty({
768
+ styles: {
769
+ ...tastyTokens,
770
+ fill: '#primary-surface',
771
+ color: '#primary-text',
772
+ },
773
+ });
774
+ ```
775
+
776
+ Custom prefix mapping:
777
+
778
+ ```ts
779
+ palette.tasty({ prefix: { primary: 'brand-', danger: 'error-' } });
780
+ ```
781
+
782
+ Custom state aliases:
783
+
784
+ ```ts
785
+ palette.tasty({ states: { dark: '@dark', highContrast: '@hc' } });
786
+ ```
787
+
788
+ ### JSON Export (Framework-Agnostic)
789
+
790
+ ```ts
791
+ const data = palette.json({ prefix: true });
792
+ // → {
793
+ // primary: { surface: { light: 'okhsl(...)', dark: 'okhsl(...)' } },
794
+ // danger: { surface: { light: 'okhsl(...)', dark: 'okhsl(...)' } },
795
+ // }
796
+ ```
797
+
798
+ ### CSS Export
799
+
800
+ Export as CSS custom property declarations, grouped by scheme variant. Each variant is a string of `--name-color: value;` lines that you can wrap in your own selectors and media queries.
801
+
802
+ ```ts
803
+ const css = theme.css();
804
+ // css.light → "--surface-color: rgb(...);\n--text-color: rgb(...);"
805
+ // css.dark → "--surface-color: rgb(...);\n--text-color: rgb(...);"
806
+ // css.lightContrast → "--surface-color: rgb(...);\n--text-color: rgb(...);"
807
+ // css.darkContrast → "--surface-color: rgb(...);\n--text-color: rgb(...);"
808
+ ```
809
+
810
+ Use in a stylesheet:
811
+
812
+ ```ts
813
+ const css = palette.css({ prefix: true });
814
+
815
+ const stylesheet = `
816
+ :root { ${css.light} }
817
+ @media (prefers-color-scheme: dark) {
818
+ :root { ${css.dark} }
819
+ }
820
+ `;
821
+ ```
822
+
823
+ Options:
824
+
825
+ | Option | Default | Description |
826
+ |---|---|---|
827
+ | `format` | `'rgb'` | Color format (`'rgb'`, `'hsl'`, `'okhsl'`, `'oklch'`) |
828
+ | `suffix` | `'-color'` | Suffix appended to each CSS property name |
829
+ | `prefix` | — | (palette only) Same prefix behavior as `tokens()` |
830
+
831
+ ```ts
832
+ // Custom suffix
833
+ theme.css({ suffix: '' });
834
+ // → "--surface: rgb(...);"
835
+
836
+ // Custom format
837
+ theme.css({ format: 'hsl' });
838
+ // → "--surface-color: hsl(...);"
839
+
840
+ // Palette with prefix
841
+ palette.css({ prefix: true });
842
+ // → "--primary-surface-color: rgb(...);\n--danger-surface-color: rgb(...);"
843
+ ```
844
+
845
+ ## Output Modes
846
+
847
+ Control which scheme variants appear in exports:
848
+
849
+ ```ts
850
+ // Light only
851
+ palette.tokens({ modes: { dark: false, highContrast: false } });
852
+ // → { light: { ... } }
853
+
854
+ // Light + dark (default)
855
+ palette.tokens({ modes: { highContrast: false } });
856
+ // → { light: { ... }, dark: { ... } }
857
+
858
+ // All four variants
859
+ palette.tokens({ modes: { dark: true, highContrast: true } });
860
+ // → { light: { ... }, dark: { ... }, lightContrast: { ... }, darkContrast: { ... } }
861
+ ```
862
+
863
+ The `modes` option works the same way on `tokens()`, `tasty()`, `json()`, and `css()`.
864
+
865
+ Resolution priority (highest first):
866
+
867
+ 1. `tokens({ modes })` / `tasty({ modes })` / `json({ modes })` / `css({ ... })` — per-call override
868
+ 2. `glaze.configure({ modes })` — global config
869
+ 3. Built-in default: `{ dark: true, highContrast: false }`
870
+
871
+ ## Configuration
872
+
873
+ ```ts
874
+ glaze.configure({
875
+ lightLightness: [10, 100], // Light scheme lightness window [lo, hi]
876
+ darkLightness: [15, 95], // Dark scheme lightness window [lo, hi]
877
+ darkDesaturation: 0.1, // Saturation reduction in dark scheme (0–1)
878
+ states: {
879
+ dark: '@dark', // State alias for dark mode tokens
880
+ highContrast: '@high-contrast',
881
+ },
882
+ modes: {
883
+ dark: true, // Include dark variants in exports
884
+ highContrast: false, // Include high-contrast variants
885
+ },
886
+ shadowTuning: { // Default tuning for all shadow colors
887
+ alphaMax: 0.6,
888
+ bgHueBlend: 0.2,
889
+ },
890
+ });
891
+ ```
892
+
893
+ ## Color Definition Shape
894
+
895
+ `ColorDef` is a discriminated union of regular colors, shadow colors, and mix colors:
896
+
897
+ ```ts
898
+ type ColorDef = RegularColorDef | ShadowColorDef | MixColorDef;
899
+
900
+ interface RegularColorDef {
901
+ lightness?: HCPair<number | RelativeValue>;
902
+ saturation?: number;
903
+ hue?: number | RelativeValue;
904
+ base?: string;
905
+ contrast?: HCPair<MinContrast>;
906
+ mode?: 'auto' | 'fixed' | 'static';
907
+ opacity?: number; // fixed alpha (0–1)
908
+ }
909
+
910
+ interface ShadowColorDef {
911
+ type: 'shadow';
912
+ bg: string; // background color name (non-shadow)
913
+ fg?: string; // foreground color name (non-shadow)
914
+ intensity: HCPair<number>; // 0–100
915
+ tuning?: ShadowTuning;
916
+ }
917
+
918
+ interface MixColorDef {
919
+ type: 'mix';
920
+ base: string; // "from" color name
921
+ target: string; // "to" color name
922
+ value: HCPair<number>; // 0–100 (mix ratio or opacity)
923
+ blend?: 'opaque' | 'transparent'; // default: 'opaque'
924
+ space?: 'okhsl' | 'srgb'; // default: 'okhsl'
925
+ contrast?: HCPair<MinContrast>;
926
+ }
927
+ ```
928
+
929
+ A root color must have absolute `lightness` (a number). A dependent color must have `base`. Relative `lightness` (a string) requires `base`. Shadow colors use `type: 'shadow'` and must reference a non-shadow `bg` color. Mix colors use `type: 'mix'` and must reference two non-shadow colors.
930
+
931
+ ## Validation
932
+
933
+ | Condition | Behavior |
934
+ |---|---|
935
+ | `contrast` without `base` | Validation error |
936
+ | Relative `lightness` without `base` | Validation error |
937
+ | `lightness` resolves outside 0–100 | Clamp silently |
938
+ | `saturation` outside 0–1 | Clamp silently |
939
+ | Circular `base` references | Validation error |
940
+ | `base` references non-existent name | Validation error |
941
+ | Shadow `bg` references non-existent color | Validation error |
942
+ | Shadow `fg` references non-existent color | Validation error |
943
+ | Shadow `bg` references another shadow color | Validation error |
944
+ | Shadow `fg` references another shadow color | Validation error |
945
+ | Regular color `base` references a shadow color | Validation error |
946
+ | Shadow `intensity` outside 0–100 | Clamp silently |
947
+ | `contrast` + `opacity` combined | Warning |
948
+ | Mix `base` references non-existent color | Validation error |
949
+ | Mix `target` references non-existent color | Validation error |
950
+ | Mix `base` references a shadow color | Validation error |
951
+ | Mix `target` references a shadow color | Validation error |
952
+ | Mix `value` outside 0–100 | Clamp silently |
953
+ | Circular references involving mix colors | Validation error |
954
+
955
+ ## Advanced: Color Math Utilities
956
+
957
+ Glaze re-exports its internal color math for advanced use:
958
+
959
+ ```ts
960
+ import {
961
+ okhslToLinearSrgb,
962
+ okhslToSrgb,
963
+ okhslToOklab,
964
+ srgbToOkhsl,
965
+ parseHex,
966
+ relativeLuminanceFromLinearRgb,
967
+ contrastRatioFromLuminance,
968
+ formatOkhsl,
969
+ formatRgb,
970
+ formatHsl,
971
+ formatOklch,
972
+ findLightnessForContrast,
973
+ resolveMinContrast,
974
+ } from '@tenphi/glaze';
975
+ ```
976
+
977
+ ## Full Example
978
+
979
+ ```ts
980
+ import { glaze } from '@tenphi/glaze';
981
+
982
+ const primary = glaze(280, 80);
983
+
984
+ primary.colors({
985
+ surface: { lightness: 97, saturation: 0.75 },
986
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
987
+ border: { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' },
988
+ bg: { lightness: 97, saturation: 0.75 },
989
+ icon: { lightness: 60, saturation: 0.94 },
990
+ 'accent-fill': { lightness: 52, mode: 'fixed' },
991
+ 'accent-text': { base: 'accent-fill', lightness: '+48', contrast: 'AA', mode: 'fixed' },
992
+ disabled: { lightness: 81, saturation: 0.4 },
993
+
994
+ // Shadow colors — computed alpha, automatic dark-mode adaptation
995
+ 'shadow-sm': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 5 },
996
+ 'shadow-md': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 10 },
997
+ 'shadow-lg': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 20 },
998
+
999
+ // Mix colors — hover overlays and tints
1000
+ 'hover': { type: 'mix', base: 'surface', target: 'accent-fill', value: 8, blend: 'transparent' },
1001
+ 'tint': { type: 'mix', base: 'surface', target: 'accent-fill', value: 20 },
1002
+
1003
+ // Fixed-alpha overlay
1004
+ overlay: { lightness: 0, opacity: 0.5 },
1005
+ });
1006
+
1007
+ const danger = primary.extend({ hue: 23 });
1008
+ const success = primary.extend({ hue: 157 });
1009
+ const warning = primary.extend({ hue: 84 });
1010
+ const note = primary.extend({ hue: 302 });
1011
+
1012
+ const palette = glaze.palette({ primary, danger, success, warning, note });
1013
+
1014
+ // Export as flat token map grouped by variant
1015
+ const tokens = palette.tokens({ prefix: true });
1016
+ // tokens.light → { 'primary-surface': 'okhsl(...)', 'primary-shadow-md': 'okhsl(... / 0.1)' }
1017
+
1018
+ // Export as tasty style-to-state bindings (for Tasty style system)
1019
+ const tastyTokens = palette.tasty({ prefix: true });
1020
+
1021
+ // Export as CSS custom properties (rgb format by default)
1022
+ const css = palette.css({ prefix: true });
1023
+ // css.light → "--primary-surface-color: rgb(...);\n--primary-shadow-md-color: rgb(... / 0.1);"
1024
+
1025
+ // Standalone shadow computation
1026
+ const v = glaze.shadow({ bg: '#f0eef5', fg: '#1a1a2e', intensity: 10 });
1027
+ const shadowCss = glaze.format(v, 'oklch');
1028
+ // → 'oklch(0.15 0.014 280 / 0.1)'
1029
+
1030
+ // Save and restore a theme
1031
+ const snapshot = primary.export();
1032
+ const restored = glaze.from(snapshot);
1033
+
1034
+ // Create from an existing brand color
1035
+ const brand = glaze.fromHex('#7a4dbf');
1036
+ brand.colors({ surface: { lightness: 97 }, text: { base: 'surface', lightness: '-52' } });
1037
+ ```
1038
+
1039
+ ## API Reference
1040
+
1041
+ ### Theme Creation
1042
+
1043
+ | Method | Description |
1044
+ |---|---|
1045
+ | `glaze(hue, saturation?)` | Create a theme from hue (0–360) and saturation (0–100) |
1046
+ | `glaze({ hue, saturation })` | Create a theme from an options object |
1047
+ | `glaze.from(data)` | Create a theme from an exported configuration |
1048
+ | `glaze.fromHex(hex)` | Create a theme from a hex color (`#rgb` or `#rrggbb`) |
1049
+ | `glaze.fromRgb(r, g, b)` | Create a theme from RGB values (0–255) |
1050
+ | `glaze.color(input)` | Create a standalone color token |
1051
+ | `glaze.shadow(input)` | Compute a standalone shadow color (returns `ResolvedColorVariant`) |
1052
+ | `glaze.format(variant, format?)` | Format any `ResolvedColorVariant` as a CSS string |
1053
+
1054
+ ### Theme Methods
1055
+
1056
+ | Method | Description |
1057
+ |---|---|
1058
+ | `theme.colors(defs)` | Add/replace colors (additive merge) |
1059
+ | `theme.color(name)` | Get a color definition |
1060
+ | `theme.color(name, def)` | Set a single color definition |
1061
+ | `theme.remove(names)` | Remove one or more colors |
1062
+ | `theme.has(name)` | Check if a color is defined |
1063
+ | `theme.list()` | List all defined color names |
1064
+ | `theme.reset()` | Clear all color definitions |
1065
+ | `theme.export()` | Export configuration as JSON-safe object |
1066
+ | `theme.extend(options)` | Create a child theme |
1067
+ | `theme.resolve()` | Resolve all colors |
1068
+ | `theme.tokens(options?)` | Export as flat token map grouped by variant |
1069
+ | `theme.tasty(options?)` | Export as [Tasty](https://cube-ui-kit.vercel.app/?path=/docs/tasty-documentation--docs) style-to-state bindings |
1070
+ | `theme.json(options?)` | Export as plain JSON |
1071
+ | `theme.css(options?)` | Export as CSS custom property declarations |
1072
+
1073
+ ### Global Configuration
1074
+
1075
+ | Method | Description |
1076
+ |---|---|
1077
+ | `glaze.configure(config)` | Set global configuration |
1078
+ | `glaze.palette(themes)` | Compose themes into a palette |
1079
+ | `glaze.getConfig()` | Get current global config |
1080
+ | `glaze.resetConfig()` | Reset to defaults |
1081
+
1082
+ ## License
1083
+
1084
+ [MIT](LICENSE)