Colore JS

Search documentation

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

Gamut and CSS output

Not every color fits on every screen. An OKLCH or Display P3 color can be more vivid than sRGB, the range older displays and hex codes cover. css() chooses a CSS format based on the color’s gamut. Use toGamut() when your output has to fit a narrower gamut.

Let Colore choose the format

import { colore } from 'colore-js';

colore('#7c3aed').css();                         // '#7c3aed'
colore('color(display-p3 0.95 0.25 0.3)').css(); // 'color(display-p3 0.95000 0.25000 0.30000)'
colore('oklch(0.7 0.35 20)').css();              // 'oklch(0.70000 0.35000 20.00)'

css() and toCss() from colore-js/fn follow one rule:

  1. The color fits sRGB: hex.
  2. It fits Display P3: color(display-p3 …).
  3. Neither: oklch(…).

Standalone transforms such as lighten() and mix() use the same rule. For a fixed format, choose an output function like toHex().

Choose the format yourself

Use a fixed method when an API, a test or a file format needs one format:

MethodOutput
hex(), rgb(), hsl()sRGB formats. Colors outside sRGB are clipped.
p3()color(display-p3 …)
oklch(), oklab()Perceptual formats that hold any color.

Object outputs such as toRgb(), toObject() and toArray() are not clipped: wide-gamut colors can have values outside the usual 0–255 range.

Map a color into sRGB

Clipping cuts each channel off at its limit, which can shift the hue. toGamut() uses the CSS Color 4 binary-search gamut mapping method. It reduces OKLCH chroma and uses a local clip when the remaining difference is small.

import { colore } from 'colore-js';

const red = colore('oklch(0.7 0.35 20)');

red.hex();                 // clipped: '#ff0032'
red.toGamut('srgb').hex(); // mapped:  '#ff5464'

The mapped color passes isInGamut() for that gamut and keeps its alpha.

Shades from palette and generateSystem already fit sRGB, so they don't need this step.

Check the gamut first

import { colore } from 'colore-js';

const color = colore('oklch(0.68 0.22 300)');

color.isInGamut('srgb'); // false
color.isInGamut('p3');   // false