import * as Schema from "effect/Schema"; export const T3_CHAT_THEME_ID = "t3-chat" as const; export const T3_CHAT_THEME_LABEL = "T3 Chat"; export const GROVE_THEME_ID = "grove" as const; export const GROVE_THEME_LABEL = "Grove"; export const OCEAN_THEME_ID = "ocean" as const; export const OCEAN_THEME_LABEL = "Ocean"; export const EMBER_THEME_ID = "ember" as const; export const EMBER_THEME_LABEL = "Ember"; export const IRIS_THEME_ID = "iris" as const; export const IRIS_THEME_LABEL = "Iris"; export const THEME_FILE_VERSION = 1 as const; export const CUSTOM_THEMES_STORAGE_KEY = "t3code:themes:v1"; export const THEME_FOLLOW_SYSTEM_STORAGE_KEY = "t3code:theme-follow-system"; export const THEME_APPEARANCE_MODE_STORAGE_KEY = "t3code:theme-appearance-mode"; export const THEME_HALVES_STORAGE_KEY = "t3code:theme-halves:v1"; const LEGACY_T3_CHAT_DARK_THEME_ID = "t3-chat-dark"; export const ThemePreference = Schema.String; export type ThemePreference = typeof ThemePreference.Type; export const THEME_COLOR_ROLES = [ "canvas", "chrome", "toolbar", "toolbarForeground", "toolbarBorder", "toolbarControl", "toolbarControlForeground", "toolbarControlHover", "surface", "surfaceRaised", "surfaceOverlay", "text", "textMuted", "border", "input", "focus", "accent", "accentForeground", "secondary", "secondaryForeground", "muted", "mutedForeground", "placeholder", "secondaryLabel", "iconMuted", "error", "errorForeground", "errorSurface", "warning", "warningForeground", "warningSurface", "update", "updateForeground", "updateSurface", "accentSurface", "accentSurfaceForeground", "messageSurface", "messageForeground", "messageAction", "messageActionForeground", "messageActionHover", "codeBackground", "codeForeground", "sidebar", "sidebarForeground", "sidebarMutedForeground", "sidebarControlSurface", "sidebarRowHover", "sidebarRowActive", "sidebarRowSelected", "sidebarBorder", "terminalBackground", "terminalForeground", "terminalCursor", "terminalSelection", "terminalScrollbar", "terminalScrollbarHover", ] as const; export type ThemeColorRole = (typeof THEME_COLOR_ROLES)[number]; const THEME_COLOR_ROLE_SET: ReadonlySet = new Set(THEME_COLOR_ROLES); export type ThemeAppearance = "light" | "dark"; export type ThemeColors = Readonly>; export type ThemeColorOverrides = Readonly>>; export type ThemeVariants = Readonly>>; export type ThemeVariantOverrides = Readonly>>; export type ThemePreferenceMode = ThemeAppearance | "system"; export type ThemeDefinition = Readonly<{ id: string; label: string; appearance: ThemeAppearance; colors: ThemeColors; variants?: ThemeVariants; /** Allows fixed Dev/Nightly artwork to render over this theme's sidebar. */ sidebarArtwork?: boolean; /** True when the palette was generated by the guided editor from its * canvas and accent; such themes reopen in guided mode. */ managed?: boolean; }>; export type ThemeFile = Readonly<{ version: typeof THEME_FILE_VERSION; id: string; name: string; appearance: ThemeAppearance; colors: ThemeColorOverrides; variants?: ThemeVariantOverrides; sidebarArtwork?: boolean; managed?: boolean; }>; const RESERVED_THEME_IDS = new Set([ "system", "light", "dark", T3_CHAT_THEME_ID, GROVE_THEME_ID, OCEAN_THEME_ID, EMBER_THEME_ID, IRIS_THEME_ID, LEGACY_T3_CHAT_DARK_THEME_ID, "t3-grove", "t3-ocean", "t3-ember", "t3-iris", ]); const customThemeListeners = new Set<() => void>(); let customThemesSnapshot: ReadonlyArray | null = null; const themePreviewListeners = new Set<() => void>(); let themePreviewSidebarArtwork: boolean | null = null; export function getThemePreviewSidebarArtwork(): boolean | null { return themePreviewSidebarArtwork; } export function subscribeToThemePreview(listener: () => void): () => void { themePreviewListeners.add(listener); return () => themePreviewListeners.delete(listener); } function setThemePreviewSidebarArtwork(next: boolean | null): void { if (themePreviewSidebarArtwork === next) return; themePreviewSidebarArtwork = next; for (const listener of themePreviewListeners) listener(); } function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } function isThemeAppearance(value: unknown): value is ThemeAppearance { return value === "light" || value === "dark"; } export function isThemeColor(value: unknown): value is string { return ( typeof value === "string" && /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value) ); } function isThemeId(value: unknown): value is string { return typeof value === "string" && /^[a-z0-9](?:[a-z0-9-]{0,47})$/.test(value); } function isThemeLabel(value: unknown): value is string { return typeof value === "string" && value.trim().length > 0 && value.trim().length <= 48; } function parseStoredThemeColors(value: unknown, appearance: ThemeAppearance): ThemeColors | null { if (!isRecord(value)) return null; const colors: Partial> = { ...getDefaultThemeColors(appearance), }; // Tolerate unknown roles and malformed values so themes saved by other // builds (for example one that adds a new role) keep their remaining colors. for (const [role, color] of Object.entries(value)) { if (THEME_COLOR_ROLE_SET.has(role) && isThemeColor(color)) { colors[role as ThemeColorRole] = color; } } return colors as ThemeColors; } function parseStoredThemeVariants( value: unknown, baseAppearance: ThemeAppearance, ): ThemeVariants | null | undefined { if (value === undefined) return undefined; if (!isRecord(value)) return null; const variants: Partial> = {}; for (const [appearance, colors] of Object.entries(value)) { if (!isThemeAppearance(appearance)) return null; // A variant matching the base appearance would be shadowed by the base // colors; drop it so the theme round-trips through parseThemeFile. if (appearance === baseAppearance) continue; const parsedColors = parseStoredThemeColors(colors, appearance); if (!parsedColors) return null; variants[appearance] = parsedColors; } return Object.keys(variants).length > 0 ? variants : undefined; } function parseStoredTheme(value: unknown): ThemeDefinition | null { if (!isRecord(value)) return null; if (!isThemeId(value.id) || RESERVED_THEME_IDS.has(value.id)) return null; if (!isThemeLabel(value.label) || !isThemeAppearance(value.appearance)) return null; const colors = parseStoredThemeColors(value.colors, value.appearance); if (!colors) return null; const variants = parseStoredThemeVariants(value.variants, value.appearance); if (value.variants !== undefined && variants === null) return null; return { id: value.id, label: value.label.trim(), appearance: value.appearance, colors, ...(variants ? { variants } : {}), ...(value.sidebarArtwork === true ? { sidebarArtwork: true } : {}), ...(value.managed === true ? { managed: true } : {}), }; } function readCustomThemesFromStorage(): ReadonlyArray { if (typeof window === "undefined") return []; try { const raw = window.localStorage.getItem(CUSTOM_THEMES_STORAGE_KEY); if (!raw) return []; const parsed: unknown = JSON.parse(raw); if (!Array.isArray(parsed)) return []; const themes: ThemeDefinition[] = []; for (const value of parsed) { const theme = parseStoredTheme(value); if (theme && !themes.some((existing) => existing.id === theme.id)) { themes.push(theme); } } return themes; } catch { return []; } } function notifyCustomThemeListeners() { for (const listener of customThemeListeners) listener(); } export function invalidateCustomThemes() { customThemesSnapshot = null; notifyCustomThemeListeners(); } export function getCustomThemes(): ReadonlyArray { if (customThemesSnapshot === null) { customThemesSnapshot = readCustomThemesFromStorage(); } return customThemesSnapshot; } export function subscribeToCustomThemes(listener: () => void): () => void { customThemeListeners.add(listener); if (typeof window === "undefined") { return () => customThemeListeners.delete(listener); } const handleStorage = (event: StorageEvent) => { if (event.key === CUSTOM_THEMES_STORAGE_KEY || event.key === null) { invalidateCustomThemes(); } }; window.addEventListener("storage", handleStorage); return () => { customThemeListeners.delete(listener); window.removeEventListener("storage", handleStorage); }; } // Earlier builds shipped every maintainer theme under a t3- prefix; only the // genuinely T3-branded palette keeps it. Stored preferences and mixes with the // old ids stay readable through this alias table. const LEGACY_THEME_ID_ALIASES: Readonly> = { [LEGACY_T3_CHAT_DARK_THEME_ID]: T3_CHAT_THEME_ID, "t3-grove": GROVE_THEME_ID, "t3-ocean": OCEAN_THEME_ID, "t3-ember": EMBER_THEME_ID, "t3-iris": IRIS_THEME_ID, }; function normalizeThemeId(themeId: string): string { return LEGACY_THEME_ID_ALIASES[themeId] ?? themeId; } /** * Map a stored preference onto the id the runtime applies, so selection state * matches the theme cards. The legacy dark-variant id stays as-is because it * still carries the appearance hint getThemePreferenceMode reads. */ export function canonicalThemePreference(theme: string): string { return theme === LEGACY_T3_CHAT_DARK_THEME_ID ? theme : normalizeThemeId(theme); } function themeIdFromPreference(theme: ThemePreference): string { return normalizeThemeId(theme); } // Older builds stored the dark T3 Chat palette as a separate theme. Keep // those preferences readable while mapping them to the dark variant. function legacyThemeMode(theme: ThemePreference): ThemeAppearance | null { return theme === LEGACY_T3_CHAT_DARK_THEME_ID ? "dark" : null; } /** * Maintainer palettes use product color roles rather than Tailwind or component * names so the same definitions can feed other clients and native surfaces. */ // Measured from the live t3.chat default theme. Translucent chat surfaces are // flattened over --chat-background so this opaque palette reproduces the // pixels users see after T3 Chat's blur and noise layers are composited. const T3_CHAT_LIGHT_COLORS: ThemeColors = { canvas: "#fdf7fd", // T3 Code's workspace header belongs to the chat panel, so keep it seamless // with the light chat canvas rather than mapping it to T3 Chat's outer shell. chrome: "#fdf7fd", toolbar: "#fdf7fd", toolbarForeground: "#501854", toolbarBorder: "#efbdeb", // T3 Chat's light chrome controls sit on its pale gradient-noise surface, // not the substantially darker solid accent token. toolbarControl: "#f3e6f5", toolbarControlForeground: "#501854", toolbarControlHover: "#eccfe3", surface: "#faf3fb", surfaceRaised: "#fdfafd", surfaceOverlay: "#ffffff", text: "#501854", textMuted: "#ac1668", border: "#eee1ed", input: "#e7c1dc", focus: "#db2777", accent: "#e33f86", accentForeground: "#ffffff", secondary: "#f1c4e6", secondaryForeground: "#77347c", muted: "#eaa7cb", mutedForeground: "#ac1668", placeholder: "#ad83b0", secondaryLabel: "#ac1668", iconMuted: "#ac1668", error: "#f7086c", errorForeground: "#9d174d", errorSurface: "#fde4f1", warning: "#f59e0b", warningForeground: "#b45309", warningSurface: "#fcf0ea", update: "#e33f86", updateForeground: "#ac1668", updateSurface: "#fadfef", accentSurface: "#f3e6f5", accentSurfaceForeground: "#454554", messageSurface: "#f7def2", messageForeground: "#492c61", messageAction: "#e33f86", messageActionForeground: "#ffffff", messageActionHover: "#d56698", // T3 Chat uses a light lavender code surface in light mode. Keeping the // dark plum pair here also leaked the dark palette into T3 Code's diffs. codeBackground: "#f5ecf9", codeForeground: "#673c8b", // The live sidebar is transparent over T3 Chat's outer shell. Use that // rendered shell color rather than its unused, darker sidebar token. sidebar: "#f2e1f4", sidebarForeground: "#454554", sidebarMutedForeground: "#ac1668", sidebarControlSurface: "#f8f8f7", sidebarRowHover: "#f8f8f7", sidebarRowActive: "#f8f8f7", sidebarRowSelected: "#f8f8f7", sidebarBorder: "#eceae9", terminalBackground: "#fdf7fd", terminalForeground: "#501854", terminalCursor: "#db2777", terminalSelection: "#f1c4e6", terminalScrollbar: "#e7c1dc", terminalScrollbarHover: "#eaa7cb", }; const T3_CHAT_DARK_COLORS: ThemeColors = { canvas: "#1f1a24", // T3 Code's workspace header belongs to the chat panel, so keep it seamless // with the canvas rather than mapping it to T3 Chat's outer shell. chrome: "#1f1a24", toolbar: "#1f1a24", toolbarForeground: "#f9f8fb", toolbarBorder: "#27242c", toolbarControl: "#362d3d", toolbarControlForeground: "#d4c7e1", toolbarControlHover: "#463753", // Cards and panels stay in T3 Chat's plum surface family. Near-black here // made the right-panel surface picker look unrelated to the chat canvas. surface: "#29232d", // Pre-composited for the composer's 80% glass layer; this resolves to the // measured #29232d input fill over the canvas. surfaceRaised: "#2c2631", surfaceOverlay: "#100a0e", text: "#f9f8fb", textMuted: "#e7d0dd", border: "#27242c", input: "#302029", focus: "#db2777", accent: "#a3004c", accentForeground: "#fbd0e8", secondary: "#362d3d", secondaryForeground: "#d4c7e1", muted: "#423a45", mutedForeground: "#e7d0dd", placeholder: "#8f8699", secondaryLabel: "#e7d0dd", iconMuted: "#d4c7e1", error: "#9d174d", errorForeground: "#fbd0e8", errorSurface: "#331a2b", warning: "#f59e0b", warningForeground: "#fbbf24", warningSurface: "#412f20", update: "#a3004c", updateForeground: "#fbd0e8", updateSurface: "#37152b", accentSurface: "#463753", accentSurfaceForeground: "#f8f1f5", messageSurface: "#2b2431", messageForeground: "#f2ebfa", messageAction: "#a3004c", messageActionForeground: "#fbd0e8", messageActionHover: "#a2004c", // Diffs and file previews are full workspace surfaces in T3 Code. Keep them // continuous with the themed canvas instead of dropping to near-black. codeBackground: "#1f1a24", codeForeground: "#d8c3ef", // The live sidebar starts from #131314, then gains its hue from a pink // gradient/noise stack. This pre-grain base lands on the same #1a131a // visible shell color after our surface-grain layer is composited. sidebar: "#171018", sidebarForeground: "#f4f4f5", sidebarMutedForeground: "#e7d0dd", sidebarControlSurface: "#261922", sidebarRowHover: "#261922", sidebarRowActive: "#261922", sidebarRowSelected: "#261922", // T3 Chat draws the chat panel edge in this muted pink. The resize rail uses // the same role on hover, so it stays pink instead of falling back to black. sidebarBorder: "#322028", terminalBackground: "#1f1a24", terminalForeground: "#f9f8fb", terminalCursor: "#db2777", terminalSelection: "#362d3d", terminalScrollbar: "#302029", terminalScrollbarHover: "#423a45", }; /** * The palette T3 Code wears with no theme installed, captured from the app's * stock tokens (index.css) so a draft seeded from the default look paints the * pixels the user is already seeing. Alpha-bearing tokens are flattened over * their real backdrops (canvas, or the sidebar for its rows) because theme * colors must be opaque hex. */ const T3_CODE_LIGHT_THEME_COLORS: ThemeColors = { canvas: "#fcfcfc", chrome: "#fcfcfc", toolbar: "#fcfcfc", toolbarForeground: "#27272a", toolbarBorder: "#e4e4e7", toolbarControl: "#ffffff", toolbarControlForeground: "#27272a", toolbarControlHover: "#f4f4f5", surface: "#ffffff", surfaceRaised: "#fcfcfc", surfaceOverlay: "#ffffff", text: "#27272a", textMuted: "#71717b", border: "#e4e4e7", input: "#d4d4d8", focus: "#1b4ed8", accent: "#1b4ed8", accentForeground: "#ffffff", secondary: "#fafafa", secondaryForeground: "#27272a", muted: "#fafafa", mutedForeground: "#71717b", placeholder: "#71717b", secondaryLabel: "#71717b", iconMuted: "#71717b", error: "#fb2c36", errorForeground: "#c10007", errorSurface: "#fcebec", warning: "#fe9a00", warningForeground: "#bb4d00", warningSurface: "#fcf4e8", update: "#1b4ed8", updateForeground: "#1b4ed8", updateSurface: "#e0e6f7", accentSurface: "#f4f4f5", accentSurfaceForeground: "#18181b", messageSurface: "#f4f4f5", messageForeground: "#27272a", messageAction: "#1b4ed8", messageActionForeground: "#ffffff", messageActionHover: "#3160db", codeBackground: "#ffffff", codeForeground: "#27272a", sidebar: "#fafafa", sidebarForeground: "#27272a", sidebarMutedForeground: "#71717b", sidebarControlSurface: "#f4f4f5", sidebarRowHover: "#fcfcfc", sidebarRowActive: "#ffffff", sidebarRowSelected: "#ffffff", sidebarBorder: "#e4e4e7", terminalBackground: "#fcfcfc", terminalForeground: "#27272a", terminalCursor: "#26384e", terminalSelection: "#d0d6dd", terminalScrollbar: "#d6d6d6", terminalScrollbarHover: "#bdbdbd", }; const T3_CODE_DARK_THEME_COLORS: ThemeColors = { canvas: "#0a0a0a", chrome: "#0a0a0a", toolbar: "#0a0a0a", toolbarForeground: "#f5f5f5", toolbarBorder: "#191919", toolbarControl: "#191919", toolbarControlForeground: "#f5f5f5", toolbarControlHover: "#141414", surface: "#111111", surfaceRaised: "#141414", surfaceOverlay: "#191919", text: "#f5f5f5", textMuted: "#818181", border: "#191919", input: "#1e1e1e", focus: "#366ffb", accent: "#366ffb", accentForeground: "#ffffff", secondary: "#141414", secondaryForeground: "#f5f5f5", muted: "#141414", mutedForeground: "#818181", placeholder: "#818181", secondaryLabel: "#818181", iconMuted: "#818181", error: "#fb414a", errorForeground: "#ff6467", errorSurface: "#301214", warning: "#fe9a00", warningForeground: "#ffb900", warningSurface: "#312108", update: "#366ffb", updateForeground: "#51a2ff", updateSurface: "#121c35", accentSurface: "#141414", accentSurfaceForeground: "#f5f5f5", messageSurface: "#141414", messageForeground: "#f5f5f5", messageAction: "#366ffb", messageActionForeground: "#ffffff", messageActionHover: "#3265e3", codeBackground: "#111111", codeForeground: "#f5f5f5", sidebar: "#000000", sidebarForeground: "#f1f3f7", sidebarMutedForeground: "#a3a3a3", sidebarControlSurface: "#0a0a0a", sidebarRowHover: "#131313", sidebarRowActive: "#1a1b1b", sidebarRowSelected: "#111111", sidebarBorder: "#141414", terminalBackground: "#0a0a0a", terminalForeground: "#f5f5f5", terminalCursor: "#b4cbff", terminalSelection: "#343a47", terminalScrollbar: "#222222", terminalScrollbarHover: "#363636", }; /** * The standard T3 Code look as a theme palette, for seeding a new theme when * no theme is installed. Distinct from {@link getDefaultThemeColors}, which * carries the flagship T3 Chat palette used to fill roles omitted by theme * files. */ export function getStandardThemeColors(appearance: ThemeAppearance): ThemeColors { return appearance === "dark" ? T3_CODE_DARK_THEME_COLORS : T3_CODE_LIGHT_THEME_COLORS; } type ThemeRgbColor = { r: number; g: number; b: number; }; type ThemeHslColor = { h: number; s: number; l: number; }; const THEME_LIGHT_FOREGROUND: ThemeRgbColor = { r: 255, g: 250, b: 255 }; const THEME_DARK_FOREGROUND: ThemeRgbColor = { r: 36, g: 21, b: 35 }; const THEME_WHITE_FOREGROUND: ThemeRgbColor = { r: 255, g: 255, b: 255 }; const THEME_BLACK_FOREGROUND: ThemeRgbColor = { r: 0, g: 0, b: 0 }; function parseThemeRgbColor(value: string, fallback: ThemeRgbColor): ThemeRgbColor { const match = value.trim().match(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i); if (!match) return fallback; const raw = match[1]; if (!raw) return fallback; const hex = raw.length <= 4 ? raw .slice(0, 3) .split("") .map((part) => part.repeat(2)) .join("") : raw.slice(0, 6); if (hex.length !== 6) return fallback; return { r: Number.parseInt(hex.slice(0, 2), 16), g: Number.parseInt(hex.slice(2, 4), 16), b: Number.parseInt(hex.slice(4, 6), 16), }; } function themeRgbToHexColor(color: ThemeRgbColor): string { return `#${[color.r, color.g, color.b] .map((channel) => Math.round(Math.min(255, Math.max(0, channel))) .toString(16) .padStart(2, "0"), ) .join("")}`; } function themeRgbToHsl(color: ThemeRgbColor): ThemeHslColor { const red = color.r / 255; const green = color.g / 255; const blue = color.b / 255; const max = Math.max(red, green, blue); const min = Math.min(red, green, blue); const delta = max - min; const lightness = (max + min) / 2; if (delta === 0) return { h: 0, s: 0, l: lightness }; const saturation = delta / (1 - Math.abs(2 * lightness - 1)); let hue = 0; if (max === red) hue = ((green - blue) / delta) % 6; else if (max === green) hue = (blue - red) / delta + 2; else hue = (red - green) / delta + 4; return { h: (hue * 60 + 360) % 360, s: saturation, l: lightness }; } function themeHslToRgb(color: ThemeHslColor): ThemeRgbColor { const hue = ((color.h % 360) + 360) % 360; const chroma = (1 - Math.abs(2 * color.l - 1)) * color.s; const hueSector = hue / 60; const secondary = chroma * (1 - Math.abs((hueSector % 2) - 1)); const match = color.l - chroma / 2; const [red, green, blue] = hueSector < 1 ? [chroma, secondary, 0] : hueSector < 2 ? [secondary, chroma, 0] : hueSector < 3 ? [0, chroma, secondary] : hueSector < 4 ? [0, secondary, chroma] : hueSector < 5 ? [secondary, 0, chroma] : [chroma, 0, secondary]; return { r: (red + match) * 255, g: (green + match) * 255, b: (blue + match) * 255 }; } function mixThemeRgbColors( base: ThemeRgbColor, overlay: ThemeRgbColor, amount: number, ): ThemeRgbColor { return { r: base.r + (overlay.r - base.r) * amount, g: base.g + (overlay.g - base.g) * amount, b: base.b + (overlay.b - base.b) * amount, }; } function themeRelativeLuminance(color: ThemeRgbColor): number { const linearize = (channel: number) => { const normalized = channel / 255; return normalized <= 0.03928 ? normalized / 12.92 : ((normalized + 0.055) / 1.055) ** 2.4; }; return 0.2126 * linearize(color.r) + 0.7152 * linearize(color.g) + 0.0722 * linearize(color.b); } // --------------------------------------------------------------------------- // Vivid palette engine: perceptual (OKLCH) derivation for user-created themes. // Built-in themes keep the legacy derivation so their shipped palettes and the // boot splash copies stay byte-identical. type ThemeOklch = { L: number; C: number; h: number }; function srgbChannelToLinear(channel: number): number { const c = channel / 255; return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; } function linearChannelToSrgb(channel: number): number { const c = channel <= 0.0031308 ? channel * 12.92 : 1.055 * channel ** (1 / 2.4) - 0.055; return Math.round(Math.min(1, Math.max(0, c)) * 255); } function themeRgbToOklch(color: ThemeRgbColor): ThemeOklch { const r = srgbChannelToLinear(color.r); const g = srgbChannelToLinear(color.g); const b = srgbChannelToLinear(color.b); 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); const L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s; const a = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s; const bb = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s; return { L, C: Math.hypot(a, bb), h: (Math.atan2(bb, a) * 180) / Math.PI }; } function oklchToRgbUnclamped({ L, C, h }: ThemeOklch): { r: number; g: number; b: number } { const hr = (h * Math.PI) / 180; const a = C * Math.cos(hr); const bb = C * Math.sin(hr); const l = (L + 0.3963377774 * a + 0.2158037573 * bb) ** 3; const m = (L - 0.1055613458 * a - 0.0638541728 * bb) ** 3; const s = (L - 0.0894841775 * a - 1.291485548 * bb) ** 3; return { r: 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, g: -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, b: -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s, }; } /** Convert to sRGB, walking chroma toward grey until the color is in gamut. */ function themeOklchToRgb(color: ThemeOklch): ThemeRgbColor { let { C } = color; for (let step = 0; step < 12; step += 1) { const linear = oklchToRgbUnclamped({ ...color, C }); const inGamut = [linear.r, linear.g, linear.b].every( (channel) => channel >= -0.0001 && channel <= 1.0001, ); if (inGamut) { return { r: linearChannelToSrgb(linear.r), g: linearChannelToSrgb(linear.g), b: linearChannelToSrgb(linear.b), }; } C *= 0.82; } const linear = oklchToRgbUnclamped({ ...color, C: 0 }); return { r: linearChannelToSrgb(linear.r), g: linearChannelToSrgb(linear.g), b: linearChannelToSrgb(linear.b), }; } /** Binary-search the lightness that reaches the contrast target against a background. */ function solveOklchLightness( base: ThemeOklch, against: ThemeRgbColor, minContrast: number, direction: "lighter" | "darker", ): ThemeOklch { let low = direction === "lighter" ? base.L : 0; let high = direction === "lighter" ? 1 : base.L; let candidate = { ...base }; if (themeContrastRatio(themeOklchToRgb(candidate), against) >= minContrast) return candidate; for (let step = 0; step < 18; step += 1) { const mid = (low + high) / 2; candidate = { ...base, L: mid }; const contrast = themeContrastRatio(themeOklchToRgb(candidate), against); if (contrast >= minContrast) { if (direction === "lighter") high = mid; else low = mid; } else { if (direction === "lighter") low = mid; else high = mid; } } return { ...base, L: direction === "lighter" ? high : low }; } /** * The status colors T3 Code shows without a theme, read from the app's own * tokens (red-500 / amber-500 families). Generated palettes fall back to * these instead of the flagship theme's, so an imported or created theme * never inherits a brand tint on destructive buttons and warnings. */ const STANDARD_STATUS_COLORS = { light: { error: "#fb2c36", errorForeground: "#c10007", warning: "#fe9a00", warningForeground: "#bb4d00", }, dark: { error: "#fb414a", errorForeground: "#ff6467", warning: "#fe9a00", warningForeground: "#ffb900", }, } as const; /** * Status surfaces are the standard color laid over the theme's own canvas * (the unthemed app uses 8% in light and 16% in dark), so alerts still sit on * the palette while the signal color stays standard. */ function standardStatusColors(canvas: ThemeRgbColor): { error: string; errorForeground: string; errorSurface: string; warning: string; warningForeground: string; warningSurface: string; } { // Keyed off the canvas rather than the appearance slot: a dark canvas saved // as a light theme still needs the dark pair, or the alert foreground lands // on a dark surface unreadable. const appearance: ThemeAppearance = themeRelativeLuminance(canvas) < 0.179 ? "dark" : "light"; const standard = STANDARD_STATUS_COLORS[appearance]; const surfaceMix = appearance === "dark" ? 0.16 : 0.08; const surfaceOf = (value: string) => mixThemeRgbColors(canvas, parseThemeRgbColor(value, canvas), surfaceMix); // The standard foregrounds are tuned against the unthemed canvas; on a // tinted one they can fall just short, so lightness is nudged until the // pair clears 4.5 while the hue stays standard. const readableOn = (foreground: string, surface: ThemeRgbColor) => themeRgbToHexColor( themeOklchToRgb( solveOklchLightness( themeRgbToOklch(parseThemeRgbColor(foreground, canvas)), surface, // A hair above 4.5: the solve happens in OKLCH and the result is // quantized to 8-bit hex, which can shave the last hundredth off. 4.6, appearance === "dark" ? "lighter" : "darker", ), ), ); const errorSurface = surfaceOf(standard.error); const warningSurface = surfaceOf(standard.warning); return { ...standard, errorForeground: readableOn(standard.errorForeground, errorSurface), errorSurface: themeRgbToHexColor(errorSurface), warningForeground: readableOn(standard.warningForeground, warningSurface), warningSurface: themeRgbToHexColor(warningSurface), }; } /** * Derive a full palette from two exact seed colors, in OKLCH. Surfaces climb a * perceptually even lightness ramp that carries the accent hue at low chroma, * a companion action color is rotated off the accent, and every foreground is * contrast-solved against its own surface. */ export function createVividThemeColors( appearance: ThemeAppearance, backgroundValue: string, accentValue: string, ): ThemeColors { const defaults = getDefaultThemeColors(appearance); const canvasRgb = parseThemeRgbColor( backgroundValue, appearance === "dark" ? { r: 24, g: 15, b: 27 } : { r: 250, g: 245, b: 250 }, ); const accentRgb = parseThemeRgbColor(accentValue, { r: 168, g: 67, b: 112 }); const canvas = themeRgbToOklch(canvasRgb); const accent = themeRgbToOklch(accentRgb); // The ramp and every contrast search follow the canvas the user actually // picked, not the appearance slot, so a dark canvas saved as a light theme // still gets light text and raised surfaces. 0.179 is the relative // luminance where white and black text have equal contrast headroom. const dark = themeRelativeLuminance(canvasRgb) < 0.179; const hue = accent.C < 0.02 ? canvas.h : accent.h; const tintC = Math.min(0.045, Math.max(0.008, accent.C * 0.22)); const step = dark ? 1 : -1; const surfaceAt = (deltaL: number, chroma = tintC): ThemeOklch => ({ L: Math.min(0.98, Math.max(0.05, canvas.L + step * deltaL)), C: chroma, h: hue, }); const hex = (color: ThemeOklch) => themeRgbToHexColor(themeOklchToRgb(color)); // Text carries a whisper of the accent hue instead of falling back to a // fixed foreground, and is solved to WCAG AAA against the canvas. const textBase: ThemeOklch = { L: dark ? 0.95 : 0.2, C: Math.min(0.035, accent.C * 0.25), h: hue, }; const text = solveOklchLightness(textBase, canvasRgb, 7, dark ? "lighter" : "darker"); const textRgb = themeOklchToRgb(text); const textMutedRgb = standardMutedThemeText(canvasRgb, textRgb); // The companion action rotates off the accent so a two-color theme still // gets the dual-voice character of the hand-tuned palettes. const action: ThemeOklch = { L: Math.min(0.85, Math.max(0.35, accent.L + (dark ? 0.06 : -0.02))), C: Math.max(accent.C * 0.9, 0.06), h: (hue + 50) % 360, }; const actionRgb = themeOklchToRgb(action); const actionForeground = readableThemeForeground(actionRgb); const accentForeground = readableThemeForeground(accentRgb); const sidebar = surfaceAt(0.045, tintC * 1.4); const sidebarRgb = themeOklchToRgb(sidebar); const surface = surfaceAt(0.015); const surfaceRaised = surfaceAt(0.05); const surfaceOverlay = surfaceAt(0.075); const border = surfaceAt(dark ? 0.16 : 0.12, Math.min(0.07, accent.C * 0.35)); const input = surfaceAt(dark ? 0.21 : 0.16, Math.min(0.08, accent.C * 0.4)); const secondary = surfaceAt(dark ? 0.1 : 0.06, Math.min(0.09, accent.C * 0.5)); const secondaryRgb = themeOklchToRgb(secondary); const muted = surfaceAt(dark ? 0.06 : 0.04, Math.min(0.06, accent.C * 0.35)); const accentSurface = surfaceAt(dark ? 0.13 : 0.08, Math.min(0.11, accent.C * 0.55)); const accentSurfaceRgb = themeOklchToRgb(accentSurface); const messageSurface = surfaceAt(dark ? 0.16 : 0.1, Math.min(0.13, accent.C * 0.6)); const messageSurfaceRgb = themeOklchToRgb(messageSurface); const codeBackground = surfaceAt(0.035, tintC * 0.8); const updateSurface = surfaceAt(dark ? 0.14 : 0.09, Math.min(0.12, accent.C * 0.55)); const foregroundOn = (surfaceRgb: ThemeRgbColor): string => themeRgbToHexColor( themeOklchToRgb(solveOklchLightness(textBase, surfaceRgb, 4.6, dark ? "lighter" : "darker")), ); const actionHover: ThemeOklch = { ...action, L: action.L + (dark ? 0.06 : -0.06) }; return { ...defaults, ...standardStatusColors(canvasRgb), canvas: themeRgbToHexColor(canvasRgb), // The top bar shares the canvas so the main panel reads as one surface. chrome: themeRgbToHexColor(canvasRgb), toolbar: themeRgbToHexColor(canvasRgb), toolbarForeground: themeRgbToHexColor(textRgb), toolbarBorder: hex(surfaceAt(dark ? 0.14 : 0.1, Math.min(0.08, accent.C * 0.4))), toolbarControl: hex(surfaceAt(dark ? 0.09 : 0.05, tintC * 1.3)), toolbarControlForeground: themeRgbToHexColor(textRgb), toolbarControlHover: hex(surfaceAt(dark ? 0.14 : 0.09, tintC * 1.6)), surface: hex(surface), surfaceRaised: hex(surfaceRaised), surfaceOverlay: hex(surfaceOverlay), text: themeRgbToHexColor(textRgb), textMuted: themeRgbToHexColor(textMutedRgb), border: hex(border), input: hex(input), focus: themeRgbToHexColor(accentRgb), accent: themeRgbToHexColor(accentRgb), accentForeground: themeRgbToHexColor(accentForeground), secondary: hex(secondary), secondaryForeground: foregroundOn(secondaryRgb), muted: hex(muted), mutedForeground: themeRgbToHexColor(textMutedRgb), placeholder: themeRgbToHexColor(textMutedRgb), secondaryLabel: themeRgbToHexColor(textMutedRgb), iconMuted: themeRgbToHexColor(textMutedRgb), update: themeRgbToHexColor(accentRgb), updateForeground: foregroundOn(themeOklchToRgb(updateSurface)), updateSurface: hex(updateSurface), accentSurface: hex(accentSurface), accentSurfaceForeground: foregroundOn(accentSurfaceRgb), messageSurface: hex(messageSurface), messageForeground: foregroundOn(messageSurfaceRgb), messageAction: themeRgbToHexColor(actionRgb), messageActionForeground: themeRgbToHexColor(actionForeground), messageActionHover: hex(actionHover), codeBackground: hex(codeBackground), codeForeground: themeRgbToHexColor(textRgb), sidebar: hex(sidebar), sidebarForeground: foregroundOn(sidebarRgb), sidebarMutedForeground: themeRgbToHexColor(standardMutedThemeText(sidebarRgb, textRgb)), sidebarControlSurface: hex(surfaceAt(dark ? 0.1 : 0.07, tintC * 1.5)), sidebarRowHover: hex(surfaceAt(dark ? 0.08 : 0.06, Math.min(0.08, accent.C * 0.45))), sidebarRowActive: hex(surfaceAt(dark ? 0.12 : 0.09, Math.min(0.1, accent.C * 0.55))), sidebarRowSelected: hex(surfaceAt(dark ? 0.14 : 0.1, Math.min(0.11, accent.C * 0.6))), sidebarBorder: hex(surfaceAt(dark ? 0.17 : 0.12, Math.min(0.08, accent.C * 0.4))), terminalBackground: themeRgbToHexColor(canvasRgb), terminalForeground: themeRgbToHexColor(textRgb), terminalCursor: themeRgbToHexColor(accentRgb), terminalSelection: hex(surfaceAt(dark ? 0.18 : 0.12, Math.min(0.12, accent.C * 0.55))), terminalScrollbar: hex(surfaceAt(dark ? 0.22 : 0.16, tintC)), terminalScrollbarHover: hex(surfaceAt(dark ? 0.3 : 0.22, tintC)), }; } function themeContrastRatio(first: ThemeRgbColor, second: ThemeRgbColor): number { const firstLuminance = themeRelativeLuminance(first); const secondLuminance = themeRelativeLuminance(second); const lighter = Math.max(firstLuminance, secondLuminance); const darker = Math.min(firstLuminance, secondLuminance); return (lighter + 0.05) / (darker + 0.05); } function readableThemeForeground(background: ThemeRgbColor): ThemeRgbColor { const lightContrast = themeContrastRatio(background, THEME_LIGHT_FOREGROUND); const darkContrast = themeContrastRatio(background, THEME_DARK_FOREGROUND); if (Math.max(lightContrast, darkContrast) >= 4.5) { return lightContrast >= darkContrast ? THEME_LIGHT_FOREGROUND : THEME_DARK_FOREGROUND; } return themeContrastRatio(background, THEME_WHITE_FOREGROUND) >= themeContrastRatio(background, THEME_BLACK_FOREGROUND) ? THEME_WHITE_FOREGROUND : THEME_BLACK_FOREGROUND; } function readableThemeText( background: ThemeRgbColor, foreground: ThemeRgbColor, amount: number, minimumRatio: number, ): ThemeRgbColor { const softened = mixThemeRgbColors(foreground, background, amount); if (themeContrastRatio(softened, background) >= minimumRatio) return softened; // Find the quietest point between the requested mix and the primary // foreground that still clears the contrast floor. Returning the primary // foreground here made secondary labels jump from slightly too dim to full // brightness, which is especially conspicuous in dark custom themes. let readable = foreground; let lowerAmount = 0; let upperAmount = amount; for (let index = 0; index < 12; index += 1) { const candidateAmount = (lowerAmount + upperAmount) / 2; const candidate = mixThemeRgbColors(foreground, background, candidateAmount); if (themeContrastRatio(candidate, background) >= minimumRatio) { readable = candidate; lowerAmount = candidateAmount; } else { upperAmount = candidateAmount; } } return readable; } // Match the perceived strength of the stock palettes rather than choosing an // arbitrary foreground mix. These are the measured contrast ratios of zinc-500 // on the standard light canvas and #818181 on the standard dark canvas. const STANDARD_LIGHT_MUTED_CONTRAST = 4.705; const STANDARD_DARK_MUTED_CONTRAST = 5.082; function standardMutedThemeText( background: ThemeRgbColor, foreground: ThemeRgbColor, ): ThemeRgbColor { const target = themeRelativeLuminance(background) < 0.179 ? STANDARD_DARK_MUTED_CONTRAST : STANDARD_LIGHT_MUTED_CONTRAST; return readableThemeText(background, foreground, 1, target); } function managedThemeBackground(value: string, appearance: ThemeAppearance): ThemeRgbColor { const selected = parseThemeRgbColor( value, appearance === "dark" ? { r: 24, g: 15, b: 27 } : { r: 250, g: 245, b: 250 }, ); const hsl = themeRgbToHsl(selected); return themeHslToRgb({ h: hsl.h, // A background tint should support the selected mode, not turn the whole // app into a high-saturation surface. s: Math.min(hsl.s, appearance === "dark" ? 0.3 : 0.2), l: appearance === "dark" ? Math.min(0.13, Math.max(0.07, hsl.l)) : Math.min(0.985, Math.max(0.94, hsl.l)), }); } function managedThemeAccent( value: string, appearance: ThemeAppearance, background: ThemeRgbColor, ): ThemeRgbColor { const selected = parseThemeRgbColor(value, { r: 168, g: 67, b: 112 }); const hsl = themeRgbToHsl(selected); const preferredLightness = appearance === "dark" ? Math.min(0.72, Math.max(0.42, hsl.l)) : Math.min(0.58, Math.max(0.35, hsl.l)); const lightnessRange: readonly [number, number] = appearance === "dark" ? [0.42, 0.82] : [0.22, 0.58]; const saturation = Math.min(hsl.s, 0.82); const candidates = Array.from({ length: 61 }, (_, index) => { const lightness = lightnessRange[0] + ((lightnessRange[1] - lightnessRange[0]) * index) / (61 - 1); const color = themeHslToRgb({ h: hsl.h, s: saturation, l: lightness }); return { color, lightness, contrast: themeContrastRatio(color, background) }; }); // Leave a little room for rounding when the generated RGB values become a // six-digit hex token. const readableCandidates = candidates.filter((candidate) => candidate.contrast >= 4.7); const pool = readableCandidates.length > 0 ? readableCandidates : candidates; return pool.reduce((best, candidate) => { const distance = Math.abs(candidate.lightness - preferredLightness); const bestDistance = Math.abs(best.lightness - preferredLightness); return distance < bestDistance || (distance === bestDistance && candidate.contrast > best.contrast) ? candidate : best; }).color; } /** * Creates the guided palette used by the basic theme editor. The two user * colors control the mood, while dependent roles are generated together so * text, surfaces, message actions, code, and terminal UI stay coherent. */ export function createManagedThemeColors( appearance: ThemeAppearance, backgroundValue: string, accentValue: string, options?: { /** Use the seeds exactly as given instead of nudging them into the * readability envelope. Derived foregrounds still adapt for contrast. */ exactSeeds?: boolean; }, ): ThemeColors { const defaults = getDefaultThemeColors(appearance); const canvas = options?.exactSeeds ? parseThemeRgbColor( backgroundValue, appearance === "dark" ? { r: 24, g: 15, b: 27 } : { r: 250, g: 245, b: 250 }, ) : managedThemeBackground(backgroundValue, appearance); const accent = options?.exactSeeds ? parseThemeRgbColor(accentValue, { r: 168, g: 67, b: 112 }) : managedThemeAccent(accentValue, appearance, canvas); const text = readableThemeForeground(canvas); const textMuted = standardMutedThemeText(canvas, text); // The top bar is part of the main panel, not a separate chrome layer: it // shares the canvas, and its controls sit on the panel's own surfaces. const chrome = canvas; const sidebar = mixThemeRgbColors(canvas, accent, 0.08); const surfaceRaised = mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.12 : 0.035); const surfaceOverlay = mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.18 : 0.06); const secondary = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.2 : 0.08); const muted = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.13 : 0.06); const accentSurface = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.3 : 0.14); const messageSurface = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.36 : 0.18); const toolbarControl = mixThemeRgbColors(chrome, accent, appearance === "dark" ? 0.2 : 0.08); const toolbarBorder = mixThemeRgbColors(chrome, accent, appearance === "dark" ? 0.35 : 0.14); const accentForeground = readableThemeForeground(accent); // Code and terminal are large surfaces: they keep the canvas hue instead of // drifting toward the foreground grey. Code sits just above the canvas — // a whisper of the text tint — and the terminal sits on the canvas itself. const codeBackground = mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.06 : 0.025); const terminalBackground = canvas; const messageActionHover = mixThemeRgbColors( accent, accentForeground === THEME_LIGHT_FOREGROUND || accentForeground === THEME_WHITE_FOREGROUND ? THEME_BLACK_FOREGROUND : THEME_WHITE_FOREGROUND, 0.12, ); // The update family follows the accent instead of inheriting the default // palette's brand color, so generated themes carry their own identity in // update pills and banners. Error and warning stay semantic defaults. const updateSurface = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.32 : 0.16); const updateForeground = mixThemeRgbColors( accent, appearance === "dark" ? THEME_WHITE_FOREGROUND : THEME_BLACK_FOREGROUND, 0.35, ); return { ...defaults, ...standardStatusColors(canvas), update: themeRgbToHexColor(accent), updateForeground: themeRgbToHexColor(updateForeground), updateSurface: themeRgbToHexColor(updateSurface), canvas: themeRgbToHexColor(canvas), chrome: themeRgbToHexColor(chrome), toolbar: themeRgbToHexColor(chrome), toolbarForeground: themeRgbToHexColor(text), toolbarBorder: themeRgbToHexColor(toolbarBorder), toolbarControl: themeRgbToHexColor(toolbarControl), toolbarControlForeground: themeRgbToHexColor(text), toolbarControlHover: themeRgbToHexColor(accentSurface), surface: themeRgbToHexColor(canvas), surfaceRaised: themeRgbToHexColor(surfaceRaised), surfaceOverlay: themeRgbToHexColor(surfaceOverlay), text: themeRgbToHexColor(text), textMuted: themeRgbToHexColor(textMuted), // Borders blend through the accent before lightening so control chrome // carries the theme hue like the hand-tuned palettes (#5c345b, #e0d3e1) // instead of flattening to grey. border: themeRgbToHexColor( mixThemeRgbColors( mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.22 : 0.1), text, 0.1, ), ), input: themeRgbToHexColor( mixThemeRgbColors( mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.3 : 0.14), text, appearance === "dark" ? 0.14 : 0.13, ), ), focus: themeRgbToHexColor(accent), accent: themeRgbToHexColor(accent), accentForeground: themeRgbToHexColor(accentForeground), secondary: themeRgbToHexColor(secondary), secondaryForeground: themeRgbToHexColor(readableThemeForeground(secondary)), muted: themeRgbToHexColor(muted), mutedForeground: themeRgbToHexColor(textMuted), placeholder: themeRgbToHexColor(textMuted), secondaryLabel: themeRgbToHexColor(textMuted), iconMuted: themeRgbToHexColor(textMuted), accentSurface: themeRgbToHexColor(accentSurface), accentSurfaceForeground: themeRgbToHexColor(readableThemeForeground(accentSurface)), messageSurface: themeRgbToHexColor(messageSurface), messageForeground: themeRgbToHexColor(readableThemeForeground(messageSurface)), messageAction: themeRgbToHexColor(accent), messageActionForeground: themeRgbToHexColor(accentForeground), messageActionHover: themeRgbToHexColor(messageActionHover), codeBackground: themeRgbToHexColor(codeBackground), codeForeground: themeRgbToHexColor(readableThemeForeground(codeBackground)), sidebar: themeRgbToHexColor(sidebar), sidebarForeground: themeRgbToHexColor(readableThemeForeground(sidebar)), sidebarMutedForeground: themeRgbToHexColor(standardMutedThemeText(sidebar, text)), sidebarControlSurface: themeRgbToHexColor( mixThemeRgbColors(sidebar, text, appearance === "dark" ? 0.16 : 0.08), ), sidebarRowHover: themeRgbToHexColor(mixThemeRgbColors(sidebar, accent, 0.12)), sidebarRowActive: themeRgbToHexColor(mixThemeRgbColors(sidebar, accent, 0.2)), sidebarRowSelected: themeRgbToHexColor(mixThemeRgbColors(sidebar, accent, 0.24)), sidebarBorder: themeRgbToHexColor( mixThemeRgbColors(sidebar, text, appearance === "dark" ? 0.35 : 0.12), ), terminalBackground: themeRgbToHexColor(terminalBackground), terminalForeground: themeRgbToHexColor(readableThemeForeground(terminalBackground)), terminalCursor: themeRgbToHexColor(accent), terminalSelection: themeRgbToHexColor( mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.35 : 0.18), ), terminalScrollbar: themeRgbToHexColor( mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.42 : 0.22), ), terminalScrollbarHover: themeRgbToHexColor( mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.55 : 0.32), ), }; } export const T3_CHAT_THEME: ThemeDefinition = { id: T3_CHAT_THEME_ID, label: T3_CHAT_THEME_LABEL, appearance: "light", colors: T3_CHAT_LIGHT_COLORS, variants: { dark: T3_CHAT_DARK_COLORS, }, }; /** Theme-file defaults follow the flagship palette for the requested mode. */ export function getDefaultThemeColors(appearance: ThemeAppearance): ThemeColors { return appearance === "dark" ? T3_CHAT_DARK_COLORS : T3_CHAT_LIGHT_COLORS; } /** * A companion action color in the T3 Chat mold. This gives send buttons, * status pills, and theme previews a second voice; foreground and hover follow * the same rules as the managed generator. */ function themeActionColors( action: string, ): Pick { const rgb = parseThemeRgbColor(action, THEME_DARK_FOREGROUND); const foreground = readableThemeForeground(rgb); const towardOpposite = foreground === THEME_LIGHT_FOREGROUND || foreground === THEME_WHITE_FOREGROUND ? THEME_BLACK_FOREGROUND : THEME_WHITE_FOREGROUND; return { messageAction: action, messageActionForeground: themeRgbToHexColor(foreground), messageActionHover: themeRgbToHexColor(mixThemeRgbColors(rgb, towardOpposite, 0.12)), }; } export const GROVE_THEME: ThemeDefinition = { id: GROVE_THEME_ID, label: GROVE_THEME_LABEL, appearance: "light", colors: { ...createManagedThemeColors("light", "#f2f8f4", "#19734a"), ...themeActionColors("#8f6410"), }, variants: { dark: { ...createManagedThemeColors("dark", "#1d2b24", "#69d69a"), ...themeActionColors("#e3b34e"), }, }, }; export const OCEAN_THEME: ThemeDefinition = { id: OCEAN_THEME_ID, label: OCEAN_THEME_LABEL, appearance: "light", colors: { ...createManagedThemeColors("light", "#f2f7fb", "#2878b8"), ...themeActionColors("#0a6f75"), }, variants: { dark: { ...createManagedThemeColors("dark", "#1b2938", "#70b9ee"), ...themeActionColors("#5bd0d6"), }, }, }; export const EMBER_THEME: ThemeDefinition = { id: EMBER_THEME_ID, label: EMBER_THEME_LABEL, appearance: "light", colors: { ...createManagedThemeColors("light", "#fff6ef", "#c4602f"), ...themeActionColors("#b23535"), }, variants: { dark: { ...createManagedThemeColors("dark", "#30231e", "#f39a62"), ...themeActionColors("#f78a7a"), }, }, }; export const IRIS_THEME: ThemeDefinition = { id: IRIS_THEME_ID, label: IRIS_THEME_LABEL, appearance: "light", colors: { ...createManagedThemeColors("light", "#f7f4fc", "#7254b9"), ...themeActionColors("#a82c87"), }, variants: { dark: { ...createManagedThemeColors("dark", "#29243b", "#ad92f5"), ...themeActionColors("#f099d8"), }, }, }; const BUILT_IN_THEME_DEFINITIONS: ReadonlyArray = [ T3_CHAT_THEME, GROVE_THEME, OCEAN_THEME, EMBER_THEME, IRIS_THEME, ]; export function getThemeDefinition(theme: ThemePreference): ThemeDefinition | null { const themeId = themeIdFromPreference(theme); return ( BUILT_IN_THEME_DEFINITIONS.find((definition) => definition.id === themeId) ?? getCustomThemes().find((definition) => definition.id === themeId) ?? null ); } export function getThemeColorsForMode( theme: ThemeDefinition, mode: ThemeAppearance, ): ThemeColors | null { if (mode === theme.appearance) return theme.colors; return theme.variants?.[mode] ?? null; } export function getThemeModes(theme: ThemeDefinition): ReadonlyArray { return (["light", "dark"] as const).filter((mode) => getThemeColorsForMode(theme, mode) !== null); } export function getThemePreferenceMode(theme: ThemePreference): ThemeAppearance | null { if (theme === "system") return null; if (theme === "light" || theme === "dark") return theme; const legacyMode = legacyThemeMode(theme); if (legacyMode) return legacyMode; return getThemeDefinition(theme)?.appearance ?? null; } export function themeIdFromName(name: string): string { const normalized = name .trim() .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 48); return normalized || "custom-theme"; } export class ThemeLibraryStorageError extends Schema.TaggedErrorClass()( "ThemeLibraryStorageError", { storageKey: Schema.String, cause: Schema.Defect() }, ) { override get message(): string { return `Failed to write the theme library to ${this.storageKey}.`; } } export const isThemeLibraryStorageError = Schema.is(ThemeLibraryStorageError); function saveCustomThemes(themes: ReadonlyArray): void { if (typeof window === "undefined") return; try { window.localStorage.setItem(CUSTOM_THEMES_STORAGE_KEY, JSON.stringify(themes)); customThemesSnapshot = themes; } catch (cause) { throw new ThemeLibraryStorageError({ storageKey: CUSTOM_THEMES_STORAGE_KEY, cause }); } notifyCustomThemeListeners(); } export function installCustomTheme(theme: ThemeDefinition): ThemeDefinition { if (RESERVED_THEME_IDS.has(theme.id)) { throw new Error(`The theme id "${theme.id}" is reserved.`); } if ( [...BUILT_IN_THEME_DEFINITIONS, ...getCustomThemes()].some( (existing) => existing.id === theme.id, ) ) { throw new Error(`A theme named "${theme.label}" is already installed.`); } saveCustomThemes([...getCustomThemes(), theme]); return theme; } export function updateCustomTheme(theme: ThemeDefinition): ThemeDefinition { if (RESERVED_THEME_IDS.has(theme.id)) { throw new Error(`The theme id "${theme.id}" is reserved.`); } const themes = getCustomThemes(); const themeIndex = themes.findIndex((existing) => existing.id === theme.id); if (themeIndex === -1) { throw new Error(`The theme "${theme.label}" is not installed.`); } const nextThemes = [...themes]; nextThemes[themeIndex] = theme; saveCustomThemes(nextThemes); return theme; } export function removeCustomTheme(themeId: string): void { const nextThemes = getCustomThemes().filter((theme) => theme.id !== themeId); if (nextThemes.length === getCustomThemes().length) return; saveCustomThemes(nextThemes); } function parseThemeColorOverrides(value: unknown): ThemeColorOverrides { if (!isRecord(value)) throw new Error("Theme colors must be objects."); const overrides: Partial> = {}; for (const [role, color] of Object.entries(value)) { if (!THEME_COLOR_ROLE_SET.has(role)) { throw new Error(`"${role}" is not a supported theme color role.`); } if (!isThemeColor(color)) { throw new Error(`The color for "${role}" must be a hex color such as #8b5cf6.`); } overrides[role as ThemeColorRole] = color; } if (Object.keys(overrides).length === 0) { throw new Error("Add at least one color role to the theme file."); } return overrides; } export function parseThemeFile(value: unknown): ThemeDefinition { if (!isRecord(value)) { throw new Error("Theme files must contain a JSON object."); } if (value.version !== THEME_FILE_VERSION) { throw new Error(`This theme file uses an unsupported version. Expected ${THEME_FILE_VERSION}.`); } const name = value.name; const appearance = value.appearance; const rawColors = value.colors; if (!isThemeLabel(name)) throw new Error("Theme files need a name (48 characters or fewer)."); if (!isThemeAppearance(appearance)) { throw new Error('Theme files need an appearance of "light" or "dark".'); } if (!isRecord(rawColors)) throw new Error("Theme files need a colors object."); const id = value.id === undefined ? themeIdFromName(name) : value.id; if (!isThemeId(id)) { throw new Error("Theme ids may only contain lowercase letters, numbers, and hyphens."); } if (RESERVED_THEME_IDS.has(id)) { throw new Error(`The theme id "${id}" is reserved.`); } const overrides = parseThemeColorOverrides(rawColors); const fallback = getDefaultThemeColors(appearance); const variants: Partial> = {}; if (value.variants !== undefined) { if (!isRecord(value.variants)) throw new Error("Theme variants must be an object."); for (const [variantAppearance, variantColors] of Object.entries(value.variants)) { if (!isThemeAppearance(variantAppearance)) { throw new Error('Theme variants may only be named "light" or "dark".'); } if (variantAppearance === appearance) { throw new Error(`Theme variants must not repeat the base appearance "${appearance}".`); } const variantFallback = getDefaultThemeColors(variantAppearance); variants[variantAppearance] = { ...variantFallback, ...parseThemeColorOverrides(variantColors), }; } } return { id, label: name.trim(), appearance, colors: { ...fallback, ...overrides }, ...(Object.keys(variants).length > 0 ? { variants } : {}), ...(value.sidebarArtwork === true ? { sidebarArtwork: true } : {}), ...(value.managed === true ? { managed: true } : {}), }; } export function serializeThemeFile(theme: ThemeDefinition): string { const file: ThemeFile = { version: THEME_FILE_VERSION, id: theme.id, name: theme.label, appearance: theme.appearance, colors: theme.colors, ...(theme.variants ? { variants: theme.variants } : {}), ...(theme.sidebarArtwork ? { sidebarArtwork: true } : {}), ...(theme.managed ? { managed: true } : {}), }; return `${JSON.stringify(file, null, 2)}\n`; } const APP_THEME_VARIABLES: Readonly> = { canvas: "--app-theme-canvas", chrome: "--app-theme-chrome", toolbar: "--app-theme-toolbar", toolbarForeground: "--app-theme-toolbar-foreground", toolbarBorder: "--app-theme-toolbar-border", toolbarControl: "--app-theme-toolbar-control", toolbarControlForeground: "--app-theme-toolbar-control-foreground", toolbarControlHover: "--app-theme-toolbar-control-hover", surface: "--app-theme-surface", surfaceRaised: "--app-theme-surface-raised", surfaceOverlay: "--app-theme-surface-overlay", text: "--app-theme-text", textMuted: "--app-theme-text-muted", border: "--app-theme-border", input: "--app-theme-input", focus: "--app-theme-focus", accent: "--app-theme-accent", accentForeground: "--app-theme-accent-foreground", secondary: "--app-theme-secondary", secondaryForeground: "--app-theme-secondary-foreground", muted: "--app-theme-muted", mutedForeground: "--app-theme-muted-foreground", placeholder: "--app-theme-placeholder", secondaryLabel: "--app-theme-secondary-label", iconMuted: "--app-theme-icon-muted", error: "--app-theme-error", errorForeground: "--app-theme-error-foreground", errorSurface: "--app-theme-error-surface", warning: "--app-theme-warning", warningForeground: "--app-theme-warning-foreground", warningSurface: "--app-theme-warning-surface", update: "--app-theme-update", updateForeground: "--app-theme-update-foreground", updateSurface: "--app-theme-update-surface", accentSurface: "--app-theme-accent-surface", accentSurfaceForeground: "--app-theme-accent-surface-foreground", messageSurface: "--app-theme-message-surface", messageForeground: "--app-theme-message-foreground", messageAction: "--app-theme-message-action", messageActionForeground: "--app-theme-message-action-foreground", messageActionHover: "--app-theme-message-action-hover", codeBackground: "--app-theme-code-background", codeForeground: "--app-theme-code-foreground", sidebar: "--app-theme-sidebar", sidebarForeground: "--app-theme-sidebar-foreground", sidebarMutedForeground: "--app-theme-sidebar-muted-foreground", sidebarControlSurface: "--app-theme-sidebar-control-surface", sidebarRowHover: "--app-theme-sidebar-row-hover", sidebarRowActive: "--app-theme-sidebar-row-active", sidebarRowSelected: "--app-theme-sidebar-row-selected", sidebarBorder: "--app-theme-sidebar-border", terminalBackground: "--app-theme-terminal-background", terminalForeground: "--app-theme-terminal-foreground", terminalCursor: "--app-theme-terminal-cursor", terminalSelection: "--app-theme-terminal-selection-background", terminalScrollbar: "--app-theme-terminal-scrollbar", terminalScrollbarHover: "--app-theme-terminal-scrollbar-hover", }; export function getThemeColorVariable(role: ThemeColorRole): string { return APP_THEME_VARIABLES[role]; } /** Marks the document as wearing an unsaved draft rather than a stored theme. */ export const THEME_PREVIEW_ID = "__preview"; /** * Paint a draft palette onto the live app without installing it, so the editor * can be judged against the real interface instead of a miniature. Callers * restore the stored theme (refreshTheme) when the draft goes away. */ export function applyThemeColorPreview( colors: ThemeColors, appearance: ThemeAppearance, sidebarArtwork = false, ): void { if (typeof document === "undefined") return; const root = document.documentElement; if (!root?.style) return; setThemePreviewSidebarArtwork(sidebarArtwork); root.dataset.themeId = THEME_PREVIEW_ID; root.classList.toggle("dark", appearance === "dark"); for (const [role, value] of Object.entries(colors) as Array<[ThemeColorRole, string]>) { // A half-typed hex keeps the last good value instead of blanking the role. if (isThemeColor(value)) root.style.setProperty(APP_THEME_VARIABLES[role], value); } } export function applyThemePalette(theme: ThemePreference, appearance?: ThemeAppearance): void { if (typeof document === "undefined") return; const root = document.documentElement; if (!root?.style) return; setThemePreviewSidebarArtwork(null); const palette = getThemeDefinition(theme); if (palette) { root.dataset.themeId = palette.id; const mode = appearance ?? legacyThemeMode(theme) ?? palette.appearance; const colors = getThemeColorsForMode(palette, mode) ?? palette.colors; for (const [role, value] of Object.entries(colors) as Array<[ThemeColorRole, string]>) { root.style.setProperty(APP_THEME_VARIABLES[role], value); } return; } delete root.dataset.themeId; for (const variable of Object.values(APP_THEME_VARIABLES)) { root.style.removeProperty(variable); } } export function resolveThemeAppearance( theme: ThemePreference, systemDark: boolean, followSystem?: boolean, appearanceMode?: ThemePreferenceMode, halves?: ThemeHalves | null, ): "light" | "dark" { const systemAppearance = systemDark ? "dark" : "light"; const mode = appearanceMode ?? ((followSystem ?? theme === "system") ? "system" : null); if (mode === "system") { // A configured half guarantees the appearance is renderable even when the // base theme lacks that mode. if (halves?.[systemAppearance]) return systemAppearance; const definition = getThemeDefinition(theme); return definition && getThemeColorsForMode(definition, systemAppearance) === null ? definition.appearance : systemAppearance; } if (mode === "light" || mode === "dark") { if (halves?.[mode]) return mode; const definition = getThemeDefinition(theme); return definition && getThemeColorsForMode(definition, mode) === null ? definition.appearance : mode; } return getThemePreferenceMode(theme) ?? "light"; } export function resolveDesktopTheme( theme: ThemePreference, followSystem?: boolean, appearanceMode?: ThemePreferenceMode, halves?: ThemeHalves | null, ): "light" | "dark" | "system" { const mode = appearanceMode ?? ((followSystem ?? theme === "system") ? "system" : null); if (mode === "system") { const definition = getThemeDefinition(theme); // A configured half fills in an appearance the base theme cannot render. const hasLightMode = halves?.light !== undefined || (definition !== null && getThemeColorsForMode(definition, "light") !== null); const hasDarkMode = halves?.dark !== undefined || (definition !== null && getThemeColorsForMode(definition, "dark") !== null); return definition && (!hasLightMode || !hasDarkMode) ? definition.appearance : "system"; } if (mode === "light" || mode === "dark") { if (halves?.[mode]) return mode; const definition = getThemeDefinition(theme); return definition && getThemeColorsForMode(definition, mode) === null ? definition.appearance : mode; } return getThemePreferenceMode(theme) ?? "system"; } export function isKnownThemePreference(theme: string): boolean { if (theme === "light" || theme === "dark" || theme === "system") return true; return getThemeDefinition(theme) !== null; } /** * An automatic-mode mix: a different theme per resolved appearance. Halves * only name real themes that can render their half; anything else is dropped * so a stale mix degrades to the base preference. */ export type ThemeHalves = Readonly<{ light?: string; dark?: string }>; export function parseThemeHalves(raw: string | null): ThemeHalves | null { if (!raw) return null; try { const value: unknown = JSON.parse(raw); if (!isRecord(value)) return null; const halves: { light?: string; dark?: string } = {}; for (const appearance of ["light", "dark"] as const) { const themeId = value[appearance]; if (typeof themeId !== "string") continue; const definition = getThemeDefinition(themeId); if (definition && getThemeColorsForMode(definition, appearance) !== null) { // Store the definition's id so legacy aliases resolve to the same // value the runtime applies to the document. halves[appearance] = definition.id; } } return halves.light !== undefined || halves.dark !== undefined ? halves : null; } catch { return null; } } /** The theme that should render the given appearance under a mix, if any. */ export function resolveThemeHalf( theme: ThemePreference, halves: ThemeHalves | null, appearance: ThemeAppearance, ): ThemePreference { return halves?.[appearance] ?? theme; }