Colore JS

Search documentation

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

toCss

toCss writes a color as CSS and chooses the output format based on gamut. Use it for styles and tokens that can accept more than one CSS color syntax.

Example

import { colore } from 'colore-js';
import { toCss } from 'colore-js/fn';

colore('#7c3aed').css(); // '#7c3aed'
toCss('#7c3aed'); // '#7c3aed'

Syntax

colore(color).css(): string
toCss(color: ColorInput): string

Parameters

NameTypeDescription
colorColorInputAny supported CSS color, tagged color object, Color instance or packed RGB number.

Returns

Hex for colors within sRGB, color(display-p3 …) for colors that need Display P3, and oklch(…) for colors outside both. Each format has its own output rounding. toCss selects a format and leaves any gamut mapping to you.

Throws

A ColorError if the input color is invalid.

Keep a wide-gamut color in a stylesheet

Pass the edited color to toCss so it can keep a wider format when required. The two examples land on opposite sides of Display P3’s gamut boundary.

import { toCss } from 'colore-js/fn';

toCss('color(display-p3 0 1 0)'); // 'color(display-p3 0.00000 1.00000 0.00000)'
toCss('oklch(0.7 0.35 20)'); // 'oklch(0.70000 0.35000 20.00)'

Use a fixed serializer when the consumer requires a specific format. For a perceptually mapped hex fallback, call toGamut('srgb') before .hex().