oklch.ts

OKLCH/OKLab ↔ sRGB conversions and gamut math.

Design-time and test use only — nothing here ships to browsers. The CSS output of fuz_css stays pure oklch()/calc(); this module exists so derivation scripts can fit curve knobs against real colors and so tests can assert that every default stop is inside the sRGB gamut and meets the contrast gates (see wcag.ts).

Matrices and constants follow css-color-4 and Björn Ottosson's reference implementation.

view source

Declarations
#

14 declarations

linear_component_to_srgb
#

oklch.ts view source

(c: number): number import {linear_component_to_srgb} from '@fuzdev/fuz_css/oklch.js';

Converts a linear-light sRGB component to gamma-encoded in [0, 1].

c

type number

returns

number

Oklab
#

oklch.ts view source

Oklab import type {Oklab} from '@fuzdev/fuz_css/oklch.js';

A color in OKLab: lightness in [0, 1], a/b roughly in [-0.4, 0.4].

oklab_to_oklch
#

oklch.ts view source

([lightness, a, b]: Oklab): Oklch import {oklab_to_oklch} from '@fuzdev/fuz_css/oklch.js';

Converts OKLab to OKLCH. Hue is normalized to [0, 360); an achromatic color gets hue 0.

__0

type Oklab

returns

Oklch

oklab_to_srgb
#

oklch.ts view source

([lightness, a, b]: Oklab): RgbUnit import {oklab_to_srgb} from '@fuzdev/fuz_css/oklch.js';

Converts OKLab to gamma-encoded sRGB.

__0

type Oklab

returns

RgbUnit

components that may fall outside [0, 1] when the color is out of gamut

Oklch
#

oklch.ts view source

Oklch import type {Oklch} from '@fuzdev/fuz_css/oklch.js';

A color in OKLCH: lightness in [0, 1], chroma ≥ 0 (unbounded), hue in degrees.

oklch_delta_e
#

oklch.ts view source

(a: Oklch, b: Oklch): number import {oklch_delta_e} from '@fuzdev/fuz_css/oklch.js';

Computes ΔEOK between two OKLCH colors — the Euclidean distance in OKLab. Just-noticeable differences sit around 0.02; css-color-4's gamut mapping uses 0.02 as its "local MINDE" threshold.

a

type Oklch

b

type Oklch

returns

number

oklch_in_srgb_gamut
#

oklch.ts view source

(lch: Oklch, epsilon?: number): boolean import {oklch_in_srgb_gamut} from '@fuzdev/fuz_css/oklch.js';

Checks whether an OKLCH color converts to sRGB with all components inside [0, 1].

lch

type Oklch

epsilon

tolerance for float noise at the gamut boundary

type number
default 1e-6

returns

boolean

oklch_max_srgb_chroma
#

oklch.ts view source

(lightness: number, hue: number, precision?: number): number import {oklch_max_srgb_chroma} from '@fuzdev/fuz_css/oklch.js';

Finds the largest chroma at a given OKLCH lightness and hue such that every chroma below it is also inside the sRGB gamut. Returns 0 when the lightness itself is out of range.

This is deliberately the *safe* maximum for ramps, not the outermost gamut intersection: the sRGB gamut is non-convex in OKLab (most visibly near the blue primary), so the constant-(L, H) chroma ray can exit and re-enter the gamut. A ramp needs the whole [0, C] segment representable, so the scan stops at the first exit.

lightness

type number

hue

type number

precision

terminate when the refinement interval is smaller than this

type number
default 1e-5

returns

number

oklch_to_oklab
#

oklch.ts view source

([lightness, chroma, hue]: Oklch): Oklab import {oklch_to_oklab} from '@fuzdev/fuz_css/oklch.js';

Converts OKLCH to OKLab.

__0

type Oklch

returns

Oklab

oklch_to_srgb
#

oklch.ts view source

(lch: Oklch): RgbUnit import {oklch_to_srgb} from '@fuzdev/fuz_css/oklch.js';

Converts OKLCH to gamma-encoded sRGB.

lch

type Oklch

returns

RgbUnit

components that may fall outside [0, 1] when the color is out of gamut

RgbUnit
#

oklch.ts view source

RgbUnit import type {RgbUnit} from '@fuzdev/fuz_css/oklch.js';

An RGB color with components in the unit interval [0, 1]. Values are gamma-encoded sRGB unless a function documents linear-light. Out-of-gamut results are not clamped, so components may fall outside [0, 1].

srgb_component_to_linear
#

oklch.ts view source

(c: number): number import {srgb_component_to_linear} from '@fuzdev/fuz_css/oklch.js';

Converts a gamma-encoded sRGB component in [0, 1] to linear-light.

c

type number

returns

number

srgb_to_oklab
#

oklch.ts view source

([r, g, b]: RgbUnit): Oklab import {srgb_to_oklab} from '@fuzdev/fuz_css/oklch.js';

Converts gamma-encoded sRGB to OKLab.

__0

type RgbUnit

returns

Oklab

srgb_to_oklch
#

oklch.ts view source

(rgb: RgbUnit): Oklch import {srgb_to_oklch} from '@fuzdev/fuz_css/oklch.js';

Converts gamma-encoded sRGB to OKLCH.

rgb

type RgbUnit

returns

Oklch

Imported by
#