Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 50 ยท Color

HSV, HSL, Gamma and Perceptual Color

Learn HSV and HSL color models, conversion code, gamma correction, sRGB versus linear light, luminance and contrast ratios, with a live converter.

RGB is perfect for hardware and awkward for people. Ask a designer to make a color "a little lighter and less saturated" and there is no RGB slider that does it; you have to change all three numbers at once. HSV and HSL reorganise the same colors into hue, saturation and a brightness-like value, which matches how we talk about color. They are still just a different way of writing RGB, and conversion is cheap.

There is a second, deeper issue. RGB numbers are not proportional to the amount of light. Blending, scaling and shading performed on them directly give subtly wrong results unless you understand gamma. This lesson covers both ideas: the human-friendly models, and the correct way to do arithmetic on colors.

Hue, saturation and value

Both models share the first component:

  • Hue (H) is the position on the color wheel, measured in degrees: 0 is red, 60 yellow, 120 green, 180 cyan, 240 blue, 300 magenta, and 360 is red again.
  • Saturation (S) is how pure the color is, from 0 (a grey) to 1 (fully vivid).
  • The third component describes brightness, in two flavours.

HSV (also called HSB) has Value (V): the strength of the brightest channel. At V = 1 and S = 1 you get a pure, bright color; lowering S mixes in white; lowering V mixes in black. HSL has Lightness (L): 0 is black, 1 is white, and the pure colors sit at L = 0.5. In HSL, increasing lightness above 0.5 fades toward white, below 0.5 toward black, which makes "lighter" and "darker" variants of a color natural to compute (many CSS design systems are built this way).

flowchart LR
  RGB["RGB<br/>3 light intensities"] -->|"max, min, difference"| HSV["HSV<br/>hue, saturation, value"]
  RGB -->|"max, min, average"| HSL["HSL<br/>hue, saturation, lightness"]
  HSV -->|"chroma and sector"| RGB
  HSL -->|"chroma and sector"| RGB

HSV and HSL are different views of the same RGB cube; hue is computed the same way in both.

Converting RGB to HSV

Work with R, G and B in the range 0 to 1. Let max and min be the largest and smallest channel, and d = max - min (the chroma).

TEXT
V = max
S = (max == 0) ? 0 : d / max
H = 0                                  if d == 0 (a grey: hue is undefined)
H = 60 * (((G - B) / d) mod 6)         if max == R
H = 60 * ((B - R) / d + 2)             if max == G
H = 60 * ((R - G) / d + 4)             if max == B

For HSL keep the same H, and use L = (max + min) / 2 and S = d / (1 - |2L - 1|) (or 0 when d == 0).

Converting HSV to RGB

