API
@pbroom/color-kit/interop
Tuples, packed buffers and allocation-free writers for canvas, WebGL, WebGPU and three.js.
33 exports26 values7 types
@pbroom/color-kit/interop moves colors into and out of flat number arrays: the
vec3/vec4 floats that three.js, WebGL uniforms and WebGPU buffers expect.
It replaces CSS strings as the bridge to a renderer, so values stay exact
and unclamped instead of being rounded to 8 bits and clipped to sRGB.
Domains
- Writers.
to<Space>Array(color, out?, offset?, options?)writes three channels; the*Array4variants add alpha. Pass a typed array asoutand nothing is allocated. - Readers.
from<Space>Array(array, offset?)andfrom<Space>Array4read colors back. - Batches.
packColorswrites a list of colors into oneFloat32Array(or your own array) with optional alpha, stride and offset;unpackColorsreverses it.
Spaces are linearSrgb (three.js's working space), srgb, linearP3,
display-p3, oklab and oklch. Output is unclamped by default; clamp
clips RGB channels. Gamut mapping is composed rather than an option:
toLinearSrgbArray(toSrgbGamut(color), out).
import { parse } from '@pbroom/color-kit';
import { packColors } from '@pbroom/color-kit/interop';
const colors = [parse('#3b82f6'), parse('oklch(0.7 0.25 150)')];
const buffer = packColors(colors, 'linearSrgb', undefined, { alpha: true });
buffer.length; // → 8 (two vec4s)
Array.from(buffer.subarray(4, 7), (v) => +v.toFixed(4));
// → [-0.0937, 0.5598, 0.0385]: negative red, outside sRGB, kept as isThe guide GPU and three.js interop covers layouts and recipes.
Buffers
Types
- type
ColorTupleA three-component color tuple, such as linear-sRGB [r, g, b] or OKLab [L, a, b]. - type
ColorTuple4A four-component color tuple: three channels followed by alpha, such as linear-sRGB [r, g, b, a]. - type
ArraySpaceChannel layouts supported by the array interop helpers. - interface
ArrayWriteOptionsOut-of-gamut handling for the array writers. - interface
PackColorsOptionsOptions for packColors. - interface
UnpackColorsOptionsOptions for unpackColors. - interface
WritableArrayLikeAny indexable, writable numeric array: a number[], a ColorTuple, or a typed array such as Float32Array / Float64Array.
Writers
- function
toLinearSrgbArrayWrite a color as linear-light sRGB [r, g, b] (0–1 in gamut). - function
toLinearSrgbArray4Write a color as linear-light sRGB [r, g, b] (0–1 in gamut). - function
toSrgbArrayWrite a color as gamma-encoded sRGB [r, g, b], 0–1 in gamut (not 0–255). - function
toSrgbArray4Write a color as gamma-encoded sRGB [r, g, b], 0–1 in gamut (not 0–255). - function
toLinearP3ArrayWrite a color as linear-light Display P3 [r, g, b] (0–1 in the P3 gamut). - function
toLinearP3Array4Write a color as linear-light Display P3 [r, g, b] (0–1 in the P3 gamut). - function
toP3ArrayWrite a color as gamma-encoded Display P3 [r, g, b], 0–1 in gamut, for display-p3 canvases and WebGPU contexts configured with colorSpace: 'display-p3'. - function
toP3Array4Write a color as gamma-encoded Display P3 [r, g, b], 0–1 in gamut, for display-p3 canvases and WebGPU contexts configured with colorSpace: 'display-p3'. - function
toOklabArrayWrite a color as OKLab [L, a, b]. - function
toOklabArray4Write a color as OKLab [L, a, b]. - function
toOklchArrayWrite a color as OKLCH [l, c, h], with hue in degrees copied from the color as-is. - function
toOklchArray4Write a color as OKLCH [l, c, h], with hue in degrees copied from the color as-is.
Readers
- function
fromLinearSrgbArrayRead a color from linear-light sRGB [r, g, b] stored in any array-like (number[], Float32Array, ...). - function
fromLinearSrgbArray4Read a color from linear-light sRGB [r, g, b] stored in any array-like (number[], Float32Array, ...). - function
fromSrgbArrayRead a color from gamma-encoded sRGB [r, g, b] (0–1) stored in any array-like (number[], Float32Array, ...). - function
fromSrgbArray4Read a color from gamma-encoded sRGB [r, g, b] (0–1) stored in any array-like (number[], Float32Array, ...). - function
fromLinearP3ArrayRead a color from linear-light Display P3 [r, g, b] stored in any array-like (number[], Float32Array, ...). - function
fromLinearP3Array4Read a color from linear-light Display P3 [r, g, b] stored in any array-like (number[], Float32Array, ...). - function
fromP3ArrayRead a color from gamma-encoded Display P3 [r, g, b] (0–1) stored in any array-like (number[], Float32Array, ...). - function
fromP3Array4Read a color from gamma-encoded Display P3 [r, g, b] (0–1) stored in any array-like (number[], Float32Array, ...). - function
fromOklabArrayRead a color from OKLab [L, a, b] stored in any array-like (number[], Float32Array, ...). - function
fromOklabArray4Read a color from OKLab [L, a, b] stored in any array-like (number[], Float32Array, ...). - function
fromOklchArrayRead a color from OKLCH [l, c, h] stored in any array-like (number[], Float32Array, ...). - function
fromOklchArray4Read a color from OKLCH [l, c, h] stored in any array-like (number[], Float32Array, ...).