Sub topic 14
HSV and HSL: hue, saturation, and value
This chapter assumes you know the additive RGB model, 8-bit channels, and basic NumPy. HSV and HSL describe the same colours as RGB in a different set of numbers. They are not a different physical model.
Why a second description of RGB
RGB suits displays, because a screen has red, green, and blue emitters. It suits people badly. Suppose you want "the same colour, but paler". In RGB you have to change all three channels in a coordinated way. In HSV and HSL the question maps to one number.
Both spaces are rearrangements of the RGB cube into a cylinder-like shape:
- Hue is the angle around the colour wheel, from 0° to 360°. Red is 0°, green is 120°, blue is 240°, and the angles wrap around.
- Saturation is how far a colour is from grey, from 0 (grey) to 1 (fully vivid).
- Value (HSV) or lightness (HSL) is how bright the colour is.
Because the conversion is a fixed formula, HSV and HSL inherit the interpretation of the RGB values you feed in. If those values are sRGB, the result is "HSV of sRGB". Another RGB space gives different hue angles for the same pixel numbers.
How the conversion works
Scale R, G, B to the range 0 to 1. Let M be the largest of the three, m the smallest, and C = M − m (the chroma, or spread).
- Hue. If C = 0, the pixel is grey and hue is undefined (libraries return 0). Otherwise:
- If M is red: H = 60 × (((G − B) / C) mod 6)
- If M is green: H = 60 × ((B − R) / C + 2)
- If M is blue: H = 60 × ((R − G) / C + 4)
- Value (HSV). V = M.
- Saturation (HSV). S = C / M, or 0 when M = 0.
- Lightness (HSL). L = (M + m) / 2.
- Saturation (HSL). S = C / (1 − |2L − 1|), or 0 when L is 0 or 1.
Hue depends only on which channel is largest and on the ratio between the other two. That is why hue stays stable when you scale a colour darker or lighter.
HSV versus HSL
| Property | HSV | HSL |
|---|---|---|
| Third component | Value = max channel | Lightness = mean of max and min |
| Pure red (255, 0, 0) | V = 1.0 | L = 0.5 |
| White (255, 255, 255) | S = 0, V = 1 | S = 0, L = 1 |
| Pastel red (255, 170, 170) | S = 0.33, V = 1.0 | S = 1.0, L = 0.83 |
| Typical use | Colour pickers, segmentation by colour | CSS colours, UI design tools |
In HSV, full-brightness colours all sit at V = 1, and white is the corner where S = 0. In HSL, the vivid colours sit at L = 0.5, and L = 1 is always white. HSL therefore treats "pastel" as fully saturated but light, while HSV treats it as desaturated but bright. Both describe the same pixel.
Worked example
Python's standard library does the conversion. The order of its return values differs between the two functions, which is a common source of bugs.
import colorsys
samples = [
("red", (255, 0, 0)),
("orange", (255, 128, 0)),
("grey", (128, 128, 128)),
("dark red", (128, 0, 0)),
("pastel", (255, 170, 170)),
]
for name, rgb in samples:
r, g, b = (c / 255 for c in rgb)
h, s, v = colorsys.rgb_to_hsv(r, g, b) # returns H, S, V
h2, l, s2 = colorsys.rgb_to_hls(r, g, b) # returns H, L, S (note the order)
print(f"{name:9} HSV=({h*360:5.1f}, {s:.2f}, {v:.2f}) "
f"HSL=({h2*360:5.1f}, {s2:.2f}, {l:.2f})")The script prints these values:
| Colour | HSV (H°, S, V) | HSL (H°, S, L) |
|---|---|---|
| red | 0.0, 1.00, 1.00 | 0.0, 1.00, 0.50 |
| orange | 30.1, 1.00, 1.00 | 30.1, 1.00, 0.50 |
| grey | 0.0, 0.00, 0.50 | 0.0, 0.00, 0.50 |
| dark red | 0.0, 1.00, 0.50 | 0.0, 1.00, 0.25 |
| pastel | 0.0, 0.33, 1.00 | 0.0, 1.00, 0.83 |
Dark red and red share hue and HSV saturation. Only V changes. Grey has S = 0, so its hue of 0 carries no meaning.
Using HSV in OpenCV
OpenCV is the most common place you meet HSV in computer vision. It stores 8-bit hue in 0 to 179, which is the angle divided by 2, so that it fits in a uint8. Saturation and value use 0 to 255.
import cv2
import numpy as np
# One row, two pixels, in OpenCV's default BGR order: red and green
bgr = np.array([[[0, 0, 255], [0, 255, 0]]], dtype=np.uint8)
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
print(hsv)
# [[[ 0 255 255]
# [ 60 255 255]]]Green is 120° in the usual convention and appears as 60 here.
Common errors
- Hue wraps around. Red sits at both 0° and 360°. Averaging 350° and 10° gives 180° (cyan), not red. Compare hues with a circular distance, and select red with two ranges (for example 0 to 10 and 170 to 179 in OpenCV 8-bit):
def hue_distance(h1, h2):
d = abs(h1 - h2) % 360
return min(d, 360 - d)
print(hue_distance(350, 10)) # 20- Mixed-up scales. Python's
colorsysuses 0 to 1 for everything. OpenCV 8-bit uses 0 to 179 for hue, and OpenCV float32 images use 0 to 360. CSS uses degrees and percentages. Check the scale of every library you call. - Channel order. Convert with the constant that matches your array,
COLOR_BGR2HSVorCOLOR_RGB2HSV. Using the wrong one swaps red and blue, which moves every hue. - Unstable hue at low saturation. Near grey, tiny noise in the RGB values swings the hue by large angles. Dark pixels (low V) behave the same way. Filter by S and V before trusting H.
- Value is not perceived brightness. Pure yellow (255, 255, 0) and pure blue (0, 0, 255) both have V = 1. Their luma, using the weights 0.299, 0.587, and 0.114, is about 226 and 29. Do not use V where you need brightness as people see it.
- No gamma correction. The formulas run on the stored, gamma-encoded values. Changing V by 10% does not change the emitted light by 10%.
Exercise
A pixel has RGB values (200, 100, 50) in 8-bit sRGB. Compute its HSV hue, saturation, and value, with hue in degrees and S and V in the range 0 to 1.
Show answer
Scale to 0-1: R = 0.784, G = 0.392, B = 0.196. M = 0.784 (red), m = 0.196, C = 0.588. Hue = 60 × (((G − B) / C) mod 6) = 60 × (0.196 / 0.588) = 60 × 0.333 = 20°. V = M = 0.784. S = C / M = 0.588 / 0.784 = 0.75. The HSV result is (20°, 0.75, 0.78), an orange-brown.