applyThemeToCss
applyThemeToCss rebrands the theme file you already have. Give it your stylesheet and a system from generateSystem, and it rewrites the existing color tokens in place: same variable names, same value format, nothing else in the file touched. It can also add what the theme is missing, such as a dark mode or chart colors.
Example
import { applyThemeToCss, generateSystem } from 'colore-js';
const css = `:root {
--bg: #ffffff;
--text: #17212b;
--brand: #4f46e5;
}`;
const result = applyThemeToCss(css, generateSystem('#0f766e'), { add: true });
console.log(result.css);
:root {
--bg: #f3f6f5;
--text: #070707;
--brand: #0f766e;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #070707;
--text: #f3f6f5;
}
}
The brand is exactly the color you gave, and the new dark mode uses a media query because this hand-written theme has no .dark class.
Syntax
applyThemeToCss(css: string, system: ColorSystem, options?: ApplyThemeOptions): ThemeEditResult
Parameters
Options
Returns
A ThemeEditResult:
Change only the brand
Pass only and modes to keep the change small. Here only the light-mode primary changes, and its paired text is updated with it.
import { applyThemeToCss, generateSystem } from 'colore-js';
const globalsCss = `:root {
--primary: oklch(0.205 0 0);
--primary-foreground: oklch(0.985 0 0);
--muted-foreground: oklch(0.55 0 0);
}`;
const result = applyThemeToCss(globalsCss, generateSystem('#0f766e'), {
only: ['brand'],
modes: ['light'],
});
console.log(result.changes);
[
{ scope: ':root', variable: '--primary', from: 'oklch(0.205 0 0)', to: 'oklch(0.511 0.086 186.391)' },
{ scope: ':root', variable: '--primary-foreground', from: 'oklch(0.985 0 0)', to: 'oklch(1 0 0)' }
]
What it recognizes
- shadcn/ui names such as
--primary,--muted-foreground,--sidebarand--chart-1, in v4 OKLCH or v3 HSL channels. - Tailwind CSS v4 scales such as
--color-brand-500in@theme. - Hand-written names such as
--bg,--text,--surfaceand--on-primary.
Hex, OKLCH, RGB, HSL and bare HSL channel values keep their format. Other formats are written as OKLCH.
What add fills in
- A dark mode for every light token that differs by mode: a
.darkblock for shadcn/ui and Tailwind, and@media (prefers-color-scheme: dark)for hand-written themes. chart-1tochart-5anddestructivefor shadcn/ui and Tailwind themes. Addonly: ['status']to addsuccessandwarningwith their foregrounds.- Matching
--color-*aliases when a shadcn/ui file declares them in@theme inline.
Check the result with auditCssVariables, or run colore theme "#0f766e" --apply globals.css --add from the CLI to edit, repair and audit in one step.