Skip to content

rgbToHsl

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

h/s/l are rounded to 1 decimal place to avoid floating-point noise.

Available since next

import { rgbToHsl } from '@helpers4/color';
rgbToHsl(color: RgbColor): HslColor
ParameterTypeDescription
colorRgbColorThe color to convert. r/g/b are expected in 0-255, a defaults to 1 (opaque) when omitted.

HslColor — The equivalent HSL(A) color: h in 0-360, s/l in 0-100

Useful for generating tints/shades by adjusting lightness.

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

When r, g, and b are equal, the color is achromatic (s = 0).

rgbToHsl({ r: 128, g: 128, b: 128 })
// => { h: 0, s: 0, l: 50.2, a: 1 }

View source on GitHub