Colore JS

Search documentation

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

Color string output

Use css() to write a color to a stylesheet. Choose a specific method like hex() or oklch() when your output needs that format.

Example

import { colore } from 'colore-js';

const blue = colore('#2563eb80');
console.log(blue.hex()); // '#2563eb80'
console.log(blue.rgb()); // 'rgb(37 99 235 / 0.502)'

Syntax

MethodReturns
hex()Six-digit RGB hex, or eight-digit hex for alpha
rgb(), hsl(), hwb()The selected CSS function with sRGB channels
lab(), lch()CSS Lab or LCH coordinates
oklab(), oklch()CSS OKLab or OKLCH coordinates
p3()A color(display-p3 …) string
css(), toString(), toJSON()A CSS string chosen by gamut

Parameters

These methods take no arguments. For named numeric channels, use Color object output.

Returns

A string. css() uses hex when the color fits sRGB, Display P3 when it fits there, and OKLCH for wider colors. An explicit sRGB format such as hex() clips channels when the color is outside sRGB.

Keep a wide-gamut color in a stylesheet

import { colore } from 'colore-js';

const red = colore('color(display-p3 1 0 0)');
console.log(red.css()); // 'color(display-p3 1.00000 0.00000 0.00000)'
console.log(red.toGamut('srgb').hex()); // '#ff0b0c'

Use the CSS string on a surface that can display P3. Call toGamut('srgb') first when you need a hex fallback that stays closer to the original appearance.