Skip to content

theme/color

import { … } from 'zidane-ui'ui/src/theme/color.ts

Functions#

capChromaFunction#

Return color with chroma capped at max.

TypeScript
function capChroma(color: string, max: number): string
Parameters
NameTypeDescription
colorstring

The CSS color.

maxnumber

The highest chroma to keep.

string

ui/src/theme/color.ts:381

contrastRatioFunction#

WCAG 2.x contrast ratio, 1..21.

TypeScript
function contrastRatio(a: string | Rgb, b: string | Rgb): number
Parameters
NameTypeDescription
astring | Rgb

The first color.

bstring | Rgb

The second color.

number

ui/src/theme/color.ts:309

ensureContrastFunction#

Nudge fg lightness away from bg until contrastRatio(fg, bg) >= min, keeping hue/chroma. Returns the first passing color, or the extreme (white / black by surface luminance) when the hue can't reach the target.

TypeScript
function ensureContrast(fg: string, bg: string, min: number): string
Parameters
NameTypeDescription
fgstring

The foreground color to adjust.

bgstring

The background it sits on.

minnumber

The contrast ratio to reach.

string

ui/src/theme/color.ts:414

formatColorFunction#

Formats a color as CSS: #rrggbb, or rgba() when alpha is below 1.

TypeScript
function formatColor(c: Rgb): string
Parameters
NameTypeDescription
cRgb

The color to format.

string

The CSS color string.

ui/src/theme/color.ts:123

isDarkFunction#

Tells whether a surface color is dark enough that white text reads better than black.

TypeScript
function isDark(color: string): boolean
Parameters
NameTypeDescription
colorstring

The CSS color of the surface.

boolean

true when its luminance is below DARK_LUMINANCE_THRESHOLD.

ui/src/theme/color.ts:329

luminanceFunction#

WCAG relative luminance, 0 (black) .. 1 (white). Alpha is ignored.

TypeScript
function luminance(color: string | Rgb): number
Parameters
NameTypeDescription
colorstring | Rgb

A CSS color string or an sRGB color. An unparseable string counts as black.

number

ui/src/theme/color.ts:295

mixFunction#

Perceptual mix in OKLab. t = 0 → a, t = 1 → b. Alpha is mixed linearly.

TypeScript
function mix(a: string, b: string, t: number): string
Parameters
NameTypeDescription
astring

The color at t = 0. Returned when either color cannot be parsed.

bstring

The color at t = 1.

tnumber

How far from a to b, clamped to 0..1.

string

ui/src/theme/color.ts:344

oklchFunction#

Convenience: parse → OKLCH → format, for terse palette code.

TypeScript
function oklch(l: number, c: number, h: number, a?: number): string
Parameters
NameTypeDefaultDescription
lnumber

Lightness, 0..1.

cnumber

Chroma, 0..~0.4.

hnumber

Hue in degrees.

a?number1

Alpha, 0..1.

string

The CSS color string, gamut-mapped to sRGB.

ui/src/theme/color.ts:145

oklchToRgbFunction#

OKLCH → sRGB with gamut mapping: if the color is out of gamut, chroma is reduced (binary search) until it fits. Lightness and hue are preserved, which is what you want when deriving ramps: a too-vivid accent becomes the most vivid displayable version of itself instead of clipping to a different hue.

TypeScript
function oklchToRgb(c: Oklch): Rgb
Parameters
NameTypeDescription
cOklch

The OKLCH color.

Rgb

The sRGB color, clamped to 0..1.

ui/src/theme/color.ts:241

parseColorFunction#

Parses a CSS color into sRGB. It accepts #rgb, #rgba, #rrggbb, #rrggbbaa, rgb(), rgba() and oklch(); an OKLCH color out of gamut is chroma-reduced to fit.

TypeScript
function parseColor(input: string): Rgb | undefined
Parameters
NameTypeDescription
inputstring

The color string.

Rgb | undefined

The color, or undefined when the string is not a supported color.

ui/src/theme/color.ts:49

readableOnFunction#

Pick a readable foreground for bg: preferred if it already passes min, else preferred pushed until it does, else pure white/black.

TypeScript
function readableOn(bg: string, preferred: string, min?: number): string
Parameters
NameTypeDefaultDescription
bgstring

The background color.

preferredstring

The foreground to use when it is readable.

min?number4.5

The contrast ratio to reach. Defaults to 4.5.

string

ui/src/theme/color.ts:448

rgbToOklchFunction#

Converts an sRGB color to OKLCH. Near-gray colors get a chroma and hue of 0.

TypeScript
function rgbToOklch(c: Rgb): Oklch
Parameters
NameTypeDescription
cRgb

The sRGB color.

Oklch

The OKLCH color.

ui/src/theme/color.ts:221

tintedNeutralFunction#

Build a color of the given lightness that carries a faint tint of hue source, the "neutral with personality" every good editor theme uses instead of pure gray. Chroma is tint, clamped to what stays subtle.

TypeScript
function tintedNeutral(l: number, tintSource: string, tint: number): string
Parameters
NameTypeDescription
lnumber

Lightness, 0..1.

tintSourcestring

The color whose hue the neutral carries. Without a parseable one the result is pure gray.

tintnumber

The chroma of the tint, clamped to 0..0.06.

string

ui/src/theme/color.ts:399

toOklchFunction#

Parses a CSS color and converts it to OKLCH.

TypeScript
function toOklch(color: string): Oklch | undefined
Parameters
NameTypeDescription
colorstring

The CSS color string.

Oklch | undefined

The OKLCH color, or undefined when the string cannot be parsed.

ui/src/theme/color.ts:281

withAlphaFunction#

Returns color with its alpha replaced.

TypeScript
function withAlpha(color: string, alpha: number): string
Parameters
NameTypeDescription
colorstring

The CSS color. Returned unchanged when it cannot be parsed.

alphanumber

The new alpha, clamped to 0..1.

string

The CSS color string.

ui/src/theme/color.ts:156

withLightnessFunction#

Return color with lightness set (0..1), chroma/hue preserved (gamut-mapped).

TypeScript
function withLightness(color: string, l: number): string
Parameters
NameTypeDescription
colorstring

The CSS color.

lnumber

The new lightness, 0..1.

string

ui/src/theme/color.ts:367

Constants#

DARK_LUMINANCE_THRESHOLDConstant#

Threshold under which a surface is "dark" for the purpose of picking a white vs black foreground. 0.179 is the point where white and black text have equal WCAG contrast against the surface.

TypeScript
const DARK_LUMINANCE_THRESHOLD = 0.179

ui/src/theme/color.ts:321

Interfaces#

OklchInterface#

A color in OKLCH: perceptual lightness, chroma and hue, plus alpha.

TypeScript
interface Oklch
Oklch properties
NameTypeDescription
anumber

Alpha, 0..1

cnumber

Chroma, 0..~0.4

hnumber

Hue in degrees, 0..360

lnumber

Lightness, 0..1

ui/src/theme/color.ts:25

RgbInterface#

A color in sRGB, every channel from 0 to 1.

TypeScript
interface Rgb
Rgb properties
NameTypeDescription
anumber

Alpha, 0..1

bnumber

Blue channel, 0..1

gnumber

Green channel, 0..1

rnumber

Red channel, 0..1

ui/src/theme/color.ts:13