Skip to content

hslToCss

Formats an HSL(A) color as a CSS hsl() value, in the modern space-separated syntax.

The hue is wrapped into 0-360, saturation and lightness are clamped to 0-100, all three rounded to 1 decimal place; alpha is clamped to 0-1, rounded to 3 decimals, and left out when the color is opaque.

Available since v3.1.5

import { hslToCss } from '@helpers4/color';
// or, from the all-in-one package (same code, one install):
import { hslToCss } from 'helpers4/color';
hslToCss(color: HslColor): string
ParameterTypeDescription
colorHslColorThe color to format. `a` defaults to 1 (opaque) when omitted.

string — hsl(h s% l%), or hsl(h s% l% / a) when translucent

The alpha is left out.

hslToCss({ h: 210, s: 100, l: 50 }) // => 'hsl(210 100% 50%)'

The hue is wrapped into 0-360 and the alpha comes after a slash.

hslToCss({ h: -30, s: 80, l: 40, a: 0.25 }) // => 'hsl(330 80% 40% / 0.25)'

An HSL(A) color with channels expressed as plain numbers.

interface HslColor {
  a?: number;
  h: number;
  l: number;
  s: number;
}

View source on GitHub