Skip to documentation
Design system / Sources & standards
Current repository source
customTheme.ts
src/theming/customTheme.ts
This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.
import type { CSSProperties } from "react";
import { getTenantThemeStyle, type ColorMode } from "./tenantThemes";
export const CUSTOM_SECONDARY_LIGHTNESS = 0.94;
export const CUSTOM_ACCENT_LIGHTNESS = 0.985;
type Oklch = { lightness: number; chroma: number; hue: number };
type CustomPalette = { primary: string; secondary: string; accent: string };
function srgbToLinear(value: number): number {
const channel = value / 255;
return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
}
function hexToOklch(hex: string): Oklch {
const channels = [1, 3, 5].map((offset) => srgbToLinear(Number.parseInt(hex.slice(offset, offset + 2), 16)));
const [red, green, blue] = channels;
const l = 0.4122214708 * red + 0.5363325363 * green + 0.0514459929 * blue;
const m = 0.2119034982 * red + 0.6806995451 * green + 0.1073969566 * blue;
const s = 0.0883024619 * red + 0.2817188376 * green + 0.6299787005 * blue;
const lRoot = Math.cbrt(l);
const mRoot = Math.cbrt(m);
const sRoot = Math.cbrt(s);
const a = 1.9779984951 * lRoot - 2.428592205 * mRoot + 0.4505937099 * sRoot;
const b = 0.0259040371 * lRoot + 0.7827717662 * mRoot - 0.808675766 * sRoot;
return { lightness: 0.2104542553 * lRoot + 0.793617785 * mRoot - 0.0040720468 * sRoot, chroma: Math.sqrt(a ** 2 + b ** 2), hue: ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360 };
}
function toOklch({ lightness, chroma, hue }: Oklch): string {
return `oklch(${lightness.toFixed(3)} ${Math.min(chroma, 0.15).toFixed(3)} ${hue.toFixed(1)})`;
}
function withLightness(hex: string, lightness: number, chromaCap = 0.15): string {
const color = hexToOklch(hex);
return toOklch({ ...color, lightness, chroma: Math.min(color.chroma, chromaCap) });
}
function linearToSrgb(value: number): number {
const channel = Math.max(0, Math.min(1, value));
return channel <= 0.0031308 ? channel * 12.92 : 1.055 * channel ** (1 / 2.4) - 0.055;
}
function toHex(value: number): string {
return Math.round(linearToSrgb(value) * 255).toString(16).padStart(2, "0");
}
function withLightnessRgb(hex: string, lightness: number, chromaCap = 0.15): [number, number, number] {
const color = hexToOklch(hex);
const chroma = Math.min(color.chroma, chromaCap);
return oklchToLinearRgb({ ...color, lightness, chroma });
}
function oklchToLinearRgb({ lightness, chroma, hue }: Oklch): [number, number, number] {
const hueRadians = (hue * Math.PI) / 180;
const a = chroma * Math.cos(hueRadians);
const b = chroma * Math.sin(hueRadians);
const lRoot = lightness + 0.3963377774 * a + 0.2158037573 * b;
const mRoot = lightness - 0.1055613458 * a - 0.0638541728 * b;
const sRoot = lightness - 0.0894841775 * a - 1.291485548 * b;
const l = lRoot ** 3;
const m = mRoot ** 3;
const s = sRoot ** 3;
return [4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s];
}
function withLightnessHex(hex: string, lightness: number, chromaCap = 0.15): string {
const [red, green, blue] = withLightnessRgb(hex, lightness, chromaCap);
return `#${toHex(red)}${toHex(green)}${toHex(blue)}`;
}
function derivePrimaryForeground(hex: string): string {
const dark = withLightnessHex(hex, 0.1);
const light = withLightnessHex(hex, 0.98);
return contrastRatio(hex, dark) > contrastRatio(hex, light) ? dark : light;
}
function relativeLuminance(hex: string): number {
const [red, green, blue] = [1, 3, 5].map((offset) => srgbToLinear(Number.parseInt(hex.slice(offset, offset + 2), 16)));
return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
}
function contrastRatio(left: string, right: string): number {
const [lighter, darker] = [relativeLuminance(left), relativeLuminance(right)].sort((a, b) => b - a);
return (lighter + 0.05) / (darker + 0.05);
}
function cssLuminance(value: string): number {
if (value.startsWith("#")) return relativeLuminance(value);
const match = /^oklch\(([\d.]+) ([\d.]+) ([\d.]+)\)$/.exec(value);
if (!match) return Number.NaN;
const [red, green, blue] = oklchToLinearRgb({ lightness: Number(match[1]), chroma: Number(match[2]), hue: Number(match[3]) }).map((channel) => Math.max(0, Math.min(1, channel)));
return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
}
function cssContrastRatio(left: string, right: string): number {
const [lighter, darker] = [cssLuminance(left), cssLuminance(right)].sort((a, b) => b - a);
return (lighter + 0.05) / (darker + 0.05);
}
export function normalizeCustomPalette(palette: CustomPalette, mode: ColorMode = "light"): CustomPalette {
return {
primary: palette.primary.toLowerCase(),
secondary: withLightnessHex(palette.secondary, mode === "dark" ? 0.31 : CUSTOM_SECONDARY_LIGHTNESS, mode === "dark" ? 0.025 : 0.15),
accent: withLightnessHex(palette.accent, mode === "dark" ? 0.36 : CUSTOM_ACCENT_LIGHTNESS, mode === "dark" ? 0.05 : 0.15),
};
}
export function isCustomPaletteAccessible(palette: CustomPalette, mode: ColorMode = "light"): boolean {
const normalized = normalizeCustomPalette(palette, mode);
const style = getCustomThemeStyle(normalized, mode) as Record<string, string>;
const linkSurfaces = ["--background", "--card", "--popover", "--secondary", "--muted", "--accent", "--surface-raised", "--surface-overlay", "--surface-sunken", "--sidebar", "--sidebar-accent", "--selected", "--info-muted"];
return (
cssContrastRatio(style["--primary"], style["--primary-foreground"]) >= 4.5 &&
linkSurfaces.every((surface) => cssContrastRatio(style["--text-link"], style[surface]) >= 4.5) &&
cssContrastRatio(style["--secondary"], style["--secondary-foreground"]) >= 4.5 &&
cssContrastRatio(style["--accent"], style["--accent-foreground"]) >= 4.5
);
}
export function getCustomThemeStyle(palette: CustomPalette, mode: ColorMode = "light"): CSSProperties {
const primaryForeground = derivePrimaryForeground(palette.primary);
if (mode === "dark") {
const normalized = normalizeCustomPalette(palette, "dark");
const surfaceText = "#f3f4f6";
return {
...getTenantThemeStyle("default-dark"),
"--primary": normalized.primary, "--primary-foreground": primaryForeground,
"--secondary": normalized.secondary, "--secondary-foreground": surfaceText,
"--muted": normalized.secondary, "--muted-foreground": "#bbc1ca",
"--accent": normalized.accent, "--accent-foreground": surfaceText,
"--surface-sunken": normalized.secondary, "--text-link": normalized.primary,
"--ring": normalized.primary, "--border-focus": normalized.primary, "--focus-ring": normalized.primary,
"--selected": normalized.accent, "--selected-foreground": surfaceText,
"--info": normalized.primary, "--info-foreground": primaryForeground,
"--info-muted": normalized.accent, "--info-muted-foreground": surfaceText, "--info-border": normalized.primary,
"--chart-1": normalized.primary, "--sidebar-primary": normalized.primary,
"--sidebar-primary-foreground": primaryForeground, "--sidebar-accent": normalized.accent,
"--sidebar-accent-foreground": surfaceText, "--sidebar-ring": normalized.primary,
} as CSSProperties;
}
const secondary = withLightness(palette.secondary, CUSTOM_SECONDARY_LIGHTNESS);
const accent = withLightness(palette.accent, CUSTOM_ACCENT_LIGHTNESS);
const secondaryForeground = "#1f2937";
const accentForeground = "#1f2937";
return {
...getTenantThemeStyle("default"),
"--background": withLightness(palette.primary, 0.985, 0.015),
"--card": "#ffffff",
"--popover": "#ffffff",
"--primary": palette.primary,
"--primary-foreground": primaryForeground,
"--secondary": secondary,
"--secondary-foreground": secondaryForeground,
"--muted": secondary,
"--muted-foreground": "#4b5563",
"--accent": accent,
"--accent-foreground": accentForeground,
"--border": withLightness(palette.secondary, 0.84, 0.08),
"--input": withLightness(palette.secondary, 0.84, 0.08),
"--ring": palette.primary,
"--surface-default": withLightness(palette.primary, 0.985, 0.015),
"--surface-raised": withLightness(palette.primary, 0.97, 0.025),
"--surface-overlay": "#ffffff",
"--surface-sunken": secondary,
"--text-link": palette.primary,
"--border-default": withLightness(palette.secondary, 0.84, 0.08),
"--border-subtle": withLightness(palette.secondary, 0.91, 0.04),
"--border-focus": palette.primary,
"--focus-ring": palette.primary,
"--focus-ring-offset": withLightness(palette.primary, 0.985, 0.015),
"--selected": accent,
"--selected-foreground": accentForeground,
"--info": palette.primary,
"--info-foreground": primaryForeground,
"--info-muted": accent,
"--info-muted-foreground": accentForeground,
"--info-border": palette.primary,
"--chart-1": palette.primary,
"--sidebar": withLightness(palette.secondary, 0.95, 0.03),
"--sidebar-primary": palette.primary,
"--sidebar-primary-foreground": primaryForeground,
"--sidebar-accent": accent,
"--sidebar-accent-foreground": accentForeground,
"--sidebar-border": withLightness(palette.secondary, 0.84, 0.08),
"--sidebar-ring": palette.primary,
} as CSSProperties;
}
All sources and standards