diff --git a/packages/runtime/src/themeTokens.ts b/packages/runtime/src/themeTokens.ts index ea9c97a..7ce18c5 100644 --- a/packages/runtime/src/themeTokens.ts +++ b/packages/runtime/src/themeTokens.ts @@ -53,6 +53,8 @@ const NEUTRAL_CHROMA = 0.05; /** How far past the site's own background/text a neutral may sit and still be on the axis (pure white * text in Lights out is slightly past the site's 231,233,234; black in Default is far past and is a shadow). */ const AXIS_SLACK = 0.12; +/** How close to a surface or to the secondary text a foreground may sit and still count as that role's own shade. */ +const DIM_SLACK = 0.01; export type ColorModel = { sourceBg: Oklab; @@ -63,6 +65,14 @@ export type ColorModel = { exact: Map; sourceAccent: Oklab; accent: Oklab | null; + /** + * Text only: the stretch of the site's axis between its most raised surface and its secondary text. + * A foreground the site paints there (a dimmed icon, a gray it also uses for borders) would land + * between the theme's background and its secondary text, under every contrast a theme guarantees; + * it becomes the theme's secondary text instead. Below `from` a foreground is as dark (or light) as + * the site's surfaces: a label on a filled surface, which keeps its place. + */ + dim: { from: number; to: number; color: Rgb } | null; }; /** @@ -112,8 +122,15 @@ export function buildColorModel(source: SourceAxis, target: TargetAxis, usage: U // accent backgrounds keep the site's lightness offsets (hover, pressed) through projectColor. if (target.accent && usage !== 'background') for (const key of source.accent) if (!exact.has(key)) exact.set(key, hexToRgb(target.accent)); anchors.sort((a, b) => a.t - b.t); + let dim: ColorModel['dim'] = null; + if (usage === 'text' && target.muted && source.muted.length) { + const positions = (roles: AxisRole[]) => roles.flatMap(role => source[role].map(key => position(rgbToOklab(keyRgb(key))))); + const from = Math.max(0, ...positions(['bg', 'surface', 'elevated'])) + DIM_SLACK; + const to = Math.min(...positions(['muted'])) - DIM_SLACK; + if (to > from) dim = { from, to, color: hexToRgb(target.muted) }; + } return { - sourceBg, sourceText, anchors, exact, + sourceBg, sourceText, anchors, exact, dim, sourceAccent: rgbToOklab(keyRgb(source.accent[0]!)), accent: target.accent ? rgbToOklab(hexToRgb(target.accent)) : null, }; @@ -150,6 +167,7 @@ export function projectColor(model: ColorModel, rgb: Rgb): Rgb | null { if (listed) return listed; const c = rgbToOklab(rgb); const t = axisPosition(model, rgb); + if (t !== null && model.dim && t > model.dim.from && t < model.dim.to) return model.dim.color; if (t !== null) return oklabToRgb(onCurve(model, t)); if (model.accent && isAccentFamily(model, c)) { // Keep the site's lightness offset (hover is darker, tints lighter) on the theme's accent. diff --git a/packages/runtime/tests/themeTokens.test.ts b/packages/runtime/tests/themeTokens.test.ts index aa4630b..a738d50 100644 --- a/packages/runtime/tests/themeTokens.test.ts +++ b/packages/runtime/tests/themeTokens.test.ts @@ -50,3 +50,23 @@ test('derived roles follow the base structure', () => { } assert.equal(deriveColors({ bg: '#ffffff', text: '#000000', accent: '#000000', border: '#123456' }).border, '#123456'); }); + +test('a foreground the site paints dimmer than its secondary text becomes the theme\'s secondary text', () => { + const hex = (h: string) => [parseInt(h.slice(1, 3), 16), parseInt(h.slice(3, 5), 16), parseInt(h.slice(5, 7), 16)]; + const text = buildColorModel(SOURCE, THEME, 'text')!; + // The site's border gray used as an icon color, and grays around it: all between the most raised surface and the secondary text. + for (const dim of ['50,52,58', '44,46,50', '70,72,78', '110,114,120']) assert.deepEqual(projectColor(text, rgb(dim)), hex(THEME.muted), dim); + // The ends keep their place: a surface-dark foreground is a label on a filled surface, brighter than secondary is text. + assert.deepEqual(projectColor(text, rgb('10,10,12')), hex(THEME.bg)); + assert.notDeepEqual(projectColor(text, rgb('20,21,24')), hex(THEME.muted)); + assert.notDeepEqual(projectColor(text, rgb('32,33,38')), hex(THEME.muted)); + assert.deepEqual(projectColor(text, rgb('140,144,150')), hex(THEME.muted)); + assert.notDeepEqual(projectColor(text, rgb('190,192,196')), hex(THEME.muted)); + assert.deepEqual(projectColor(text, rgb('235,236,238')), hex(THEME.text)); + // Backgrounds and borders are not foregrounds: the same gray stays the theme's border. + assert.deepEqual(projectColor(buildColorModel(SOURCE, THEME, 'border')!, rgb('50,52,58')), hex(THEME.border)); + assert.deepEqual(projectColor(buildColorModel(SOURCE, THEME, 'background')!, rgb('50,52,58')), hex(THEME.border)); + assert.equal(buildColorModel(SOURCE, THEME, 'background')!.dim, null); + // A theme without a secondary text color has nothing to raise it to. + assert.equal(buildColorModel(SOURCE, { bg: THEME.bg, text: THEME.text }, 'text')!.dim, null); +});