// Theme files use kebab-case keys and #rrggbb[aa] colours; components see the // WIT `theme.tokens` record (camelCase in jco). const camel = (k) => k.replace(/-([a-z])/g, (_, c) => c.toUpperCase()); export function parseColor(s) { const m = /^#([0-9a-fA-F]{6})([0-9a-fA-F]{2})?$/.exec(s); if (!m) throw new Error(`invalid colour "${s}" (expected #rrggbb or #rrggbbaa)`); const n = parseInt(m[1], 16); return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255, a: m[2] ? parseInt(m[2], 16) : 255 }; } const PALETTE_KEYS = ['desktop-top', 'desktop-bottom', 'surface', 'surface-hover', 'surface-popup', 'foreground', 'foreground-muted', 'accent', 'accent-foreground', 'border']; const NUM_KEYS = ['font-size', 'radius', 'spacing', 'panel-height']; export function tokensFromJson(json) { const palette = {}; for (const k of PALETTE_KEYS) { if (!(k in (json.palette ?? {}))) throw new Error(`theme "${json.name}": missing palette.${k}`); palette[camel(k)] = parseColor(json.palette[k]); } const out = { name: json.name, palette }; for (const k of NUM_KEYS) { if (!Number.isInteger(json[k]) || json[k] < 0) throw new Error(`theme "${json.name}": ${k} must be a non-negative integer`); out[camel(k)] = json[k]; } return out; }