import { colord, extend } from 'colord'; import a11yPlugin from 'colord/plugins/a11y'; import mixPlugin from 'colord/plugins/mix'; extend([a11yPlugin, mixPlugin]); export const PROTOTYPE_LIGHT_PRIMARY = '#b96508'; export const PROTOTYPE_DARK_PRIMARY = '#d1963a'; export const PROTOTYPE_DARK_SEED = '#f2ad41'; const clamp = (value, low, high) => Math.min(high, Math.max(low, value)); function toneAtContrast(color, surface, minimum, lighter) { const { h: hue, s: saturation } = color.toHsl(); const tone = (lightness) => colord({ h: hue, s: saturation, l: lightness }); let low = 0; let high = 100; for (let step = 0; step < 12; step++) { const middle = (low + high) / 2; if (tone(middle).contrast(surface) >= minimum === lighter) high = middle; else low = middle; } return tone(lighter ? high : low); } const readable = (color, surface, dark) => color.contrast(surface) >= 4.7 ? color : toneAtContrast(color, surface, 4.7, dark); export function resolveBrandPalette(configured, dark, surface) { const prototype = dark ? PROTOTYPE_DARK_PRIMARY : PROTOTYPE_LIGHT_PRIMARY; const parsed = colord(configured); const input = parsed.isValid() ? parsed.alpha(1) : colord(prototype); const isPrototype = input.toHex() === prototype; const hsl = input.toHsl(); const seed = isPrototype ? input : colord({ ...hsl, s: Math.min(hsl.s, 68), l: clamp(hsl.l, 28, 70) }); const onSolid = dark ? '#11151c' : '#ffffff'; let normal; let hover; let active; if (dark) { const base = seed.contrast(onSolid); const target = clamp(base, 5.3, isPrototype ? 7.2 : 6.45); const fill = (ratio) => toneAtContrast(seed, onSolid, ratio, true); normal = base === target ? seed : fill(target); hover = fill(target + 0.5); active = fill(target - 0.55); } else { normal = readable(seed, onSolid, false); hover = normal.darken(0.035); active = normal.darken(0.07); } const backdrop = colord(surface); const tint = backdrop.mix(normal, dark ? 0.14 : 0.085); const tintHover = backdrop.mix(normal, dark ? 0.2 : 0.135); const ink = readable(normal, tintHover, dark).toHex(); return { isPrototype, onSolid, normal: normal.toHex(), hover: hover.toHex(), active: active.toHex(), accent: isPrototype ? (dark ? PROTOTYPE_DARK_SEED : prototype) : ink, selectionBg: isPrototype ? (dark ? '#382b1b' : '#fff3df') : tint.toHex(), selectionText: isPrototype ? (dark ? PROTOTYPE_DARK_SEED : '#a45a07') : ink, tint: tint.toHex(), tintHover: tintHover.toHex(), }; } export function resolveGradientLabel(stops, surface) { const backdrop = colord(surface); const base = backdrop.toRgb(); const colors = stops.map((stop) => { const parsed = colord(stop); return parsed.isValid() ? parsed.toRgb() : base; }); const composite = (channel, from, to, progress) => from[channel] * from.a * (1 - progress) + to[channel] * to.a * progress + base[channel] * (1 - from.a * (1 - progress) - to.a * progress); const samples = colors.flatMap((from, index) => { const to = colors[index + 1] ?? from; return [0, 0.25, 0.5, 0.75].map((progress) => colord({ r: composite('r', from, to, progress), g: composite('g', from, to, progress), b: composite('b', from, to, progress), }), ); }); const inks = ['#ffffff', '#11151c']; const minimum = (ink) => Math.min(...samples.map((sample) => sample.contrast(ink))); const chosen = minimum(inks[0]) >= minimum(inks[1]) ? inks[0] : inks[1]; if (samples.length > 0 && minimum(chosen) >= 4.5) return { color: chosen }; return { color: backdrop.contrast(inks[0]) >= backdrop.contrast(inks[1]) ? inks[0] : inks[1], background: backdrop.toHex(), }; }