SPB Git

Repository Public

themes.ts 5.2 KB 78 lines typescript

1/**2 * TypeScript copy of the palettes of sheet.css, for outputs that cannot read CSS variables (standalone SVG export).3 * tests/unit/renderer.themes.test.ts asserts that it matches sheet.css token for token.4 */5import { THEMES, TONES, type Tone } from "@/schema";67export type ThemePreset = (typeof THEMES)[number];8export type BaseToken = "bg" | "surface" | "raised" | "ink" | "muted" | "faint" | "line" | "grid";9export type ThemePalette = Record<BaseToken, string> & { scheme: "light" | "dark"; tones: Record<Tone, { strong: string; soft: string }> };1011const tones = (v: [string, string][]): Record<Tone, { strong: string; soft: string }> =>12  Object.fromEntries(TONES.map((tone, i) => [tone, { strong: v[i][0], soft: v[i][1] }])) as Record<Tone, { strong: string; soft: string }>;1314// Tone order: neutral, blue, teal, green, amber, red, violet, pink.15export const THEME_PALETTES: Record<ThemePreset, ThemePalette> = {16  "scientific-light": {17    scheme: "light", bg: "#ffffff", surface: "#ffffff", raised: "#f5f6f8", ink: "#101828", muted: "#475467", faint: "#667085", line: "#e2e5ea", grid: "#eceef2",18    tones: tones([["#475467", "#f2f4f7"], ["#1d4ed8", "#e8efff"], ["#0f766e", "#dff5f2"], ["#15803d", "#e3f6e8"], ["#a45c00", "#fdf0d5"], ["#b42318", "#fde8e6"], ["#6d28d9", "#efe8ff"], ["#be185d", "#fde7f1"]]),19  },20  "scientific-dark": {21    scheme: "dark", bg: "#0d1117", surface: "#141a23", raised: "#1b2330", ink: "#e9edf3", muted: "#b1bac8", faint: "#8b95a6", line: "#2a3444", grid: "#212a38",22    tones: tones([["#b1bac8", "#222b39"], ["#8ab4ff", "#172a4d"], ["#5eead4", "#0f3532"], ["#6ee7a0", "#12351f"], ["#fbbf5a", "#3a2a0c"], ["#ff9b8f", "#431a16"], ["#c4a5ff", "#2c1f52"], ["#ff9ac6", "#45142c"]]),23  },24  editorial: {25    scheme: "light", bg: "#fbfaf7", surface: "#ffffff", raised: "#f3f0e8", ink: "#1a1814", muted: "#55504a", faint: "#746e66", line: "#e3ded2", grid: "#ece8de",26    tones: tones([["#55504a", "#f1eee6"], ["#1e40af", "#e6ecfb"], ["#0f6b66", "#dcf1ee"], ["#2f6b1f", "#e6f2de"], ["#9a5300", "#f9ebcf"], ["#a8291b", "#f9e2de"], ["#5b2bb5", "#ece4fb"], ["#a81a57", "#f9e1ec"]]),27  },28  blueprint: {29    scheme: "dark", bg: "#0b1f3a", surface: "#0f2748", raised: "#143159", ink: "#eaf2ff", muted: "#b6c8e4", faint: "#8ea6c9", line: "#27477a", grid: "#1a3663",30    tones: tones([["#b6c8e4", "#173560"], ["#9cc4ff", "#1a3f78"], ["#6fe3d6", "#0f4450"], ["#86e8a8", "#12443f"], ["#ffd27a", "#44391f"], ["#ffa69b", "#4b2636"], ["#cdb4ff", "#2f2d6e"], ["#ffa8cf", "#4a2350"]]),31  },32  finance: {33    scheme: "light", bg: "#f7f9f8", surface: "#ffffff", raised: "#edf2f0", ink: "#0c1a17", muted: "#41534e", faint: "#5f716c", line: "#d9e2df", grid: "#e6edeb",34    tones: tones([["#41534e", "#edf2f0"], ["#1c4a8f", "#e2ecf9"], ["#0b6b5e", "#d8f0eb"], ["#14713a", "#def3e5"], ["#94590a", "#f8ecd2"], ["#ab2a22", "#f8e3e1"], ["#5a379e", "#ebe5f7"], ["#a3245f", "#f7e2ec"]]),35  },36};3738/** Font stacks of a standalone export (single quotes: the values end up inside XML attributes). */39export const FONT_STACKS = {40  body: "'Inter Variable', Inter, system-ui, -apple-system, 'Segoe UI', sans-serif",41  display: "'Fraunces Variable', Fraunces, 'Iowan Old Style', Georgia, serif",42  mono: "'JetBrains Mono Variable', 'JetBrains Mono', ui-monospace, Menlo, monospace",43} as const;4445/** Every --sh-* custom property of a theme + accent, resolved to a concrete value. */46export function resolveTokens(preset: ThemePreset, accent: Tone): Record<string, string> {47  const p = THEME_PALETTES[preset];48  const out: Record<string, string> = {49    "--sh-bg": p.bg, "--sh-surface": p.surface, "--sh-raised": p.raised, "--sh-ink": p.ink, "--sh-muted": p.muted, "--sh-faint": p.faint,50    "--sh-line": p.line, "--sh-grid": p.grid, "--sh-accent": p.tones[accent].strong, "--sh-accent-soft": p.tones[accent].soft,51    "--sh-font-body": FONT_STACKS.body, "--sh-font-display": FONT_STACKS.display, "--sh-font-mono": FONT_STACKS.mono,52  };53  for (const tone of TONES) { out[`--sh-t-${tone}`] = p.tones[tone].strong; out[`--sh-t-${tone}-soft`] = p.tones[tone].soft; }54  return out;55}5657export type Rgb = [number, number, number];58export function parseHex(hex: string): Rgb {59  const h = hex.replace("#", "");60  const full = h.length === 3 ? h.split("").map((c) => c + c).join("") : h;61  return [parseInt(full.slice(0, 2), 16), parseInt(full.slice(2, 4), 16), parseInt(full.slice(4, 6), 16)];62}63export const toHex = (rgb: Rgb): string => "#" + rgb.map((c) => Math.max(0, Math.min(255, Math.round(c))).toString(16).padStart(2, "0")).join("");64/** Same arithmetic as CSS `color-mix(in srgb, a p%, b)` for opaque colours. */65export const mixHex = (a: string, percent: number, b: string): string => {66  const A = parseHex(a), B = parseHex(b), k = percent / 100;67  return toHex([A[0] * k + B[0] * (1 - k), A[1] * k + B[1] * (1 - k), A[2] * k + B[2] * (1 - k)]);68};69export function luminance(hex: string): number {70  const [r, g, b] = parseHex(hex).map((c) => { const s = c / 255; return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4; });71  return 0.2126 * r + 0.7152 * g + 0.0722 * b;72}73/** WCAG contrast ratio of two opaque colours. */74export function contrast(a: string, b: string): number {75  const la = luminance(a), lb = luminance(b);76  return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);77}78
Files main
Menu

Theme