@eluan/theme-generator 0.3.0 → 0.4.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/README.md +43 -14
- package/dist/cli/generate.js +22 -3
- package/dist/color/darkLift.d.ts +23 -0
- package/dist/color/darkLift.js +61 -0
- package/dist/color/dim.d.ts +35 -0
- package/dist/color/dim.js +89 -0
- package/dist/compile/css.d.ts +13 -5
- package/dist/compile/css.js +72 -42
- package/dist/generate.js +46 -6
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -2
- package/dist/preview/smoke.d.ts +1 -1
- package/dist/preview/smoke.js +17 -10
- package/dist/tokens/contrast.d.ts +2 -2
- package/dist/tokens/contrast.js +38 -32
- package/dist/tokens/semanticMap.d.ts +17 -1
- package/dist/tokens/semanticMap.js +87 -9
- package/dist/types.d.ts +59 -4
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @eluan/theme-generator
|
|
2
2
|
|
|
3
|
-
CLI and library that generates accessible
|
|
3
|
+
CLI and library that generates accessible light, dim and dark mode [Eluan](https://github.com/vasfio/eluan) theme token sets from one to three accent colors. Contrast is checked (and auto-corrected) against a configurable minimum ratio, so generated themes are WCAG-aware out of the box.
|
|
4
4
|
|
|
5
5
|
The programmatic entry point (`@eluan/theme-generator`) is pure color math with no Node built-ins, so it runs in the browser as well as in Node. Only the `eluan-theme` binary touches the filesystem.
|
|
6
6
|
|
|
@@ -33,6 +33,9 @@ Options:
|
|
|
33
33
|
| `--out <dir>` | `./tokens` | Output directory |
|
|
34
34
|
| `--min-contrast <num>` | `4.5` | Minimum contrast ratio to enforce |
|
|
35
35
|
| `--neutral-tint <num>` | `0.04` | Neutral tint ratio |
|
|
36
|
+
| `--dark-lift <num>` | `0.08` | OKLCH lightness added to dark-mode colours, 0–0.2 |
|
|
37
|
+
| `--dim-chroma <num>` | `0.8` | Share of chroma dim-mode colours keep, 0.5–1 |
|
|
38
|
+
| `--dim-warmth <num>` | `1` | How far dim-mode greys move toward warm teak, 0–1 |
|
|
36
39
|
| `--config <path>` | — | Path to a JSON config file |
|
|
37
40
|
|
|
38
41
|
You can also drop a `eluan-theme.config.json` in your project root; CLI flags override its values.
|
|
@@ -41,7 +44,7 @@ You can also drop a `eluan-theme.config.json` in your project root; CLI flags ov
|
|
|
41
44
|
{ "name": "ocean", "accents": ["#FF4A2C"], "outDir": "./tokens" }
|
|
42
45
|
```
|
|
43
46
|
|
|
44
|
-
The generator writes primitive + semantic token JSON, theme CSS,
|
|
47
|
+
The generator writes primitive + semantic token JSON, theme CSS, a smoke-test preview HTML you can open to eyeball the result, and the two files for applying the theme in every mode: `custom-theme.css` (register it like a `createTheme()` result) and `create-theme.json` (`{ name, tokens, modes }` for `createTheme()`).
|
|
45
48
|
|
|
46
49
|
## Programmatic API
|
|
47
50
|
|
|
@@ -53,14 +56,38 @@ const result = generateTheme({
|
|
|
53
56
|
themeName: "ocean",
|
|
54
57
|
minContrast: 4.5,
|
|
55
58
|
neutralTintRatio: 0.04,
|
|
59
|
+
darkLift: 0.08, // optional; brighter dark-mode colours (0 turns it off)
|
|
60
|
+
dimChroma: 0.8, // optional; toned-down dim-mode colours (1 turns it off)
|
|
61
|
+
dimWarmth: 1, // optional; warm teak greys in dim mode (0 keeps brand greys)
|
|
56
62
|
})
|
|
57
63
|
|
|
58
|
-
result.
|
|
59
|
-
result.
|
|
64
|
+
result.customTheme // { name, extends, css }: pass to <EluanProvider customThemes={[…]} />
|
|
65
|
+
result.createThemeOptions // { tokens, modes: { dim, dark } }: spread into createTheme() from @eluan/core
|
|
60
66
|
result.report // markdown report: warnings, contrast corrections
|
|
67
|
+
result.css.theme // standalone CSS for every mode, scoped to [data-theme="ocean"]
|
|
68
|
+
result.createThemeTokens // light-mode token map only (kept for compatibility)
|
|
61
69
|
```
|
|
62
70
|
|
|
63
|
-
|
|
71
|
+
`customTheme` and `createThemeOptions` cover light, dim and dark mode and layer on top of `minimal`, so its fonts stay in place. They also set the base grey aliases (`--backgrounds-*`, `--foregrounds-*`) for each mode, so any CSS of your own that reads them matches the generated greys; components only read theme tokens. `css.theme` replaces the theme instead, so it drops `minimal`'s fonts; prefer the first two in apps that use `EluanProvider`.
|
|
72
|
+
|
|
73
|
+
### Contrast correction
|
|
74
|
+
|
|
75
|
+
When a foreground falls short of the minimum against its background, the generator moves it the fewest steps along its own ramp that pass: darker in light mode, lighter in dark mode, and in dim whichever way suits the ground. Brand-coloured text like the Outline and Ghost button labels stays as close to its brand step as contrast allows. Only when no step on the ramp passes does it fall back to neutral, white or black. Text that sits on more than one surface is checked against each, and the hardest decides: Outline and Ghost button text (`action-secondary-fg`, `action-tertiary-fg`) must clear the minimum on both the page and the panel ground. Every correction is listed in `result.report`.
|
|
76
|
+
|
|
77
|
+
### Brighter dark mode
|
|
78
|
+
|
|
79
|
+
Dark grounds swallow mid-tone colour, so dark mode resolves against `result.primitivesDark`: every colour ramp (accents and status hues, never neutral) lifted in OKLCH lightness by `darkLift`. The lift is full on steps 300–600 and tapers to nothing at 50 and 950, so surfaces and tints stay dark. Contrast is checked for every pair against the lifted ramp, and the report lists any corrections; on bright accents that usually means dark text on the primary button in dark mode. Light mode is unaffected.
|
|
80
|
+
|
|
81
|
+
### Toned-down dim mode
|
|
82
|
+
|
|
83
|
+
Dim is light mode with the volume down, built the way `minimal`'s own dim is. Every semantic token resolves one step deeper on its ramp than in light (page ground 50 → 100, status tint 100 → 200, status text 700 → 800; 950 stays put), against `result.primitivesDim`:
|
|
84
|
+
|
|
85
|
+
- **Colour ramps** (accents and status hues) keep `dimChroma` of the OKLCH chroma they have in light mode, 0.8 by default, so nothing shouts on the softer ground. Each dim step takes the chroma of its light counterpart, the step above, so tints get quieter rather than louder.
|
|
86
|
+
- **Greys** move `dimWarmth` of the way toward the hue and chroma of `minimal`'s teak ramp, 1 by default. Lightness stays on the theme's curve, so step contrast holds. At 0 dim keeps the brand-tinted greys.
|
|
87
|
+
|
|
88
|
+
Each entry's dim ref is pinned in the semantic map (`semanticMap[token].dim`) and gets its own contrast pass, so the report can list dim corrections too. Light and dark output don't change with either option.
|
|
89
|
+
|
|
90
|
+
Lower-level helpers are exported too — `generateScale`, `buildPrimitives`, `buildDarkPrimitives`, `buildDimPrimitives`, `buildSemanticMap`, `dimRef`, `checkContrast`, `autoCorrect`, `compileThemeCSS`, `compileCreateThemeTokens`, `compileCustomThemeCSS`, and the `hexToOklch` / `oklchToHex` color utilities. See the exported TypeScript types for the full shape of `GeneratorResult`.
|
|
64
91
|
|
|
65
92
|
## Use in the browser
|
|
66
93
|
|
|
@@ -69,26 +96,28 @@ The package entry pulls in nothing from Node — no `fs`, no `crypto` — so you
|
|
|
69
96
|
```tsx
|
|
70
97
|
"use client"
|
|
71
98
|
|
|
72
|
-
import { generateTheme
|
|
99
|
+
import { generateTheme } from "@eluan/theme-generator"
|
|
73
100
|
|
|
74
101
|
function applyTheme(accent: string) {
|
|
75
|
-
const
|
|
102
|
+
const { customTheme, createThemeOptions } = generateTheme({
|
|
76
103
|
accents: [accent],
|
|
77
104
|
themeName: "live",
|
|
78
105
|
minContrast: 4.5,
|
|
79
106
|
neutralTintRatio: 0.04,
|
|
80
107
|
})
|
|
81
108
|
|
|
82
|
-
// Option A —
|
|
109
|
+
// Option A — with EluanProvider: register it and switch to it
|
|
110
|
+
// <EluanProvider customThemes={[customTheme]} /> … setTheme("live")
|
|
111
|
+
|
|
112
|
+
// Option B — without the Provider: inject the CSS and set the attributes it targets
|
|
83
113
|
const style = document.createElement("style")
|
|
84
|
-
style.textContent =
|
|
114
|
+
style.textContent = customTheme.css
|
|
85
115
|
document.head.append(style)
|
|
86
|
-
document.documentElement.dataset.theme = "
|
|
116
|
+
document.documentElement.dataset.theme = "minimal"
|
|
117
|
+
document.documentElement.dataset.themeCustom = "live"
|
|
87
118
|
|
|
88
|
-
// Option
|
|
89
|
-
|
|
90
|
-
const dark = compileCreateThemeTokens(result.semanticMap, result.primitives, "dark")
|
|
91
|
-
return { light, dark }
|
|
119
|
+
// Option C — the token maps, for createTheme({ name: "live", ...createThemeOptions })
|
|
120
|
+
return createThemeOptions
|
|
92
121
|
}
|
|
93
122
|
```
|
|
94
123
|
|
package/dist/cli/generate.js
CHANGED
|
@@ -21,6 +21,9 @@ Options:
|
|
|
21
21
|
--out <dir> Output directory (default: ./tokens)
|
|
22
22
|
--min-contrast <num> Minimum contrast ratio (default: 4.5)
|
|
23
23
|
--neutral-tint <num> Neutral tint ratio (default: 0.04)
|
|
24
|
+
--dark-lift <num> Lightness added to dark-mode colours, 0-0.2 (default: 0.08)
|
|
25
|
+
--dim-chroma <num> Chroma kept in dim-mode colours, 0.5-1 (default: 0.8)
|
|
26
|
+
--dim-warmth <num> How far dim-mode greys move toward warm teak, 0-1 (default: 1)
|
|
24
27
|
--config <path> Path to config file (JSON)
|
|
25
28
|
--help Show this help
|
|
26
29
|
|
|
@@ -129,6 +132,9 @@ function parseCliArgs() {
|
|
|
129
132
|
out: { type: 'string' },
|
|
130
133
|
'min-contrast': { type: 'string' },
|
|
131
134
|
'neutral-tint': { type: 'string' },
|
|
135
|
+
'dark-lift': { type: 'string' },
|
|
136
|
+
'dim-chroma': { type: 'string' },
|
|
137
|
+
'dim-warmth': { type: 'string' },
|
|
132
138
|
config: { type: 'string' },
|
|
133
139
|
help: { type: 'boolean', default: false },
|
|
134
140
|
},
|
|
@@ -149,11 +155,20 @@ function parseCliArgs() {
|
|
|
149
155
|
const neutralTintRatio = values['neutral-tint']
|
|
150
156
|
? parseFloat(values['neutral-tint'])
|
|
151
157
|
: (config?.neutralTintRatio ?? 0.04);
|
|
158
|
+
const darkLift = values['dark-lift']
|
|
159
|
+
? parseFloat(values['dark-lift'])
|
|
160
|
+
: config?.darkLift;
|
|
161
|
+
const dimChroma = values['dim-chroma']
|
|
162
|
+
? parseFloat(values['dim-chroma'])
|
|
163
|
+
: config?.dimChroma;
|
|
164
|
+
const dimWarmth = values['dim-warmth']
|
|
165
|
+
? parseFloat(values['dim-warmth'])
|
|
166
|
+
: config?.dimWarmth;
|
|
152
167
|
const outDir = values.out ?? config?.outDir ?? './tokens';
|
|
153
168
|
if (accents.length === 0) {
|
|
154
169
|
return null;
|
|
155
170
|
}
|
|
156
|
-
return { accents, themeName, minContrast, neutralTintRatio, outDir };
|
|
171
|
+
return { accents, themeName, minContrast, neutralTintRatio, darkLift, dimChroma, dimWarmth, outDir };
|
|
157
172
|
}
|
|
158
173
|
function generate(opts) {
|
|
159
174
|
const { outDir, ...config } = opts;
|
|
@@ -166,14 +181,18 @@ function generate(opts) {
|
|
|
166
181
|
const result = generateTheme(config);
|
|
167
182
|
mkdirSync(resolvedOut, { recursive: true });
|
|
168
183
|
writeFileSync(join(resolvedOut, 'primitives.json'), compilePrimitivesJSON(result.primitives));
|
|
169
|
-
writeFileSync(join(resolvedOut, 'semantic.json'), compileSemanticJSON(result.semanticMap, result.primitives));
|
|
184
|
+
writeFileSync(join(resolvedOut, 'semantic.json'), compileSemanticJSON(result.semanticMap, result.primitives, result.primitivesDark, result.primitivesDim));
|
|
170
185
|
const fullCSS = [
|
|
171
186
|
result.css.primitives,
|
|
172
187
|
'',
|
|
173
188
|
result.css.theme,
|
|
174
189
|
].join('\n\n');
|
|
175
190
|
writeFileSync(join(resolvedOut, 'theme.css'), fullCSS);
|
|
176
|
-
|
|
191
|
+
// The dual-mode consumer path: register with EluanProvider's customThemes,
|
|
192
|
+
// or pass to createTheme({ name, ...options }) from @eluan/core
|
|
193
|
+
writeFileSync(join(resolvedOut, 'custom-theme.css'), result.customTheme.css);
|
|
194
|
+
writeFileSync(join(resolvedOut, 'create-theme.json'), JSON.stringify({ name: config.themeName, ...result.createThemeOptions }, null, 2));
|
|
195
|
+
const previewHTML = generateSmokeHTML(result.primitives, result.semanticMap, config.themeName, result.primitivesDark, result.primitivesDim);
|
|
177
196
|
writeFileSync(join(resolvedOut, 'preview.html'), previewHTML);
|
|
178
197
|
writeFileSync(join(resolvedOut, 'report.md'), result.report);
|
|
179
198
|
if (result.corrections.length > 0) {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PrimitiveTokens, Scale, ScaleStepValue } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Default OKLCH lightness added to the dark-mode colour ramps. Dark grounds
|
|
4
|
+
* swallow mid-tone colour, so the same step reads dimmer than it does on a
|
|
5
|
+
* light page; lifting the ramp keeps accents and status colours as vivid in
|
|
6
|
+
* dark mode as they look in light mode.
|
|
7
|
+
*/
|
|
8
|
+
export declare const DEFAULT_DARK_LIFT = 0.08;
|
|
9
|
+
/** Largest lift accepted; beyond this mid steps start to read as pastels. */
|
|
10
|
+
export declare const MAX_DARK_LIFT = 0.2;
|
|
11
|
+
/**
|
|
12
|
+
* Share of the lift each step gets: full on the mid steps people notice
|
|
13
|
+
* (buttons, borders, chart series), tapering to nothing at both ends so dark
|
|
14
|
+
* surfaces and tints (900–950) stay dark and the lightest steps don't clip
|
|
15
|
+
* to white.
|
|
16
|
+
*/
|
|
17
|
+
export declare const DARK_LIFT_WEIGHTS: Record<ScaleStepValue, number>;
|
|
18
|
+
export declare function liftScale(scale: Scale, lift: number): Scale;
|
|
19
|
+
/**
|
|
20
|
+
* The primitives dark mode resolves against: every colour ramp lifted by
|
|
21
|
+
* `lift`, neutral unchanged. A lift of 0 returns the light primitives as-is.
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildDarkPrimitives(primitives: PrimitiveTokens, lift: number): PrimitiveTokens;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { SCALE_STEPS } from '../types.js';
|
|
2
|
+
import { gamutMap, oklchToHex } from './convert.js';
|
|
3
|
+
/**
|
|
4
|
+
* Default OKLCH lightness added to the dark-mode colour ramps. Dark grounds
|
|
5
|
+
* swallow mid-tone colour, so the same step reads dimmer than it does on a
|
|
6
|
+
* light page; lifting the ramp keeps accents and status colours as vivid in
|
|
7
|
+
* dark mode as they look in light mode.
|
|
8
|
+
*/
|
|
9
|
+
export const DEFAULT_DARK_LIFT = 0.08;
|
|
10
|
+
/** Largest lift accepted; beyond this mid steps start to read as pastels. */
|
|
11
|
+
export const MAX_DARK_LIFT = 0.2;
|
|
12
|
+
/**
|
|
13
|
+
* Share of the lift each step gets: full on the mid steps people notice
|
|
14
|
+
* (buttons, borders, chart series), tapering to nothing at both ends so dark
|
|
15
|
+
* surfaces and tints (900–950) stay dark and the lightest steps don't clip
|
|
16
|
+
* to white.
|
|
17
|
+
*/
|
|
18
|
+
export const DARK_LIFT_WEIGHTS = {
|
|
19
|
+
50: 0,
|
|
20
|
+
100: 0.25,
|
|
21
|
+
200: 0.5,
|
|
22
|
+
300: 1,
|
|
23
|
+
400: 1,
|
|
24
|
+
500: 1,
|
|
25
|
+
600: 1,
|
|
26
|
+
700: 0.75,
|
|
27
|
+
800: 0.5,
|
|
28
|
+
900: 0.25,
|
|
29
|
+
950: 0,
|
|
30
|
+
};
|
|
31
|
+
/** The colour ramps that get lifted. Neutral stays put: it draws the dark grounds. */
|
|
32
|
+
const LIFTED = ['primary', 'secondary', 'tertiary', 'error', 'warning', 'success', 'info'];
|
|
33
|
+
/** Smallest lightness gap kept between neighbouring steps, so the ramp stays ordered. */
|
|
34
|
+
const MIN_STEP_GAP = 0.005;
|
|
35
|
+
export function liftScale(scale, lift) {
|
|
36
|
+
const lifted = {};
|
|
37
|
+
let previous = Infinity;
|
|
38
|
+
for (const step of SCALE_STEPS) {
|
|
39
|
+
const { oklch } = scale[step];
|
|
40
|
+
const l = Math.min(oklch.l + lift * DARK_LIFT_WEIGHTS[step], 0.98, previous - MIN_STEP_GAP);
|
|
41
|
+
const raw = { ...oklch, l };
|
|
42
|
+
lifted[step] = { oklch: gamutMap(raw), hex: oklchToHex(raw) };
|
|
43
|
+
previous = l;
|
|
44
|
+
}
|
|
45
|
+
return lifted;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The primitives dark mode resolves against: every colour ramp lifted by
|
|
49
|
+
* `lift`, neutral unchanged. A lift of 0 returns the light primitives as-is.
|
|
50
|
+
*/
|
|
51
|
+
export function buildDarkPrimitives(primitives, lift) {
|
|
52
|
+
if (lift === 0)
|
|
53
|
+
return primitives;
|
|
54
|
+
const dark = { ...primitives };
|
|
55
|
+
for (const name of LIFTED) {
|
|
56
|
+
const scale = primitives[name];
|
|
57
|
+
if (scale)
|
|
58
|
+
dark[name] = liftScale(scale, lift);
|
|
59
|
+
}
|
|
60
|
+
return dark;
|
|
61
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { PrimitiveTokens, Scale, ScaleStepValue } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Default chroma multiplier for the dim-mode colour ramps. Dim is light mode
|
|
4
|
+
* toned down: the same steps, one deeper, with accents and status colours
|
|
5
|
+
* pulled back so nothing shouts on the softer ground.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DEFAULT_DIM_CHROMA = 0.8;
|
|
8
|
+
/** Lowest chroma multiplier accepted; below this accents turn muddy. */
|
|
9
|
+
export declare const MIN_DIM_CHROMA = 0.5;
|
|
10
|
+
/** Default pull of the dim greys toward teak. 1 matches `minimal`'s dim; 0 keeps the brand-tinted greys. */
|
|
11
|
+
export declare const DEFAULT_DIM_WARMTH = 1;
|
|
12
|
+
/**
|
|
13
|
+
* Hue and chroma of `minimal`'s teak ramp (packages/tokens/src/primitives.css),
|
|
14
|
+
* the warm greys its dim mode draws on. Only hue and chroma are borrowed:
|
|
15
|
+
* lightness stays on the theme's curve so step contrast is unchanged.
|
|
16
|
+
*/
|
|
17
|
+
export declare const TEAK_HUE_CHROMA: Record<ScaleStepValue, {
|
|
18
|
+
h: number;
|
|
19
|
+
c: number;
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Dim refs sit one step deeper than light, and chroma climbs steeply toward
|
|
23
|
+
* the mid steps, so scaling each step's own chroma would still leave dim tints
|
|
24
|
+
* louder than light's (100 → 200 nearly doubles it). Each step instead takes
|
|
25
|
+
* the chroma of the step above, its light counterpart, times `factor`: every
|
|
26
|
+
* dim colour keeps `factor` of the saturation it had in light mode.
|
|
27
|
+
*/
|
|
28
|
+
export declare function toneDownScale(scale: Scale, factor: number): Scale;
|
|
29
|
+
export declare function warmScale(scale: Scale, warmth: number): Scale;
|
|
30
|
+
/**
|
|
31
|
+
* The primitives dim mode resolves against: every colour ramp toned down to
|
|
32
|
+
* `chroma` of its light-mode saturation, and neutral pulled `warmth` of the way toward teak's hue and
|
|
33
|
+
* chroma. With chroma 1 and warmth 0 it returns the light primitives as-is.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildDimPrimitives(primitives: PrimitiveTokens, chroma: number, warmth: number): PrimitiveTokens;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { SCALE_STEPS } from '../types.js';
|
|
2
|
+
import { gamutMap, oklchToHex } from './convert.js';
|
|
3
|
+
/**
|
|
4
|
+
* Default chroma multiplier for the dim-mode colour ramps. Dim is light mode
|
|
5
|
+
* toned down: the same steps, one deeper, with accents and status colours
|
|
6
|
+
* pulled back so nothing shouts on the softer ground.
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_DIM_CHROMA = 0.8;
|
|
9
|
+
/** Lowest chroma multiplier accepted; below this accents turn muddy. */
|
|
10
|
+
export const MIN_DIM_CHROMA = 0.5;
|
|
11
|
+
/** Default pull of the dim greys toward teak. 1 matches `minimal`'s dim; 0 keeps the brand-tinted greys. */
|
|
12
|
+
export const DEFAULT_DIM_WARMTH = 1;
|
|
13
|
+
/**
|
|
14
|
+
* Hue and chroma of `minimal`'s teak ramp (packages/tokens/src/primitives.css),
|
|
15
|
+
* the warm greys its dim mode draws on. Only hue and chroma are borrowed:
|
|
16
|
+
* lightness stays on the theme's curve so step contrast is unchanged.
|
|
17
|
+
*/
|
|
18
|
+
export const TEAK_HUE_CHROMA = {
|
|
19
|
+
50: { h: 100.82, c: 0.0106 },
|
|
20
|
+
100: { h: 102.89, c: 0.0302 },
|
|
21
|
+
200: { h: 96.16, c: 0.0556 },
|
|
22
|
+
300: { h: 93.94, c: 0.0781 },
|
|
23
|
+
400: { h: 89.04, c: 0.0868 },
|
|
24
|
+
500: { h: 81.36, c: 0.0805 },
|
|
25
|
+
600: { h: 74.01, c: 0.0721 },
|
|
26
|
+
700: { h: 65.88, c: 0.0617 },
|
|
27
|
+
800: { h: 58.2, c: 0.0515 },
|
|
28
|
+
900: { h: 56.46, c: 0.043 },
|
|
29
|
+
950: { h: 53.07, c: 0.0303 },
|
|
30
|
+
};
|
|
31
|
+
/** The colour ramps that get toned down. Neutral is warmed instead. */
|
|
32
|
+
const TONED = ['primary', 'secondary', 'tertiary', 'error', 'warning', 'success', 'info'];
|
|
33
|
+
function entry(raw) {
|
|
34
|
+
return { oklch: gamutMap(raw), hex: oklchToHex(raw) };
|
|
35
|
+
}
|
|
36
|
+
/** Hue `t` of the way from `from` to `to`, along the shorter arc. */
|
|
37
|
+
function mixHue(from, to, t) {
|
|
38
|
+
const delta = ((to - from + 540) % 360) - 180;
|
|
39
|
+
return (from + delta * t + 360) % 360;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Dim refs sit one step deeper than light, and chroma climbs steeply toward
|
|
43
|
+
* the mid steps, so scaling each step's own chroma would still leave dim tints
|
|
44
|
+
* louder than light's (100 → 200 nearly doubles it). Each step instead takes
|
|
45
|
+
* the chroma of the step above, its light counterpart, times `factor`: every
|
|
46
|
+
* dim colour keeps `factor` of the saturation it had in light mode.
|
|
47
|
+
*/
|
|
48
|
+
export function toneDownScale(scale, factor) {
|
|
49
|
+
const toned = {};
|
|
50
|
+
SCALE_STEPS.forEach((step, i) => {
|
|
51
|
+
const counterpart = scale[SCALE_STEPS[Math.max(i - 1, 0)]];
|
|
52
|
+
toned[step] = entry({ ...scale[step].oklch, c: counterpart.oklch.c * factor });
|
|
53
|
+
});
|
|
54
|
+
return toned;
|
|
55
|
+
}
|
|
56
|
+
export function warmScale(scale, warmth) {
|
|
57
|
+
const warmed = {};
|
|
58
|
+
for (const step of SCALE_STEPS) {
|
|
59
|
+
const { oklch } = scale[step];
|
|
60
|
+
const teak = TEAK_HUE_CHROMA[step];
|
|
61
|
+
warmed[step] = entry({
|
|
62
|
+
...oklch,
|
|
63
|
+
h: mixHue(oklch.h, teak.h, warmth),
|
|
64
|
+
c: oklch.c + (teak.c - oklch.c) * warmth,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return warmed;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The primitives dim mode resolves against: every colour ramp toned down to
|
|
71
|
+
* `chroma` of its light-mode saturation, and neutral pulled `warmth` of the way toward teak's hue and
|
|
72
|
+
* chroma. With chroma 1 and warmth 0 it returns the light primitives as-is.
|
|
73
|
+
*/
|
|
74
|
+
export function buildDimPrimitives(primitives, chroma, warmth) {
|
|
75
|
+
if (chroma === 1 && warmth === 0)
|
|
76
|
+
return primitives;
|
|
77
|
+
const dim = { ...primitives };
|
|
78
|
+
if (chroma !== 1) {
|
|
79
|
+
for (const name of TONED) {
|
|
80
|
+
const scale = primitives[name];
|
|
81
|
+
if (scale)
|
|
82
|
+
dim[name] = toneDownScale(scale, chroma);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (warmth !== 0 && primitives.neutral) {
|
|
86
|
+
dim.neutral = warmScale(primitives.neutral, warmth);
|
|
87
|
+
}
|
|
88
|
+
return dim;
|
|
89
|
+
}
|
package/dist/compile/css.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import type { PrimitiveTokens, SemanticMap } from '../types.js';
|
|
1
|
+
import type { Mode, PrimitiveTokens, SemanticMap } from '../types.js';
|
|
2
2
|
export declare function compilePrimitivesJSON(primitives: PrimitiveTokens): string;
|
|
3
|
-
export declare function compileSemanticJSON(map: SemanticMap, primitives: PrimitiveTokens): string;
|
|
3
|
+
export declare function compileSemanticJSON(map: SemanticMap, primitives: PrimitiveTokens, darkPrimitives?: PrimitiveTokens, dimPrimitives?: PrimitiveTokens): string;
|
|
4
4
|
export declare function compilePrimitivesCSS(primitives: PrimitiveTokens, themeName: string): string;
|
|
5
|
-
export declare function compileModeCSS(map: SemanticMap, primitives: PrimitiveTokens, mode:
|
|
6
|
-
export declare function compileThemeCSS(map: SemanticMap, primitives: PrimitiveTokens, themeName: string, inputHash: string): string;
|
|
7
|
-
export declare function compileCreateThemeTokens(map: SemanticMap, primitives: PrimitiveTokens, mode:
|
|
5
|
+
export declare function compileModeCSS(map: SemanticMap, primitives: PrimitiveTokens, mode: Mode, themeName: string): string;
|
|
6
|
+
export declare function compileThemeCSS(map: SemanticMap, primitives: PrimitiveTokens, themeName: string, inputHash: string, darkPrimitives?: PrimitiveTokens, dimPrimitives?: PrimitiveTokens): string;
|
|
7
|
+
export declare function compileCreateThemeTokens(map: SemanticMap, primitives: PrimitiveTokens, mode: Mode): Record<string, string>;
|
|
8
|
+
/**
|
|
9
|
+
* CSS for a custom theme layered on `minimal`, in the same shape `createTheme()`
|
|
10
|
+
* from @eluan/core emits: the light tokens on `[data-theme-custom="<name>"]`
|
|
11
|
+
* and the dim and dark tokens on that plus `[data-mode]`. Because the theme
|
|
12
|
+
* sits on top of `minimal` instead of replacing it, `minimal`'s fonts stay in
|
|
13
|
+
* place. Without `dim`, dim mode uses the light tokens.
|
|
14
|
+
*/
|
|
15
|
+
export declare function compileCustomThemeCSS(themeName: string, light: Record<string, string>, dark: Record<string, string>, dim?: Record<string, string>): string;
|
package/dist/compile/css.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { modeRef } from '../tokens/semanticMap.js';
|
|
1
2
|
const SCALE_STEPS = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
|
|
2
3
|
function resolveHex(ref, primitives) {
|
|
3
4
|
if (ref === 'white')
|
|
@@ -41,17 +42,22 @@ export function compilePrimitivesJSON(primitives) {
|
|
|
41
42
|
}
|
|
42
43
|
return JSON.stringify(doc, null, 2);
|
|
43
44
|
}
|
|
44
|
-
export function compileSemanticJSON(map, primitives) {
|
|
45
|
+
export function compileSemanticJSON(map, primitives, darkPrimitives = primitives, dimPrimitives = primitives) {
|
|
45
46
|
const doc = {};
|
|
46
47
|
for (const [token, entry] of Object.entries(map)) {
|
|
48
|
+
const dim = modeRef(entry, 'dim');
|
|
47
49
|
doc[token] = {
|
|
48
50
|
$type: 'color',
|
|
49
51
|
light: {
|
|
50
52
|
$value: resolveHex(entry.light, primitives),
|
|
51
53
|
$ref: entry.light,
|
|
52
54
|
},
|
|
55
|
+
dim: {
|
|
56
|
+
$value: resolveHex(dim, dimPrimitives),
|
|
57
|
+
$ref: dim,
|
|
58
|
+
},
|
|
53
59
|
dark: {
|
|
54
|
-
$value: resolveHex(entry.dark,
|
|
60
|
+
$value: resolveHex(entry.dark, darkPrimitives),
|
|
55
61
|
$ref: entry.dark,
|
|
56
62
|
},
|
|
57
63
|
role: entry.role,
|
|
@@ -80,21 +86,35 @@ export function compilePrimitivesCSS(primitives, themeName) {
|
|
|
80
86
|
}
|
|
81
87
|
return `/* Generated by eluan-theme-generator — ${themeName} primitives */\n\n[data-theme="${themeName}"] {\n${blocks.join('\n')}\n}`;
|
|
82
88
|
}
|
|
89
|
+
// Dim is light one step deeper throughout, matching dimRef()
|
|
90
|
+
const PALETTE_STEPS = {
|
|
91
|
+
light: { main: 500, background: 50, tint: 100, foreground: 700, shade: 800 },
|
|
92
|
+
dim: { main: 600, background: 100, tint: 200, foreground: 800, shade: 900 },
|
|
93
|
+
dark: { main: 400, background: 950, tint: 900, foreground: 200, shade: 300 },
|
|
94
|
+
};
|
|
95
|
+
const NEUTRAL_STEPS = {
|
|
96
|
+
light: {
|
|
97
|
+
bg: { primary: 50, secondary: 100, tertiary: 200, quaternary: 300, quinary: 400 },
|
|
98
|
+
fg: { primary: 950, secondary: 800, tertiary: 600, quaternary: 500, quinary: 400 },
|
|
99
|
+
},
|
|
100
|
+
dim: {
|
|
101
|
+
bg: { primary: 100, secondary: 200, tertiary: 300, quaternary: 400, quinary: 500 },
|
|
102
|
+
fg: { primary: 950, secondary: 900, tertiary: 700, quaternary: 600, quinary: 500 },
|
|
103
|
+
},
|
|
104
|
+
dark: {
|
|
105
|
+
bg: { primary: 800, secondary: 900, tertiary: 950, quaternary: 700, quinary: 600 },
|
|
106
|
+
fg: { primary: 50, secondary: 100, tertiary: 200, quaternary: 300, quinary: 400 },
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
const MODE_LABELS = { light: 'Light', dim: 'Dim', dark: 'Dark' };
|
|
83
110
|
function compileModeCSSBlock(map, primitives, mode, scaleName, cssPrefix) {
|
|
84
111
|
const scale = primitives[scaleName];
|
|
85
112
|
if (!scale)
|
|
86
113
|
return '';
|
|
87
114
|
const lines = [];
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const fgStep = mode === 'light' ? 700 : 200;
|
|
92
|
-
const shadeStep = mode === 'light' ? 800 : 300;
|
|
93
|
-
lines.push(` --${cssPrefix}-main: ${scale[mainStep].hex};`);
|
|
94
|
-
lines.push(` --${cssPrefix}-background: ${scale[bgStep].hex};`);
|
|
95
|
-
lines.push(` --${cssPrefix}-tint: ${scale[tintStep].hex};`);
|
|
96
|
-
lines.push(` --${cssPrefix}-foreground: ${scale[fgStep].hex};`);
|
|
97
|
-
lines.push(` --${cssPrefix}-shade: ${scale[shadeStep].hex};`);
|
|
115
|
+
for (const [role, step] of Object.entries(PALETTE_STEPS[mode])) {
|
|
116
|
+
lines.push(` --${cssPrefix}-${role}: ${scale[step].hex};`);
|
|
117
|
+
}
|
|
98
118
|
return lines.join('\n');
|
|
99
119
|
}
|
|
100
120
|
export function compileModeCSS(map, primitives, mode, themeName) {
|
|
@@ -102,19 +122,17 @@ export function compileModeCSS(map, primitives, mode, themeName) {
|
|
|
102
122
|
const neutral = primitives.neutral;
|
|
103
123
|
if (!neutral)
|
|
104
124
|
throw new Error('Neutral scale is required');
|
|
105
|
-
const bgSteps = mode
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
? { primary: 950, secondary: 800, tertiary: 600, quaternary: 500, quinary: 400 }
|
|
110
|
-
: { primary: 50, secondary: 100, tertiary: 200, quaternary: 300, quinary: 400 };
|
|
125
|
+
const { bg: bgSteps, fg: fgSteps } = NEUTRAL_STEPS[mode];
|
|
126
|
+
// The semantic map's entries win when present, so this matches css.theme
|
|
127
|
+
// and the custom theme even after a contrast correction moves one.
|
|
128
|
+
const ramp = (token, step) => map[token] ? resolveHex(modeRef(map[token], mode), primitives) : neutral[step].hex;
|
|
111
129
|
lines.push(` /* Backgrounds */`);
|
|
112
130
|
for (const [level, step] of Object.entries(bgSteps)) {
|
|
113
|
-
lines.push(` --backgrounds-${level}: ${
|
|
131
|
+
lines.push(` --backgrounds-${level}: ${ramp(`backgrounds-${level}`, step)};`);
|
|
114
132
|
}
|
|
115
133
|
lines.push(`\n /* Foregrounds */`);
|
|
116
134
|
for (const [level, step] of Object.entries(fgSteps)) {
|
|
117
|
-
lines.push(` --foregrounds-${level}: ${
|
|
135
|
+
lines.push(` --foregrounds-${level}: ${ramp(`foregrounds-${level}`, step)};`);
|
|
118
136
|
}
|
|
119
137
|
const paletteScales = [
|
|
120
138
|
['primary', 'accent'],
|
|
@@ -130,39 +148,51 @@ export function compileModeCSS(map, primitives, mode, themeName) {
|
|
|
130
148
|
lines.push(block);
|
|
131
149
|
}
|
|
132
150
|
}
|
|
133
|
-
|
|
134
|
-
return `/* Generated by eluan-theme-generator — ${themeName} ${mode} mode */\n\n[data-theme="${themeName}"][data-mode="${mode}"],\n[data-theme="${themeName}"][data-mode="${modeLabel}"] {\n${lines.join('\n')}\n}`;
|
|
151
|
+
return `/* Generated by eluan-theme-generator — ${themeName} ${mode} mode */\n\n[data-theme="${themeName}"][data-mode="${mode}"],\n[data-theme="${themeName}"][data-mode="${MODE_LABELS[mode]}"] {\n${lines.join('\n')}\n}`;
|
|
135
152
|
}
|
|
136
|
-
export function compileThemeCSS(map, primitives, themeName, inputHash) {
|
|
153
|
+
export function compileThemeCSS(map, primitives, themeName, inputHash, darkPrimitives = primitives, dimPrimitives = primitives) {
|
|
137
154
|
const lines = [];
|
|
138
155
|
lines.push(`/* Generated by eluan-theme-generator v0.1.0 */`);
|
|
139
156
|
lines.push(`/* Input hash: ${inputHash} */`);
|
|
140
157
|
lines.push(`/* Theme: ${themeName} */`);
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
158
|
+
const sources = { light: primitives, dim: dimPrimitives, dark: darkPrimitives };
|
|
159
|
+
for (const mode of ['light', 'dim', 'dark']) {
|
|
160
|
+
const tokenLines = [];
|
|
161
|
+
for (const [token, entry] of Object.entries(map)) {
|
|
162
|
+
const ref = modeRef(entry, mode);
|
|
163
|
+
tokenLines.push(` --${token}: ${resolveHex(ref, sources[mode])}; /* ${ref} */`);
|
|
164
|
+
}
|
|
165
|
+
lines.push('');
|
|
166
|
+
lines.push(`[data-theme="${themeName}"][data-mode="${mode}"],`);
|
|
167
|
+
lines.push(`[data-theme="${themeName}"][data-mode="${MODE_LABELS[mode]}"] {`);
|
|
168
|
+
lines.push(tokenLines.join('\n'));
|
|
169
|
+
lines.push('}');
|
|
149
170
|
}
|
|
150
|
-
lines.push(`[data-theme="${themeName}"][data-mode="light"],`);
|
|
151
|
-
lines.push(`[data-theme="${themeName}"][data-mode="Light"] {`);
|
|
152
|
-
lines.push(lightLines.join('\n'));
|
|
153
|
-
lines.push('}');
|
|
154
|
-
lines.push('');
|
|
155
|
-
lines.push(`[data-theme="${themeName}"][data-mode="dark"],`);
|
|
156
|
-
lines.push(`[data-theme="${themeName}"][data-mode="Dark"] {`);
|
|
157
|
-
lines.push(darkLines.join('\n'));
|
|
158
|
-
lines.push('}');
|
|
159
171
|
return lines.join('\n');
|
|
160
172
|
}
|
|
161
173
|
export function compileCreateThemeTokens(map, primitives, mode) {
|
|
162
174
|
const tokens = {};
|
|
163
175
|
for (const [token, entry] of Object.entries(map)) {
|
|
164
|
-
|
|
165
|
-
tokens[`--${token}`] = resolveHex(ref, primitives);
|
|
176
|
+
tokens[`--${token}`] = resolveHex(modeRef(entry, mode), primitives);
|
|
166
177
|
}
|
|
167
178
|
return tokens;
|
|
168
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* CSS for a custom theme layered on `minimal`, in the same shape `createTheme()`
|
|
182
|
+
* from @eluan/core emits: the light tokens on `[data-theme-custom="<name>"]`
|
|
183
|
+
* and the dim and dark tokens on that plus `[data-mode]`. Because the theme
|
|
184
|
+
* sits on top of `minimal` instead of replacing it, `minimal`'s fonts stay in
|
|
185
|
+
* place. Without `dim`, dim mode uses the light tokens.
|
|
186
|
+
*/
|
|
187
|
+
export function compileCustomThemeCSS(themeName, light, dark, dim) {
|
|
188
|
+
const name = themeName.replace(/["\\]/g, '\\$&');
|
|
189
|
+
const body = (tokens) => Object.entries(tokens)
|
|
190
|
+
.map(([prop, value]) => ` ${prop}: ${value};`)
|
|
191
|
+
.join('\n');
|
|
192
|
+
const modeBlock = (mode, tokens) => `[data-theme-custom="${name}"][data-mode="${mode}"],\n[data-theme-custom="${name}"][data-mode="${MODE_LABELS[mode]}"] {\n${body(tokens)}\n}`;
|
|
193
|
+
return [
|
|
194
|
+
`[data-theme-custom="${name}"] {\n${body(light)}\n}`,
|
|
195
|
+
...(dim ? [modeBlock('dim', dim)] : []),
|
|
196
|
+
modeBlock('dark', dark),
|
|
197
|
+
].join('\n\n');
|
|
198
|
+
}
|
package/dist/generate.js
CHANGED
|
@@ -2,7 +2,9 @@ import { validateAccents } from './input/validate.js';
|
|
|
2
2
|
import { buildPrimitives } from './tokens/primitives.js';
|
|
3
3
|
import { buildSemanticMap } from './tokens/semanticMap.js';
|
|
4
4
|
import { autoCorrect } from './tokens/contrast.js';
|
|
5
|
-
import {
|
|
5
|
+
import { buildDarkPrimitives, DEFAULT_DARK_LIFT, MAX_DARK_LIFT } from './color/darkLift.js';
|
|
6
|
+
import { buildDimPrimitives, DEFAULT_DIM_CHROMA, DEFAULT_DIM_WARMTH, MIN_DIM_CHROMA } from './color/dim.js';
|
|
7
|
+
import { compilePrimitivesCSS, compileModeCSS, compileThemeCSS, compileCreateThemeTokens, compileCustomThemeCSS, } from './compile/css.js';
|
|
6
8
|
/*
|
|
7
9
|
* FNV-1a (64-bit) constants.
|
|
8
10
|
*
|
|
@@ -45,6 +47,10 @@ export function computeInputHash(config) {
|
|
|
45
47
|
neutralTintRatio: config.neutralTintRatio,
|
|
46
48
|
systemHues: config.systemHues,
|
|
47
49
|
curve: config.curve,
|
|
50
|
+
// Only when set, so configs that predate these options keep their fingerprint
|
|
51
|
+
darkLift: config.darkLift,
|
|
52
|
+
dimChroma: config.dimChroma,
|
|
53
|
+
dimWarmth: config.dimWarmth,
|
|
48
54
|
});
|
|
49
55
|
// Take the high 48 bits: FNV-1a mixes upward through the multiply, so the high
|
|
50
56
|
// nibbles are the better-distributed end of the digest.
|
|
@@ -59,6 +65,9 @@ function buildReport(config, warnings, corrections, primitives, inputHash) {
|
|
|
59
65
|
lines.push(`**Accents:** ${config.accents.join(', ')}`);
|
|
60
66
|
lines.push(`**Min contrast:** ${config.minContrast}:1`);
|
|
61
67
|
lines.push(`**Neutral tint ratio:** ${config.neutralTintRatio}`);
|
|
68
|
+
lines.push(`**Dark lift:** ${config.darkLift ?? DEFAULT_DARK_LIFT}`);
|
|
69
|
+
lines.push(`**Dim chroma:** ${config.dimChroma ?? DEFAULT_DIM_CHROMA}`);
|
|
70
|
+
lines.push(`**Dim warmth:** ${config.dimWarmth ?? DEFAULT_DIM_WARMTH}`);
|
|
62
71
|
lines.push('');
|
|
63
72
|
if (warnings.length > 0) {
|
|
64
73
|
lines.push(`## Warnings`);
|
|
@@ -107,7 +116,7 @@ function buildReport(config, warnings, corrections, primitives, inputHash) {
|
|
|
107
116
|
lines.push(`## Output Files`);
|
|
108
117
|
lines.push('');
|
|
109
118
|
lines.push(`- \`primitives.json\` — W3C Design Tokens format`);
|
|
110
|
-
lines.push(`- \`semantic.json\` — Semantic map with light/dark refs`);
|
|
119
|
+
lines.push(`- \`semantic.json\` — Semantic map with light/dim/dark refs`);
|
|
111
120
|
lines.push(`- \`theme.css\` — Drop-in CSS for Eluan`);
|
|
112
121
|
lines.push(`- \`preview.html\` — Visual smoke test`);
|
|
113
122
|
return lines.join('\n');
|
|
@@ -116,35 +125,66 @@ export function generateTheme(config) {
|
|
|
116
125
|
const { accents: accentInputs, warnings } = validateAccents(config.accents);
|
|
117
126
|
const inputHash = computeInputHash(config);
|
|
118
127
|
const primitives = buildPrimitives(accentInputs, config);
|
|
128
|
+
const darkLift = config.darkLift ?? DEFAULT_DARK_LIFT;
|
|
129
|
+
if (!Number.isFinite(darkLift) || darkLift < 0 || darkLift > MAX_DARK_LIFT) {
|
|
130
|
+
throw new Error(`darkLift must be between 0 and ${MAX_DARK_LIFT}, got ${config.darkLift}.`);
|
|
131
|
+
}
|
|
132
|
+
const primitivesDark = buildDarkPrimitives(primitives, darkLift);
|
|
133
|
+
const dimChroma = config.dimChroma ?? DEFAULT_DIM_CHROMA;
|
|
134
|
+
if (!Number.isFinite(dimChroma) || dimChroma < MIN_DIM_CHROMA || dimChroma > 1) {
|
|
135
|
+
throw new Error(`dimChroma must be between ${MIN_DIM_CHROMA} and 1, got ${config.dimChroma}.`);
|
|
136
|
+
}
|
|
137
|
+
const dimWarmth = config.dimWarmth ?? DEFAULT_DIM_WARMTH;
|
|
138
|
+
if (!Number.isFinite(dimWarmth) || dimWarmth < 0 || dimWarmth > 1) {
|
|
139
|
+
throw new Error(`dimWarmth must be between 0 and 1, got ${config.dimWarmth}.`);
|
|
140
|
+
}
|
|
141
|
+
const primitivesDim = buildDimPrimitives(primitives, dimChroma, dimWarmth);
|
|
119
142
|
let semanticMap = buildSemanticMap();
|
|
120
143
|
const lightResult = autoCorrect(semanticMap, primitives, 'light', config.minContrast);
|
|
121
144
|
semanticMap = lightResult.correctedMap;
|
|
122
145
|
const lightCorrections = lightResult.corrections;
|
|
123
|
-
const
|
|
146
|
+
const dimResult = autoCorrect(semanticMap, primitivesDim, 'dim', config.minContrast);
|
|
147
|
+
semanticMap = dimResult.correctedMap;
|
|
148
|
+
const dimCorrections = dimResult.corrections;
|
|
149
|
+
const darkResult = autoCorrect(semanticMap, primitivesDark, 'dark', config.minContrast);
|
|
124
150
|
semanticMap = darkResult.correctedMap;
|
|
125
151
|
const darkCorrections = darkResult.corrections;
|
|
126
|
-
const allCorrections = [...lightCorrections, ...darkCorrections];
|
|
152
|
+
const allCorrections = [...lightCorrections, ...dimCorrections, ...darkCorrections];
|
|
127
153
|
const primitivesCSS = compilePrimitivesCSS(primitives, config.themeName);
|
|
128
154
|
const lightModeCSS = compileModeCSS(semanticMap, primitives, 'light', config.themeName);
|
|
129
|
-
const
|
|
130
|
-
const
|
|
155
|
+
const dimModeCSS = compileModeCSS(semanticMap, primitivesDim, 'dim', config.themeName);
|
|
156
|
+
const darkModeCSS = compileModeCSS(semanticMap, primitivesDark, 'dark', config.themeName);
|
|
157
|
+
const themeCSS = compileThemeCSS(semanticMap, primitives, config.themeName, inputHash, primitivesDark, primitivesDim);
|
|
131
158
|
const createThemeTokens = {};
|
|
132
159
|
const lightTokens = compileCreateThemeTokens(semanticMap, primitives, 'light');
|
|
133
160
|
for (const [k, v] of Object.entries(lightTokens)) {
|
|
134
161
|
createThemeTokens[k] = v;
|
|
135
162
|
}
|
|
163
|
+
const dimTokens = compileCreateThemeTokens(semanticMap, primitivesDim, 'dim');
|
|
164
|
+
const darkTokens = compileCreateThemeTokens(semanticMap, primitivesDark, 'dark');
|
|
165
|
+
const createThemeOptions = { tokens: createThemeTokens, modes: { dim: dimTokens, dark: darkTokens } };
|
|
166
|
+
const customTheme = {
|
|
167
|
+
name: config.themeName,
|
|
168
|
+
extends: 'minimal',
|
|
169
|
+
css: compileCustomThemeCSS(config.themeName, createThemeTokens, darkTokens, dimTokens),
|
|
170
|
+
};
|
|
136
171
|
const report = buildReport(config, warnings, allCorrections, primitives, inputHash);
|
|
137
172
|
return {
|
|
138
173
|
primitives,
|
|
174
|
+
primitivesDark,
|
|
175
|
+
primitivesDim,
|
|
139
176
|
semanticMap,
|
|
140
177
|
corrections: allCorrections,
|
|
141
178
|
css: {
|
|
142
179
|
primitives: primitivesCSS,
|
|
143
180
|
lightMode: lightModeCSS,
|
|
181
|
+
dimMode: dimModeCSS,
|
|
144
182
|
darkMode: darkModeCSS,
|
|
145
183
|
theme: themeCSS,
|
|
146
184
|
},
|
|
147
185
|
createThemeTokens,
|
|
186
|
+
createThemeOptions,
|
|
187
|
+
customTheme,
|
|
148
188
|
report,
|
|
149
189
|
};
|
|
150
190
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,11 +3,13 @@ export { validateAccents, normalizeHex, classifyAccent } from './input/validate.
|
|
|
3
3
|
export { hexToOklch, oklchToHex, gamutMap } from './color/convert.js';
|
|
4
4
|
export { DEFAULT_CURVE, validateCurve } from './color/curves.js';
|
|
5
5
|
export { generateScale } from './color/scale.js';
|
|
6
|
+
export { buildDarkPrimitives, liftScale, DEFAULT_DARK_LIFT, MAX_DARK_LIFT, DARK_LIFT_WEIGHTS } from './color/darkLift.js';
|
|
7
|
+
export { buildDimPrimitives, toneDownScale, warmScale, DEFAULT_DIM_CHROMA, MIN_DIM_CHROMA, DEFAULT_DIM_WARMTH, TEAK_HUE_CHROMA, } from './color/dim.js';
|
|
6
8
|
export { generateSystemScales, DEFAULT_SYSTEM_HUES } from './color/systemHues.js';
|
|
7
9
|
export { generateNeutralScale } from './color/neutral.js';
|
|
8
|
-
export { buildSemanticMap } from './tokens/semanticMap.js';
|
|
10
|
+
export { buildSemanticMap, contrastGrounds, dimRef, modeRef, withDimRefs } from './tokens/semanticMap.js';
|
|
9
11
|
export { buildPrimitives } from './tokens/primitives.js';
|
|
10
12
|
export { checkContrast, autoCorrect } from './tokens/contrast.js';
|
|
11
|
-
export { compilePrimitivesJSON, compileSemanticJSON, compilePrimitivesCSS, compileModeCSS, compileThemeCSS, compileCreateThemeTokens, } from './compile/css.js';
|
|
13
|
+
export { compilePrimitivesJSON, compileSemanticJSON, compilePrimitivesCSS, compileModeCSS, compileThemeCSS, compileCreateThemeTokens, compileCustomThemeCSS, } from './compile/css.js';
|
|
12
14
|
export { generateSmokeHTML } from './preview/smoke.js';
|
|
13
|
-
export type { Oklch, ScaleStep, ScaleEntry, Scale, ScaleName, SystemHueName, PrimitiveTokens, TokenRef, SemanticRole, SemanticEntry, SemanticMap, GeneratorConfig, AccentClassification, ValidatedAccent, ContrastCorrection, GeneratorResult, } from './types.js';
|
|
15
|
+
export type { Oklch, ScaleStep, ScaleEntry, Scale, ScaleName, SystemHueName, PrimitiveTokens, TokenRef, SemanticRole, Mode, SemanticEntry, SemanticMap, GeneratorConfig, AccentClassification, ValidatedAccent, ContrastCorrection, GeneratorResult, GeneratedCustomTheme, } from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -3,10 +3,12 @@ export { validateAccents, normalizeHex, classifyAccent } from './input/validate.
|
|
|
3
3
|
export { hexToOklch, oklchToHex, gamutMap } from './color/convert.js';
|
|
4
4
|
export { DEFAULT_CURVE, validateCurve } from './color/curves.js';
|
|
5
5
|
export { generateScale } from './color/scale.js';
|
|
6
|
+
export { buildDarkPrimitives, liftScale, DEFAULT_DARK_LIFT, MAX_DARK_LIFT, DARK_LIFT_WEIGHTS } from './color/darkLift.js';
|
|
7
|
+
export { buildDimPrimitives, toneDownScale, warmScale, DEFAULT_DIM_CHROMA, MIN_DIM_CHROMA, DEFAULT_DIM_WARMTH, TEAK_HUE_CHROMA, } from './color/dim.js';
|
|
6
8
|
export { generateSystemScales, DEFAULT_SYSTEM_HUES } from './color/systemHues.js';
|
|
7
9
|
export { generateNeutralScale } from './color/neutral.js';
|
|
8
|
-
export { buildSemanticMap } from './tokens/semanticMap.js';
|
|
10
|
+
export { buildSemanticMap, contrastGrounds, dimRef, modeRef, withDimRefs } from './tokens/semanticMap.js';
|
|
9
11
|
export { buildPrimitives } from './tokens/primitives.js';
|
|
10
12
|
export { checkContrast, autoCorrect } from './tokens/contrast.js';
|
|
11
|
-
export { compilePrimitivesJSON, compileSemanticJSON, compilePrimitivesCSS, compileModeCSS, compileThemeCSS, compileCreateThemeTokens, } from './compile/css.js';
|
|
13
|
+
export { compilePrimitivesJSON, compileSemanticJSON, compilePrimitivesCSS, compileModeCSS, compileThemeCSS, compileCreateThemeTokens, compileCustomThemeCSS, } from './compile/css.js';
|
|
12
14
|
export { generateSmokeHTML } from './preview/smoke.js';
|
package/dist/preview/smoke.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { PrimitiveTokens, SemanticMap } from '../types.js';
|
|
2
|
-
export declare function generateSmokeHTML(primitives: PrimitiveTokens, semanticMap: SemanticMap, themeName: string): string;
|
|
2
|
+
export declare function generateSmokeHTML(primitives: PrimitiveTokens, semanticMap: SemanticMap, themeName: string, darkPrimitives?: PrimitiveTokens, dimPrimitives?: PrimitiveTokens): string;
|
package/dist/preview/smoke.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { checkContrast } from '../tokens/contrast.js';
|
|
2
|
+
import { contrastGrounds, modeRef } from '../tokens/semanticMap.js';
|
|
2
3
|
const SCALE_STEPS = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
|
|
3
4
|
function resolveHex(ref, primitives) {
|
|
4
5
|
if (ref === 'white')
|
|
@@ -43,23 +44,29 @@ function semanticPairingHTML(token, fgHex, bgHex, fgRef, bgRef, mode) {
|
|
|
43
44
|
: `<span style="color:#dc2626;font-weight:bold">FAIL</span>`;
|
|
44
45
|
return `<div style="background:${bgHex};color:${fgHex};padding:12px 16px;border-radius:6px;margin-bottom:4px;font-family:monospace;font-size:13px;display:flex;justify-content:space-between;align-items:center"><span>${token} (${mode})</span><span>${ratio.toFixed(2)}:1 ${badge}</span></div>`;
|
|
45
46
|
}
|
|
46
|
-
export function generateSmokeHTML(primitives, semanticMap, themeName) {
|
|
47
|
+
export function generateSmokeHTML(primitives, semanticMap, themeName, darkPrimitives = primitives, dimPrimitives = primitives) {
|
|
47
48
|
const scaleNames = Object.keys(primitives);
|
|
48
49
|
const scaleSection = scaleNames
|
|
49
50
|
.map((name) => scaleSwatchesHTML(name, primitives))
|
|
50
51
|
.join('');
|
|
51
52
|
const fgTokens = Object.entries(semanticMap).filter(([, entry]) => entry.role === 'fg' && entry.contrastAgainst);
|
|
52
53
|
const pairingRows = [];
|
|
54
|
+
const sources = { light: primitives, dim: dimPrimitives, dark: darkPrimitives };
|
|
53
55
|
for (const [token, entry] of fgTokens) {
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
const grounds = contrastGrounds(entry);
|
|
57
|
+
for (const ground of grounds) {
|
|
58
|
+
const bgEntry = semanticMap[ground];
|
|
59
|
+
if (!bgEntry)
|
|
60
|
+
continue;
|
|
61
|
+
const label = grounds.length > 1 ? `${token} on ${ground}` : token;
|
|
62
|
+
for (const mode of ['light', 'dim', 'dark']) {
|
|
63
|
+
const fgRef = modeRef(entry, mode);
|
|
64
|
+
const bgRef = modeRef(bgEntry, mode);
|
|
65
|
+
const source = sources[mode];
|
|
66
|
+
const fgHex = resolveHex(fgRef, source);
|
|
67
|
+
const bgHex = resolveHex(bgRef, source);
|
|
68
|
+
pairingRows.push(semanticPairingHTML(label, fgHex, bgHex, fgRef, bgRef, mode));
|
|
69
|
+
}
|
|
63
70
|
}
|
|
64
71
|
}
|
|
65
72
|
return `<!DOCTYPE html>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ContrastCorrection, PrimitiveTokens, SemanticMap } from '../types.js';
|
|
1
|
+
import type { ContrastCorrection, Mode, PrimitiveTokens, SemanticMap } from '../types.js';
|
|
2
2
|
export declare function checkContrast(fgHex: string, bgHex: string): number;
|
|
3
|
-
export declare function autoCorrect(map: SemanticMap, primitives: PrimitiveTokens, mode:
|
|
3
|
+
export declare function autoCorrect(map: SemanticMap, primitives: PrimitiveTokens, mode: Mode, defaultMinContrast: number): {
|
|
4
4
|
correctedMap: SemanticMap;
|
|
5
5
|
corrections: ContrastCorrection[];
|
|
6
6
|
};
|
package/dist/tokens/contrast.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { wcagContrast } from 'culori';
|
|
2
|
+
import { contrastGrounds, modeRef } from './semanticMap.js';
|
|
2
3
|
const ORDERED_STEPS = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
|
|
3
4
|
export function checkContrast(fgHex, bgHex) {
|
|
4
5
|
return wcagContrast(fgHex, bgHex);
|
|
5
6
|
}
|
|
7
|
+
/** Contrast against the hardest of several grounds. */
|
|
8
|
+
function worstContrast(fgHex, bgHexes) {
|
|
9
|
+
return Math.min(...bgHexes.map((bgHex) => checkContrast(fgHex, bgHex)));
|
|
10
|
+
}
|
|
6
11
|
function resolveTokenRef(ref, primitives) {
|
|
7
12
|
if (ref === 'white')
|
|
8
13
|
return '#ffffff';
|
|
@@ -24,25 +29,31 @@ function parseRef(ref) {
|
|
|
24
29
|
const [scaleName, stepStr] = ref.split('.');
|
|
25
30
|
return { scaleName, step: Number(stepStr) };
|
|
26
31
|
}
|
|
27
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Walks the fg's ramp one step at a time from `originalRef` in `direction`
|
|
34
|
+
* ('light' toward the dark steps, for text on a light ground; 'dark' toward
|
|
35
|
+
* the light steps) and takes the first step that passes: the smallest change
|
|
36
|
+
* that fixes the pair, so brand-coloured text stays as close to its brand
|
|
37
|
+
* step as contrast allows. Falls back to neutral, white or black when no step
|
|
38
|
+
* on the ramp passes.
|
|
39
|
+
*/
|
|
40
|
+
function shiftFgTowardsContrast(originalRef, bgHexes, primitives, minContrast, direction) {
|
|
28
41
|
const parsed = parseRef(originalRef);
|
|
29
42
|
if (!parsed)
|
|
30
43
|
return null;
|
|
31
|
-
const { scaleName } = parsed;
|
|
44
|
+
const { scaleName, step: originalStep } = parsed;
|
|
32
45
|
const scale = primitives[scaleName];
|
|
33
46
|
if (!scale)
|
|
34
47
|
return null;
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
const index = ORDERED_STEPS.indexOf(originalStep);
|
|
49
|
+
const stepsToTry = direction === 'light'
|
|
50
|
+
? ORDERED_STEPS.slice(index + 1)
|
|
51
|
+
: ORDERED_STEPS.slice(0, index).reverse();
|
|
39
52
|
for (const step of stepsToTry) {
|
|
40
|
-
if (attempts >= 3)
|
|
41
|
-
break;
|
|
42
53
|
const entry = scale[step];
|
|
43
54
|
if (!entry)
|
|
44
55
|
continue;
|
|
45
|
-
const ratio =
|
|
56
|
+
const ratio = worstContrast(entry.hex, bgHexes);
|
|
46
57
|
if (ratio >= minContrast) {
|
|
47
58
|
return {
|
|
48
59
|
ref: `${scaleName}.${step}`,
|
|
@@ -50,9 +61,8 @@ function shiftFgTowardsContrast(originalRef, bgHex, primitives, minContrast, mod
|
|
|
50
61
|
kind: 'step-shift',
|
|
51
62
|
};
|
|
52
63
|
}
|
|
53
|
-
attempts++;
|
|
54
64
|
}
|
|
55
|
-
const hardFallbacks =
|
|
65
|
+
const hardFallbacks = direction === 'light'
|
|
56
66
|
? [
|
|
57
67
|
{ ref: 'neutral.950', hex: resolveTokenRef('neutral.950', primitives) ?? '#000000' },
|
|
58
68
|
{ ref: 'white', hex: '#ffffff' },
|
|
@@ -65,7 +75,7 @@ function shiftFgTowardsContrast(originalRef, bgHex, primitives, minContrast, mod
|
|
|
65
75
|
];
|
|
66
76
|
let bestFallback = null;
|
|
67
77
|
for (const fb of hardFallbacks) {
|
|
68
|
-
const ratio =
|
|
78
|
+
const ratio = worstContrast(fb.hex, bgHexes);
|
|
69
79
|
if (ratio >= minContrast && (!bestFallback || ratio > bestFallback.contrast)) {
|
|
70
80
|
bestFallback = { ref: fb.ref, contrast: ratio };
|
|
71
81
|
}
|
|
@@ -73,8 +83,8 @@ function shiftFgTowardsContrast(originalRef, bgHex, primitives, minContrast, mod
|
|
|
73
83
|
if (bestFallback) {
|
|
74
84
|
return { ref: bestFallback.ref, contrast: bestFallback.contrast, kind: 'hard-fallback' };
|
|
75
85
|
}
|
|
76
|
-
const whiteRatio =
|
|
77
|
-
const blackRatio =
|
|
86
|
+
const whiteRatio = worstContrast('#ffffff', bgHexes);
|
|
87
|
+
const blackRatio = worstContrast('#000000', bgHexes);
|
|
78
88
|
const best = whiteRatio >= blackRatio ? { ref: 'white', contrast: whiteRatio } : { ref: 'black', contrast: blackRatio };
|
|
79
89
|
return { ref: best.ref, contrast: best.contrast, kind: 'hard-fallback' };
|
|
80
90
|
}
|
|
@@ -86,35 +96,31 @@ export function autoCorrect(map, primitives, mode, defaultMinContrast) {
|
|
|
86
96
|
continue;
|
|
87
97
|
if (entry.role !== 'fg' && entry.role !== 'border')
|
|
88
98
|
continue;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (
|
|
99
|
+
const bgHexes = contrastGrounds(entry)
|
|
100
|
+
.flatMap((ground) => (map[ground] ? [resolveTokenRef(modeRef(map[ground], mode), primitives)] : []))
|
|
101
|
+
.filter((hex) => hex !== null);
|
|
102
|
+
if (bgHexes.length === 0)
|
|
93
103
|
continue;
|
|
94
|
-
const
|
|
95
|
-
const fgRef = mode === 'light' ? entry.light : entry.dark;
|
|
96
|
-
const bgHex = resolveTokenRef(bgRef, primitives);
|
|
104
|
+
const fgRef = modeRef(entry, mode);
|
|
97
105
|
const fgHex = resolveTokenRef(fgRef, primitives);
|
|
98
|
-
if (!
|
|
106
|
+
if (!fgHex)
|
|
99
107
|
continue;
|
|
100
108
|
// The config-wide minimum is a floor: per-entry values can raise it but
|
|
101
109
|
// not lower it, except for entries that opt into a lower WCAG tier.
|
|
102
110
|
const minContrast = entry.allowBelowDefault && entry.minContrast !== undefined
|
|
103
111
|
? entry.minContrast
|
|
104
112
|
: Math.max(entry.minContrast ?? 0, defaultMinContrast);
|
|
105
|
-
const ratio =
|
|
113
|
+
const ratio = worstContrast(fgHex, bgHexes);
|
|
106
114
|
if (ratio >= minContrast)
|
|
107
115
|
continue;
|
|
108
|
-
|
|
116
|
+
// Dim grounds run from near-white pages to mid-tone buttons, so its search
|
|
117
|
+
// follows the ground: toward dark text where black reads better, else light.
|
|
118
|
+
const direction = mode === 'dim'
|
|
119
|
+
? (worstContrast('#000000', bgHexes) >= worstContrast('#ffffff', bgHexes) ? 'light' : 'dark')
|
|
120
|
+
: mode;
|
|
121
|
+
const correction = shiftFgTowardsContrast(fgRef, bgHexes, primitives, minContrast, direction);
|
|
109
122
|
if (correction) {
|
|
110
|
-
|
|
111
|
-
if (mode === 'light') {
|
|
112
|
-
newEntry.light = correction.ref;
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
newEntry.dark = correction.ref;
|
|
116
|
-
}
|
|
117
|
-
corrected[tokenName] = newEntry;
|
|
123
|
+
corrected[tokenName] = { ...corrected[tokenName], [mode]: correction.ref };
|
|
118
124
|
corrections.push({
|
|
119
125
|
token: tokenName,
|
|
120
126
|
mode,
|
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
import type { SemanticMap } from '../types.js';
|
|
1
|
+
import type { Mode, SemanticEntry, SemanticMap, TokenRef } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The dim ref for a light ref: one step deeper on the same ramp, 950 staying
|
|
4
|
+
* put. `minimal`'s own dim follows the same rule (page 0 → 100, tint 100 →
|
|
5
|
+
* 200, status text 700 → 800), which is what keeps dim a softer light mode
|
|
6
|
+
* rather than a lighter dark one.
|
|
7
|
+
*/
|
|
8
|
+
export declare function dimRef(light: TokenRef): TokenRef;
|
|
9
|
+
/** The ref an entry resolves to in `mode`; dim falls back to `dimRef(entry.light)`. */
|
|
10
|
+
export declare function modeRef(entry: SemanticEntry, mode: Mode): TokenRef;
|
|
11
|
+
/** The ground tokens an entry is checked against, as a list. */
|
|
12
|
+
export declare function contrastGrounds(entry: SemanticEntry): string[];
|
|
13
|
+
/**
|
|
14
|
+
* Pins every entry's dim ref from its light ref, so later light-mode contrast
|
|
15
|
+
* corrections can't move dim along with them.
|
|
16
|
+
*/
|
|
17
|
+
export declare function withDimRefs(map: SemanticMap): SemanticMap;
|
|
2
18
|
export declare function buildSemanticMap(): SemanticMap;
|
|
@@ -1,5 +1,57 @@
|
|
|
1
|
+
import { SCALE_STEPS } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The dim ref for a light ref: one step deeper on the same ramp, 950 staying
|
|
4
|
+
* put. `minimal`'s own dim follows the same rule (page 0 → 100, tint 100 →
|
|
5
|
+
* 200, status text 700 → 800), which is what keeps dim a softer light mode
|
|
6
|
+
* rather than a lighter dark one.
|
|
7
|
+
*/
|
|
8
|
+
export function dimRef(light) {
|
|
9
|
+
if (light === 'white' || light === 'black')
|
|
10
|
+
return light;
|
|
11
|
+
const [scale, step] = light.split('.');
|
|
12
|
+
const index = SCALE_STEPS.indexOf(Number(step));
|
|
13
|
+
const deeper = SCALE_STEPS[Math.min(index + 1, SCALE_STEPS.length - 1)];
|
|
14
|
+
return `${scale}.${deeper}`;
|
|
15
|
+
}
|
|
16
|
+
/** The ref an entry resolves to in `mode`; dim falls back to `dimRef(entry.light)`. */
|
|
17
|
+
export function modeRef(entry, mode) {
|
|
18
|
+
if (mode === 'dim')
|
|
19
|
+
return entry.dim ?? dimRef(entry.light);
|
|
20
|
+
return entry[mode];
|
|
21
|
+
}
|
|
22
|
+
/** The ground tokens an entry is checked against, as a list. */
|
|
23
|
+
export function contrastGrounds(entry) {
|
|
24
|
+
return entry.contrastAgainst === undefined ? [] : [].concat(entry.contrastAgainst);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Pins every entry's dim ref from its light ref, so later light-mode contrast
|
|
28
|
+
* corrections can't move dim along with them.
|
|
29
|
+
*/
|
|
30
|
+
export function withDimRefs(map) {
|
|
31
|
+
const filled = {};
|
|
32
|
+
for (const [token, entry] of Object.entries(map)) {
|
|
33
|
+
filled[token] = { ...entry, dim: modeRef(entry, 'dim') };
|
|
34
|
+
}
|
|
35
|
+
return filled;
|
|
36
|
+
}
|
|
1
37
|
export function buildSemanticMap() {
|
|
2
|
-
return {
|
|
38
|
+
return withDimRefs({
|
|
39
|
+
// ── Base greys ─────────────────────────────────────────────
|
|
40
|
+
// The mode-level ramp aliases `minimal` defines in modes.css. Most tokens
|
|
41
|
+
// here supersede them, but some components read them directly (Secondary
|
|
42
|
+
// and Ghost-family buttons, Card, Progress, SegmentedControl), so a theme
|
|
43
|
+
// that leaves them alone shows `minimal`'s greys there.
|
|
44
|
+
'backgrounds-primary': { light: 'neutral.50', dark: 'neutral.800', role: 'bg' },
|
|
45
|
+
'backgrounds-secondary': { light: 'neutral.100', dark: 'neutral.900', role: 'bg' },
|
|
46
|
+
'backgrounds-tertiary': { light: 'neutral.200', dark: 'neutral.950', role: 'bg' },
|
|
47
|
+
'backgrounds-quaternary': { light: 'neutral.300', dark: 'neutral.700', role: 'bg' },
|
|
48
|
+
'backgrounds-quinary': { light: 'neutral.400', dark: 'neutral.600', role: 'bg' },
|
|
49
|
+
// Secondary button text sits on the tertiary fill; the rest on page and panel.
|
|
50
|
+
'foregrounds-primary': { light: 'neutral.950', dark: 'neutral.50', role: 'fg', contrastAgainst: ['backgrounds-primary', 'backgrounds-secondary', 'backgrounds-tertiary'], minContrast: 4.5 },
|
|
51
|
+
'foregrounds-secondary': { light: 'neutral.800', dark: 'neutral.100', role: 'fg', contrastAgainst: ['backgrounds-primary', 'backgrounds-secondary'], minContrast: 4.5 },
|
|
52
|
+
'foregrounds-tertiary': { light: 'neutral.600', dark: 'neutral.200', role: 'fg', contrastAgainst: ['backgrounds-primary', 'backgrounds-secondary'], minContrast: 4.5 },
|
|
53
|
+
'foregrounds-quaternary': { light: 'neutral.500', dark: 'neutral.300', role: 'fg', contrastAgainst: ['backgrounds-primary', 'backgrounds-secondary'], minContrast: 3.0, allowBelowDefault: true },
|
|
54
|
+
'foregrounds-quinary': { light: 'neutral.400', dark: 'neutral.400', role: 'fg' },
|
|
3
55
|
// ── Container ──────────────────────────────────────────────
|
|
4
56
|
'container-bg': { light: 'neutral.50', dark: 'neutral.800', role: 'bg' },
|
|
5
57
|
'container-bg-alt': { light: 'neutral.100', dark: 'neutral.900', role: 'bg' },
|
|
@@ -10,6 +62,16 @@ export function buildSemanticMap() {
|
|
|
10
62
|
'container-fg': { light: 'neutral.950', dark: 'neutral.50', role: 'fg', contrastAgainst: 'container-bg', minContrast: 4.5 },
|
|
11
63
|
'container-fg-alt': { light: 'neutral.800', dark: 'neutral.100', role: 'fg', contrastAgainst: 'container-bg', minContrast: 4.5 },
|
|
12
64
|
'container-fg-inverse': { light: 'neutral.50', dark: 'neutral.950', role: 'fg', contrastAgainst: 'container-bg-inverse', minContrast: 4.5 },
|
|
65
|
+
// Tooltip surface — matches container-bg in light and container-border in
|
|
66
|
+
// dark, so the tooltip's border disappears against its own background.
|
|
67
|
+
'tooltip-bg': { light: 'neutral.50', dark: 'neutral.700', role: 'bg' },
|
|
68
|
+
// Dialog/Sheet scrim, drawn at 0.8 opacity — the darkest step in both
|
|
69
|
+
// modes so it dims the page behind an overlay instead of washing it out.
|
|
70
|
+
'overlay-bg': { light: 'neutral.950', dark: 'neutral.950', role: 'bg' },
|
|
71
|
+
// Keyboard focus outline — a non-text indicator, so it targets the WCAG
|
|
72
|
+
// 3:1 UI tier (1.4.11) against the page ground, like interactive-fg-alt.
|
|
73
|
+
// Mid-ramp rather than neutral.950/50 so focus never matches the pressed fill.
|
|
74
|
+
'focus-ring': { light: 'neutral.500', dark: 'neutral.400', role: 'fg', contrastAgainst: 'container-bg', minContrast: 3.0, allowBelowDefault: true },
|
|
13
75
|
// ── Interactive ────────────────────────────────────────────
|
|
14
76
|
'interactive-bg': { light: 'neutral.50', dark: 'neutral.800', role: 'bg' },
|
|
15
77
|
'interactive-bg-alt': { light: 'neutral.100', dark: 'neutral.900', role: 'bg' },
|
|
@@ -23,9 +85,10 @@ export function buildSemanticMap() {
|
|
|
23
85
|
'interactive-border-alt': { light: 'neutral.300', dark: 'neutral.600', role: 'border' },
|
|
24
86
|
'interactive-border-disabled': { light: 'neutral.300', dark: 'neutral.600', role: 'border' },
|
|
25
87
|
'interactive-fg': { light: 'neutral.950', dark: 'neutral.50', role: 'fg', contrastAgainst: 'interactive-bg', minContrast: 4.5 },
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
|
|
88
|
+
// Muted text: placeholders, unselected tabs and segments, and the
|
|
89
|
+
// descriptions in Card, Dialog and Sheet. Body text, so it meets the text
|
|
90
|
+
// floor on the page and panel grounds it sits on.
|
|
91
|
+
'interactive-fg-alt': { light: 'neutral.600', dark: 'neutral.300', role: 'fg', contrastAgainst: ['interactive-bg', 'interactive-bg-alt', 'container-bg', 'container-bg-alt'], minContrast: 4.5 },
|
|
29
92
|
'interactive-fg-readonly': { light: 'neutral.800', dark: 'neutral.100', role: 'fg', contrastAgainst: 'interactive-bg', minContrast: 4.5 },
|
|
30
93
|
'interactive-fg-disabled': { light: 'neutral.400', dark: 'neutral.500', role: 'fg' },
|
|
31
94
|
'interactive-fg-active': { light: 'primary.100', dark: 'primary.900', role: 'fg', contrastAgainst: 'interactive-bg-active', minContrast: 4.5 },
|
|
@@ -52,7 +115,9 @@ export function buildSemanticMap() {
|
|
|
52
115
|
'action-secondary-border-alt': { light: 'primary.800', dark: 'primary.300', role: 'border' },
|
|
53
116
|
'action-secondary-border-disabled': { light: 'primary.100', dark: 'primary.900', role: 'border' },
|
|
54
117
|
'action-secondary-border-inverse': { light: 'primary.50', dark: 'primary.950', role: 'border' },
|
|
55
|
-
|
|
118
|
+
// Outline button text, on page and panel surfaces alike: checked against
|
|
119
|
+
// both, like action-tertiary-fg. Whichever is harder in a mode decides.
|
|
120
|
+
'action-secondary-fg': { light: 'primary.500', dark: 'primary.400', role: 'fg', contrastAgainst: ['container-bg', 'container-bg-alt'], minContrast: 4.5 },
|
|
56
121
|
'action-secondary-fg-alt': { light: 'neutral.950', dark: 'neutral.50', role: 'fg' },
|
|
57
122
|
'action-secondary-fg-disabled': { light: 'neutral.400', dark: 'neutral.500', role: 'fg' },
|
|
58
123
|
'action-secondary-fg-active': { light: 'neutral.50', dark: 'neutral.50', role: 'fg', contrastAgainst: 'action-secondary-bg-active', minContrast: 4.5 },
|
|
@@ -70,12 +135,22 @@ export function buildSemanticMap() {
|
|
|
70
135
|
'action-tertiary-border-alt': { light: 'primary.800', dark: 'primary.300', role: 'border' },
|
|
71
136
|
'action-tertiary-border-disabled': { light: 'neutral.300', dark: 'neutral.600', role: 'border' },
|
|
72
137
|
'action-tertiary-border-inverse': { light: 'primary.100', dark: 'primary.900', role: 'border' },
|
|
73
|
-
|
|
138
|
+
// Ghost button text sits on page and panel surfaces alike, so it is checked
|
|
139
|
+
// against both: -bg (the page) and -bg-alt (the panel, container-bg-alt's
|
|
140
|
+
// refs). Whichever is harder in a mode decides.
|
|
141
|
+
'action-tertiary-fg': { light: 'primary.500', dark: 'primary.400', role: 'fg', contrastAgainst: ['action-tertiary-bg', 'action-tertiary-bg-alt'], minContrast: 4.5 },
|
|
74
142
|
'action-tertiary-fg-alt': { light: 'neutral.950', dark: 'neutral.50', role: 'fg' },
|
|
75
143
|
'action-tertiary-fg-disabled': { light: 'neutral.400', dark: 'neutral.500', role: 'fg' },
|
|
76
144
|
'action-tertiary-fg-active': { light: 'primary.700', dark: 'primary.200', role: 'fg', contrastAgainst: 'action-tertiary-bg-active', minContrast: 4.5 },
|
|
77
145
|
'action-tertiary-fg-selected': { light: 'primary.700', dark: 'primary.200', role: 'fg', contrastAgainst: 'action-tertiary-bg-selected', minContrast: 4.5 },
|
|
78
146
|
'action-tertiary-fg-inverse': { light: 'neutral.50', dark: 'neutral.950', role: 'fg' },
|
|
147
|
+
// ── Action Neutral ─────────────────────────────────────────
|
|
148
|
+
// The Secondary button: a grey fill that deepens on hover, on the same
|
|
149
|
+
// steps as backgrounds-tertiary and -quaternary. Its text is checked
|
|
150
|
+
// against both fills.
|
|
151
|
+
'action-neutral-bg': { light: 'neutral.200', dark: 'neutral.950', role: 'bg' },
|
|
152
|
+
'action-neutral-bg-hover': { light: 'neutral.300', dark: 'neutral.700', role: 'bg' },
|
|
153
|
+
'action-neutral-fg': { light: 'neutral.950', dark: 'neutral.50', role: 'fg', contrastAgainst: ['action-neutral-bg', 'action-neutral-bg-hover'], minContrast: 4.5 },
|
|
79
154
|
// ── Destructive ────────────────────────────────────────────
|
|
80
155
|
'destructive-bg': { light: 'error.50', dark: 'error.950', role: 'bg' },
|
|
81
156
|
'destructive-bg-alt': { light: 'error.100', dark: 'error.900', role: 'bg' },
|
|
@@ -88,7 +163,9 @@ export function buildSemanticMap() {
|
|
|
88
163
|
'destructive-border-alt': { light: 'error.800', dark: 'error.300', role: 'border' },
|
|
89
164
|
'destructive-border-disabled': { light: 'error.100', dark: 'error.900', role: 'border' },
|
|
90
165
|
'destructive-border-inverse': { light: 'error.50', dark: 'error.950', role: 'border' },
|
|
91
|
-
|
|
166
|
+
// Checked against -bg-alt, the harder of the two grounds it sits on (Banner
|
|
167
|
+
// uses the tint; Button and Badge the lighter -bg).
|
|
168
|
+
'destructive-fg': { light: 'error.700', dark: 'error.200', role: 'fg', contrastAgainst: 'destructive-bg-alt', minContrast: 4.5 },
|
|
92
169
|
'destructive-fg-alt': { light: 'neutral.950', dark: 'neutral.50', role: 'fg' },
|
|
93
170
|
'destructive-fg-disabled': { light: 'neutral.400', dark: 'neutral.500', role: 'fg' },
|
|
94
171
|
'destructive-fg-active': { light: 'neutral.50', dark: 'neutral.950', role: 'fg', contrastAgainst: 'destructive-bg-active', minContrast: 4.5 },
|
|
@@ -101,7 +178,8 @@ export function buildSemanticMap() {
|
|
|
101
178
|
'positive-border': { light: 'success.500', dark: 'success.400', role: 'border' },
|
|
102
179
|
'positive-border-alt': { light: 'success.800', dark: 'success.300', role: 'border' },
|
|
103
180
|
'positive-border-inverse': { light: 'success.50', dark: 'success.950', role: 'border' },
|
|
104
|
-
|
|
181
|
+
// Same as destructive-fg: checked against the tint Banner draws it on.
|
|
182
|
+
'positive-fg': { light: 'success.700', dark: 'success.200', role: 'fg', contrastAgainst: 'positive-bg-alt', minContrast: 4.5 },
|
|
105
183
|
'positive-fg-alt': { light: 'neutral.950', dark: 'neutral.50', role: 'fg' },
|
|
106
184
|
'positive-fg-inverse': { light: 'success.50', dark: 'success.950', role: 'fg', contrastAgainst: 'positive-bg-inverse', minContrast: 4.5 },
|
|
107
185
|
// ── Cautionary ─────────────────────────────────────────────
|
|
@@ -159,5 +237,5 @@ export function buildSemanticMap() {
|
|
|
159
237
|
'dataviz-8-main': { light: 'info.600', dark: 'info.300', role: 'decorative' },
|
|
160
238
|
'dataviz-8-tint': { light: 'info.200', dark: 'info.800', role: 'decorative' },
|
|
161
239
|
'dataviz-8-shade': { light: 'info.900', dark: 'info.100', role: 'decorative' },
|
|
162
|
-
};
|
|
240
|
+
});
|
|
163
241
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -22,16 +22,27 @@ export type SystemHueName = 'error' | 'warning' | 'success' | 'info';
|
|
|
22
22
|
export type PrimitiveTokens = Partial<Record<ScaleName, Scale>>;
|
|
23
23
|
export type TokenRef = `${ScaleName}.${ScaleStepValue}` | 'white' | 'black';
|
|
24
24
|
export type SemanticRole = 'bg' | 'fg' | 'border' | 'overlay' | 'decorative';
|
|
25
|
+
export type Mode = 'light' | 'dim' | 'dark';
|
|
25
26
|
export type SemanticEntry = {
|
|
26
27
|
light: TokenRef;
|
|
27
28
|
dark: TokenRef;
|
|
28
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Dim-mode ref. When omitted, dim uses the light ref one step deeper on its
|
|
31
|
+
* ramp (950 stays put), so dim reads as light mode toned down.
|
|
32
|
+
*/
|
|
33
|
+
dim?: TokenRef;
|
|
34
|
+
/**
|
|
35
|
+
* The ground token(s) this fg/border must clear `minContrast` on. With
|
|
36
|
+
* several, the hardest of them decides, for text that sits on more than one
|
|
37
|
+
* surface (page and panel, say).
|
|
38
|
+
*/
|
|
39
|
+
contrastAgainst?: string | string[];
|
|
29
40
|
minContrast?: number;
|
|
30
41
|
/**
|
|
31
42
|
* Honor `minContrast` as-is even when it is below the config-wide minimum.
|
|
32
43
|
* For entries that deliberately target a lower WCAG tier (e.g. the 3:1
|
|
33
|
-
*
|
|
34
|
-
*
|
|
44
|
+
* UI tier for the focus ring) and must not be raised by a stricter
|
|
45
|
+
* `GeneratorConfig.minContrast`.
|
|
35
46
|
*/
|
|
36
47
|
allowBelowDefault?: boolean;
|
|
37
48
|
role: SemanticRole;
|
|
@@ -49,6 +60,25 @@ export type GeneratorConfig = {
|
|
|
49
60
|
neutralTintRatio: number;
|
|
50
61
|
curve?: ScaleStep[];
|
|
51
62
|
systemHues?: Partial<Record<SystemHueName, number>>;
|
|
63
|
+
/**
|
|
64
|
+
* OKLCH lightness added to the colour ramps dark mode uses, so accents and
|
|
65
|
+
* status colours read as vivid on a dark ground as they do on a light one.
|
|
66
|
+
* Full on the mid steps, tapering to 0 at 50 and 950; neutral is never
|
|
67
|
+
* lifted. 0 turns it off. Default 0.08, maximum 0.2.
|
|
68
|
+
*/
|
|
69
|
+
darkLift?: number;
|
|
70
|
+
/**
|
|
71
|
+
* Chroma multiplier for the colour ramps dim mode uses, so accents and
|
|
72
|
+
* status colours sit back on the softer ground. 1 turns it off. Default 0.8,
|
|
73
|
+
* minimum 0.5.
|
|
74
|
+
*/
|
|
75
|
+
dimChroma?: number;
|
|
76
|
+
/**
|
|
77
|
+
* How far dim mode's greys move toward the warm teak ramp `minimal`'s dim
|
|
78
|
+
* uses: 1 takes teak's hue and chroma, 0 keeps the brand-tinted greys.
|
|
79
|
+
* Default 1.
|
|
80
|
+
*/
|
|
81
|
+
dimWarmth?: number;
|
|
52
82
|
};
|
|
53
83
|
export type AccentClassification = 'normal' | 'low-chroma' | 'extreme-lightness';
|
|
54
84
|
export type ValidatedAccent = {
|
|
@@ -58,7 +88,7 @@ export type ValidatedAccent = {
|
|
|
58
88
|
};
|
|
59
89
|
export type ContrastCorrection = {
|
|
60
90
|
token: string;
|
|
61
|
-
mode:
|
|
91
|
+
mode: Mode;
|
|
62
92
|
originalRef: TokenRef;
|
|
63
93
|
correctedRef: TokenRef;
|
|
64
94
|
originalContrast: number;
|
|
@@ -67,14 +97,39 @@ export type ContrastCorrection = {
|
|
|
67
97
|
};
|
|
68
98
|
export type GeneratorResult = {
|
|
69
99
|
primitives: PrimitiveTokens;
|
|
100
|
+
/** The primitives dark mode resolves against: `primitives` with the colour ramps lifted by `darkLift`. */
|
|
101
|
+
primitivesDark: PrimitiveTokens;
|
|
102
|
+
/** The primitives dim mode resolves against: colour ramps toned down by `dimChroma`, neutral warmed by `dimWarmth`. */
|
|
103
|
+
primitivesDim: PrimitiveTokens;
|
|
70
104
|
semanticMap: SemanticMap;
|
|
71
105
|
corrections: ContrastCorrection[];
|
|
72
106
|
css: {
|
|
73
107
|
primitives: string;
|
|
74
108
|
lightMode: string;
|
|
109
|
+
dimMode: string;
|
|
75
110
|
darkMode: string;
|
|
76
111
|
theme: string;
|
|
77
112
|
};
|
|
113
|
+
/** Light-mode token map for `createTheme({ tokens })`. Kept for compatibility; prefer `createThemeOptions`. */
|
|
78
114
|
createThemeTokens: Record<string, string>;
|
|
115
|
+
/** Light, dim and dark token maps for `createTheme({ name, ...createThemeOptions })` from @eluan/core. */
|
|
116
|
+
createThemeOptions: {
|
|
117
|
+
tokens: Record<string, string>;
|
|
118
|
+
modes: {
|
|
119
|
+
dim: Record<string, string>;
|
|
120
|
+
dark: Record<string, string>;
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* A ready custom theme, the same shape `createTheme()` returns: pass it to
|
|
125
|
+
* `<EluanProvider customThemes={[customTheme]} />`. Covers light, dim and dark mode.
|
|
126
|
+
*/
|
|
127
|
+
customTheme: GeneratedCustomTheme;
|
|
79
128
|
report: string;
|
|
80
129
|
};
|
|
130
|
+
/** Structurally identical to @eluan/core's `CustomTheme`. */
|
|
131
|
+
export type GeneratedCustomTheme = {
|
|
132
|
+
name: string;
|
|
133
|
+
extends: 'minimal';
|
|
134
|
+
css: string;
|
|
135
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eluan/theme-generator",
|
|
3
|
-
"description": "CLI and browser-safe library that generates accessible
|
|
4
|
-
"version": "0.
|
|
3
|
+
"description": "CLI and browser-safe library that generates accessible light, dim and dark mode Eluan theme token sets from accent colors",
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Frolda",
|
|
7
7
|
"homepage": "https://github.com/vasfio/eluan#readme",
|
|
@@ -53,9 +53,9 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/prompts": "^2.4.9",
|
|
56
|
-
"esbuild": "^0.
|
|
56
|
+
"esbuild": "^0.28.1",
|
|
57
57
|
"typescript": "^5.4.5",
|
|
58
|
-
"vitest": "^
|
|
58
|
+
"vitest": "^4.1.12"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|