Sub topic 14
HSV and HSL: Hue, Saturation, and Value
Assume you know that an image can store RGB channel values and that Python arrays contain numbers. This chapter uses those ideas to explain two alternative colour models: HSV and HSL.
Why use HSV or HSL?
RGB describes colour as mixtures of red, green, and blue light. That matches many display devices, but it is not always convenient for editing or selecting colours. HSV and HSL separate a colour's general type from its strength and brightness.
- Hue is the colour's position around a circle, usually measured from 0° to 360°. Red is near 0°, green near 120°, and blue near 240°.
- Saturation describes how much colour is present. Zero saturation is grey.
- Value in HSV describes the largest RGB component. A value of zero is black.
- Lightness in HSL places the colour between black and white. A lightness of 50% is the middle of that range.
HSV uses value, while HSL uses lightness. They are related, but they are not interchangeable.
HSV versus HSL
HSV is often pictured as a cylinder or cone. At the centre, saturation is zero, so every hue produces a grey value. Increasing saturation moves away from the centre. Increasing value moves toward a brighter colour.
HSL is often pictured as a double cone. Lightness 0% is black, 100% is white, and 50% contains the strongest versions of many hues. Therefore, an HSL lightness of 50% does not mean the colour has half the visible brightness.
| Model | Brightness component | Common use |
|---|---|---|
| HSV | Value, the maximum RGB component | Colour pickers and simple thresholding |
| HSL | Lightness, the midpoint of minimum and maximum RGB | UI colour adjustments |
Converting colours in Python
Python's colorsys module uses values from 0.0 to 1.0. Its HSL-like functions are named hls because they use the order hue, lightness, saturation.
import colorsys
rgb = (0.2, 0.6, 0.9)
h, s, v = colorsys.rgb_to_hsv(*rgb)
h2, l, s2 = colorsys.rgb_to_hls(*rgb)
print("HSV:", round(h * 360, 1), round(s, 3), round(v, 3))
print("HSL:", round(h2 * 360, 1), round(s2, 3), round(l, 3))
recovered_rgb = colorsys.hsv_to_rgb(h, s, v)
print("Recovered RGB:", tuple(round(x, 3) for x in recovered_rgb))For image processing, convert each pixel independently, then apply a rule such as selecting pixels with a hue near green. Remember that hue wraps around: values near 0° and 360° are both red.
Exercise
Why can HSV value and HSL lightness give different results for the same RGB pixel?
Show answer
HSV value is the largest RGB component, while HSL lightness is the midpoint of the smallest and largest components.