From db08e12a32cbeb1331b21a4701e3f1d6f35f161b Mon Sep 17 00:00:00 2001 From: Lea Verou Date: Wed, 5 Mar 2025 13:42:23 -0500 Subject: [PATCH] Pave the way for being able to have core colors that are not mapped to any tint --- src/styles/color/scripts/palettes-analyzed.js | 31 ++++++++++++------- src/styles/color/scripts/palettes.js | 24 +++++++++++--- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/styles/color/scripts/palettes-analyzed.js b/src/styles/color/scripts/palettes-analyzed.js index 45430a753..d296b6457 100644 --- a/src/styles/color/scripts/palettes-analyzed.js +++ b/src/styles/color/scripts/palettes-analyzed.js @@ -24,15 +24,22 @@ for (let paletteId in palettes) { palettes[paletteId] = tokens; for (let hue in tokens) { - let tints = Object.assign({}, tokens[hue]); - tokens[hue] = tints; + let scale = Object.assign({}, tokens[hue]); + tokens[hue] = scale; - let maxChromaTint = DEFAULT_ACCENT; - let maxChroma = tints[DEFAULT_ACCENT].c || 0; + let maxChromaTint = DEFAULT_ACCENT; // TODO handle scale.core + let maxChroma = scale.core?.get('oklch.c') ?? (scale[DEFAULT_ACCENT].c || 0); - for (let tint in tints) { - let color = tints[tint].to('oklch'); - tints[tint] = color; + for (let tint in scale) { + let color = scale[tint]; + + if (!color || color.constructor.name !== 'Color') { + // Not a color + continue; + } + + color = color.to('oklch'); + scale[tint] = color; if (tint === '05') { // The object has both '5' and '05' keys, but '05' is out of order @@ -47,14 +54,14 @@ for (let paletteId in palettes) { } } - tints['05'] = tints['5']; + scale['05'] = scale['5']; - tints.maxChroma = tints.maxChromaRaw = maxChroma; - tints.maxChromaTint = tints.maxChromaTintRaw = maxChromaTint; + scale.maxChroma = scale.maxChromaRaw = maxChroma; + scale.maxChromaTint = scale.maxChromaTintRaw = maxChromaTint; if (maxChromaTint < MIN_ACCENT || maxChromaTint > MAX_ACCENT) { - tints.maxChromaTint = clamp(MIN_ACCENT, maxChromaTint, MAX_ACCENT); - tints.maxChroma = tints[maxChromaTint].c; + scale.maxChromaTint = clamp(MIN_ACCENT, maxChromaTint, MAX_ACCENT); + scale.maxChroma = scale[maxChromaTint].c; } } } diff --git a/src/styles/color/scripts/palettes.js b/src/styles/color/scripts/palettes.js index 1507b58d0..588807a1c 100644 --- a/src/styles/color/scripts/palettes.js +++ b/src/styles/color/scripts/palettes.js @@ -10,7 +10,7 @@ import { PALETTE_DIR } from './util.js'; export const paletteFiles = fs.readdirSync(PALETTE_DIR + '/').filter(file => file.endsWith('.css')); export const declarationRegex = - /^\s*--wa-color-(?[a-z]+)-(?[0-9]+):\s*(?.+?)\s*(\/\*.+?\*\/)?\s*;$/gm; + /^\s*--wa-color-(?[a-z]+)(?:-(?[0-9]+|key))?:\s*(?.+?)\s*(\/\*.+?\*\/)?\s*;$/gm; export const rawCSS = {}; function parse(contents, file) { @@ -24,8 +24,24 @@ function parse(contents, file) { const ret = {}; for (let match of matches) { - let { hue, level, color } = match.groups; + let { hue, level = '', color } = match.groups; ret[hue] ??= {}; + let scale = ret[hue]; + + if (level === 'key') { + scale.maxChromaTint = color; + continue; + } + + if (!level) { + if (color.startsWith('var(')) { + // Core color aliased to another color, ignore + continue; + } else { + // Custom core color + level = 'core'; + } + } // Attempt to convert color to Color object, fall back to string if this fails // This will happen for e.g. colors defined via color-mix() @@ -38,13 +54,13 @@ function parse(contents, file) { if (level.startsWith('0')) { // Leading zeroes throw off sorting, add both properties // NOTE: Ideally one of the two would be added as non-enumerable, but then we cannot access it via 11ty data - ret[hue][level] = color; + scale[level] = color; // Drop leading zeroes level = level.replace(/^0+/, ''); } - ret[hue][level] = color; + scale[level] = color; } return ret;