medesign 1.4.0 → 1.6.1

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.
Files changed (51) hide show
  1. package/README.md +90 -72
  2. package/bin/mebolder.js +4 -0
  3. package/bin/medesign.js +370 -35
  4. package/bin/melint.js +4 -0
  5. package/bin/mepolish.js +4 -0
  6. package/bin/mequiet.js +4 -0
  7. package/bin/mesearch.js +4 -0
  8. package/bin/mesnapshot.js +4 -0
  9. package/examples/cadastral-qaqc-slide.html +577 -0
  10. package/examples/executive-summary-report.html +597 -0
  11. package/examples/executive-summary-report.pdf +0 -0
  12. package/examples/geospatial-banner-1200x630.html +370 -0
  13. package/examples/geospatial-banner-1200x630.svg +149 -0
  14. package/examples/linear-issue-board.html +1273 -0
  15. package/examples/snapshots/cadastral-qaqc-slide-desktop-1440x900.png +0 -0
  16. package/examples/snapshots/cadastral-qaqc-slide-mobile-390x844.png +0 -0
  17. package/examples/snapshots/cadastral-qaqc-slide-tablet-820x1180.png +0 -0
  18. package/examples/snapshots/executive-summary-report-desktop-1440x900.png +0 -0
  19. package/examples/snapshots/executive-summary-report-mobile-390x844.png +0 -0
  20. package/examples/snapshots/executive-summary-report-tablet-820x1180.png +0 -0
  21. package/examples/snapshots/geospatial-banner-1200x630-og-banner-1200x630.png +0 -0
  22. package/examples/snapshots/linear-issue-board-desktop-1440x900.png +0 -0
  23. package/examples/snapshots/linear-issue-board-mobile-390x844.png +0 -0
  24. package/examples/snapshots/linear-issue-board-tablet-820x1180.png +0 -0
  25. package/examples/snapshots/spotify-web-player-desktop-1440x900.png +0 -0
  26. package/examples/snapshots/spotify-web-player-mobile-390x844.png +0 -0
  27. package/examples/snapshots/spotify-web-player-tablet-820x1180.png +0 -0
  28. package/examples/spotify-web-player.html +1446 -0
  29. package/package.json +16 -4
  30. package/presets/metha/guidelines.md +26 -17
  31. package/presets/metha/preset.json +16 -16
  32. package/references/ai-tells.md +9 -2
  33. package/references/brand-design-tokens.md +27 -28
  34. package/references/brief-intake.md +90 -0
  35. package/references/catalog.json +1031 -1023
  36. package/references/cli-architecture.md +1 -1
  37. package/references/compositor-smoothness.md +161 -0
  38. package/references/design-core.md +11 -4
  39. package/references/executive-reports.md +234 -0
  40. package/references/image-mockup-direction.md +53 -53
  41. package/references/image-to-code.md +46 -46
  42. package/references/modern-craft-invariants.md +55 -9
  43. package/references/prompt-templates.md +49 -0
  44. package/references/quality-gate.md +42 -12
  45. package/references/typography-thai-latin.md +206 -0
  46. package/references/view-transitions-engine.md +89 -0
  47. package/references/web-retrofit-protocol.md +103 -0
  48. package/scripts/export-tokens.js +419 -403
  49. package/tests/browser-audit.js +463 -442
  50. package/tests/linter.js +49 -4
  51. package/tests/snapshot.js +266 -0
