@pbroom/color-kit/interopWriters

toSrgbArray4

Write a color as gamma-encoded sRGB [r, g, b], 0–1 in gamut (not 0–255). Use this for sRGB-encoded render targets and for three.js Color.setRGB(r, g, b, SRGBColorSpace).

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

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

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

Alpha is written as the fourth component: [c0, c1, c2, alpha].

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: toSrgbArray4(toSrgbGamut(color), out) (or toP3Gamut).

Overload 1 of 3

Parameters

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

Returns

ColorTuple4out, 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 ColorTuple4 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 ColorTuple4 is returned.
offset?number
Index in out of the first component to write.
options?ArrayWriteOptions
Out-of-gamut handling (clamp).

Returns

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

Example

TypeScript
// vec4 uniform: [c0, c1, c2, alpha]
const scratch = new Float32Array(4);
gl.uniform4fv(loc, toSrgbArray4(color, scratch));

Also exported from