theme/color
import { … } from 'zidane-ui'ui/src/theme/color.ts
Functions#
capChromaFunction#
Return color with chroma capped at max.
function capChroma(color: string, max: number): stringParameters
| Name | Type | Description |
|---|---|---|
| color | string | The CSS color. |
| max | number | The highest chroma to keep. |
Returns
stringcontrastRatioFunction#
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.
function ensureContrast(fg: string, bg: string, min: number): stringParameters
| Name | Type | Description |
|---|---|---|
| fg | string | The foreground color to adjust. |
| bg | string | The background it sits on. |
| min | number | The contrast ratio to reach. |
Returns
stringformatColorFunction#
isDarkFunction#
Tells whether a surface color is dark enough that white text reads better than black.
function isDark(color: string): booleanParameters
| Name | Type | Description |
|---|---|---|
| color | string | The CSS color of the surface. |
Returns
booleantrue when its luminance is below DARK_LUMINANCE_THRESHOLD.
luminanceFunction#
mixFunction#
Perceptual mix in OKLab. t = 0 → a, t = 1 → b. Alpha is mixed linearly.
function mix(a: string, b: string, t: number): stringParameters
| Name | Type | Description |
|---|---|---|
| a | string | The color at |
| b | string | The color at |
| t | number | How far from |
Returns
stringoklchFunction#
Convenience: parse → OKLCH → format, for terse palette code.
function oklch(l: number, c: number, h: number, a?: number): stringParameters
| Name | Type | Default | Description |
|---|---|---|---|
| l | number | Lightness, 0..1. | |
| c | number | Chroma, 0..~0.4. | |
| h | number | Hue in degrees. | |
| a? | number | 1 | Alpha, 0..1. |
Returns
stringThe CSS color string, gamut-mapped to sRGB.
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.
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.
readableOnFunction#
Pick a readable foreground for bg: preferred if it already passes
min, else preferred pushed until it does, else pure white/black.
function readableOn(bg: string, preferred: string, min?: number): stringParameters
| Name | Type | Default | Description |
|---|---|---|---|
| bg | string | The background color. | |
| preferred | string | The foreground to use when it is readable. | |
| min? | number | 4.5 | The contrast ratio to reach. Defaults to 4.5. |
Returns
stringrgbToOklchFunction#
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.
function tintedNeutral(l: number, tintSource: string, tint: number): stringParameters
| Name | Type | Description |
|---|---|---|
| l | number | Lightness, 0..1. |
| tintSource | string | The color whose hue the neutral carries. Without a parseable one the result is pure gray. |
| tint | number | The chroma of the tint, clamped to 0..0.06. |
Returns
stringtoOklchFunction#
withAlphaFunction#
Returns color with its alpha replaced.
function withAlpha(color: string, alpha: number): stringParameters
| Name | Type | Description |
|---|---|---|
| color | string | The CSS color. Returned unchanged when it cannot be parsed. |
| alpha | number | The new alpha, clamped to 0..1. |
Returns
stringThe CSS color string.
withLightnessFunction#
Return color with lightness set (0..1), chroma/hue preserved (gamut-mapped).
function withLightness(color: string, l: number): stringParameters
| Name | Type | Description |
|---|---|---|
| color | string | The CSS color. |
| l | number | The new lightness, 0..1. |
Returns
stringConstants#
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.
const DARK_LUMINANCE_THRESHOLD = 0.179Interfaces#
OklchInterface#
A color in OKLCH: perceptual lightness, chroma and hue, plus alpha.
interface OklchProperties
| Name | Type | Description |
|---|---|---|
| a | number | Alpha, 0..1 |
| c | number | Chroma, 0..~0.4 |
| h | number | Hue in degrees, 0..360 |
| l | number | Lightness, 0..1 |
RgbInterface#
A color in sRGB, every channel from 0 to 1.
interface RgbProperties
| Name | Type | Description |
|---|---|---|
| a | number | Alpha, 0..1 |
| b | number | Blue channel, 0..1 |
| g | number | Green channel, 0..1 |
| r | number | Red channel, 0..1 |