Colore JS

Search documentation

Find a function or jump to a section.
v2.0.1

Color object output

Read numeric channels when you need values for a color picker, renderer or calculation. Use toObject(space) to keep the space with the channels, or toArray(space) when the caller already knows the channel order.

Example

import { colore } from 'colore-js';

const blue = colore('#2563eb');
console.log(blue.toRgb()); // { r: 37, g: 99, b: 235, alpha: 1 }
console.log(blue.toOklch().l.toFixed(3)); // '0.546'

Syntax

MethodReturns
toRgb(){ r, g, b, alpha } with RGB channels rounded to integers
toHsl(), toHsv(), toHwb()Hue and the named percentage channels, with alpha
toLab(), toLch(), toOklab(), toOklch(), toXyz(), toP3()Named channels for the requested space and alpha
toObject(space?)A TaggedColorObject you can pass back to Colore
toArray(space?)An array of coordinates followed by alpha

Parameters

NameTypeDescription
spaceColorSpace | 'srgb-linear'Defaults to 'srgb'. Encoded sRGB and P3 use 0–1, while 'srgb-linear' gives linear-light sRGB coordinates.

Throws

A ColorError if the space name is unsupported.

Keep an exact copy for later calculations

import { colore } from 'colore-js';

const blue = colore('#2563eb');
const saved = blue.toObject('srgb-linear');
console.log(colore(saved).equals(blue, 0)); // true

Numeric conversions can produce out-of-range values. For example, P3 red gives an sRGB red channel above 255 through toRgb(). Use toGamut() if the caller requires an in-gamut result.