"use client"; import { useSyncExternalStore } from "react"; // The role tokens (app/globals.css) as hex strings, for what CSS cannot reach: // a (the waveform, the mix lanes, the pitch rail) paints with // fillStyle, which takes a colour, not var(--panel). // // It follows the theme: an explicit choice (data-theme on , set by the // rail's control) and the system's preference both re-read it, so a canvas that // lists the palette in its draw effect's deps repaints on the switch. Values // are read from the computed style -- the stylesheet stays the one source -- // and the server, and hydration, see the dark base (a canvas paints only in an // effect, after it). export const ROLES = [ "ground", "panel", "panel-2", "seam", "seam-2", "text", "text-2", "muted", "muted-2", "signal", "meter", "kept", "doubt", "reject", ] as const; export type Role = (typeof ROLES)[number]; export type Palette = Record; // The dark base, as app/globals.css declares it. const DARK: Palette = { ground: "#0c0a08", panel: "#17140f", "panel-2": "#201b15", seam: "#2b251d", "seam-2": "#3d3429", text: "#ece4d6", "text-2": "#c9bfae", muted: "#9a8f7e", "muted-2": "#8a8070", signal: "#3fc1b0", meter: "#b9a2ec", kept: "#c6e283", doubt: "#ef9a2a", reject: "#de4c3c", }; let cacheKey = ""; let cache: Palette = DARK; function read(): Palette { const root = document.documentElement; const key = `${root.dataset.theme ?? ""}|${matchMedia("(prefers-color-scheme: light)").matches}`; if (key === cacheKey) return cache; const cs = getComputedStyle(root); const next = { ...DARK }; for (const r of ROLES) { const v = cs.getPropertyValue(`--${r}`).trim(); if (v) next[r] = v; } cacheKey = key; cache = next; return cache; } function subscribe(onChange: () => void) { const mo = new MutationObserver(onChange); mo.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] }); const mq = matchMedia("(prefers-color-scheme: light)"); mq.addEventListener("change", onChange); return () => { mo.disconnect(); mq.removeEventListener("change", onChange); }; } export function usePalette(): Palette { return useSyncExternalStore(subscribe, read, () => DARK); } /** `#rrggbb` at an alpha, for a canvas: alpha("#3fc1b0", 0.1). */ export function alpha(hex: string, a: number): string { const h = hex.replace("#", ""); const full = h.length === 3 ? h.split("").map((c) => c + c).join("") : h.slice(0, 6); const n = parseInt(full, 16); return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},${a})`; }