Colore JS

Search documentation

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

Color static methods

Use a static Color method to create a color from separate channels. A picker can pass its numbers directly, and you can format the result for CSS afterwards.

Example

import { Color } from 'colore-js';

console.log(Color.rgb(37, 99, 235).hex()); // '#2563eb'
console.log(Color.oklch(0.62, 0.17, 250).oklch()); // 'oklch(0.62000 0.17000 250.00)'

Syntax

MethodChannel arguments
Color.rgb(r, g, b, alpha?)sRGB channels 0–255
Color.hsl(h, s, l, alpha?), Color.hsv(h, s, v, alpha?), Color.hwb(h, w, b, alpha?)Hue in degrees and the other channels in 0–100
Color.lab(l, a, b, alpha?), Color.lch(l, c, h, alpha?)Lab or LCH, with lightness 0–100
Color.oklab(l, a, b, alpha?), Color.oklch(l, c, h, alpha?)OKLab or OKLCH, with lightness 0–1
Color.p3(r, g, b, alpha?)Encoded Display P3 channels, usually 0–1
Color.try(input)A ColorInput or Color
Color.interpolate(colors, t, space?)A list of colors, a position 0–1 and an optional interpolation space
Color.fromCssMix(expression)A supported CSS color-mix() expression

Returns

A Color. Color.try() returns null when Colore cannot read the input.

Throws

Channel constructors and strict parsing methods throw a ColorError for invalid arguments. Opacity defaults to 1. Check color spaces and ranges when you pass coordinates from another tool.

Accept a user-supplied color

Let someone enter their own accent color and keep a default when the field is empty or invalid.

import { Color } from 'colore-js';

const accent = Color.try('var(--accent)') ?? Color.rgb(37, 99, 235);
console.log(accent.hex()); // '#2563eb'