Pave the way for being able to have core colors that are not mapped to any tint

This commit is contained in:
Lea Verou
2025-03-05 13:42:23 -05:00
parent e0fc639226
commit db08e12a32
2 changed files with 39 additions and 16 deletions

View File

@@ -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;
}
}
}

View File

@@ -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-(?<hue>[a-z]+)-(?<level>[0-9]+):\s*(?<color>.+?)\s*(\/\*.+?\*\/)?\s*;$/gm;
/^\s*--wa-color-(?<hue>[a-z]+)(?:-(?<level>[0-9]+|key))?:\s*(?<color>.+?)\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;