Module: srgb

Conversion routines between CIE XYZ and sRGB color spaces. IMPORTANT: - These conversion functions do NOT perform any clamping unless explicitly stated. - Raw linear or gamma-encoded sRGB values may be <0 or >1 if out-of-gamut. - To safely format or display colors, clamp values into [0,1].
Source:

Methods

(static) formatSrgbAsHex(srgb) → {string}

Formats an sRGB object as lowercase hex string. Values are clamped to [0, 1] before conversion.
Parameters:
Name Type Description
srgb SrgbColor sRGB color with values 0-1.
Source:
Throws:
If input is not a valid sRGB object.
Type
TypeError
Returns:
Hex color string like "#ff5733".
Type
string
Example
formatSrgbAsHex({ r: 1, g: 0.341, b: 0.2 }) // "#ff5733"

(static) isSrgbInGamut(srgb) → {boolean}

Checks if an sRGB color is within gamut. Allows small epsilon for floating point errors.
Parameters:
Name Type Description
srgb SrgbColor The sRGB color to check.
Source:
Returns:
True if the color is within gamut.
Type
boolean

(static) linearSrgbToSrgb(linearSrgbColor) → {SrgbColor}

Converts linear sRGB to gamma-corrected sRGB. Input range: [0, 1] per channel expected, values outside are processed without clamping Output range: [0, 1] per channel for valid inputs, not clamped
Parameters:
Name Type Description
linearSrgbColor LinearSrgbColor The linear sRGB color { r, g, b } with values 0-1.
Source:
Returns:
The gamma-corrected sRGB color { r, g, b } with values 0-1.
Type
SrgbColor

(static) linearSrgbToXyz(linearSrgbColor) → {XyzColor}

Converts linear sRGB to CIE XYZ (D65). Input range: [0, 1] per channel expected, values outside are processed without clamping Output range: X[0, 0.95047], Y[0, 1.0], Z[0, 1.08883] for valid sRGB inputs (normalized scale) Note: XYZ values are normalized (Y=1 for white) not the traditional 0-100 scale
Parameters:
Name Type Description
linearSrgbColor LinearSrgbColor The linear sRGB color { r, g, b }.
Source:
Returns:
The CIE XYZ color { X, Y, Z } in normalized scale.
Type
XyzColor

(static) parseSrgbHex(hexStr) → {SrgbColor|null}

Parses a hex color string to normalized sRGB object.
Parameters:
Name Type Description
hexStr string Hex color string with or without '#' prefix.
Source:
Returns:
sRGB color with values 0-1, or null if invalid.
Type
SrgbColor | null
Example
parseSrgbHex('#FF5733') // { r: 1, g: 0.341, b: 0.2 }
parseSrgbHex('FF5733')  // { r: 1, g: 0.341, b: 0.2 }
parseSrgbHex('#F53')    // { r: 1, g: 0.333, b: 0.2 }
parseSrgbHex('invalid') // null

(static) srgbToLinearSrgb(srgbColor) → {LinearSrgbColor}

Converts gamma-corrected sRGB to linear sRGB. Input range: [0, 1] per channel expected, values outside are processed without clamping Output range: [0, 1] per channel for valid inputs, not clamped
Parameters:
Name Type Description
srgbColor SrgbColor The sRGB color { r, g, b } with values 0-1.
Source:
Returns:
The linear sRGB color { r, g, b } with values 0-1.
Type
LinearSrgbColor

(static) srgbToXyz(srgbColor) → {XyzColor}

Converts gamma-corrected sRGB directly to CIE XYZ (D65).
Parameters:
Name Type Description
srgbColor SrgbColor The sRGB color { r, g, b }.
Source:
Returns:
The CIE XYZ color { X, Y, Z } scaled 0-1.
Type
XyzColor

(static) xyzToLinearSrgb(xyzColor) → {LinearSrgbColor}

Converts CIE XYZ (D65) to linear sRGB. Expects XYZ values scaled 0-1.
Parameters:
Name Type Description
xyzColor XyzColor The CIE XYZ color { X, Y, Z } scaled 0-1.
Source:
Returns:
The linear sRGB color { r, g, b }.
Type
LinearSrgbColor

(static) xyzToSrgb(xyzColor) → {SrgbColor}

Converts CIE XYZ (D65) directly to gamma-corrected sRGB.
Parameters:
Name Type Description
xyzColor XyzColor The CIE XYZ color { X, Y, Z } scaled 0-1.
Source:
Returns:
The sRGB color { r, g, b }.
Type
SrgbColor