Use OKLCH in JavaScript
toOklch('#2563eb') returns oklch(0.54615 0.21521 262.88). Once a color is in OKLCH you can move its lightness and keep its hue, which is what you want for a hover color: an 8% move toward white turns #2563eb into #306ff8.
npm install colore-js
yarn add colore-js
Convert a hex color to OKLCH
Use toOklch to write the color as CSS. The three values represent lightness, chroma and hue. Chroma is the color’s intensity, and hue is its angle on the color wheel.
import { toOklch } from 'colore-js/fn';
console.log(toOklch('#2563eb')); // 'oklch(0.54615 0.21521 262.88)'
If a picker or a calculation needs numbers, call toOklch() on a Color object. The standalone function above returns a CSS string.
import { colore } from 'colore-js';
colore('#2563eb').toOklch();
// { l: 0.5461497233109911, c: 0.21520774805039064, h: 262.88091714087415, alpha: 1 }
Make the hover state lighter
lighten takes a percentage from 0 to 100. Pass 8 for 8%. This moves the current lightness 8% of the distance toward white. If your design specifies a lightness of exactly 0.7, use setOklch instead.
import { lighten, setOklch } from 'colore-js/fn';
console.log(lighten('#2563eb', 8)); // '#306ff8'
console.log(setOklch('#2563eb', { l: 0.7 })); // 'oklch(0.70000 0.21521 262.88)'
The exact-lightness result is wider than sRGB and Display P3, so Colore writes OKLCH. For an older renderer that accepts only hex, decide where to map the color before you export.
Mix a second color in OKLCH
Choose the mixing space to control the result between two colors. Here mix goes 40% of the way from brand blue to teal. The ratio is 0–1, so the same value can come directly from a slider.
import { mix } from 'colore-js/fn';
console.log(mix('#2563eb', '#0f766e', 0.4, 'oklch')); // 'oklch(0.53206 0.16355 232.29)'
console.log(mix('#2563eb', '#0f766e', 0.4, 'srgb')); // '#1c6bb9'
Use the OKLCH mix when you want to work along the hue and chroma of the colors. Choose sRGB when you need to match an existing sRGB blend. If the accent needs to sit on white or carry a label, check the contrast for that use before settling on either result.
Put the colors into a theme
For several operations on the same input, create one Color and export at the end. This gives you a consistent source for the default, hover and accent colors. The stylesheet keeps the original blue in hex and writes the wider mix in OKLCH.
import { colore } from 'colore-js';
const brand = colore('#2563eb');
const hover = brand.lighten(8);
const accent = brand.mix('#0f766e', 0.4, 'oklch');
const css = `:root {
--brand: ${brand.css()};
--brand-hover: ${hover.css()};
--accent: ${accent.css()};
}`;
console.log(css);
:root {
--brand: #2563eb;
--brand-hover: #306ff8;
--accent: oklch(0.53206 0.16355 232.29);
}
Convert OKLCH back to hex
Use toHex when a design token, email template or older tool needs hex. Colors inside sRGB convert without clipping. The accent from the mix above is outside sRGB, so hex can't hold it. You can clip it, which is what toHex does, or map it into sRGB with toGamut first, which keeps its lightness and hue closer and gives up some chroma.
import { colore } from 'colore-js';
import { toHex } from 'colore-js/fn';
const accent = 'oklch(0.53206 0.16355 232.29)';
toHex(accent); // '#0078ba', clipped
colore(accent).toGamut('srgb').hex(); // '#0077ab', gamut-mapped
colore(accent).isInGamut('srgb'); // false
Check contrast against the hex value if that's what your renderer will display. To compare a shade's hex and OKLCH values, open the color in the palette generator.
How Colore handles wide colors
Colore keeps numeric channels through a chain of edits and picks a CSS format based on the result’s gamut, so a wide color isn't clipped until you ask for hex.
Related
- Gamut and CSS output for the format rules and a mapped fallback
- Check color contrast before you use the mix for text
- Generate Tailwind CSS v4 colors for a full brand scale