@pbroom/color-kit/interopWriters

toLinearSrgbArray

Write a color as linear-light sRGB [r, g, b] (0–1 in gamut). This is three.js's default working color space (ColorManagement.workingColorSpace === 'srgb-linear') and what lighting math in shaders expects.

functionimport { toLinearSrgbArray } from '@pbroom/color-kit/interop';writers.ts:63
function toLinearSrgbArray(
  color: Color,
  out?: undefined,
  offset?: number,
  options?: ArrayWriteOptions,
): ColorTuple;

function toLinearSrgbArray<T extends WritableArrayLike>(
  color: Color,
  out: T,
  offset?: number,
  options?: ArrayWriteOptions,
): T;

function toLinearSrgbArray<T extends WritableArrayLike>(
  color: Color,
  out: T | undefined,
  offset?: number,
  options?: ArrayWriteOptions,
): ColorTuple | T;

Values are unclamped floats by default, so out-of-gamut colors produce channels outside 0–1 (correct for HDR/linear pipelines, and what three.js does). Pass { clamp: true } to clip, or gamut-map first to reduce chroma instead: toLinearSrgbArray(toSrgbGamut(color), out) (or toP3Gamut).

Overload 1 of 3

Parameters

colorColor
Color to convert.
out?undefined
Destination (number[], Float32Array, ...). When omitted a new ColorTuple is returned.
offset?number
Index in out of the first component to write.
options?ArrayWriteOptions
Out-of-gamut handling (clamp).

Returns

ColorTupleout, or a new tuple when out is omitted.

Overload 2 of 3

Parameters

colorColor
Color to convert.
outT
Destination (number[], Float32Array, ...). When omitted a new ColorTuple is returned.
offset?number
Index in out of the first component to write.
options?ArrayWriteOptions
Out-of-gamut handling (clamp).

Returns

Tout, or a new tuple when out is omitted.

Overload 3 of 3

Parameters

colorColor
Color to convert.
outT | undefined
Destination (number[], Float32Array, ...). When omitted a new ColorTuple is returned.
offset?number
Index in out of the first component to write.
options?ArrayWriteOptions
Out-of-gamut handling (clamp).

Returns

ColorTuple | Tout, or a new tuple when out is omitted.

Example

TypeScript
// three.js stores linear sRGB, so write working-space values directly
// instead of round-tripping through an 8-bit CSS string.
const threeColor = new THREE.Color().fromArray(toLinearSrgbArray(color));

// WebGL: reuse one scratch buffer, no per-frame allocation.
const scratch = new Float32Array(3);
gl.uniform3fv(loc, toLinearSrgbArray(color, scratch));

Also exported from