// The web terminal's colours: xterm's sixteen ANSI colours derived from the // client's palette, each readable on the terminal's ground. Pure, so // web/scripts/terminal-theme.test.mjs can check every season by day and night. export type Rgb = [number, number, number]; /** The colours the terminal is built from, resolved to hex. */ export interface Ground { mode: 'light' | 'dark'; background: string; foreground: string; accent: string; added: string; danger: string; } export interface TerminalColors { background: string; foreground: string; cursor: string; cursorAccent: string; selectionBackground: string; selectionForeground: string; selectionInactiveBackground: string; black: string; red: string; green: string; yellow: string; blue: string; magenta: string; cyan: string; white: string; brightBlack: string; brightRed: string; brightGreen: string; brightYellow: string; brightBlue: string; brightMagenta: string; brightCyan: string; brightWhite: string; } /** What every colour must reach against the ground, as WCAG contrast. */ export const READABLE = 4.5; /** What the greys a program means as faint (bright black) must reach. */ export const FAINT = 3; /** Where normal colours sit by day, a step deeper than the bright ones at READABLE. */ const DEEP = 6; /** * xterm's minimumContrastRatio: text a program colours for the other mode * (nushell's #e6eaff on a light ground) is darkened until it reaches this, and * dim text half of it, so faint stays fainter than the palette's own colours. */ export const MINIMUM_CONTRAST = 3; // The hues without a colour of their own in the client's palette, by day and by night. const INKS = { light: { yellow: '#a36a00', blue: '#2b62c4', magenta: '#a43c9c', cyan: '#00808c', }, dark: { yellow: '#e6c15c', blue: '#82aaff', magenta: '#e793dc', cyan: '#6fd3dc', }, }; export function parseHex(hex: string): Rgb { const value = parseInt(hex.slice(1, 7), 16); return [(value >> 16) & 255, (value >> 8) & 255, value & 255]; } export function toHex(rgb: Rgb): string { return `#${rgb .map((c) => Math.round(Math.min(255, Math.max(0, c))) .toString(16) .padStart(2, '0'), ) .join('')}`; } function linear(channel: number): number { const c = channel / 255; return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; } function gamma(c: number): number { return 255 * (c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055); } function luminance(hex: string): number { const [r, g, b] = parseHex(hex).map(linear); return 0.2126 * r + 0.7152 * g + 0.0722 * b; } /** WCAG contrast ratio between two hex colours, 1 to 21. */ export function contrast(a: string, b: string): number { const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x); return (hi + 0.05) / (lo + 0.05); } type Lab = [number, number, number]; function toOklab(hex: string): Lab { const [r, g, b] = parseHex(hex).map(linear); const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b); const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b); const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b); return [ 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s, 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s, ]; } /** The sRGB colour for an Oklab one, or null outside the gamut. */ function fromOklab([L, a, b]: Lab): string | null { const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3; const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3; const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3; const rgb = [ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s, ]; if (rgb.some((c) => c < -1e-4 || c > 1 + 1e-4)) return null; return toHex(rgb.map(gamma) as Rgb); } /** The colour at lightness L with the hue of `lab`, losing chroma until it fits sRGB. */ function atLightness(lab: Lab, L: number): string { for (let k = 1; k >= 0; k -= 0.02) { const hex = fromOklab([L, lab[1] * k, lab[2] * k]); if (hex) return hex; } return fromOklab([L, 0, 0]) ?? (L > 0.5 ? '#ffffff' : '#000000'); } /** * The colour itself if it reaches `ratio` against `ground`, else the nearest * lightness of its hue that does: darker on a light ground, lighter on a dark one. */ export function readable(color: string, ground: string, ratio: number): string { if (contrast(color, ground) >= ratio) return color; const lab = toOklab(color); const darker = luminance(ground) > 0.18; // Between the colour and black or white, binary search for the closest that reaches it. let near = lab[0]; let far = darker ? 0 : 1; for (let step = 0; step < 24; step++) { const mid = (near + far) / 2; if (contrast(atLightness(lab, mid), ground) >= ratio) far = mid; else near = mid; } return atLightness(lab, far); } /** A colour moved `by` in Oklab lightness, keeping its hue. */ function lighten(color: string, by: number): string { const lab = toOklab(color); return atLightness(lab, Math.min(1, Math.max(0, lab[0] + by))); } /** The grey between foreground and ground (in Oklab) that has the given contrast. */ function grey(foreground: string, ground: string, ratio: number): string { if (contrast(foreground, ground) <= ratio) return foreground; const [f, g] = [toOklab(foreground), toOklab(ground)]; const at = (t: number) => fromOklab(f.map((c, i) => c + (g[i] - c) * t) as Lab) ?? foreground; let [lo, hi] = [0, 1]; for (let step = 0; step < 24; step++) { const t = (lo + hi) / 2; if (contrast(at(t), ground) >= ratio) lo = t; else hi = t; } return at(lo); } /** * The terminal's theme on `ground.background`. Red and green are the palette's * danger and added, the cursor and selection its accent. Bright colours are a * step lighter by night; by day they sit at READABLE and normal ones at DEEP. By day black is the ink and * white a grey that still reads; by night black is a grey at FAINT, since * programs paint it as a ground more than they write in it. */ export function terminalColors(ground: Ground): TerminalColors { const { mode, background, foreground, accent } = ground; const bg = background; const fit = (color: string) => readable(color, bg, READABLE); // By day a lighter colour is a fainter one, so normal colours go deeper instead. const pair = (color: string) => mode === 'light' ? [readable(color, bg, DEEP), fit(color)] : [fit(color), lighten(fit(color), 0.08)]; const inks = INKS[mode]; const [red, brightRed] = pair(ground.danger); const [green, brightGreen] = pair(ground.added); const [yellow, brightYellow] = pair(inks.yellow); const [blue, brightBlue] = pair(inks.blue); const [magenta, brightMagenta] = pair(inks.magenta); const [cyan, brightCyan] = pair(inks.cyan); const text = fit(foreground); const greys = mode === 'light' ? { black: text, white: grey(text, bg, 5.2), brightBlack: grey(text, bg, FAINT + 0.4), brightWhite: fit(lighten(text, -0.12)), } : { black: grey(text, bg, FAINT), white: text, brightBlack: grey(text, bg, FAINT + 0.8), brightWhite: lighten(text, 0.08), }; const selection = fit(accent); return { background: bg, foreground: text, cursor: selection, cursorAccent: bg, selectionBackground: `${selection}55`, selectionForeground: text, selectionInactiveBackground: `${selection}2e`, red, green, yellow, blue, magenta, cyan, brightRed, brightGreen, brightYellow, brightBlue, brightMagenta, brightCyan, ...greys, }; }