generateColorPalette
Generates a complete, accessible color palette from a base color using the OKLCH color space.The base color in any valid CSS color format (hex, rgb, etc.)
Whether to generate a dark mode palette
Colors to preserve from modification (e.g.,
{ primary: '#ff0000' })Color harmony mode:
'complementary' (180° hue offset) or 'monochromatic' (same hue)A complete palette object containing:
text- Main text color (WCAG AAA contrast with background)background- Background colorprimary- Primary brand colorcontainer- Container/surface coloraccent- Accent color (complementary or monochromatic to primary)success,error,warning- Semantic state colorsonPrimary,onContainer,onAccent,onSuccess,onError,onWarning- Contrasting colors for use on their respective backgroundsborder- Border color (mixed from text and background)muted- Muted/subtle text colorring- Focus ring color (defaults to primary)
Example
adaptColorsForMode
Adapts an existing color palette to a different theme mode (light/dark) while preserving hue relationships.The current color palette to adapt
Whether the target mode is dark (
true) or light (false)Set of color role names to preserve without modification (e.g.,
new Set(['primary', 'accent']))Adapted palette with the same structure as
generateColorPalette outputExample
pickOnColor
Selects an accessible foreground color (with slight hue tint) for use on a given background color, ensuring WCAG AA compliance (4.5:1 contrast ratio).Background color in any valid CSS color format
Hex color string with optimal contrast (minimum 4.5:1 ratio)
Example
mixOklch
Mixes two colors in the OKLCH color space, producing perceptually uniform color blends.First color to mix
Second color to mix
Mix ratio (0 = fully c1, 1 = fully c2, 0.5 = equal mix)
Hex color string of the mixed color
Example
deriveBaseColorFromLocks
Derives a suitable base color from locked colors to maintain color harmony when regenerating a palette.Object containing locked colors by role name
Color harmony mode:
'complementary' or 'monochromatic'Derived base color as hex string, or
null if no suitable chromatic color is foundExample
safeChroma
Calculates a safe chroma value that won’t clip when converted to sRGB, scaled by a vibrancy factor.Lightness value in OKLCH (0-1)
Hue value in degrees (0-360)
Vibrancy multiplier (0-1, where 1 = maximum safe chroma)
Safe chroma value for the given lightness and hue
Example
maxChromaInGamut
Finds the maximum chroma value for a given lightness and hue that stays within the sRGB gamut.Lightness value in OKLCH (0-1)
Hue value in degrees (0-360)
Maximum chroma value that won’t clip (typically 0-0.4)
Example
Types
HarmonyMode
Defines the color harmony relationship between primary and accent colors.'complementary'- Accent color is 180° opposite on the color wheel'monochromatic'- Accent color shares the same hue as primary