@eluan/theme-generator 0.1.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/LICENSE +21 -0
- package/README.md +60 -0
- package/dist/cli/generate.d.ts +2 -0
- package/dist/cli/generate.js +207 -0
- package/dist/color/convert.d.ts +4 -0
- package/dist/color/convert.js +28 -0
- package/dist/color/curves.d.ts +3 -0
- package/dist/color/curves.js +29 -0
- package/dist/color/neutral.d.ts +2 -0
- package/dist/color/neutral.js +7 -0
- package/dist/color/scale.d.ts +2 -0
- package/dist/color/scale.js +19 -0
- package/dist/color/systemHues.d.ts +3 -0
- package/dist/color/systemHues.js +28 -0
- package/dist/compile/css.d.ts +7 -0
- package/dist/compile/css.js +160 -0
- package/dist/generate.d.ts +3 -0
- package/dist/generate.js +114 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/dist/input/validate.d.ts +16 -0
- package/dist/input/validate.js +93 -0
- package/dist/preview/smoke.d.ts +2 -0
- package/dist/preview/smoke.js +83 -0
- package/dist/tokens/contrast.d.ts +6 -0
- package/dist/tokens/contrast.js +126 -0
- package/dist/tokens/primitives.d.ts +2 -0
- package/dist/tokens/primitives.js +27 -0
- package/dist/tokens/semanticMap.d.ts +2 -0
- package/dist/tokens/semanticMap.js +161 -0
- package/dist/types.d.ts +68 -0
- package/dist/types.js +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eluan contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# @eluan/theme-generator
|
|
2
|
+
|
|
3
|
+
CLI and library that generates accessible, light + 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
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add -D @eluan/theme-generator
|
|
9
|
+
# or run without installing:
|
|
10
|
+
pnpm dlx @eluan/theme-generator generate --accent "#FF4A2C"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## CLI
|
|
14
|
+
|
|
15
|
+
The package exposes an `eluan-theme` binary.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# One or more accent colors (repeat --accent, up to 3)
|
|
19
|
+
eluan-theme generate --accent "#FF4A2C" --accent "#0F3D3E" --name ocean --out ./tokens
|
|
20
|
+
|
|
21
|
+
# Interactive mode (prompts for the values)
|
|
22
|
+
eluan-theme generate
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
|
|
27
|
+
| Flag | Default | Description |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| `--accent <hex>` | — | Accent color; repeat the flag for up to 3 |
|
|
30
|
+
| `--name <string>` | `custom` | Theme name |
|
|
31
|
+
| `--out <dir>` | `./tokens` | Output directory |
|
|
32
|
+
| `--min-contrast <num>` | `4.5` | Minimum contrast ratio to enforce |
|
|
33
|
+
| `--neutral-tint <num>` | `0.04` | Neutral tint ratio |
|
|
34
|
+
| `--config <path>` | — | Path to a JSON config file |
|
|
35
|
+
|
|
36
|
+
You can also drop a `eluan-theme.config.json` in your project root; CLI flags override its values.
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{ "name": "ocean", "accents": ["#FF4A2C"], "outDir": "./tokens" }
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The generator writes primitive + semantic token JSON, theme CSS, and a smoke-test preview HTML you can open to eyeball the result.
|
|
43
|
+
|
|
44
|
+
## Programmatic API
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { generateTheme } from "@eluan/theme-generator"
|
|
48
|
+
|
|
49
|
+
const result = generateTheme({
|
|
50
|
+
name: "ocean",
|
|
51
|
+
accents: ["#FF4A2C"],
|
|
52
|
+
minContrast: 4.5,
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Lower-level helpers are exported too — `generateScale`, `buildPrimitives`, `buildSemanticMap`, `checkContrast`, `autoCorrect`, `compileThemeCSS`, and the `hexToOklch` / `oklchToHex` color utilities. See the exported TypeScript types for the full shape of `GeneratorResult`.
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
MIT
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { writeFileSync, mkdirSync, existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { resolve, join } from 'node:path';
|
|
4
|
+
import { parseArgs } from 'node:util';
|
|
5
|
+
import prompts from 'prompts';
|
|
6
|
+
import { generateTheme } from '../generate.js';
|
|
7
|
+
import { generateSmokeHTML } from '../preview/smoke.js';
|
|
8
|
+
import { compilePrimitivesJSON, compileSemanticJSON } from '../compile/css.js';
|
|
9
|
+
import { normalizeHex } from '../input/validate.js';
|
|
10
|
+
function printUsage() {
|
|
11
|
+
console.log(`
|
|
12
|
+
eluan-theme generate — Generate a complete Eluan theme from accent colors
|
|
13
|
+
|
|
14
|
+
Usage:
|
|
15
|
+
eluan-theme generate --accent "#FF4A2C" [--accent "#0F3D3E"] [options]
|
|
16
|
+
eluan-theme generate (interactive mode)
|
|
17
|
+
|
|
18
|
+
Options:
|
|
19
|
+
--accent <hex> Accent color (1-3 allowed, repeat flag for multiple)
|
|
20
|
+
--name <string> Theme name (default: "custom")
|
|
21
|
+
--out <dir> Output directory (default: ./tokens)
|
|
22
|
+
--min-contrast <num> Minimum contrast ratio (default: 4.5)
|
|
23
|
+
--neutral-tint <num> Neutral tint ratio (default: 0.04)
|
|
24
|
+
--config <path> Path to config file (JSON)
|
|
25
|
+
--help Show this help
|
|
26
|
+
|
|
27
|
+
Config file:
|
|
28
|
+
Place a eluan-theme.config.json in your project root, or pass --config <path>.
|
|
29
|
+
Schema: { "name": "ocean", "accents": ["#FF4A2C"], "outDir": "./tokens" }
|
|
30
|
+
CLI flags override config file values.
|
|
31
|
+
`);
|
|
32
|
+
}
|
|
33
|
+
function loadConfigFile(configPath) {
|
|
34
|
+
if (configPath) {
|
|
35
|
+
const resolved = resolve(configPath);
|
|
36
|
+
if (!existsSync(resolved)) {
|
|
37
|
+
console.error(`Config file not found: ${resolved}`);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
return JSON.parse(readFileSync(resolved, 'utf-8'));
|
|
41
|
+
}
|
|
42
|
+
const defaultPath = resolve('eluan-theme.config.json');
|
|
43
|
+
if (existsSync(defaultPath)) {
|
|
44
|
+
return JSON.parse(readFileSync(defaultPath, 'utf-8'));
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
function isValidHex(input) {
|
|
49
|
+
return /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(input.trim());
|
|
50
|
+
}
|
|
51
|
+
async function runInteractive() {
|
|
52
|
+
console.log('');
|
|
53
|
+
console.log('eluan-theme — Interactive theme generator');
|
|
54
|
+
console.log('');
|
|
55
|
+
const nameResponse = await prompts({
|
|
56
|
+
type: 'text',
|
|
57
|
+
name: 'themeName',
|
|
58
|
+
message: 'Theme name',
|
|
59
|
+
initial: 'custom',
|
|
60
|
+
validate: (v) => v.trim().length > 0 || 'Theme name cannot be empty',
|
|
61
|
+
});
|
|
62
|
+
if (!nameResponse.themeName)
|
|
63
|
+
process.exit(0);
|
|
64
|
+
const accents = [];
|
|
65
|
+
for (let i = 0; i < 3; i++) {
|
|
66
|
+
const label = i === 0 ? 'Primary accent color (hex)' : `Accent color ${i + 1} (hex, leave empty to skip)`;
|
|
67
|
+
const response = await prompts({
|
|
68
|
+
type: 'text',
|
|
69
|
+
name: 'color',
|
|
70
|
+
message: label,
|
|
71
|
+
validate: (v) => {
|
|
72
|
+
if (i > 0 && v.trim() === '')
|
|
73
|
+
return true;
|
|
74
|
+
if (i === 0 && v.trim() === '')
|
|
75
|
+
return 'At least one accent color is required';
|
|
76
|
+
return isValidHex(v) || 'Invalid hex color. Use #RGB, #RRGGBB, or #RRGGBBAA format.';
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
if (response.color === undefined)
|
|
80
|
+
process.exit(0);
|
|
81
|
+
if (response.color.trim() === '')
|
|
82
|
+
break;
|
|
83
|
+
try {
|
|
84
|
+
const { hex } = normalizeHex(response.color);
|
|
85
|
+
accents.push(hex);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
console.error(`Invalid color: ${response.color}`);
|
|
89
|
+
process.exit(1);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const outResponse = await prompts({
|
|
93
|
+
type: 'text',
|
|
94
|
+
name: 'outDir',
|
|
95
|
+
message: 'Output directory',
|
|
96
|
+
initial: './tokens',
|
|
97
|
+
});
|
|
98
|
+
if (!outResponse.outDir)
|
|
99
|
+
process.exit(0);
|
|
100
|
+
console.log('');
|
|
101
|
+
console.log('Summary:');
|
|
102
|
+
console.log(` Theme name: ${nameResponse.themeName}`);
|
|
103
|
+
console.log(` Accents: ${accents.join(', ')}`);
|
|
104
|
+
console.log(` Output: ${resolve(outResponse.outDir)}`);
|
|
105
|
+
console.log('');
|
|
106
|
+
const confirm = await prompts({
|
|
107
|
+
type: 'confirm',
|
|
108
|
+
name: 'proceed',
|
|
109
|
+
message: 'Generate theme?',
|
|
110
|
+
initial: true,
|
|
111
|
+
});
|
|
112
|
+
if (!confirm.proceed) {
|
|
113
|
+
console.log('Aborted.');
|
|
114
|
+
process.exit(0);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
accents,
|
|
118
|
+
themeName: nameResponse.themeName,
|
|
119
|
+
minContrast: 4.5,
|
|
120
|
+
neutralTintRatio: 0.04,
|
|
121
|
+
outDir: outResponse.outDir,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function parseCliArgs() {
|
|
125
|
+
const { values } = parseArgs({
|
|
126
|
+
options: {
|
|
127
|
+
accent: { type: 'string', multiple: true },
|
|
128
|
+
name: { type: 'string' },
|
|
129
|
+
out: { type: 'string' },
|
|
130
|
+
'min-contrast': { type: 'string' },
|
|
131
|
+
'neutral-tint': { type: 'string' },
|
|
132
|
+
config: { type: 'string' },
|
|
133
|
+
help: { type: 'boolean', default: false },
|
|
134
|
+
},
|
|
135
|
+
allowPositionals: true,
|
|
136
|
+
strict: false,
|
|
137
|
+
});
|
|
138
|
+
if (values.help) {
|
|
139
|
+
printUsage();
|
|
140
|
+
process.exit(0);
|
|
141
|
+
}
|
|
142
|
+
const config = loadConfigFile(values.config);
|
|
143
|
+
const cliAccents = values.accent;
|
|
144
|
+
const accents = (cliAccents && cliAccents.length > 0) ? cliAccents : (config?.accents ?? []);
|
|
145
|
+
const themeName = values.name ?? config?.name ?? 'custom';
|
|
146
|
+
const minContrast = values['min-contrast']
|
|
147
|
+
? parseFloat(values['min-contrast'])
|
|
148
|
+
: (config?.minContrast ?? 4.5);
|
|
149
|
+
const neutralTintRatio = values['neutral-tint']
|
|
150
|
+
? parseFloat(values['neutral-tint'])
|
|
151
|
+
: (config?.neutralTintRatio ?? 0.04);
|
|
152
|
+
const outDir = values.out ?? config?.outDir ?? './tokens';
|
|
153
|
+
if (accents.length === 0) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
return { accents, themeName, minContrast, neutralTintRatio, outDir };
|
|
157
|
+
}
|
|
158
|
+
function generate(opts) {
|
|
159
|
+
const { outDir, ...config } = opts;
|
|
160
|
+
const resolvedOut = resolve(outDir);
|
|
161
|
+
console.log(`Generating theme "${config.themeName}"...`);
|
|
162
|
+
console.log(` Accents: ${config.accents.join(', ')}`);
|
|
163
|
+
console.log(` Min contrast: ${config.minContrast}:1`);
|
|
164
|
+
console.log(` Output: ${resolvedOut}`);
|
|
165
|
+
console.log('');
|
|
166
|
+
const result = generateTheme(config);
|
|
167
|
+
mkdirSync(resolvedOut, { recursive: true });
|
|
168
|
+
writeFileSync(join(resolvedOut, 'primitives.json'), compilePrimitivesJSON(result.primitives));
|
|
169
|
+
writeFileSync(join(resolvedOut, 'semantic.json'), compileSemanticJSON(result.semanticMap, result.primitives));
|
|
170
|
+
const fullCSS = [
|
|
171
|
+
result.css.primitives,
|
|
172
|
+
'',
|
|
173
|
+
result.css.theme,
|
|
174
|
+
].join('\n\n');
|
|
175
|
+
writeFileSync(join(resolvedOut, 'theme.css'), fullCSS);
|
|
176
|
+
const previewHTML = generateSmokeHTML(result.primitives, result.semanticMap, config.themeName);
|
|
177
|
+
writeFileSync(join(resolvedOut, 'preview.html'), previewHTML);
|
|
178
|
+
writeFileSync(join(resolvedOut, 'report.md'), result.report);
|
|
179
|
+
if (result.corrections.length > 0) {
|
|
180
|
+
console.log(`⚠ ${result.corrections.length} contrast correction(s) applied:`);
|
|
181
|
+
for (const c of result.corrections) {
|
|
182
|
+
console.log(` ${c.token} (${c.mode}): ${c.originalRef} → ${c.correctedRef} (${c.originalContrast.toFixed(2)} → ${c.correctedContrast.toFixed(2)})`);
|
|
183
|
+
}
|
|
184
|
+
console.log('');
|
|
185
|
+
}
|
|
186
|
+
console.log('Output files:');
|
|
187
|
+
console.log(` ${join(resolvedOut, 'primitives.json')}`);
|
|
188
|
+
console.log(` ${join(resolvedOut, 'semantic.json')}`);
|
|
189
|
+
console.log(` ${join(resolvedOut, 'theme.css')}`);
|
|
190
|
+
console.log(` ${join(resolvedOut, 'preview.html')}`);
|
|
191
|
+
console.log(` ${join(resolvedOut, 'report.md')}`);
|
|
192
|
+
console.log('');
|
|
193
|
+
console.log('Done.');
|
|
194
|
+
}
|
|
195
|
+
async function main() {
|
|
196
|
+
const command = process.argv[2];
|
|
197
|
+
if (command !== 'generate') {
|
|
198
|
+
printUsage();
|
|
199
|
+
process.exit(command ? 1 : 0);
|
|
200
|
+
}
|
|
201
|
+
let opts = parseCliArgs();
|
|
202
|
+
if (!opts) {
|
|
203
|
+
opts = await runInteractive();
|
|
204
|
+
}
|
|
205
|
+
generate(opts);
|
|
206
|
+
}
|
|
207
|
+
main();
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { converter, formatHex, clampChroma } from 'culori';
|
|
2
|
+
const toOklch = converter('oklch');
|
|
3
|
+
export function hexToOklch(hex) {
|
|
4
|
+
const parsed = toOklch(hex);
|
|
5
|
+
if (!parsed) {
|
|
6
|
+
throw new Error(`Failed to convert "${hex}" to OKLCH.`);
|
|
7
|
+
}
|
|
8
|
+
return {
|
|
9
|
+
mode: 'oklch',
|
|
10
|
+
l: parsed.l,
|
|
11
|
+
c: parsed.c,
|
|
12
|
+
h: parsed.h ?? 0,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function oklchToHex(color) {
|
|
16
|
+
const mapped = gamutMap(color);
|
|
17
|
+
const hex = formatHex({ mode: 'oklch', l: mapped.l, c: mapped.c, h: mapped.h });
|
|
18
|
+
return hex;
|
|
19
|
+
}
|
|
20
|
+
export function gamutMap(color) {
|
|
21
|
+
const clamped = clampChroma({ mode: 'oklch', l: color.l, c: color.c, h: color.h }, 'oklch');
|
|
22
|
+
return {
|
|
23
|
+
mode: 'oklch',
|
|
24
|
+
l: clamped.l,
|
|
25
|
+
c: clamped.c,
|
|
26
|
+
h: clamped.h ?? color.h,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const DEFAULT_CURVE = [
|
|
2
|
+
{ step: 50, lightness: 0.97, chromaRatio: 0.10, hueShift: 0 },
|
|
3
|
+
{ step: 100, lightness: 0.93, chromaRatio: 0.18, hueShift: 0 },
|
|
4
|
+
{ step: 200, lightness: 0.87, chromaRatio: 0.35, hueShift: 0 },
|
|
5
|
+
{ step: 300, lightness: 0.78, chromaRatio: 0.60, hueShift: 0 },
|
|
6
|
+
{ step: 400, lightness: 0.68, chromaRatio: 0.85, hueShift: 0 },
|
|
7
|
+
{ step: 500, lightness: 0.58, chromaRatio: 1.00, hueShift: 0 },
|
|
8
|
+
{ step: 600, lightness: 0.50, chromaRatio: 0.95, hueShift: 0 },
|
|
9
|
+
{ step: 700, lightness: 0.42, chromaRatio: 0.80, hueShift: 0 },
|
|
10
|
+
{ step: 800, lightness: 0.35, chromaRatio: 0.60, hueShift: 0 },
|
|
11
|
+
{ step: 900, lightness: 0.27, chromaRatio: 0.40, hueShift: 0 },
|
|
12
|
+
{ step: 950, lightness: 0.18, chromaRatio: 0.25, hueShift: 0 },
|
|
13
|
+
];
|
|
14
|
+
export function validateCurve(curve) {
|
|
15
|
+
if (curve.length !== 11) {
|
|
16
|
+
throw new Error(`Curve must have exactly 11 steps, got ${curve.length}.`);
|
|
17
|
+
}
|
|
18
|
+
for (let i = 1; i < curve.length; i++) {
|
|
19
|
+
if (curve[i].lightness >= curve[i - 1].lightness) {
|
|
20
|
+
throw new Error(`Lightness must be strictly monotonically decreasing. ` +
|
|
21
|
+
`Step ${curve[i].step} (L=${curve[i].lightness}) >= step ${curve[i - 1].step} (L=${curve[i - 1].lightness}).`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
for (const step of curve) {
|
|
25
|
+
if (step.chromaRatio < 0 || step.chromaRatio > 1) {
|
|
26
|
+
throw new Error(`chromaRatio must be in [0, 1]. Step ${step.step} has chromaRatio=${step.chromaRatio}.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { generateScale } from './scale.js';
|
|
2
|
+
const ABSOLUTE_CHROMA_CEILING = 0.02;
|
|
3
|
+
export function generateNeutralScale(accentHue, accentPeakChroma, tintRatio, curve) {
|
|
4
|
+
const rawChroma = accentPeakChroma * tintRatio;
|
|
5
|
+
const clampedChroma = Math.min(rawChroma, ABSOLUTE_CHROMA_CEILING);
|
|
6
|
+
return generateScale(accentHue, clampedChroma, curve);
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { gamutMap, oklchToHex } from './convert.js';
|
|
2
|
+
export function generateScale(hue, peakChroma, curve) {
|
|
3
|
+
const scale = {};
|
|
4
|
+
for (const step of curve) {
|
|
5
|
+
const rawOklch = {
|
|
6
|
+
mode: 'oklch',
|
|
7
|
+
l: step.lightness,
|
|
8
|
+
c: peakChroma * step.chromaRatio,
|
|
9
|
+
h: hue + step.hueShift,
|
|
10
|
+
};
|
|
11
|
+
const mapped = gamutMap(rawOklch);
|
|
12
|
+
const hex = oklchToHex(rawOklch);
|
|
13
|
+
scale[step.step] = {
|
|
14
|
+
oklch: mapped,
|
|
15
|
+
hex,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return scale;
|
|
19
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Scale, ScaleStep, SystemHueName } from '../types.js';
|
|
2
|
+
export declare const DEFAULT_SYSTEM_HUES: Record<SystemHueName, number>;
|
|
3
|
+
export declare function generateSystemScales(accentPeakChroma: number, accentIsLowChroma: boolean, curve: ScaleStep[], overrides?: Partial<Record<SystemHueName, number>>): Record<SystemHueName, Scale>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { generateScale } from './scale.js';
|
|
2
|
+
export const DEFAULT_SYSTEM_HUES = {
|
|
3
|
+
error: 27,
|
|
4
|
+
warning: 75,
|
|
5
|
+
success: 147,
|
|
6
|
+
info: 255,
|
|
7
|
+
};
|
|
8
|
+
const LOW_CHROMA_FALLBACK_HUE = 250;
|
|
9
|
+
export function generateSystemScales(accentPeakChroma, accentIsLowChroma, curve, overrides) {
|
|
10
|
+
const hues = {
|
|
11
|
+
...DEFAULT_SYSTEM_HUES,
|
|
12
|
+
...overrides,
|
|
13
|
+
};
|
|
14
|
+
if (accentIsLowChroma) {
|
|
15
|
+
for (const key of Object.keys(hues)) {
|
|
16
|
+
if (!overrides?.[key]) {
|
|
17
|
+
hues[key] = LOW_CHROMA_FALLBACK_HUE;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const systemChroma = accentIsLowChroma ? 0.14 : accentPeakChroma * 0.9;
|
|
22
|
+
return {
|
|
23
|
+
error: generateScale(hues.error, systemChroma, curve),
|
|
24
|
+
warning: generateScale(hues.warning, systemChroma, curve),
|
|
25
|
+
success: generateScale(hues.success, systemChroma, curve),
|
|
26
|
+
info: generateScale(hues.info, systemChroma, curve),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PrimitiveTokens, SemanticMap } from '../types.js';
|
|
2
|
+
export declare function compilePrimitivesJSON(primitives: PrimitiveTokens): string;
|
|
3
|
+
export declare function compileSemanticJSON(map: SemanticMap, primitives: PrimitiveTokens): string;
|
|
4
|
+
export declare function compilePrimitivesCSS(primitives: PrimitiveTokens, themeName: string): string;
|
|
5
|
+
export declare function compileModeCSS(map: SemanticMap, primitives: PrimitiveTokens, mode: 'light' | 'dark', themeName: string): string;
|
|
6
|
+
export declare function compileThemeCSS(map: SemanticMap, primitives: PrimitiveTokens, themeName: string, inputHash: string): string;
|
|
7
|
+
export declare function compileCreateThemeTokens(map: SemanticMap, primitives: PrimitiveTokens, mode: 'light' | 'dark'): Record<string, string>;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
const SCALE_STEPS = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
|
|
2
|
+
function resolveHex(ref, primitives) {
|
|
3
|
+
if (ref === 'white')
|
|
4
|
+
return '#ffffff';
|
|
5
|
+
if (ref === 'black')
|
|
6
|
+
return '#000000';
|
|
7
|
+
const [scaleName, stepStr] = ref.split('.');
|
|
8
|
+
const step = Number(stepStr);
|
|
9
|
+
return primitives[scaleName][step].hex;
|
|
10
|
+
}
|
|
11
|
+
function scaleToW3C(name, scale) {
|
|
12
|
+
const group = {};
|
|
13
|
+
for (const step of SCALE_STEPS) {
|
|
14
|
+
const entry = scale[step];
|
|
15
|
+
group[String(step)] = {
|
|
16
|
+
$type: 'color',
|
|
17
|
+
$value: entry.hex,
|
|
18
|
+
$extensions: {
|
|
19
|
+
'com.eluan': {
|
|
20
|
+
oklch: { l: entry.oklch.l, c: entry.oklch.c, h: entry.oklch.h },
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return group;
|
|
26
|
+
}
|
|
27
|
+
export function compilePrimitivesJSON(primitives) {
|
|
28
|
+
const doc = {};
|
|
29
|
+
for (const [name, scale] of Object.entries(primitives)) {
|
|
30
|
+
if (scale) {
|
|
31
|
+
doc[name] = scaleToW3C(name, scale);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return JSON.stringify(doc, null, 2);
|
|
35
|
+
}
|
|
36
|
+
export function compileSemanticJSON(map, primitives) {
|
|
37
|
+
const doc = {};
|
|
38
|
+
for (const [token, entry] of Object.entries(map)) {
|
|
39
|
+
doc[token] = {
|
|
40
|
+
$type: 'color',
|
|
41
|
+
light: {
|
|
42
|
+
$value: resolveHex(entry.light, primitives),
|
|
43
|
+
$ref: entry.light,
|
|
44
|
+
},
|
|
45
|
+
dark: {
|
|
46
|
+
$value: resolveHex(entry.dark, primitives),
|
|
47
|
+
$ref: entry.dark,
|
|
48
|
+
},
|
|
49
|
+
role: entry.role,
|
|
50
|
+
...(entry.contrastAgainst ? { contrastAgainst: entry.contrastAgainst } : {}),
|
|
51
|
+
...(entry.minContrast ? { minContrast: entry.minContrast } : {}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return JSON.stringify(doc, null, 2);
|
|
55
|
+
}
|
|
56
|
+
function compilePrimitiveCSSBlock(scaleName, scale, prefix) {
|
|
57
|
+
const lines = [];
|
|
58
|
+
for (const step of SCALE_STEPS) {
|
|
59
|
+
const entry = scale[step];
|
|
60
|
+
lines.push(` --color-${prefix}-${step}: ${entry.hex};`);
|
|
61
|
+
}
|
|
62
|
+
return lines.join('\n');
|
|
63
|
+
}
|
|
64
|
+
export function compilePrimitivesCSS(primitives, themeName) {
|
|
65
|
+
const blocks = [];
|
|
66
|
+
for (const [name, scale] of Object.entries(primitives)) {
|
|
67
|
+
if (!scale)
|
|
68
|
+
continue;
|
|
69
|
+
blocks.push(`\n /* ${name} */`);
|
|
70
|
+
blocks.push(compilePrimitiveCSSBlock(name, scale, name));
|
|
71
|
+
}
|
|
72
|
+
return `/* Generated by eluan-theme-generator — ${themeName} primitives */\n\n[data-theme="${themeName}"] {\n${blocks.join('\n')}\n}`;
|
|
73
|
+
}
|
|
74
|
+
function compileModeCSSBlock(map, primitives, mode, scaleName, cssPrefix) {
|
|
75
|
+
const scale = primitives[scaleName];
|
|
76
|
+
if (!scale)
|
|
77
|
+
return '';
|
|
78
|
+
const lines = [];
|
|
79
|
+
const ref = mode === 'light' ? 'light' : 'dark';
|
|
80
|
+
const mainStep = mode === 'light' ? 500 : 400;
|
|
81
|
+
const bgStep = mode === 'light' ? 50 : 950;
|
|
82
|
+
const tintStep = mode === 'light' ? 100 : 900;
|
|
83
|
+
const fgStep = mode === 'light' ? 700 : 200;
|
|
84
|
+
const shadeStep = mode === 'light' ? 800 : 300;
|
|
85
|
+
lines.push(` --${cssPrefix}-main: ${scale[mainStep].hex};`);
|
|
86
|
+
lines.push(` --${cssPrefix}-background: ${scale[bgStep].hex};`);
|
|
87
|
+
lines.push(` --${cssPrefix}-tint: ${scale[tintStep].hex};`);
|
|
88
|
+
lines.push(` --${cssPrefix}-foreground: ${scale[fgStep].hex};`);
|
|
89
|
+
lines.push(` --${cssPrefix}-shade: ${scale[shadeStep].hex};`);
|
|
90
|
+
return lines.join('\n');
|
|
91
|
+
}
|
|
92
|
+
export function compileModeCSS(map, primitives, mode, themeName) {
|
|
93
|
+
const lines = [];
|
|
94
|
+
const neutral = primitives.neutral;
|
|
95
|
+
if (!neutral)
|
|
96
|
+
throw new Error('Neutral scale is required');
|
|
97
|
+
const bgSteps = mode === 'light'
|
|
98
|
+
? { primary: 50, secondary: 100, tertiary: 200, quaternary: 300, quinary: 400 }
|
|
99
|
+
: { primary: 800, secondary: 900, tertiary: 950, quaternary: 700, quinary: 600 };
|
|
100
|
+
const fgSteps = mode === 'light'
|
|
101
|
+
? { primary: 950, secondary: 800, tertiary: 600, quaternary: 500, quinary: 400 }
|
|
102
|
+
: { primary: 50, secondary: 100, tertiary: 200, quaternary: 300, quinary: 400 };
|
|
103
|
+
lines.push(` /* Backgrounds */`);
|
|
104
|
+
for (const [level, step] of Object.entries(bgSteps)) {
|
|
105
|
+
lines.push(` --backgrounds-${level}: ${neutral[step].hex};`);
|
|
106
|
+
}
|
|
107
|
+
lines.push(`\n /* Foregrounds */`);
|
|
108
|
+
for (const [level, step] of Object.entries(fgSteps)) {
|
|
109
|
+
lines.push(` --foregrounds-${level}: ${neutral[step].hex};`);
|
|
110
|
+
}
|
|
111
|
+
const paletteScales = [
|
|
112
|
+
['primary', 'accent'],
|
|
113
|
+
['error', 'error'],
|
|
114
|
+
['warning', 'warning'],
|
|
115
|
+
['success', 'success'],
|
|
116
|
+
['info', 'info'],
|
|
117
|
+
];
|
|
118
|
+
for (const [scaleName, cssPrefix] of paletteScales) {
|
|
119
|
+
const block = compileModeCSSBlock(map, primitives, mode, scaleName, cssPrefix);
|
|
120
|
+
if (block) {
|
|
121
|
+
lines.push(`\n /* ${cssPrefix} */`);
|
|
122
|
+
lines.push(block);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const modeLabel = mode === 'light' ? 'Light' : 'Dark';
|
|
126
|
+
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}`;
|
|
127
|
+
}
|
|
128
|
+
export function compileThemeCSS(map, primitives, themeName, inputHash) {
|
|
129
|
+
const lines = [];
|
|
130
|
+
lines.push(`/* Generated by eluan-theme-generator v0.1.0 */`);
|
|
131
|
+
lines.push(`/* Input hash: ${inputHash} */`);
|
|
132
|
+
lines.push(`/* Theme: ${themeName} */`);
|
|
133
|
+
lines.push('');
|
|
134
|
+
const lightLines = [];
|
|
135
|
+
const darkLines = [];
|
|
136
|
+
for (const [token, entry] of Object.entries(map)) {
|
|
137
|
+
const lightHex = resolveHex(entry.light, primitives);
|
|
138
|
+
const darkHex = resolveHex(entry.dark, primitives);
|
|
139
|
+
lightLines.push(` --${token}: ${lightHex}; /* ${entry.light} */`);
|
|
140
|
+
darkLines.push(` --${token}: ${darkHex}; /* ${entry.dark} */`);
|
|
141
|
+
}
|
|
142
|
+
lines.push(`[data-theme="${themeName}"][data-mode="light"],`);
|
|
143
|
+
lines.push(`[data-theme="${themeName}"][data-mode="Light"] {`);
|
|
144
|
+
lines.push(lightLines.join('\n'));
|
|
145
|
+
lines.push('}');
|
|
146
|
+
lines.push('');
|
|
147
|
+
lines.push(`[data-theme="${themeName}"][data-mode="dark"],`);
|
|
148
|
+
lines.push(`[data-theme="${themeName}"][data-mode="Dark"] {`);
|
|
149
|
+
lines.push(darkLines.join('\n'));
|
|
150
|
+
lines.push('}');
|
|
151
|
+
return lines.join('\n');
|
|
152
|
+
}
|
|
153
|
+
export function compileCreateThemeTokens(map, primitives, mode) {
|
|
154
|
+
const tokens = {};
|
|
155
|
+
for (const [token, entry] of Object.entries(map)) {
|
|
156
|
+
const ref = mode === 'light' ? entry.light : entry.dark;
|
|
157
|
+
tokens[`--${token}`] = resolveHex(ref, primitives);
|
|
158
|
+
}
|
|
159
|
+
return tokens;
|
|
160
|
+
}
|