// Modular theme engine: Shape-Sets, Color-Sets (presets, gradients, custom tokens), // independent UI/3D comfort toggles, and live 2D/3D palette resolution. import { type ColorSet, type CustomThemeTokens, DEFAULT_THEME_CONFIG, type GradientThemeConfig, getSettings, saveSettings, type ShapeSet, type ThemeConfig, } from "./api.js"; import type { SpaceWeather } from "../shared/types.js"; export type { ColorSet, CustomThemeTokens, GradientThemeConfig, ShapeSet, ThemeConfig, }; export interface PresetDescriptor { id: T; label: string; tagline: string; } export const SHAPE_SET_OPTIONS: PresetDescriptor[] = [ { id: "chamfered", label: "Chamfered", tagline: "Angled telemetry corner cuts (Default)", }, { id: "smooth", label: "Smooth", tagline: "Soft rounded corners (Eye-Saver style)", }, { id: "sharp", label: "Sharp / Square", tagline: "Crisp 90° edges, zero corner radius", }, ]; export interface ColorSetPreset extends PresetDescriptor { swatches: [string, string, string, string]; tokens?: CustomThemeTokens; } export const COLOR_SET_OPTIONS: ColorSetPreset[] = [ { id: "eyebleed", label: "Eyebleed", tagline: "High-contrast night ops & vivid accents (Classic)", swatches: ["#0b0e14", "#19202e", "#60a5fa", "#e2e8f0"], tokens: { bgDeep: "#0b0e14", bgPanelSolid: "#12161f", bgElevated: "#19202e", border: "#2a3449", text: "#e2e8f0", textDim: "#94a3b8", accent: "#60a5fa", glow: "#60a5fa", green: "#34d399", amber: "#f59e0b", red: "#f87171", violet: "#a78bfa", panelOpacity: 0.92, }, }, { id: "subtle", label: "Subtle", tagline: "Muted slate & softened accents (Eye-Saver)", swatches: ["#10141c", "#1e2531", "#648bb8", "#d5dde8"], tokens: { bgDeep: "#10141c", bgPanelSolid: "#161b24", bgElevated: "#1e2531", border: "#2b3444", text: "#d5dde8", textDim: "#8b9ab0", accent: "#648bb8", glow: "#648bb8", green: "#47a885", amber: "#c98e3b", red: "#c96b6b", violet: "#8b7cc8", panelOpacity: 0.96, }, }, { id: "low-contrast", label: "Low Contrast", tagline: "Gentle luminance spread for long sessions", swatches: ["#191d26", "#272d3a", "#6d90ba", "#b3becf"], tokens: { bgDeep: "#191d26", bgPanelSolid: "#202530", bgElevated: "#272d3a", border: "#343c4d", text: "#b3becf", textDim: "#838fa3", accent: "#6d90ba", glow: "#6d90ba", green: "#5fa88d", amber: "#c2944c", red: "#c47678", violet: "#9182c4", panelOpacity: 0.96, }, }, { id: "catppuccin-mocha", label: "Catppuccin Mocha", tagline: "Warm pastel dark palette on deep espresso (Default)", swatches: ["#1e1e2e", "#313244", "#89b4fa", "#cdd6f4"], tokens: { bgDeep: "#1e1e2e", bgPanelSolid: "#181825", bgElevated: "#313244", border: "#45475a", text: "#cdd6f4", textDim: "#a6adc8", accent: "#89b4fa", glow: "#74c7ec", green: "#a6e3a1", amber: "#f9e2af", red: "#f38ba8", violet: "#cba6f7", panelOpacity: 0.94, }, }, { id: "daylight", label: "Daylight Light", tagline: "Clean bright mode for high-ambient rooms", swatches: ["#f1f5f9", "#e2e8f0", "#0284c7", "#0f172a"], tokens: { bgDeep: "#f1f5f9", bgPanelSolid: "#ffffff", bgElevated: "#e2e8f0", border: "#cbd5e1", text: "#0f172a", textDim: "#475569", accent: "#0284c7", glow: "#0284c7", green: "#059669", amber: "#d97706", red: "#dc2626", violet: "#7c3aed", panelOpacity: 0.96, }, }, { id: "gradient", label: "Customizable Gradients", tagline: "Multi-stop backdrop & panel gradients", swatches: ["#0b1120", "#172554", "#38bdf8", "#e2e8f0"], }, { id: "custom", label: "Fully Custom Set", tagline: "Edit every surface, text & semantic token", swatches: ["#12161f", "#34d399", "#f59e0b", "#a78bfa"], }, ]; export interface GradientStarter { id: string; label: string; config: GradientThemeConfig; } export const GRADIENT_STARTERS: GradientStarter[] = [ { id: "nebula", label: "Nebula", config: { stops: ["#0b1120", "#1e1b4b", "#0f172a"], angle: 135, mode: "linear", surfaceBase: "dark", applyToPanels: true, }, }, { id: "aurora", label: "Aurora", config: { stops: ["#071a1f", "#0f3d3e", "#1b1f3b"], angle: 150, mode: "linear", surfaceBase: "dark", applyToPanels: true, }, }, { id: "sunset", label: "Sunset", config: { stops: ["#1f1024", "#4c1d3d", "#2c1810"], angle: 125, mode: "linear", surfaceBase: "dark", applyToPanels: true, }, }, { id: "ocean", label: "Deep Ocean", config: { stops: ["#06131f", "#0c2d48", "#0b192c"], angle: 165, mode: "radial", surfaceBase: "dark", applyToPanels: true, }, }, { id: "dawn", label: "Solar Dawn", config: { stops: ["#f8fafc", "#e0f2fe", "#fef3c7"], angle: 135, mode: "linear", surfaceBase: "light", applyToPanels: true, }, }, ]; const CUSTOM_CSS_VAR_KEYS = [ "--bg-deep", "--bg-panel", "--bg-panel-solid", "--bg-elevated", "--bg-hover", "--border", "--border-bright", "--border-subtle", "--text", "--text-dim", "--text-faint", "--brand-blue", "--brand-strong", "--sim-accent", "--sim-accent-dim", "--sim-glow", "--green", "--amber", "--red", "--violet", "--bg-glass", "--bg-inset", "--bg-header-fade", "--overlay-scrim", "--badge-bg", "--btn-primary-bg", "--btn-primary-hover", "--btn-primary-text", "--btn-warn-bg", "--btn-warn-text", "--btn-danger-bg", "--btn-danger-text", "--btn-ghost-bg", "--title-color", "--bg-gradient", "--panel-gradient", ] as const; export function normalizeHex(hex: string, fallback = "#0b0e14"): string { const trimmed = hex.trim(); if (/^#[0-9a-fA-F]{6}$/.test(trimmed)) return trimmed.toLowerCase(); if (/^#[0-9a-fA-F]{3}$/.test(trimmed)) { const [, r, g, b] = trimmed; return `#${r}${r}${g}${g}${b}${b}`.toLowerCase(); } return fallback; } export function hexToRgb(hex: string): [number, number, number] { const clean = normalizeHex(hex).slice(1); return [ parseInt(clean.slice(0, 2), 16), parseInt(clean.slice(2, 4), 16), parseInt(clean.slice(4, 6), 16), ]; } export function rgbToHex(r: number, g: number, b: number): string { const clamp = (n: number) => Math.max(0, Math.min(255, Math.round(n))) .toString(16) .padStart(2, "0"); return `#${clamp(r)}${clamp(g)}${clamp(b)}`; } export function hexToRgba(hex: string, alpha: number): string { const [r, g, b] = hexToRgb(hex); const a = Math.max(0, Math.min(1, alpha)); return `rgba(${r}, ${g}, ${b}, ${Number(a.toFixed(3))})`; } export function mixHex(hexA: string, hexB: string, weightB: number): string { const w = Math.max(0, Math.min(1, weightB)); const [r1, g1, b1] = hexToRgb(hexA); const [r2, g2, b2] = hexToRgb(hexB); return rgbToHex( r1 + (r2 - r1) * w, g1 + (g2 - g1) * w, b1 + (b2 - b1) * w, ); } export function isLightHex(hex: string): boolean { const [r, g, b] = hexToRgb(hex).map((v) => { const s = v / 255; return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4); }); const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b; return lum > 0.42; } export function buildCustomTokensFromPreset( presetId: Exclude, ): CustomThemeTokens { const found = COLOR_SET_OPTIONS.find((p) => p.id === presetId); const tokens = found?.tokens ?? COLOR_SET_OPTIONS[0].tokens!; return { ...tokens }; } export function formatGradientCss(g: GradientThemeConfig): string { const stops = g.stops.length >= 2 ? g.stops : ["#0b1120", "#172554"]; if (g.mode === "radial") { return `radial-gradient(circle at 50% 25%, ${stops.join(", ")})`; } return `linear-gradient(${g.angle}deg, ${stops.join(", ")})`; } let previewThemeOverride: ThemeConfig | null = null; export function getThemeConfig(): ThemeConfig { return previewThemeOverride ?? getSettings().theme; } export function setThemePreview( theme: ThemeConfig | null, dispatchEvent = true, ): void { previewThemeOverride = theme ? structuredClone(theme) : null; applyThemeConfig(getThemeConfig(), dispatchEvent); } export function saveThemeConfig(theme: ThemeConfig): void { previewThemeOverride = null; const s = getSettings(); s.theme = structuredClone(theme); saveSettings(s); applyVisualPreferences(); } export function isLightSurface(): boolean { const t = getThemeConfig(); if (t.colorSet === "daylight") return true; if (t.colorSet === "gradient") return t.gradient.surfaceBase === "light"; if (t.colorSet === "custom") return isLightHex(t.custom.bgDeep); return false; } export function isReducedMotionEffects(): boolean { return !getThemeConfig().motionEffects; } export function getColorMode(): "dark" | "light" { return isLightSurface() ? "light" : "dark"; } function clearCustomCssVars(): void { const style = document.documentElement.style; for (const key of CUSTOM_CSS_VAR_KEYS) { style.removeProperty(key); } } function applyDerivedTokens( tokens: CustomThemeTokens, uiEffects: boolean, extras: { bgGradient?: string; panelGradient?: string } = {}, ): void { const light = isLightHex(tokens.bgDeep); const style = document.documentElement.style; const set = (k: string, v: string) => style.setProperty(k, v); const bgHover = mixHex(tokens.bgElevated, tokens.text, light ? 0.1 : 0.08); const borderBright = mixHex(tokens.border, tokens.text, 0.18); const textFaint = mixHex(tokens.textDim, tokens.bgDeep, 0.35); const brandStrong = mixHex(tokens.accent, light ? "#000000" : "#0b0e14", 0.22); set("--bg-deep", tokens.bgDeep); set("--bg-panel", hexToRgba(tokens.bgPanelSolid, tokens.panelOpacity)); set("--bg-panel-solid", tokens.bgPanelSolid); set("--bg-elevated", tokens.bgElevated); set("--bg-hover", bgHover); set("--border", tokens.border); set("--border-bright", borderBright); set("--border-subtle", hexToRgba(tokens.border, 0.55)); set("--text", tokens.text); set("--text-dim", tokens.textDim); set("--text-faint", textFaint); set("--brand-blue", tokens.accent); set("--brand-strong", brandStrong); set("--sim-accent", tokens.accent); set("--sim-accent-dim", hexToRgba(tokens.accent, 0.28)); set( "--sim-glow", uiEffects ? hexToRgba(tokens.glow, light ? 0.14 : 0.18) : "transparent", ); set("--green", tokens.green); set("--amber", tokens.amber); set("--red", tokens.red); set("--violet", tokens.violet); set("--bg-glass", hexToRgba(tokens.bgPanelSolid, Math.min(0.95, tokens.panelOpacity))); set("--bg-inset", hexToRgba(tokens.bgDeep, light ? 0.72 : 0.6)); set("--bg-header-fade", light ? "transparent" : hexToRgba(tokens.bgDeep, 0.92)); set("--overlay-scrim", light ? "rgba(15, 23, 42, 0.42)" : "rgba(7, 10, 15, 0.84)"); set("--badge-bg", light ? tokens.bgPanelSolid : hexToRgba(tokens.bgDeep, 0.72)); const accentIsLight = isLightHex(tokens.accent); set( "--btn-primary-bg", light ? tokens.accent : hexToRgba(tokens.accent, 0.22), ); set( "--btn-primary-hover", light ? brandStrong : hexToRgba(tokens.accent, 0.36), ); set( "--btn-primary-text", light ? (accentIsLight ? "#0f172a" : "#ffffff") : mixHex(tokens.accent, "#ffffff", 0.75), ); set( "--btn-warn-bg", hexToRgba(tokens.amber, 0.15), ); set( "--btn-warn-text", light ? mixHex(tokens.amber, "#000000", 0.35) : mixHex(tokens.amber, "#ffffff", 0.65), ); set("--btn-danger-bg", hexToRgba(tokens.red, 0.13)); set( "--btn-danger-text", light ? mixHex(tokens.red, "#000000", 0.35) : mixHex(tokens.red, "#ffffff", 0.65), ); set( "--btn-ghost-bg", light ? tokens.bgPanelSolid : hexToRgba(tokens.bgPanelSolid, 0.68), ); set("--title-color", light ? tokens.text : mixHex(tokens.text, "#ffffff", 0.55)); set("--bg-gradient", extras.bgGradient ?? "none"); set("--panel-gradient", extras.panelGradient ?? "none"); } function buildGradientTokens(theme: ThemeConfig): { tokens: CustomThemeTokens; bgGradient: string; panelGradient: string; } { const g = theme.gradient; const s0 = normalizeHex(g.stops[0] ?? "#0b1120", "#0b1120"); const s1 = normalizeHex(g.stops[1] ?? "#172554", "#172554"); const sLast = normalizeHex(g.stops[g.stops.length - 1] ?? s1, s1); const light = g.surfaceBase === "light"; const bgDeep = light ? mixHex("#f1f5f9", s0, 0.16) : mixHex("#0b0e14", s0, 0.45); const bgPanelSolid = light ? mixHex("#ffffff", s0, 0.08) : mixHex("#111622", s0, 0.42); const bgElevated = light ? mixHex("#e2e8f0", s1, 0.14) : mixHex("#182032", s1, 0.4); const border = light ? mixHex("#cbd5e1", s1, 0.25) : mixHex("#2a3449", s1, 0.42); // Choose an accent that stays legible against the surface base const rawAccent = isLightHex(s1) === light ? sLast : s1; const accent = light ? (isLightHex(rawAccent) ? "#0284c7" : rawAccent) : (isLightHex(rawAccent) ? rawAccent : mixHex(rawAccent, "#60a5fa", 0.62)); const tokens: CustomThemeTokens = { bgDeep, bgPanelSolid, bgElevated, border, text: light ? "#0f172a" : "#e2e8f0", textDim: light ? "#475569" : "#9fb0c7", accent, glow: accent, green: light ? "#059669" : "#34d399", amber: light ? "#d97706" : "#f59e0b", red: light ? "#dc2626" : "#f87171", violet: light ? "#7c3aed" : "#a78bfa", panelOpacity: light ? 0.9 : 0.86, }; const bgGradient = formatGradientCss(g); const panelGradient = g.applyToPanels ? `linear-gradient(155deg, ${hexToRgba(s0, light ? 0.24 : 0.34)}, ${hexToRgba(sLast, light ? 0.16 : 0.22)})` : "none"; return { tokens, bgGradient, panelGradient }; } export function applyThemeConfig( theme: ThemeConfig, dispatchEvent = true, ): void { const root = document.documentElement; const light = theme.colorSet === "daylight" || (theme.colorSet === "gradient" && theme.gradient.surfaceBase === "light") || (theme.colorSet === "custom" && isLightHex(theme.custom.bgDeep)); root.dataset.shapeSet = theme.shapeSet; root.dataset.colorSet = theme.colorSet; root.dataset.surfacePolarity = light ? "light" : "dark"; root.classList.toggle("reduced-ui-effects", !theme.uiEffects); root.classList.toggle("reduced-motion-effects", !theme.motionEffects); document.body.dataset.dynamicTheme = theme.dynamicAccent ? "on" : "off"; clearCustomCssVars(); if (theme.colorSet === "gradient") { const { tokens, bgGradient, panelGradient } = buildGradientTokens(theme); applyDerivedTokens(tokens, theme.uiEffects, { bgGradient, panelGradient }); } else if (theme.colorSet === "custom") { applyDerivedTokens(theme.custom, theme.uiEffects); } if (dispatchEvent) { window.dispatchEvent( new CustomEvent("sgywftp:theme-changed", { detail: { shapeSet: theme.shapeSet, colorSet: theme.colorSet, uiEffects: theme.uiEffects, motionEffects: theme.motionEffects, dynamicAccent: theme.dynamicAccent, }, }), ); } paletteCache = null; } export function applyVisualPreferences(): void { applyThemeConfig(getThemeConfig(), true); } export function syncSimulationTheme(opts: { weather?: SpaceWeather | null; target?: string | null; phase?: "idle" | "ascent" | "orbit" | "failed"; }): void { const theme = getThemeConfig(); applyThemeConfig(theme, false); if (!theme.dynamicAccent) { document.body.dataset.weather = "nominal"; document.body.dataset.target = "earth"; document.body.dataset.phase = "idle"; return; } if (opts.weather) { document.body.dataset.weather = opts.weather; } if (opts.target) { document.body.dataset.target = opts.target; } if (opts.phase) { document.body.dataset.phase = opts.phase; } } export interface ResolvedThemePalette { bgDeep: string; bgPanelSolid: string; bgElevated: string; border: string; text: string; textDim: string; accent: string; green: string; amber: string; red: string; violet: string; isLight: boolean; isGradient: boolean; sceneBg: string; sceneBgWithStars: string; daySkyLow: string; } let paletteCache: ResolvedThemePalette | null = null; export function getThemePalette(): ResolvedThemePalette { if (paletteCache) return paletteCache; const cfg = getThemeConfig(); const isLight = isLightSurface(); const cs = getComputedStyle(document.documentElement); const bodyCs = getComputedStyle(document.body); const readVar = (name: string, fallback: string) => cs.getPropertyValue(name).trim() || fallback; const readBodyVar = (name: string, fallback: string) => bodyCs.getPropertyValue(name).trim() || fallback; const bgDeep = readVar("--bg-deep", isLight ? "#f1f5f9" : "#0b0e14"); const bgPanelSolid = readVar( "--bg-panel-solid", isLight ? "#ffffff" : "#12161f", ); const bgElevated = readVar( "--bg-elevated", isLight ? "#e2e8f0" : "#19202e", ); const border = readVar("--border", isLight ? "#cbd5e1" : "#2a3449"); const text = readVar("--text", isLight ? "#0f172a" : "#e2e8f0"); const textDim = readVar("--text-dim", isLight ? "#475569" : "#94a3b8"); const accent = readBodyVar("--sim-accent", isLight ? "#0284c7" : "#60a5fa"); const green = readVar("--green", isLight ? "#059669" : "#34d399"); const amber = readVar("--amber", isLight ? "#d97706" : "#f59e0b"); const red = readVar("--red", isLight ? "#dc2626" : "#f87171"); const violet = readVar("--violet", isLight ? "#7c3aed" : "#a78bfa"); const sceneBg = isLight && cfg.colorSet === "daylight" ? "#dce5f0" : bgDeep; const sceneBgWithStars = isLight ? "#1e293b" : bgDeep; const daySkyLow = isLight ? "#cbdcf0" : mixHex("#0c1a33", bgDeep, 0.35); paletteCache = { bgDeep, bgPanelSolid, bgElevated, border, text, textDim, accent, green, amber, red, violet, isLight, isGradient: cfg.colorSet === "gradient", sceneBg, sceneBgWithStars, daySkyLow, }; return paletteCache; }