@@ -1,403 +1,419 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * ME-DESIGN Token Exporter
5
- * Converts preset design tokens into industry standard formats:
6
- * 1. Tailwind CSS v4 `@theme` block
7
- * 2. W3C Design Tokens Community Group (DTCG) standard specification JSON
8
- * 3. Standard CSS Custom Properties (:root { ... })
9
- *
10
- * Usage:
11
- * node scripts/export-tokens.js [--preset <name|all>] [--format <tailwind|dtcg|css|all>] [--out <dir>]
12
- */
13
-
14
- const fs = require('fs');
15
- const path = require('path');
16
-
17
- const presetsDir = path.join(__dirname, '..', 'presets');
18
-
19
- // Parse CLI Arguments
20
- const args = process.argv.slice(2);
21
- let targetPreset = 'all';
22
- let targetFormat = 'all';
23
- let outDir = null;
24
-
25
- for (let i = 0; i < args.length; i++) {
26
- if (args[i] === '--preset' && args[i + 1]) {
27
- targetPreset = args[++i].toLowerCase();
28
- } else if (args[i] === '--format' && args[i + 1]) {
29
- targetFormat = args[++i].toLowerCase();
30
- } else if (args[i] === '--out' && args[i + 1]) {
31
- outDir = args[++i];
32
- }
33
- }
34
-
35
- // Discover available presets
36
- const presetFolders = fs.readdirSync(presetsDir, { withFileTypes: true })
37
- .filter(d => d.isDirectory())
38
- .map(d => d.name)
39
- .filter(name => fs.existsSync(path.join(presetsDir, name, 'preset.json')));
40
-
41
- const selectedPresets = targetPreset === 'all'
42
- ? presetFolders
43
- : presetFolders.filter(p => p.toLowerCase() === targetPreset);
44
-
45
- if (selectedPresets.length === 0) {
46
- console.error(`Error: Preset '${targetPreset}' not found. Available presets: ${presetFolders.join(', ')}`);
47
- process.exit(1);
48
- }
49
-
50
- // Format 1: Tailwind CSS v4 @theme Generator
51
- function toTailwindV4Theme(preset) {
52
- const lines = ['/* Tailwind CSS v4 @theme configuration */', '/* Generated by ME-DESIGN Token Engine */', '@theme {'];
53
-
54
- // Colors
55
- if (preset.color) {
56
- if (preset.color.canvas) lines.push(` --color-canvas: ${preset.color.canvas};`);
57
- if (preset.color.surfaceRaised) lines.push(` --color-surface-raised: ${preset.color.surfaceRaised};`);
58
- if (preset.color.surfaceInset) lines.push(` --color-surface-inset: ${preset.color.surfaceInset};`);
59
- if (preset.color.borderSubtle) lines.push(` --color-border-subtle: ${preset.color.borderSubtle};`);
60
- if (preset.color.borderFocus) lines.push(` --color-border-focus: ${preset.color.borderFocus};`);
61
- if (preset.color.accent) lines.push(` --color-accent: ${preset.color.accent};`);
62
- if (preset.color.accentForeground) lines.push(` --color-accent-foreground: ${preset.color.accentForeground};`);
63
-
64
- if (preset.color.status) {
65
- Object.entries(preset.color.status).forEach(([k, v]) => {
66
- lines.push(` --color-status-${k}: ${v};`);
67
- });
68
- }
69
- }
70
-
71
- // Typography
72
- if (preset.typography) {
73
- if (preset.typography.display) lines.push(` --font-display: ${preset.typography.display};`);
74
- if (preset.typography.sans) lines.push(` --font-sans: ${preset.typography.sans};`);
75
- if (preset.typography.mono) lines.push(` --font-mono: ${preset.typography.mono};`);
76
- if (preset.typography.headingTracking) lines.push(` --tracking-heading: ${preset.typography.headingTracking};`);
77
- if (preset.typography.uiLineHeight) lines.push(` --leading-ui: ${preset.typography.uiLineHeight};`);
78
- if (preset.typography.proseLineHeight) lines.push(` --leading-prose: ${preset.typography.proseLineHeight};`);
79
- }
80
-
81
- // Geometry
82
- if (preset.geometry) {
83
- if (preset.geometry.radiusBase) lines.push(` --radius-base: ${preset.geometry.radiusBase};`);
84
- if (preset.geometry.radiusOuter) lines.push(` --radius-outer: ${preset.geometry.radiusOuter};`);
85
- if (preset.geometry.hitTargetMinTouch) lines.push(` --hit-target-min: ${preset.geometry.hitTargetMinTouch};`);
86
- }
87
-
88
- // Motion
89
- if (preset.motion) {
90
- if (preset.motion.easingEntry) lines.push(` --ease-entry: ${preset.motion.easingEntry};`);
91
- if (preset.motion.easingExit) lines.push(` --ease-exit: ${preset.motion.easingExit};`);
92
- if (preset.motion.durationEntry) lines.push(` --duration-entry: ${preset.motion.durationEntry};`);
93
- if (preset.motion.durationExit) lines.push(` --duration-exit: ${preset.motion.durationExit};`);
94
- }
95
-
96
- lines.push('}');
97
- return lines.join('\n');
98
- }
99
-
100
- function oklchToLinearRgb(l, c, h) {
101
- const rad = h * Math.PI / 180;
102
- const a = c * Math.cos(rad);
103
- const b = c * Math.sin(rad);
104
-
105
- const l1 = l + 0.3963377774 * a + 0.2158037573 * b;
106
- const m1 = l - 0.1055613458 * a - 0.0638541728 * b;
107
- const s1 = l - 0.0894841775 * a - 1.2914855480 * b;
108
-
109
- const l_c = l1 * l1 * l1;
110
- const m_c = m1 * m1 * m1;
111
- const s_c = s1 * s1 * s1;
112
-
113
- const r_lin = +4.0767434721 * l_c - 3.3077115913 * m_c + 0.2309699292 * s_c;
114
- const g_lin = -1.2684380046 * l_c + 2.6097574011 * m_c - 0.3413193965 * s_c;
115
- const b_lin = -0.0041960863 * l_c - 0.7034186147 * m_c + 1.7076147010 * s_c;
116
-
117
- return [r_lin, g_lin, b_lin];
118
- }
119
-
120
- function inGamut(r, g, b, eps = 0.0001) {
121
- return r >= -eps && r <= 1 + eps &&
122
- g >= -eps && g <= 1 + eps &&
123
- b >= -eps && b <= 1 + eps;
124
- }
125
-
126
- // Convert OKLCH to sRGB Hex using CSS Color 4 Gamut Mapping (binary search chroma reduction)
127
- function oklchToHex(colorStr) {
128
- if (!colorStr || !colorStr.startsWith('oklch(')) return null;
129
- const match = colorStr.match(/oklch\(\s*([\d.]+)\s+([\d.]+)\s+([\d.]+)/i);
130
- if (!match) return null;
131
- const l = parseFloat(match[1]);
132
- const c = parseFloat(match[2]);
133
- const h = parseFloat(match[3]);
134
-
135
- const [r0, g0, b0] = oklchToLinearRgb(l, c, h);
136
- let bestRgb = [r0, g0, b0];
137
-
138
- // If outside sRGB gamut, reduce chroma via binary search while preserving lightness and hue angle
139
- if (!inGamut(r0, g0, b0)) {
140
- let cLow = 0;
141
- let cHigh = c;
142
- for (let i = 0; i < 25; i++) {
143
- const cMid = (cLow + cHigh) / 2;
144
- const rgb = oklchToLinearRgb(l, cMid, h);
145
- if (inGamut(rgb[0], rgb[1], rgb[2])) {
146
- cLow = cMid;
147
- bestRgb = rgb;
148
- } else {
149
- cHigh = cMid;
150
- }
151
- }
152
- }
153
-
154
- const gamma = (x) => x <= 0.0031308 ? 12.92 * x : 1.055 * Math.pow(Math.max(0, x), 1 / 2.4) - 0.055;
155
- const R = Math.min(255, Math.max(0, Math.round(gamma(bestRgb[0]) * 255)));
156
- const G = Math.min(255, Math.max(0, Math.round(gamma(bestRgb[1]) * 255)));
157
- const B = Math.min(255, Math.max(0, Math.round(gamma(bestRgb[2]) * 255)));
158
-
159
- return '#' + [R, G, B].map(x => x.toString(16).padStart(2, '0')).join('');
160
- }
161
-
162
- // Format 2: W3C Design Tokens Community Group (DTCG) Generator
163
- // Supports 'oklch' (modern CSS Color 4) or 'srgb' (hex tokens for Figma / legacy tools)
164
- function toDtcgTokens(preset, colorSpace = 'oklch') {
165
- const dtcg = {
166
- $name: preset.name,
167
- $description: preset.description,
168
- $version: '2024.1.0',
169
- color: {},
170
- fontFamily: {},
171
- dimension: {},
172
- duration: {},
173
- cubicBezier: {}
174
- };
175
-
176
- // Colors
177
- if (preset.color) {
178
- Object.entries(preset.color).forEach(([k, v]) => {
179
- if (typeof v === 'string') {
180
- const tokenVal = colorSpace === 'srgb' ? (oklchToHex(v) || v) : v;
181
- dtcg.color[k] = {
182
- $value: tokenVal,
183
- $type: 'color',
184
- $description: `${k} color token for ${preset.name}`
185
- };
186
- } else if (typeof v === 'object') {
187
- dtcg.color[k] = {};
188
- Object.entries(v).forEach(([subK, subV]) => {
189
- const tokenVal = colorSpace === 'srgb' ? (oklchToHex(subV) || subV) : subV;
190
- dtcg.color[k][subK] = {
191
- $value: tokenVal,
192
- $type: 'color',
193
- $description: `${k}.${subK} color token for ${preset.name}`
194
- };
195
- });
196
- }
197
- });
198
- }
199
-
200
- // Typography
201
- if (preset.typography) {
202
- ['display', 'sans', 'mono'].forEach(fontKey => {
203
- if (preset.typography[fontKey]) {
204
- dtcg.fontFamily[fontKey] = {
205
- $value: preset.typography[fontKey],
206
- $type: 'fontFamily',
207
- $description: `${fontKey} font stack`
208
- };
209
- }
210
- });
211
- if (preset.typography.headingTracking) {
212
- dtcg.dimension.headingTracking = {
213
- $value: preset.typography.headingTracking,
214
- $type: 'dimension',
215
- $description: 'Optical negative tracking for headings'
216
- };
217
- }
218
- }
219
-
220
- // Geometry
221
- if (preset.geometry) {
222
- if (preset.geometry.radiusBase) {
223
- dtcg.dimension.radiusBase = {
224
- $value: preset.geometry.radiusBase,
225
- $type: 'dimension',
226
- $description: 'Base corner radius'
227
- };
228
- }
229
- if (preset.geometry.radiusOuter) {
230
- dtcg.dimension.radiusOuter = {
231
- $value: preset.geometry.radiusOuter,
232
- $type: 'dimension',
233
- $description: 'Outer card corner radius'
234
- };
235
- }
236
- if (preset.geometry.hitTargetMinTouch) {
237
- dtcg.dimension.hitTargetMin = {
238
- $value: preset.geometry.hitTargetMinTouch,
239
- $type: 'dimension',
240
- $description: 'WCAG 2.5.8 / HIG minimum touch target dimension (44px)'
241
- };
242
- }
243
- }
244
-
245
- // Motion
246
- if (preset.motion) {
247
- if (preset.motion.durationEntry) {
248
- dtcg.duration.entry = {
249
- $value: preset.motion.durationEntry,
250
- $type: 'duration',
251
- $description: 'Entry motion timing'
252
- };
253
- }
254
- if (preset.motion.durationExit) {
255
- dtcg.duration.exit = {
256
- $value: preset.motion.durationExit,
257
- $type: 'duration',
258
- $description: 'Exit motion timing (asymmetric 0.65x)'
259
- };
260
- }
261
- if (preset.motion.easingEntry) {
262
- dtcg.cubicBezier.entry = {
263
- $value: preset.motion.easingEntry,
264
- $type: 'cubicBezier',
265
- $description: 'Entry cubic-bezier timing function'
266
- };
267
- }
268
- if (preset.motion.easingExit) {
269
- dtcg.cubicBezier.exit = {
270
- $value: preset.motion.easingExit,
271
- $type: 'cubicBezier',
272
- $description: 'Exit cubic-bezier timing function'
273
- };
274
- }
275
- }
276
-
277
- return JSON.stringify(dtcg, null, 2);
278
- }
279
-
280
- // Format 3: CSS Custom Properties Generator (:root { ... })
281
- function toCssVariables(preset) {
282
- const lines = ['/* Standard CSS Custom Properties */', '/* Generated by ME-DESIGN Token Engine */', ':root {'];
283
-
284
- if (preset.color) {
285
- Object.entries(preset.color).forEach(([k, v]) => {
286
- if (typeof v === 'string') {
287
- const cssKey = k.replace(/([A-Z])/g, '-$1').toLowerCase();
288
- lines.push(` --color-${cssKey}: ${v};`);
289
- } else if (typeof v === 'object') {
290
- Object.entries(v).forEach(([subK, subV]) => {
291
- lines.push(` --color-${k}-${subK}: ${subV};`);
292
- });
293
- }
294
- });
295
- }
296
-
297
- if (preset.typography) {
298
- if (preset.typography.sans) lines.push(` --font-sans: ${preset.typography.sans};`);
299
- if (preset.typography.mono) lines.push(` --font-mono: ${preset.typography.mono};`);
300
- if (preset.typography.uiLineHeight) lines.push(` --line-height-ui: ${preset.typography.uiLineHeight};`);
301
- if (preset.typography.proseLineHeight) lines.push(` --line-height-prose: ${preset.typography.proseLineHeight};`);
302
- }
303
-
304
- if (preset.geometry) {
305
- if (preset.geometry.radiusBase) lines.push(` --radius-base: ${preset.geometry.radiusBase};`);
306
- if (preset.geometry.radiusOuter) lines.push(` --radius-outer: ${preset.geometry.radiusOuter};`);
307
- if (preset.geometry.hitTargetMinTouch) lines.push(` --hit-target-min: ${preset.geometry.hitTargetMinTouch};`);
308
- }
309
-
310
- lines.push('}');
311
- return lines.join('\n');
312
- }
313
-
314
- // Execution CLI Guard
315
- if (require.main === module) {
316
- const results = [];
317
-
318
- selectedPresets.forEach(presetId => {
319
- const jsonPath = path.join(presetsDir, presetId, 'preset.json');
320
- const preset = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
321
-
322
- const exportItem = {
323
- preset: presetId,
324
- name: preset.name,
325
- outputs: {}
326
- };
327
-
328
- if (targetFormat === 'tailwind' || targetFormat === 'all') {
329
- exportItem.outputs.tailwind = toTailwindV4Theme(preset);
330
- }
331
- if (targetFormat === 'dtcg' || targetFormat === 'all') {
332
- exportItem.outputs.dtcgOklch = toDtcgTokens(preset, 'oklch');
333
- exportItem.outputs.dtcgSrgb = toDtcgTokens(preset, 'srgb');
334
- exportItem.outputs.dtcg = exportItem.outputs.dtcgOklch;
335
- }
336
- if (targetFormat === 'css' || targetFormat === 'all') {
337
- exportItem.outputs.css = toCssVariables(preset);
338
- }
339
-
340
- results.push(exportItem);
341
- });
342
-
343
- // Output Handling
344
- if (outDir) {
345
- if (!fs.existsSync(outDir)) {
346
- fs.mkdirSync(outDir, { recursive: true });
347
- }
348
-
349
- results.forEach(res => {
350
- if (res.outputs.tailwind) {
351
- fs.writeFileSync(path.join(outDir, `${res.preset}-theme.css`), res.outputs.tailwind, 'utf8');
352
- }
353
- if (res.outputs.dtcgOklch) {
354
- fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.oklch.json`), res.outputs.dtcgOklch, 'utf8');
355
- fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.dtcg.json`), res.outputs.dtcgOklch, 'utf8');
356
- }
357
- if (res.outputs.dtcgSrgb) {
358
- fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.srgb.json`), res.outputs.dtcgSrgb, 'utf8');
359
- }
360
- if (res.outputs.css) {
361
- fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.css`), res.outputs.css, 'utf8');
362
- }
363
- });
364
-
365
- console.log(`Successfully exported tokens for [${selectedPresets.join(', ')}] into '${outDir}'.`);
366
- } else {
367
- // Stdout
368
- results.forEach(res => {
369
- console.log(`\n========================================`);
370
- console.log(`PRESET: ${res.name} (${res.preset})`);
371
- console.log(`========================================\n`);
372
- if (res.outputs.tailwind) {
373
- console.log(`--- [Tailwind v4 @theme] ---`);
374
- console.log(res.outputs.tailwind);
375
- console.log('');
376
- }
377
- if (res.outputs.dtcgOklch) {
378
- console.log(`--- [W3C DTCG Format JSON (OKLCH Native)] ---`);
379
- console.log(res.outputs.dtcgOklch);
380
- console.log('');
381
- }
382
- if (res.outputs.dtcgSrgb) {
383
- console.log(`--- [W3C DTCG Format JSON (sRGB Gamut-Mapped Hex)] ---`);
384
- console.log(res.outputs.dtcgSrgb);
385
- console.log('');
386
- }
387
- if (res.outputs.css) {
388
- console.log(`--- [CSS Custom Properties] ---`);
389
- console.log(res.outputs.css);
390
- console.log('');
391
- }
392
- });
393
- }
394
- }
395
-
396
- module.exports = {
397
- oklchToHex,
398
- oklchToLinearRgb,
399
- inGamut,
400
- toTailwindV4Theme,
401
- toDtcgTokens,
402
- toCssVariables
403
- };
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * ME-DESIGN Token Exporter
5
+ * Converts preset design tokens into industry standard formats:
6
+ * 1. Tailwind CSS v4 `@theme` block
7
+ * 2. W3C Design Tokens Community Group (DTCG) standard specification JSON
8
+ * 3. Standard CSS Custom Properties (:root { ... })
9
+ *
10
+ * Usage:
11
+ * node scripts/export-tokens.js [--preset <name|all>] [--format <tailwind|dtcg|css|all>] [--out <dir>]
12
+ */
13
+
14
+ const fs = require('fs');
15
+ const path = require('path');
16
+
17
+ const presetsDir = path.join(__dirname, '..', 'presets');
18
+
19
+ // Parse CLI Arguments
20
+ const args = process.argv.slice(2);
21
+
22
+ if (process.argv.includes('--help') || process.argv.includes('-h')) {
23
+ console.log(`
24
+ \x1b[1mME-DESIGN Token Exporter\x1b[0m
25
+ \x1b[90mExport preset design tokens to Tailwind v4 @theme, W3C DTCG, or CSS\x1b[0m
26
+
27
+ \x1b[1mUsage:\x1b[0m
28
+ medesign tokens [export] [options]
29
+
30
+ \x1b[1mOptions:\x1b[0m
31
+ --preset <apple|tesla|metha|all> Target preset (default: all)
32
+ --format <tailwind|dtcg|css|all> Target format (default: all)
33
+ --out <directory> Output directory (default: stdout / dist/tokens)
34
+ `);
35
+ process.exit(0);
36
+ }
37
+ let targetPreset = 'all';
38
+ let targetFormat = 'all';
39
+ let outDir = null;
40
+
41
+ for (let i = 0; i < args.length; i++) {
42
+ if (args[i] === '--preset' && args[i + 1]) {
43
+ targetPreset = args[++i].toLowerCase();
44
+ } else if (args[i] === '--format' && args[i + 1]) {
45
+ targetFormat = args[++i].toLowerCase();
46
+ } else if (args[i] === '--out' && args[i + 1]) {
47
+ outDir = args[++i];
48
+ }
49
+ }
50
+
51
+ // Discover available presets
52
+ const presetFolders = fs.readdirSync(presetsDir, { withFileTypes: true })
53
+ .filter(d => d.isDirectory())
54
+ .map(d => d.name)
55
+ .filter(name => fs.existsSync(path.join(presetsDir, name, 'preset.json')));
56
+
57
+ const selectedPresets = targetPreset === 'all'
58
+ ? presetFolders
59
+ : presetFolders.filter(p => p.toLowerCase() === targetPreset);
60
+
61
+ if (selectedPresets.length === 0) {
62
+ console.error(`Error: Preset '${targetPreset}' not found. Available presets: ${presetFolders.join(', ')}`);
63
+ process.exit(1);
64
+ }
65
+
66
+ // Format 1: Tailwind CSS v4 @theme Generator
67
+ function toTailwindV4Theme(preset) {
68
+ const lines = ['/* Tailwind CSS v4 @theme configuration */', '/* Generated by ME-DESIGN Token Engine */', '@theme {'];
69
+
70
+ // Colors
71
+ if (preset.color) {
72
+ if (preset.color.canvas) lines.push(` --color-canvas: ${preset.color.canvas};`);
73
+ if (preset.color.surfaceRaised) lines.push(` --color-surface-raised: ${preset.color.surfaceRaised};`);
74
+ if (preset.color.surfaceInset) lines.push(` --color-surface-inset: ${preset.color.surfaceInset};`);
75
+ if (preset.color.borderSubtle) lines.push(` --color-border-subtle: ${preset.color.borderSubtle};`);
76
+ if (preset.color.borderFocus) lines.push(` --color-border-focus: ${preset.color.borderFocus};`);
77
+ if (preset.color.accent) lines.push(` --color-accent: ${preset.color.accent};`);
78
+ if (preset.color.accentForeground) lines.push(` --color-accent-foreground: ${preset.color.accentForeground};`);
79
+
80
+ if (preset.color.status) {
81
+ Object.entries(preset.color.status).forEach(([k, v]) => {
82
+ lines.push(` --color-status-${k}: ${v};`);
83
+ });
84
+ }
85
+ }
86
+
87
+ // Typography
88
+ if (preset.typography) {
89
+ if (preset.typography.display) lines.push(` --font-display: ${preset.typography.display};`);
90
+ if (preset.typography.sans) lines.push(` --font-sans: ${preset.typography.sans};`);
91
+ if (preset.typography.mono) lines.push(` --font-mono: ${preset.typography.mono};`);
92
+ if (preset.typography.headingTracking) lines.push(` --tracking-heading: ${preset.typography.headingTracking};`);
93
+ if (preset.typography.uiLineHeight) lines.push(` --leading-ui: ${preset.typography.uiLineHeight};`);
94
+ if (preset.typography.proseLineHeight) lines.push(` --leading-prose: ${preset.typography.proseLineHeight};`);
95
+ }
96
+
97
+ // Geometry
98
+ if (preset.geometry) {
99
+ if (preset.geometry.radiusBase) lines.push(` --radius-base: ${preset.geometry.radiusBase};`);
100
+ if (preset.geometry.radiusOuter) lines.push(` --radius-outer: ${preset.geometry.radiusOuter};`);
101
+ if (preset.geometry.hitTargetMinTouch) lines.push(` --hit-target-min: ${preset.geometry.hitTargetMinTouch};`);
102
+ }
103
+
104
+ // Motion
105
+ if (preset.motion) {
106
+ if (preset.motion.easingEntry) lines.push(` --ease-entry: ${preset.motion.easingEntry};`);
107
+ if (preset.motion.easingExit) lines.push(` --ease-exit: ${preset.motion.easingExit};`);
108
+ if (preset.motion.durationEntry) lines.push(` --duration-entry: ${preset.motion.durationEntry};`);
109
+ if (preset.motion.durationExit) lines.push(` --duration-exit: ${preset.motion.durationExit};`);
110
+ }
111
+
112
+ lines.push('}');
113
+ return lines.join('\n');
114
+ }
115
+
116
+ function oklchToLinearRgb(l, c, h) {
117
+ const rad = h * Math.PI / 180;
118
+ const a = c * Math.cos(rad);
119
+ const b = c * Math.sin(rad);
120
+
121
+ const l1 = l + 0.3963377774 * a + 0.2158037573 * b;
122
+ const m1 = l - 0.1055613458 * a - 0.0638541728 * b;
123
+ const s1 = l - 0.0894841775 * a - 1.2914855480 * b;
124
+
125
+ const l_c = l1 * l1 * l1;
126
+ const m_c = m1 * m1 * m1;
127
+ const s_c = s1 * s1 * s1;
128
+
129
+ const r_lin = +4.0767434721 * l_c - 3.3077115913 * m_c + 0.2309699292 * s_c;
130
+ const g_lin = -1.2684380046 * l_c + 2.6097574011 * m_c - 0.3413193965 * s_c;
131
+ const b_lin = -0.0041960863 * l_c - 0.7034186147 * m_c + 1.7076147010 * s_c;
132
+
133
+ return [r_lin, g_lin, b_lin];
134
+ }
135
+
136
+ function inGamut(r, g, b, eps = 0.0001) {
137
+ return r >= -eps && r <= 1 + eps &&
138
+ g >= -eps && g <= 1 + eps &&
139
+ b >= -eps && b <= 1 + eps;
140
+ }
141
+
142
+ // Convert OKLCH to sRGB Hex using CSS Color 4 Gamut Mapping (binary search chroma reduction)
143
+ function oklchToHex(colorStr) {
144
+ if (!colorStr || !colorStr.startsWith('oklch(')) return null;
145
+ const match = colorStr.match(/oklch\(\s*([\d.]+)\s+([\d.]+)\s+([\d.]+)/i);
146
+ if (!match) return null;
147
+ const l = parseFloat(match[1]);
148
+ const c = parseFloat(match[2]);
149
+ const h = parseFloat(match[3]);
150
+
151
+ const [r0, g0, b0] = oklchToLinearRgb(l, c, h);
152
+ let bestRgb = [r0, g0, b0];
153
+
154
+ // If outside sRGB gamut, reduce chroma via binary search while preserving lightness and hue angle
155
+ if (!inGamut(r0, g0, b0)) {
156
+ let cLow = 0;
157
+ let cHigh = c;
158
+ for (let i = 0; i < 25; i++) {
159
+ const cMid = (cLow + cHigh) / 2;
160
+ const rgb = oklchToLinearRgb(l, cMid, h);
161
+ if (inGamut(rgb[0], rgb[1], rgb[2])) {
162
+ cLow = cMid;
163
+ bestRgb = rgb;
164
+ } else {
165
+ cHigh = cMid;
166
+ }
167
+ }
168
+ }
169
+
170
+ const gamma = (x) => x <= 0.0031308 ? 12.92 * x : 1.055 * Math.pow(Math.max(0, x), 1 / 2.4) - 0.055;
171
+ const R = Math.min(255, Math.max(0, Math.round(gamma(bestRgb[0]) * 255)));
172
+ const G = Math.min(255, Math.max(0, Math.round(gamma(bestRgb[1]) * 255)));
173
+ const B = Math.min(255, Math.max(0, Math.round(gamma(bestRgb[2]) * 255)));
174
+
175
+ return '#' + [R, G, B].map(x => x.toString(16).padStart(2, '0')).join('');
176
+ }
177
+
178
+ // Format 2: W3C Design Tokens Community Group (DTCG) Generator
179
+ // Supports 'oklch' (modern CSS Color 4) or 'srgb' (hex tokens for Figma / legacy tools)
180
+ function toDtcgTokens(preset, colorSpace = 'oklch') {
181
+ const dtcg = {
182
+ $name: preset.name,
183
+ $description: preset.description,
184
+ $version: '2024.1.0',
185
+ color: {},
186
+ fontFamily: {},
187
+ dimension: {},
188
+ duration: {},
189
+ cubicBezier: {}
190
+ };
191
+
192
+ // Colors
193
+ if (preset.color) {
194
+ Object.entries(preset.color).forEach(([k, v]) => {
195
+ if (typeof v === 'string') {
196
+ const tokenVal = colorSpace === 'srgb' ? (oklchToHex(v) || v) : v;
197
+ dtcg.color[k] = {
198
+ $value: tokenVal,
199
+ $type: 'color',
200
+ $description: `${k} color token for ${preset.name}`
201
+ };
202
+ } else if (typeof v === 'object') {
203
+ dtcg.color[k] = {};
204
+ Object.entries(v).forEach(([subK, subV]) => {
205
+ const tokenVal = colorSpace === 'srgb' ? (oklchToHex(subV) || subV) : subV;
206
+ dtcg.color[k][subK] = {
207
+ $value: tokenVal,
208
+ $type: 'color',
209
+ $description: `${k}.${subK} color token for ${preset.name}`
210
+ };
211
+ });
212
+ }
213
+ });
214
+ }
215
+
216
+ // Typography
217
+ if (preset.typography) {
218
+ ['display', 'sans', 'mono'].forEach(fontKey => {
219
+ if (preset.typography[fontKey]) {
220
+ dtcg.fontFamily[fontKey] = {
221
+ $value: preset.typography[fontKey],
222
+ $type: 'fontFamily',
223
+ $description: `${fontKey} font stack`
224
+ };
225
+ }
226
+ });
227
+ if (preset.typography.headingTracking) {
228
+ dtcg.dimension.headingTracking = {
229
+ $value: preset.typography.headingTracking,
230
+ $type: 'dimension',
231
+ $description: 'Optical negative tracking for headings'
232
+ };
233
+ }
234
+ }
235
+
236
+ // Geometry
237
+ if (preset.geometry) {
238
+ if (preset.geometry.radiusBase) {
239
+ dtcg.dimension.radiusBase = {
240
+ $value: preset.geometry.radiusBase,
241
+ $type: 'dimension',
242
+ $description: 'Base corner radius'
243
+ };
244
+ }
245
+ if (preset.geometry.radiusOuter) {
246
+ dtcg.dimension.radiusOuter = {
247
+ $value: preset.geometry.radiusOuter,
248
+ $type: 'dimension',
249
+ $description: 'Outer card corner radius'
250
+ };
251
+ }
252
+ if (preset.geometry.hitTargetMinTouch) {
253
+ dtcg.dimension.hitTargetMin = {
254
+ $value: preset.geometry.hitTargetMinTouch,
255
+ $type: 'dimension',
256
+ $description: 'WCAG 2.5.8 / HIG minimum touch target dimension (44px)'
257
+ };
258
+ }
259
+ }
260
+
261
+ // Motion
262
+ if (preset.motion) {
263
+ if (preset.motion.durationEntry) {
264
+ dtcg.duration.entry = {
265
+ $value: preset.motion.durationEntry,
266
+ $type: 'duration',
267
+ $description: 'Entry motion timing'
268
+ };
269
+ }
270
+ if (preset.motion.durationExit) {
271
+ dtcg.duration.exit = {
272
+ $value: preset.motion.durationExit,
273
+ $type: 'duration',
274
+ $description: 'Exit motion timing (asymmetric 0.65x)'
275
+ };
276
+ }
277
+ if (preset.motion.easingEntry) {
278
+ dtcg.cubicBezier.entry = {
279
+ $value: preset.motion.easingEntry,
280
+ $type: 'cubicBezier',
281
+ $description: 'Entry cubic-bezier timing function'
282
+ };
283
+ }
284
+ if (preset.motion.easingExit) {
285
+ dtcg.cubicBezier.exit = {
286
+ $value: preset.motion.easingExit,
287
+ $type: 'cubicBezier',
288
+ $description: 'Exit cubic-bezier timing function'
289
+ };
290
+ }
291
+ }
292
+
293
+ return JSON.stringify(dtcg, null, 2);
294
+ }
295
+
296
+ // Format 3: CSS Custom Properties Generator (:root { ... })
297
+ function toCssVariables(preset) {
298
+ const lines = ['/* Standard CSS Custom Properties */', '/* Generated by ME-DESIGN Token Engine */', ':root {'];
299
+
300
+ if (preset.color) {
301
+ Object.entries(preset.color).forEach(([k, v]) => {
302
+ if (typeof v === 'string') {
303
+ const cssKey = k.replace(/([A-Z])/g, '-$1').toLowerCase();
304
+ lines.push(` --color-${cssKey}: ${v};`);
305
+ } else if (typeof v === 'object') {
306
+ Object.entries(v).forEach(([subK, subV]) => {
307
+ lines.push(` --color-${k}-${subK}: ${subV};`);
308
+ });
309
+ }
310
+ });
311
+ }
312
+
313
+ if (preset.typography) {
314
+ if (preset.typography.sans) lines.push(` --font-sans: ${preset.typography.sans};`);
315
+ if (preset.typography.mono) lines.push(` --font-mono: ${preset.typography.mono};`);
316
+ if (preset.typography.uiLineHeight) lines.push(` --line-height-ui: ${preset.typography.uiLineHeight};`);
317
+ if (preset.typography.proseLineHeight) lines.push(` --line-height-prose: ${preset.typography.proseLineHeight};`);
318
+ }
319
+
320
+ if (preset.geometry) {
321
+ if (preset.geometry.radiusBase) lines.push(` --radius-base: ${preset.geometry.radiusBase};`);
322
+ if (preset.geometry.radiusOuter) lines.push(` --radius-outer: ${preset.geometry.radiusOuter};`);
323
+ if (preset.geometry.hitTargetMinTouch) lines.push(` --hit-target-min: ${preset.geometry.hitTargetMinTouch};`);
324
+ }
325
+
326
+ lines.push('}');
327
+ return lines.join('\n');
328
+ }
329
+
330
+ // Execution CLI Guard
331
+ if (require.main === module) {
332
+ const results = [];
333
+
334
+ selectedPresets.forEach(presetId => {
335
+ const jsonPath = path.join(presetsDir, presetId, 'preset.json');
336
+ const preset = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
337
+
338
+ const exportItem = {
339
+ preset: presetId,
340
+ name: preset.name,
341
+ outputs: {}
342
+ };
343
+
344
+ if (targetFormat === 'tailwind' || targetFormat === 'all') {
345
+ exportItem.outputs.tailwind = toTailwindV4Theme(preset);
346
+ }
347
+ if (targetFormat === 'dtcg' || targetFormat === 'all') {
348
+ exportItem.outputs.dtcgOklch = toDtcgTokens(preset, 'oklch');
349
+ exportItem.outputs.dtcgSrgb = toDtcgTokens(preset, 'srgb');
350
+ exportItem.outputs.dtcg = exportItem.outputs.dtcgOklch;
351
+ }
352
+ if (targetFormat === 'css' || targetFormat === 'all') {
353
+ exportItem.outputs.css = toCssVariables(preset);
354
+ }
355
+
356
+ results.push(exportItem);
357
+ });
358
+
359
+ // Output Handling
360
+ if (outDir) {
361
+ if (!fs.existsSync(outDir)) {
362
+ fs.mkdirSync(outDir, { recursive: true });
363
+ }
364
+
365
+ results.forEach(res => {
366
+ if (res.outputs.tailwind) {
367
+ fs.writeFileSync(path.join(outDir, `${res.preset}-theme.css`), res.outputs.tailwind, 'utf8');
368
+ }
369
+ if (res.outputs.dtcgOklch) {
370
+ fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.oklch.json`), res.outputs.dtcgOklch, 'utf8');
371
+ fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.dtcg.json`), res.outputs.dtcgOklch, 'utf8');
372
+ }
373
+ if (res.outputs.dtcgSrgb) {
374
+ fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.srgb.json`), res.outputs.dtcgSrgb, 'utf8');
375
+ }
376
+ if (res.outputs.css) {
377
+ fs.writeFileSync(path.join(outDir, `${res.preset}-tokens.css`), res.outputs.css, 'utf8');
378
+ }
379
+ });
380
+
381
+ console.log(`Successfully exported tokens for [${selectedPresets.join(', ')}] into '${outDir}'.`);
382
+ } else {
383
+ // Stdout
384
+ results.forEach(res => {
385
+ console.log(`\n========================================`);
386
+ console.log(`PRESET: ${res.name} (${res.preset})`);
387
+ console.log(`========================================\n`);
388
+ if (res.outputs.tailwind) {
389
+ console.log(`--- [Tailwind v4 @theme] ---`);
390
+ console.log(res.outputs.tailwind);
391
+ console.log('');
392
+ }
393
+ if (res.outputs.dtcgOklch) {
394
+ console.log(`--- [W3C DTCG Format JSON (OKLCH Native)] ---`);
395
+ console.log(res.outputs.dtcgOklch);
396
+ console.log('');
397
+ }
398
+ if (res.outputs.dtcgSrgb) {
399
+ console.log(`--- [W3C DTCG Format JSON (sRGB Gamut-Mapped Hex)] ---`);
400
+ console.log(res.outputs.dtcgSrgb);
401
+ console.log('');
402
+ }
403
+ if (res.outputs.css) {
404
+ console.log(`--- [CSS Custom Properties] ---`);
405
+ console.log(res.outputs.css);
406
+ console.log('');
407
+ }
408
+ });
409
+ }
410
+ }
411
+
412
+ module.exports = {
413
+ oklchToHex,
414
+ oklchToLinearRgb,
415
+ inGamut,
416
+ toTailwindV4Theme,
417
+ toDtcgTokens,
418
+ toCssVariables
419
+ };