TEXT
C = V * S                              (chroma)
X = C * (1 - |(H / 60) mod 2 - 1|)
m = V - C
(R', G', B') = (C, X, 0) for H in [0, 60),  (X, C, 0) for [60, 120),
               (0, C, X) for [120, 180),    (0, X, C) for [180, 240),
               (X, 0, C) for [240, 300),    (C, 0, X) for [300, 360)
(R, G, B) = (R' + m, G' + m, B' + m)

The hue picks one of six sectors on the color wheel, and X is the ramping channel inside that sector. Test with H = 0, S = 1, V = 1: C = 1, X = 0, m = 0, giving (1, 0, 0), pure red.

The converter below implements all of this. Drag the sliders and watch how HSL and HSV differ for the same color. It also shows the WCAG relative luminance and the contrast ratio against white and black text, explained at the end of the lesson.

HSV, HSL and contrast converter

Notice that with S = 100% and V = 100%, the HSL lightness is 50%, and that the HSV and HSL saturation values differ for the same color. They are different yardsticks, not the same quantity. Notice also how, for many mid-bright colors, only one of white and black text gives enough contrast.

Why RGB numbers are not light intensities

Here is a surprising fact. A pixel with value (128, 128, 128) does not emit half as much light as (255, 255, 255). It emits only about 22%. The reason is gamma encoding.

Human vision is far more sensitive to differences between dark tones than between bright ones. If you stored light intensity linearly in 8 bits, there would be too few steps in the shadows (visible banding) and wasted steps in the highlights. So images are stored with a curve that devotes more codes to the darks. The standard curve for the web is sRGB, which is close to raising the stored value to the power 2.2.

flowchart LR
  L["Linear light<br/>(physical energy)"] -->|"encode: roughly L^(1/2.2)"| S["Stored sRGB value<br/>(what is in the image file)"]
  S -->|"decode: roughly S^2.2"| D["Linear light again<br/>(what the screen emits)"]

The curve is applied when the image is saved, and undone by the display.

The exact sRGB transfer functions, for values between 0 and 1, are:

TEXT
decode (sRGB to linear):  c <= 0.04045 ?  c / 12.92  :  ((c + 0.055) / 1.055) ^ 2.4
encode (linear to sRGB):  c <= 0.0031308 ? c * 12.92 : 1.055 * c ^ (1 / 2.4) - 0.055

The straight line near black avoids an infinite slope at zero; the power part is close to gamma 2.2.

The common mistake: doing math on encoded values

Lighting, blending, blurring, scaling images, anti-aliasing coverage: all of these are physically meaningful only on linear light, because light adds up linearly. If you average an encoded black (0) and an encoded white (255) you get 127.5 and think "50% grey", but the pixel then emits only 21% of the white's light. The physically correct 50% mix is a linear value of 0.5, which must be encoded to about 188 before being stored.

The correct recipe for any computation on colors is:

  1. Decode the sRGB values to linear.
  2. Do the math (blend, light, average).
  3. Encode the result back to sRGB.

Graphics APIs help: WebGL and OpenGL have sRGB texture and framebuffer formats that perform the decode and encode in hardware.

The next demo shows the effect with your own eyes. In the first row, alternating black and white lines (which average to half the light) are shown next to two grey squares. View it at 100% browser zoom, and from a distance, so the lines blur together. The line pattern should match the square labelled 188, not the one labelled 128. The second row shows a black-to-white blend computed two ways.

Gamma: the 50% grey that is not 128

The upper strip in row 2 looks too dark in the middle compared with an even light ramp; the lower strip is the evenly stepped light output. The text shows the numbers: 128 emits about 21.6% of white's light, and a true 50% needs a stored value of 188.

Luminance, contrast and perceptual color

Because the eye is more sensitive to green than to red or blue, a color's perceived brightness is a weighted sum of the linear channels. The relative luminance used by accessibility standards (WCAG) is:

TEXT
Y = 0.2126 * R_lin + 0.7152 * G_lin + 0.0722 * B_lin

The contrast ratio between two colors with luminances Y1 >= Y2 is (Y1 + 0.05) / (Y2 + 0.05). White on black gives 21:1; the web guideline asks for at least 4.5:1 for normal text and 3:1 for large text. You used this in the converter above.

Even this is only an approximation of how people judge color. In RGB, HSV and HSL, equal numeric steps do not look equal: a pure yellow at L = 50% looks far brighter than a pure blue at the same L. Perceptual color spaces such as CIE Lab (and its modern successor Oklab) are designed so that distances and lightness correspond better to what we see. They are the right choice for building color scales, mixing colors that look smooth, and choosing accessible palettes. CSS now supports them directly (oklch(), lab()), and the next lesson on gradients, palettes and dithering shows where the choice of space changes the picture.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In HSV, which component is the position on the color wheel?

    Hue is an angle from 0 to 360 degrees: red at 0, green at 120, blue at 240.
  2. 2. What is the HSV value of a pure grey (R = G = B = 0.5)?

    A grey has no chroma (max equals min), so saturation is 0 and the hue means nothing; V is the largest channel, 0.5.
  3. 3. Why should blending and lighting normally be done on linear values, not on stored sRGB values?

    Encoded values are roughly the square root of light energy. Averaging them gives results that are too dark; decode first, compute, then encode.
  4. 4. About what stored sRGB value displays 50% of the light of white?

    Encoding 0.5 with the sRGB curve gives about 0.735, or 188 out of 255.
  5. 5. What does a contrast ratio of 4.5:1 mean for text?

    WCAG AA asks for at least 4.5:1 for normal text and 3:1 for large text; 7:1 is the stricter AAA level.

Summary

  • HSV and HSL re-express RGB as hue (an angle), saturation and value or lightness, which makes "lighter", "duller" and "rotate the hue" easy; conversion uses max, min and the chroma d = max - min.
  • sRGB values are gamma-encoded, roughly light raised to 1/2.2; 128 emits only about 22% of the light of 255.
  • Decode to linear light before blending, lighting, scaling or averaging, and encode the result when storing.
  • Relative luminance 0.2126 R + 0.7152 G + 0.0722 B (on linear values) gives contrast ratios; perceptual spaces like Oklab make color scales that look even.

Next, we put these ideas to work: Gradients, Palettes and Dithering.