Designers pick colors in hue, saturation and lightness, and browsers and image files use red, green and blue. The RGB to HSL converter takes the red, green and blue values from 0 to 255 and gives the hue, the saturation and the lightness in HSL, and the saturation and value in HSV.
How the calculation works
- Divide each channel by 255 to get values between 0 and 1. Let max and min be the largest and smallest, and delta = max − min.
- Lightness (HSL) = (max + min) ÷ 2. Saturation (HSL) = delta ÷ (1 − |2 × lightness − 1|), or 0 for a gray.
- Hue depends on the largest channel. If it is red, hue = 60 × ((green − blue) ÷ delta). If green, 60 × (2 + (blue − red) ÷ delta). If blue, 60 × (4 + (red − green) ÷ delta). Add 360 if the result is negative.
- HSV: value = max and saturation = delta ÷ max. HSL is used in CSS as hsl(hue, saturation%, lightness%).
Example
Take rgb(52, 152, 219), the blue #3498DB. The channels are 0.204, 0.596 and 0.859. The largest is blue (0.859) and the smallest is red (0.204), so delta = 0.655. Lightness is (0.859 + 0.204) ÷ 2 = 53.1% and saturation is 0.655 ÷ (1 − 0.063) = 69.9%. The hue is 60 × (4 + (0.204 − 0.596) ÷ 0.655) = 204.1°. In CSS this is hsl(204, 70%, 53%).
Why it matters
HSL makes it simple to build a palette: keep the hue and change the lightness for shades, or turn the hue for related colors. It is used in CSS, design tools and image editing, and converting from a hex code or an RGB value gives you the starting point.
Good to know
The conversion is exact for the sRGB values you enter, but the results are rounded for display. The hue wraps around at 360 degrees, so 359 degrees and 1 degree are close. HSL saturation and lightness are not perceptually uniform: two colors with the same lightness can look very different in brightness, which is why contrast must be checked on the real colors.
Related tools
The hair color mix calculator blends shades in a different way and the resistor color code calculator is another color-based tool.