Colore JS

Search documentation

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

toOklch

toOklch converts hex or another supported CSS color to an oklch() string. The result exposes lightness, chroma and hue and can represent colors beyond sRGB.

Example

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

colore('#0f766e').oklch(); // 'oklch(0.51094 0.08606 186.39)'
toOklch('#0f766e'); // 'oklch(0.51094 0.08606 186.39)'

Syntax

colore(color).oklch(): string
toOklch(color: ColorInput): string

Parameters

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

Returns

An oklch(l c h) string. Lightness and chroma use five decimal places, and hue uses two. A translucent color includes slash-separated alpha.

Throws

A ColorError if the input color is invalid.

Read OKLCH channels to edit lightness

The class method toOklch() gives you numbers without CSS serialization rounding. Save the CSS string when you need to write the color to a stylesheet.

import { colore } from 'colore-js';

const brand = colore('#0f766e');
Number(brand.toOklch().l.toFixed(3)); // 0.511
brand.setOklch({ l: 0.6 }).oklch(); // 'oklch(0.60000 0.08606 186.39)'

Lightness is expressed from 0 to 1. Chroma describes how vivid the color is, and hue is an angle in degrees.