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
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'
Related
- Color constructor and inputs
- fromCssMix for CSS expressions
- interpolate for colors between stops