toHsl
toHsl turns a color into CSS hsl() with a hue in degrees and saturation and lightness in percent. Use it for styles or tools that expect HSL.
Example
import { colore } from 'colore-js';
import { toHsl } from 'colore-js/fn';
colore('#0f766e').hsl(); // 'hsl(175 77% 26%)'
toHsl('#0f766e'); // 'hsl(175 77% 26%)'
Syntax
colore(color).hsl(): string
toHsl(color: ColorInput): string
Parameters
Returns
A CSS string with hue, saturation and lightness rounded to whole numbers. Translucent colors include alpha with up to four decimals. Colors outside sRGB are clipped first.
Throws
A ColorError if the input is invalid.
Read HSL channels for a color picker
Read the class object to keep the fractional hue, saturation and lightness. The standalone function gives you a formatted CSS string for displaying or exporting the value.
import { colore } from 'colore-js';
import { toHsl } from 'colore-js/fn';
toHsl('hsl(190.25 72.5% 40.25% / 50%)'); // 'hsl(190 73% 40% / 0.5)'
Number(colore('hsl(190.25 72.5% 40.25% / 50%)').toHsl().s.toFixed(2)); // 72.5
For a color that needs to remain wide-gamut, choose OKLCH or automatic CSS output. HSL is an sRGB format.
Related
- toOklch for perceptual lightness and chroma
- toCss for automatic format selection
- Object output methods for numeric channels