parse
parse reads a CSS color string into color coordinates and alpha that you can pass between functions. The result has the same tagged linear sRGB shape for hex, OKLCH, Display P3 and other supported inputs.
Example
import { parse } from 'colore-js';
parse('rgb(255 0 0 / 50%)'); // {space: "srgb-linear", r: 1, g: 0, b: 0, alpha: 0.5}
Syntax
parse(input: string): ColorValue
Parameters
Returns
A ColorValue with space: 'srgb-linear', r, g, b and alpha. Channels are linear-light sRGB and can extend outside 0–1 for wide-gamut inputs. Alpha is between 0 and 1.
Throws
A ColorError if the input is empty, malformed or uses unsupported syntax. This includes currentColor, which requires element context.
Read a color once and pass it to another function
parse accepts hex, RGB, HSL, HWB, Lab, LCH, OKLab, OKLCH, named colors, transparent, color(srgb …), color(display-p3 …) and supported color-mix() expressions. Store the parsed value for code that needs the same color more than once.
import { parse } from 'colore-js';
import { toCss } from 'colore-js/fn';
const accent = parse('color(display-p3 0 1 0)');
accent.space; // 'srgb-linear'
toCss(accent); // 'color(display-p3 0.00000 1.00000 0.00000)'
Use parseToSpace when you need the input’s own channels, such as the chroma in oklch(). Use isValidColor for a boolean check on user input.
Related
- parseToSpace to read the source color space
- isValidColor to accept or reject input
- parseColorMix to accept only CSS mix expressions