Colore JS

Search documentation

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

Color inputs and detection

A stylesheet gives you strings. A color picker may give you numbers. Colore accepts both, as long as it knows which space and units your numbers use.

Pass a CSS color or a packed integer

Colore accepts hex, rgb(), hsl(), hwb(), lab(), lch(), oklab(), oklch(), color(srgb …), color(display-p3 …), named CSS colors, transparent and supported color-mix() expressions. An integer packs red, green and blue into 0xRRGGBB.

import { colore } from 'colore-js';

console.log(colore('rebeccapurple').hex()); // '#663399'
console.log(colore(0x2563eb).hex()); // '#2563eb'

Label the units you pass

Plain { r, g, b } objects use RGB channels from 0 to 255. Add space: 'srgb' for encoded 0–1 channels. The space: 'srgb-linear' tag means linear-light channels instead. Use alpha to represent opacity.

import { colore } from 'colore-js';

const color = colore({ space: 'srgb', r: 0.5, g: 0.25, b: 1 });
console.log(color.hex()); // '#8040ff'

Check color spaces and ranges for the other object tags.

Check a string before using it

detectFormat recognizes the syntax. isValidColor also checks the contents. Use parse for numeric channels or Color.try to get a Color with a fallback path.

import { detectFormat, isValidColor } from 'colore-js';

console.log(detectFormat('rgb(12)')); // 'rgb'
console.log(isValidColor('rgb(12)')); // false