parseSrgbHex(hex: string): SrgbColor | nullParses a hex color string into an sRGB color object.
const color = parseSrgbHex('#FF5733');
// Returns: { r: 1, g: 0.341, b: 0.2 }
formatSrgbAsHex(srgb: SrgbColor): stringFormats an sRGB color object as a hex string.
const hex = formatSrgbAsHex({ r: 1, g: 0.341, b: 0.2 });
// Returns: '#FF5733'
srgbToLinearSrgb(srgb: SrgbColor): LinearSrgbColorConverts gamma-corrected sRGB to linear sRGB.
linearSrgbToSrgb(linear: LinearSrgbColor): SrgbColorConverts linear sRGB to gamma-corrected sRGB.
srgbToXyz(srgb: SrgbColor): XyzColorConverts sRGB to CIE XYZ color space.
xyzToSrgb(xyz: XyzColor): SrgbColorConverts CIE XYZ to sRGB color space.
srgbToLab(srgb: SrgbColor): LabColorConverts sRGB to CIELAB color space.
const lab = srgbToLab({ r: 0.5, g: 0.5, b: 0.5 });
// Returns: { L: 53.39, a: 0, b: 0 }
labToSrgb(lab: LabColor): SrgbColorConverts CIELAB to sRGB color space.
labToLch(lab: LabColor): LchColorConverts CIELAB to CIELCH (cylindrical representation).
const lch = labToLch({ L: 50, a: 20, b: 30 });
// Returns: { L: 50, C: 36.06, h: 56.31 }
lchToLab(lch: LchColor): LabColorConverts CIELCH to CIELAB.
srgbToLch(srgb: SrgbColor): LchColorDirect conversion from sRGB to CIELCH.
lchToSrgb(lch: LchColor): SrgbColorDirect conversion from CIELCH to sRGB.
srgbToOklab(srgb: SrgbColor): OklabColorConverts sRGB to Oklab perceptual color space.
const oklab = srgbToOklab({ r: 0.5, g: 0.7, b: 0.3 });
// Returns: { L: 0.698, a: -0.072, b: 0.106 }
oklabToSrgb(oklab: OklabColor): SrgbColorConverts Oklab to sRGB.
oklabToOklch(oklab: OklabColor): OklchColorConverts Oklab to OkLCH (cylindrical representation).
oklchToOklab(oklch: OklchColor): OklabColorConverts OkLCH to Oklab.
srgbToOklch(srgb: SrgbColor): OklchColorDirect conversion from sRGB to OkLCH.
oklchToSrgb(oklch: OklchColor): SrgbColorDirect conversion from OkLCH to sRGB.
srgbToDisplayP3(srgb: SrgbColor): DisplayP3ColorConverts sRGB to Display P3 color space.
const p3 = srgbToDisplayP3({ r: 1, g: 0, b: 0 });
// Returns: { r: 0.9175, g: 0.2003, b: 0.1386 }
displayP3ToSrgb(p3: DisplayP3Color): SrgbColorConverts Display P3 to sRGB.
parseDisplayP3FromCSS(css: string): DisplayP3Color | nullParses CSS color(display-p3) syntax.
srgbToRec2020(srgb: SrgbColor): Rec2020ColorConverts sRGB to Rec. 2020 color space.
rec2020ToSrgb(rec2020: Rec2020Color): SrgbColorConverts Rec. 2020 to sRGB.
parseRec2020FromCSS(css: string): Rec2020Color | nullParses CSS color(rec2020) syntax.
The AdaptiveOklab class provides surround-aware color adaptation.
new AdaptiveOklab(options?: AdaptiveOklabOptions)
Options:
surround: 'white' | 'gray' | 'dark' (default: 'gray')x0: number (default: 0.5) - Hue correction factorfromSrgb(srgb: SrgbColor): OklabColorConverts sRGB to Adaptive Oklab.
const adapter = new AdaptiveOklab({ surround: 'dark' });
const adapted = adapter.fromSrgb({ r: 0.5, g: 0.5, b: 0.5 });
toSrgb(oklab: OklabColor): SrgbColorConverts Adaptive Oklab back to sRGB.
fromXyz(xyz: XyzColor): OklabColorConverts XYZ to Adaptive Oklab.
toXyz(oklab: OklabColor): XyzColorConverts Adaptive Oklab to XYZ.
surroundReturns the current surround setting.
paramsReturns the adaptation parameters.
srgbToCiecam16(srgb: SrgbColor, conditions: Ciecam16ViewingConditions): Ciecam16AppearanceCalculates CIECAM16 appearance correlates.
const appearance = srgbToCiecam16(
{ r: 0.5, g: 0.5, b: 0.5 },
{
adaptingLuminance: 100,
backgroundLuminanceFactor: 0.2,
surroundType: 'average',
degreeOfAdaptation: 1
}
);
// Returns: { J, C, h, M, s, Q }
Viewing conditions:
adaptingLuminance: cd/m² of adapting fieldbackgroundLuminanceFactor: Relative luminance of backgroundsurroundType: 'average' | 'dim' | 'dark'degreeOfAdaptation: 0-1 (optional, default: 1)srgbToCam16Ucs(srgb: SrgbColor, conditions: Ciecam16ViewingConditions): Cam16UcsColorConverts sRGB to CAM16-UCS perceptually uniform space.
cam16UcsToSrgb(ucs: Cam16UcsColor, conditions: Ciecam16ViewingConditions): SrgbColor | nullConverts CAM16-UCS back to sRGB.
cam16UcsColorDifference(ucs1: Cam16UcsColor, ucs2: Cam16UcsColor): numberCalculates perceptually uniform color difference.
calculateWcagContrast(fg: SrgbColor, bg: SrgbColor): numberCalculates WCAG contrast ratio between two colors.
const contrast = calculateWcagContrast(
{ r: 0, g: 0, b: 0 }, // black text
{ r: 1, g: 1, b: 1 } // white background
);
// Returns: 21 (maximum contrast)
isWcagContrastSufficient(fg: SrgbColor, bg: SrgbColor, level: 'AA' | 'AAA', size?: 'normal' | 'large'): booleanChecks if contrast meets WCAG requirements.
const meetsAA = isWcagContrastSufficient(fg, bg, 'AA', 'normal');
// Returns: true if contrast >= 4.5
getSrgbRelativeLuminance(srgb: SrgbColor): numberCalculates relative luminance for WCAG calculations.
calculateCiede2000(lab1: LabColor, lab2: LabColor): numberCalculates CIEDE2000 perceptual color difference.
const deltaE = calculateCiede2000(
{ L: 50, a: 0, b: 0 },
{ L: 51, a: 0, b: 0 }
);
// Returns: ~0.77 (barely perceptible)
calculateOklchDifference(oklch1: OklchColor, oklch2: OklchColor): numberCalculates color difference in OkLCH space.
isSrgbInGamut(srgb: SrgbColor, epsilon?: number): booleanChecks if an sRGB color is within the displayable gamut.
const inGamut = isSrgbInGamut({ r: 0.5, g: 0.5, b: 0.5 });
// Returns: true
const outOfGamut = isSrgbInGamut({ r: 1.5, g: 0.5, b: -0.1 });
// Returns: false
isLabInTypicalRange(lab: LabColor): booleanChecks if Lab values are in typical ranges.
isOklabInTypicalRange(oklab: OklabColor): booleanChecks if Oklab values are in typical ranges.
isValidSrgbObject(obj: any): booleanType guard for sRGB color objects.
clampSrgb(srgb: SrgbColor): SrgbColorClamps sRGB values to [0,1] range.
const clamped = clampSrgb({ r: 1.5, g: -0.1, b: 0.5 });
// Returns: { r: 1, g: 0, b: 0.5 }
scaleToSrgbGamut(srgb: SrgbColor): SrgbColorScales out-of-gamut colors proportionally.
getSrgbGamutInfo(srgb: SrgbColor): GamutInfoReturns detailed gamut information.
gamutMapOklch(oklch: OklchColor, targetGamut: 'srgb' | 'display-p3' | 'rec2020'): OklchColorMaps colors to target gamut using CSS Color 4 algorithm.
const vibrant = { L: 0.7, C: 0.5, h: 30 }; // Out of sRGB
const mapped = gamutMapOklch(vibrant, 'srgb');
// Returns: Color with reduced chroma that fits in sRGB
gamutMapSrgb(srgb: SrgbColor, targetGamut: string): SrgbColorMaps sRGB colors to target gamut.
getMaxChroma(L: number, h: number, space?: 'lch' | 'oklch', precision?: number): numberFinds maximum chroma for given lightness and hue.
parseCSS(css: string): SrgbColor | nullParses CSS color strings to sRGB.
Supported formats:
#RGB, #RRGGBB, #RRGGBBAArgb(255, 0, 0), rgb(100% 0% 0%)rgb(255 0 0), rgb(255 0 0 / 0.5)lab(50% 40 30)lch(50% 40 30deg)oklab(0.7 0.1 0.1)oklch(0.7 0.2 150deg)color(display-p3 1 0 0.5)color(rec2020 1 0 0.5)red, blue, etc.const color1 = parseCSS('#FF5733');
const color2 = parseCSS('rgb(255, 87, 51)');
const color3 = parseCSS('oklch(70% 0.2 150deg)');
const color4 = parseCSS('color(display-p3 1 0 0.5)');
formatCssRgb(srgb: SrgbColor): stringFormats as CSS rgb() function.
formatCssLab(lab: LabColor): stringFormats as CSS lab() function.
formatCssLch(lch: LchColor): stringFormats as CSS lch() function.
formatCssOklab(oklab: OklabColor): stringFormats as CSS oklab() function.
formatCssOklch(oklch: OklchColor): stringFormats as CSS oklch() function.
formatCssColor(color: DisplayP3Color | Rec2020Color, space: string): stringFormats as CSS color() function.
clamp(value: number, min: number, max: number): numberClamps a value to a range.
degreesToRadians(degrees: number): numberConverts degrees to radians.
radiansToDegrees(radians: number): numberConverts radians to degrees.
normalizeHue(hue: number): numberNormalizes hue to [0, 360) range.
signPreservingPow(base: number, exponent: number): numberPower function that preserves sign.
multiplyMatrixVector(matrix: number[][], vector: number[]): number[]Matrix-vector multiplication for color transforms.
interface SrgbColor {
r: number; // [0, 1]
g: number; // [0, 1]
b: number; // [0, 1]
}
interface LabColor {
L: number; // [0, 100]
a: number; // typically [-128, 127]
b: number; // typically [-128, 127]
}
interface LchColor {
L: number; // [0, 100]
C: number; // [0, ∞)
h: number; // [0, 360)
}
interface OklabColor {
L: number; // [0, 1]
a: number; // typically [-0.4, 0.4]
b: number; // typically [-0.4, 0.4]
}
interface OklchColor {
L: number; // [0, 1]
C: number; // [0, ∞)
h: number; // [0, 360)
}
interface XyzColor {
X: number; // [0, 1] (Y-normalized)
Y: number; // [0, 1]
Z: number; // [0, 1]
}
For complete type definitions, see index.d.ts.