Skip to content

hslToRgb

Converts an HSL(A) color into RGB(A).

Available since v3.0.0

import { hslToRgb } from '@helpers4/color';
// or, from the all-in-one package (same code, one install):
import { hslToRgb } from 'helpers4/color';
hslToRgb(color: HslColor): RgbColor
ParameterTypeDescription
colorHslColorThe color to convert. `h` is normalized modulo 360 (negative values wrap around), `s`/`l` are expected in 0-100, `a` defaults to 1 (opaque) when omitted.

RgbColor — The equivalent RGB(A) color, with r/g/b rounded to 0-255

Useful after generating a hue programmatically (e.g. evenly spaced chart colors).

hslToRgb({ h: 120, s: 100, l: 50 })
// => { r: 0, g: 255, b: 0, a: 1 }

Negative or out-of-range hues are normalized before conversion.

hslToRgb({ h: -360, s: 100, l: 50 })
// => { r: 255, g: 0, b: 0, a: 1 }  (same as h: 0)

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

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

An RGB(A) color with channels expressed as plain numbers, convenient for further conversion or manipulation.

interface RgbColor {
  a?: number;
  b: number;
  g: number;
  r: number;
}

View source on GitHub