ThemeEngine

A class that holds a validated theme config and its token tree: read computed tokens, check contrast at the theme’s level, or swap configs.

ThemeEngine wraps generateAllTokens(config): it validates a config, generates its token tree once, and keeps both together so they cannot drift apart.

ts
import { ThemeEngine, mergeThemeConfig } from "@flexpixels/cortex";

const engine = new ThemeEngine(
  mergeThemeConfig({ seeds: { primary: "#4f46e5", secondary: "#c026d3", neutral: "#94a3b8" } }),
);

engine.getPrefix(); // "fp"
engine.getTokens(); // AllTokens: the token tree
engine.getContrast("#ffffff", "#4f46e5"); // { passes: true, ratio: 6.29, level: "AA" }

engine.updateConfig({ settings: { schemes: "light" } }); // merges: prefix, accessibility and the rest stay

Constructor

ts
new ThemeEngine(config?: ThemeConfig)

Without an argument the engine uses defaultThemeConfig. The constructor validates config with validateThemeConfig (a ZodError if it is invalid), stores a frozen copy, and generates the token tree. Changing your own object afterwards has no effect on the engine.

validateThemeConfig fills every default except the three brand seeds, which you must pass, and applies the bundle of a typography.scenario / elevation.scenario the config names to the fields it leaves out. To start from a config without seeds, pass one from mergeThemeConfig.

Methods

MethodReturnsPurpose
getTokens()AllTokensThe token tree, frozen (color, s, elevation, text, interaction, layout, scrollbar, …). The same tree generateFullThemeCSS and exportDTCGTokens serialize.
getConfig()ThemeConfigThe validated config, frozen. Change it with updateConfig.
getPrefix()stringconfig.settings.prefix (default "fp").
getContrast(fg, bg){ passes: boolean; ratio: number; level: "AA" | "AAA" }The WCAG ratio of two CSS colours (hex, oklch(…), named, anything culori parses), rounded to two decimals, and whether it meets settings.accessibility (4.5:1 for AA, 7:1 for AAA). Throws if either colour cannot be parsed.
updateConfig(partial)voidMerges partial over the engine’s config (as mergeThemeConfig(partial, engine.getConfig()) does: the fields it names change, their siblings stay, a field given as null is taken out or returns to its default, and a scenario applies its bundle when it is not the one the engine already has) and regenerates the whole tree. If validation or generation fails, it throws and the engine keeps its previous config and tokens.
setConfig(config)voidReplaces the engine’s config with config, validated as the constructor validates it. Nothing of the old config stays. Fails the same way.

For the APCA value as well as the ratio, call checkContrast directly.

When to use it

  • Long-lived processes — a build server, an MCP server, a dev-time editor — that keep one theme and read from it repeatedly.
  • Inspecting tokens without producing CSS: walk getTokens(), compare two engines’ trees, or feed a serializer of your own.
  • Contrast checks at the theme’s level: getContrast reads settings.accessibility for you.

For a one-off “config to CSS” step, skip the class and call generateThemeCSS(config) or generateFullThemeCSS(config); both create an engine internally.

  • generateAllTokens(config) — the function the engine wraps, without validation or freezing.
  • defaultThemeConfig — the complete default config.
  • generateFullThemeCSS — serializing the tree to CSS.