Sub topic 6
From RGB to a designer-friendly description
This chapter assumes you know that screens mix red, green, and blue light, and that you can read short Python and CSS snippets. You should also have met hue, saturation, and value as ideas from the earlier chapters. Here you will see how a color stored as RGB numbers becomes a description you can reason about and edit.
Why RGB is hard to edit by hand
RGB is the format screens use. A color is three channel values, usually integers from 0 to 255, for the red, green, and blue light a pixel emits. The values mix additively: more light of every channel moves the color toward white.
RGB suits hardware but not designers. Take this blue:
rgb(51, 102, 204) which is #3366ccSuppose you want a lighter version of the same blue. You cannot tell from the numbers which channel to change, or by how much. Suppose you want a duller version. That also changes several channels at once. The three numbers do not line up with the three questions a designer asks:
- Which color family is this? (hue)
- How pure or gray is it? (saturation)
- How light or dark is it? (value)
Each of those answers is spread across all three channels. A designer-friendly model recomputes the same color so that each question has its own number.
The relationships hidden in the channels
You can read the three properties out of RGB by comparing the channels with one another. Three patterns matter:
- The largest channel sets the brightness. The brighter the strongest channel, the lighter the color is in the HSV sense of value.
- The gap between the largest and smallest channel sets the purity. If all three channels are equal, there is no gap and the color is a gray. A bigger gap means a more saturated color.
- Which channel is largest, and how the other two compare, sets the hue. Red-dominant colors sit near the red end of the wheel, green-dominant near green, and blue-dominant near blue. The ratio of the other two channels slides the hue between those anchors.
| RGB | Largest channel | Gap (max − min) | What it tells you |
|---|---|---|---|
| (128, 128, 128) | any (all equal) | 0 | Gray. Hue has no meaning. |
| (255, 0, 0) | red | 255 | Pure red, fully saturated. |
| (51, 102, 204) | blue | 153 | A blue of medium-high saturation. |
| (180, 190, 220) | blue | 40 | A pale, grayish blue. |
The last row shows why a large channel value alone does not mean a vivid color. The channels are all high, but they are close together, so the color is pale and low in saturation.
The conversion step by step
The RGB-to-HSV conversion follows directly from those three patterns. Work with channels scaled to the range 0 to 1, then let max and min be the largest and smallest channel and delta = max − min.
- Value:
V = max - Saturation:
S = delta / max, or 0 whenmaxis 0 - Hue: depends on which channel is
max, and is measured in degrees on the wheel.
if max is red: H = 60 × (((G − B) / delta) mod 6)
if max is green: H = 60 × ((B − R) / delta + 2)
if max is blue: H = 60 × ((R − G) / delta + 4)
if delta is 0: H = 0 (undefined; any value works)The constants 0, 2, and 4 place red, green, and blue at 0°, 120°, and 240° on the wheel. The fraction then moves the hue up to 60° either side of that anchor, depending on the second-largest channel.
A worked conversion in Python
This function implements the formulas above and checks them against the standard library's colorsys module. Note that colorsys works with all values from 0 to 1, including hue, so multiply its hue by 360 to get degrees.
import colorsys
def rgb_to_hsv(r, g, b):
"""r, g, b in 0-255. Returns hue in degrees, saturation and value in 0-1."""
r, g, b = r / 255, g / 255, b / 255
mx, mn = max(r, g, b), min(r, g, b)
delta = mx - mn
if delta == 0:
h = 0.0
elif mx == r:
h = 60 * (((g - b) / delta) % 6)
elif mx == g:
h = 60 * ((b - r) / delta + 2)
else:
h = 60 * ((r - g) / delta + 4)
s = 0.0 if mx == 0 else delta / mx
return h, s, mx
h, s, v = rgb_to_hsv(51, 102, 204)
print(round(h), round(s, 2), round(v, 2)) # 220 0.75 0.8
ch, cs, cv = colorsys.rgb_to_hsv(51 / 255, 102 / 255, 204 / 255)
print(round(ch * 360), round(cs, 2), round(cv, 2)) # 220 0.75 0.8Both calls agree: the blue has a hue of 220°, a saturation of 0.75, and a value of 0.8. Now the numbers answer the designer's questions directly. It is a blue, fairly pure, and fairly light.
Editing in the friendly space, then converting back
The payoff comes when you edit one property and leave the others alone. Screens still need RGB, so you convert back at the end.
import colorsys
def hsv_to_hex(h, s, v):
"""h in degrees, s and v in 0-1. Returns a hex string for CSS."""
r, g, b = colorsys.hsv_to_rgb(h / 360, s, v)
return "#{:02x}{:02x}{:02x}".format(round(r * 255), round(g * 255), round(b * 255))
print(hsv_to_hex(220, 0.75, 0.8)) # #3366cc, the original blue
# Same hue, half the saturation: a muted blue
muted = hsv_to_hex(220, 0.75 * 0.5, 0.8)
# Same hue and saturation, lower value: a darker blue
darker = hsv_to_hex(220, 0.75, 0.4)
print(muted, darker)Each edit is one multiplication on one number. Doing the same edits in RGB would mean working out three new channel values for each result.
The same color in CSS
CSS can express the color in either notation, so you rarely need to convert by hand in a stylesheet. CSS offers HSL rather than HSV, which the next chapter compares in detail. For now, treat HSL as a close cousin whose saturation and lightness numbers differ from HSV's.
| Notation | Value |
|---|---|
| Hex | `#3366cc` |
| RGB | `rgb(51 102 204)` |
| HSL | `hsl(220 60% 50%)` |
.button {
background: hsl(220 60% 50%);
}
.button:hover {
background: hsl(220 60% 40%); /* same hue and saturation, darker */
}Only the third number changes between the two rules, and that is the whole point of the conversion.
Common errors
- Lightening by adding to every channel. Adding 40 to each of (51, 102, 204) gives (91, 142, 244). The hue stays at 220°, but saturation drops from 0.75 to about 0.63 because the gap stays the same while the maximum grows. Near 255 the channels also clip, which bends the hue. Edit value or lightness instead.
- Mixing up scales.
colorsysuses 0 to 1 for hue. CSS uses degrees, and a bare number inhsl()means degrees. Multiply or divide by 360 at the boundary. - Reading the wrong order from
colorsys. The function for HSL isrgb_to_hls, and it returns hue, lightness, then saturation. It does not return hue, saturation, lightness. - Trusting the hue of a gray. When
deltais 0, hue is arbitrary. Code that sorts or groups colors by hue will scatter grays unpredictably unless it handles them separately. - Forgetting that hue wraps. 359° and 1° are neighbors. Averaging them as plain numbers gives 180°, which is the opposite side of the wheel. Use modular arithmetic.
- Assuming equal numbers look equal. Two colors with the same value can look very different in brightness, for example a yellow and a blue. This is a perception issue that a later chapter covers.
Exercise
Convert the red rgb(204, 51, 51) to hue, saturation, and value by hand, using the formulas in this chapter. What is the result?
Show answer
Scale to 0 to 1: R = 0.8, G = 0.2, B = 0.2. The largest channel is red, so max = 0.8, min = 0.2, and delta = 0.6. Value = 0.8. Saturation = 0.6 / 0.8 = 0.75. Hue = 60 × (((0.2 − 0.2) / 0.6) mod 6) = 0°. The result is hsv(0°, 0.75, 0.8), a red with the same saturation and value as the chapter's blue.
Exercise
A designer lightens rgb(51, 102, 204) by adding 40 to each channel and gets rgb(91, 142, 244). Without a tool, say what happened to hue, saturation, and value, and name a better way to lighten the color.
Show answer
The hue stays at 220°, because the gaps between channels are unchanged. Value rises from 0.8 to about 0.96 (244 / 255). Saturation falls from 0.75 to about 0.63 (153 / 244), so the color looks paler than just lighter. A better way is to convert to HSV or HSL, raise only value or lightness, and convert back, so saturation and hue stay under control.