Helpers
These functions are building blocks for custom color pipelines. They operate on raw numeric values rather than CSS color strings.
import { srgbGammaDecode, oklabToLinearSRGB, isInGamut } from 'colorizr';Gamma Transfer
Section titled “Gamma Transfer”The sRGB transfer function (piecewise gamma) and its inverse. These handle negative values via sign preservation, which is important for out-of-gamut colors during gamut mapping.
srgbGammaDecode
Section titled “srgbGammaDecode”Convert sRGB encoded values to linear light.
srgbGammaDecode(input: number): numberimport { srgbGammaDecode } from 'colorizr';
srgbGammaDecode(0.5); // 0.214srgbGammaDecode(1.0); // 1.0srgbGammaDecode(0.0); // 0.0srgbGammaEncode
Section titled “srgbGammaEncode”Convert linear light values to sRGB encoded.
srgbGammaEncode(input: number): numberimport { srgbGammaEncode } from 'colorizr';
srgbGammaEncode(0.214); // ~0.5srgbGammaEncode(1.0); // 1.0srgbGammaEncode(0.0); // 0.0Linear RGB Conversion
Section titled “Linear RGB Conversion”Convert OkLab values directly to linear RGB tuples without rounding or clamping. Useful for custom gamut mapping, color blending in linear space, or feeding into canvas/WebGL pipelines.
oklabToLinearSRGB
Section titled “oklabToLinearSRGB”Convert OkLab to linear sRGB.
oklabToLinearSRGB(L: number, a: number, b: number): ColorTupleimport { oklabToLinearSRGB } from 'colorizr';
oklabToLinearSRGB(0.63, 0.26, 0.015); // [0.579..., 0.011..., 0.036...]oklabToLinearP3
Section titled “oklabToLinearP3”Convert OkLab to linear Display P3. Uses the SRGB_TO_P3 matrix internally.
oklabToLinearP3(L: number, a: number, b: number): ColorTupleimport { oklabToLinearP3 } from 'colorizr';
oklabToLinearP3(0.63, 0.26, 0.015); // [0.486..., 0.031..., 0.050...]Gamut Check
Section titled “Gamut Check”isInGamut
Section titled “isInGamut”Check whether a linear RGB color tuple is within the [0, 1] gamut, with a small tolerance (GAMUT_EPSILON).
isInGamut(color: ColorTuple): booleanimport { oklabToLinearSRGB, isInGamut } from 'colorizr';
const linear = oklabToLinearSRGB(0.5, 0.0, 0.0);isInGamut(linear); // true
// Out-of-gamut wide colorconst wide = oklabToLinearSRGB(0.7, 0.4, 0.0);isInGamut(wide); // false