/** * The Bytefly web palette and scales. * * The colour set is **derived** from the terminal themes — `themes/bytefly-dark.json` and * `themes/bytefly-light.json` — so a served page and the terminal cannot drift: a page is the same * product in another medium. A token exists here as a mapping to a theme var, a resolution of a * theme colour, or a step the theme has no name for. The web's own language — the UI face, mono * only where the text *is* code, the radii, the two shadows — is stated on top of it. * * Value-only: the CSS is a constant computed at module load, with no state. */ import darkTheme from "../../themes/bytefly-dark.json"; import lightTheme from "../../themes/bytefly-light.json"; /** The two appearances a page draws. */ export type Appearance = "dark" | "light"; /** A theme document, as far as a palette reads it. */ interface ThemeDocument { vars: Record; colors: Record; } const THEMES: Record = { dark: darkTheme as ThemeDocument, light: lightTheme as ThemeDocument, }; /** * Each web token, and the theme var that states it. `--dim` and the border pair are the colours * absent from this table: a theme states them for a terminal's cell grid, and a page asks more of * them (see `paletteOf`). */ const ROLES: ReadonlyArray = [ ["--canvas", "canvas"], ["--surface", "canvasSubtle"], ["--text", "text"], ["--muted", "muted"], ["--accent", "blue"], ["--accent-alt", "purple"], ["--accent-2", "pink"], ["--success", "green"], ["--danger", "red"], ["--success-soft", "successBg"], ["--danger-soft", "errorBg"], ]; /** The syntax token names, as a highlighter writes the class suffix, and the theme colour behind one. */ const SYNTAX_ALIASES: ReadonlyArray = [ ["comment", "syntaxComment"], ["keyword", "syntaxKeyword"], ["string", "syntaxString"], ["number", "syntaxNumber"], ["literal", "syntaxNumber"], ["tag", "syntaxString"], ["attr", "syntaxFunction"], ["prop", "syntaxFunction"], ["var", "syntaxType"], ]; /** * What a theme cannot name, one entry per appearance. * * `raisedStep` lifts a plane off the surface, `dimStep` takes the faint tier one step down from * `muted`, `accentAlpha` tints a selected row, and `searchStep` pulls the search highlight toward * the canvas when a page puts small text on it. `scrim`/`shadow` are a page's overlays and * elevation rather than colours on a grid. `borderStep`/`borderStrongStep` are how far a rule * travels from the canvas toward the text: a terminal draws its borders on a cell grid at 3:1, * which a page reads as a frame rather than as a hairline, so the pair is a step instead. */ const DERIVED: Record< Appearance, { raisedStep: number; dimStep: number; borderStep: number; borderStrongStep: number; accentAlpha: number; searchStep: number; warnStep: number; scrim: string; shadow1: string; shadow2: string; } > = { dark: { raisedStep: 0.06, dimStep: 0.04, borderStep: 0.16, borderStrongStep: 0.26, // A card's paper is this tint over a surface, and the board holds its faint tier to // 4.5:1 there: at the 15% a selected row can take, `--dim` reaches 4.09:1 (dark) and // 4.31:1 (light), so the tint is the strength both jobs can share. accentAlpha: 0.08, // The theme's search highlight is a terminal's background, chosen to sit behind the terminal's // own foreground. A page puts 12px `--muted` on it — the board's plan-lane paper — where 4.05:1 // is under AA, so the paper takes a step toward the canvas: 4.97:1 for `--muted`, 4.64:1 for // `--dim`, and still amber. searchStep: 0.4, // Nothing to step: the theme's yellow on the raised plane already reaches 6.03:1, past the // 4.5 a page asks of 13px text, so the dark page keeps the colour the terminal states. warnStep: 0, scrim: "rgba(3,6,12,.62)", shadow1: "0 1px 2px rgba(0,0,0,.45),0 2px 8px rgba(0,0,0,.35)", shadow2: "0 8px 24px rgba(0,0,0,.55),0 1px 3px rgba(0,0,0,.4)", }, light: { // A smaller step: at the dark one, a label on the raised plane drops under AA. The hairline // is smaller than dark's for the same reason — one step toward the text reads the same on // both canvases, and the pair is pinned by its ratio in `palette.test.ts`. raisedStep: 0.03, dimStep: 0.08, borderStep: 0.11, borderStrongStep: 0.2, accentAlpha: 0.06, // Nothing to step: the theme's highlight already clears 4.5 for both tiers on a light canvas // (5.93:1 for `--muted`, 4.88:1 for `--dim`), so light's paper stays the theme's value. searchStep: 0, // A terminal wears its yellow as foreground on a dark canvas; a page puts 13px warn text on the // raised plane, where the theme's own value reaches 4.34:1. A tenth of the way toward the text // is 4.89:1 — margin rather than the 0.05 that would pass at 4.58. warnStep: 0.1, scrim: "rgba(15,23,42,.32)", shadow1: "0 1px 2px rgba(16,24,40,.06),0 2px 8px rgba(16,24,40,.08)", shadow2: "0 8px 24px rgba(16,24,40,.12),0 1px 3px rgba(16,24,40,.08)", }, }; /** The per-appearance steps above, for tests that hold a derivation to its inputs. */ export const WEB_DERIVATION = DERIVED; /** Theme-independent scales: fonts, sizes, spacing, radius, motion, focus. */ const SCALES = '--font-ui:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;' + '--font-mono:ui-monospace,SFMono-Regular,"SF Mono",Menlo,Consolas,monospace;' + "--fs-xs:12px;--fs-sm:13px;--fs-md:14px;--fs-base:15px;--fs-lg:19px;--fs-xl:24px;--fs-code:13px;" + "--lh-tight:1.25;--lh-snug:1.4;--lh-normal:1.5;--lh-relaxed:1.6;" + "--fw-normal:400;--fw-medium:500;--fw-semibold:600;--fw-bold:700;" + "--space-1:4px;--space-2:8px;--space-3:12px;--space-4:16px;--space-5:20px;--space-6:24px;--space-7:32px;--space-8:48px;" + "--radius-sm:4px;--radius-md:6px;--radius-lg:8px;--radius-badge:4px;" + "--measure-prose:78ch;" + "--motion-fast:80ms;--motion-base:140ms;--motion-ease:cubic-bezier(.2,.6,.2,1);--focus-ring:2px"; /** A theme colour value — a hex or the name of one of its vars — as a colour. */ function resolve(theme: ThemeDocument, value: string | number): string { if (typeof value === "number") return String(value); return value.startsWith("#") ? value : (theme.vars[value] ?? value); } /** * `#rrggbb` mixed toward another colour: `t` of the way from `a` to `b`. * * Exported so a test can hold a derivation to its inputs — a value that clears a floor by a * hundredth has to be checked as a relation, not as the hex it happens to produce today. */ export function mix(a: string, b: string, t: number): string { const channel = (hex: string, at: number): number => Number.parseInt(hex.replace("#", "").slice(at, at + 2), 16); const between = (x: number, y: number): number => Math.round(x + (y - x) * t); const r = between(channel(a, 0), channel(b, 0)); const g = between(channel(a, 2), channel(b, 2)); const bl = between(channel(a, 4), channel(b, 4)); return `#${[r, g, bl].map((value) => value.toString(16).padStart(2, "0")).join("")}`; } /** `#rrggbb` (or `#rgb`) plus alpha -> `rgba(r,g,b,a)`. */ function rgba(hex: string, alpha: number): string { const raw = hex.replace("#", ""); const full = raw.length === 3 ? raw .split("") .map((char) => char + char) .join("") : raw; const r = Number.parseInt(full.slice(0, 2), 16); const g = Number.parseInt(full.slice(2, 4), 16); const b = Number.parseInt(full.slice(4, 6), 16); return `rgba(${r},${g},${b},${alpha})`; } /** * The full custom-property set for one appearance. * * Every colour is the theme's, except three that a terminal does not ask for. `--dim` is one step * off `muted` toward the canvas: a terminal asks 3:1 of its `dim` on a canvas, while the kit asks * 4.5 where a page reads it, and the theme's value reaches 4.08:1 (dark) and 4.05:1 (light) on a * raised plane. `--accent-soft` carries the alpha both of its jobs can share — a selected row and a * card's paper, which the board holds to the same 4.5. And `--search-soft` is the theme's search * highlight stepped toward the canvas, because the board sets 12px `--muted` on it as a lane's * paper. A floor is a guarantee, so it does not move to keep a mapping literal. */ function paletteOf(appearance: Appearance): Record { const theme = THEMES[appearance]; const steps = DERIVED[appearance]; const tokens: Record = {}; for (const [token, variable] of ROLES) tokens[token] = theme.vars[variable] ?? ""; tokens["--dim"] = mix(theme.vars.muted ?? "", theme.vars.canvas ?? "", steps.dimStep); tokens["--border"] = mix(theme.vars.canvas ?? "", theme.vars.text ?? "", steps.borderStep); tokens["--border-strong"] = mix(theme.vars.canvas ?? "", theme.vars.text ?? "", steps.borderStrongStep); // One step above `--surface`, for a block that must read as a plane rather than an outline. tokens["--raised"] = mix(theme.vars.canvasSubtle ?? "", theme.vars.text ?? "", steps.raisedStep); // Small warn text is a page's need rather than a terminal's, so the colour is the theme's yellow // stepped toward the text per appearance — the one reason this token is derived here instead of // mapped in `ROLES`. Ratios live with the step. tokens["--warn"] = mix(theme.vars.yellow ?? "", theme.vars.text ?? "", steps.warnStep); tokens["--accent-soft"] = rgba(theme.vars.blue ?? "", steps.accentAlpha); tokens["--search-soft"] = mix(theme.vars.searchBg ?? "", theme.vars.canvas ?? "", steps.searchStep); // A label on the accent fill is the canvas colour: the light theme's dark blue takes white, // and the dark theme's light blue takes the canvas it sits on. tokens["--on-accent"] = theme.vars.canvas ?? ""; tokens["--scrim"] = steps.scrim; tokens["--shadow-1"] = steps.shadow1; tokens["--shadow-2"] = steps.shadow2; for (const [token, name] of SYNTAX_ALIASES) tokens[`--tok-${token}`] = resolve(theme, theme.colors[name] ?? ""); return tokens; } /** Every token value per appearance, for tests and for building the CSS. */ export const WEB_THEME_TOKENS: { dark: Record; light: Record } = { dark: paletteOf("dark"), light: paletteOf("light"), }; /** `a{color:var(--tok-keyword)}…` for each syntax token. */ const SYNTAX_RULES = [...new Set(SYNTAX_ALIASES.map(([token]) => token))] .map((token) => `.tok-${token}{color:var(--tok-${token})}`) .join(""); function declarations(tokens: Record): string { return Object.entries(tokens) .map(([token, value]) => `${token}:${value}`) .join(";"); } /** * The theme CSS: the theme's dark tokens at `:root`, an explicit light override, the system light * fallback, and the `tok-*` colour rules. Pages prepend this to their own component CSS. */ export const WEB_THEME_CSS = `:root{${declarations(WEB_THEME_TOKENS.dark)};${SCALES};color-scheme:dark} :root[data-theme="light"]{${declarations(WEB_THEME_TOKENS.light)};color-scheme:light} :root[data-theme="dark"]{color-scheme:dark} @media (prefers-color-scheme:light){:root:not([data-theme]){${declarations(WEB_THEME_TOKENS.light)};color-scheme:light}} ${SYNTAX_RULES} /* Paper is white: print the light palette whatever the screen is showing. */ @media print{:root{${declarations(WEB_THEME_TOKENS.light)};color-scheme:light}}`;