Sub topic 4
Channel values: 0 to 255 and 8-bit colour
This chapter assumes you know that RGB mixes red, green and blue light, and that you can read simple arithmetic. The code examples use JavaScript and Python, but you can follow the ideas without running them.
What a channel value is
An RGB colour is stored as three numbers, one for each channel: red, green and blue. Each number says how bright that light is. A low number means little light. A high number means a lot of light.
In most screen work each channel is an 8-bit number. A bit is a single 0 or 1. Eight bits can form 2^8 = 256 different patterns, so a channel can hold the whole numbers from 0 to 255.
- 0 means the channel is fully off.
- 255 means the channel is fully on.
- Values in between are partial brightness.
Because a channel holds 256 levels, a colour with three channels can take 256 × 256 × 256 = 16,777,216 values. This is often called "24-bit colour" (3 channels × 8 bits) or "16.7 million colours".
Reading a triple
A colour is written as three values in the order red, green, blue. Some examples:
| Colour | R | G | B |
|---|---|---|---|
| Black | 0 | 0 | 0 |
| White | 255 | 255 | 255 |
| Pure red | 255 | 0 | 0 |
| Orange | 255 | 136 | 0 |
| Mid grey | 128 | 128 | 128 |
When all three channels are equal, the result is a neutral grey. White is the brightest grey and black is the darkest. Orange is mostly red, some green and no blue.
CSS accepts the same triple directly. The two declarations below describe one colour, and #ff8800 is the hex form you will meet in the next chapter.
.button {
background-color: rgb(255 136 0);
border-color: #ff8800;
}Why 256 levels
The number 256 comes from how computers store data. A byte is 8 bits, and one byte per channel is a convenient fit. It was also close to what people can tell apart on typical displays in smooth gradients, so 8 bits became the default for the web and for most desktop images.
The range is a storage choice and not a physical limit. Other bit depths exist:
| Bit depth per channel | Levels per channel | Typical use |
|---|---|---|
| 8 | 256 | Web, apps, most photos |
| 10 | 1,024 | HDR video, some monitors |
| 16 | 65,536 | Photo editing, RAW processing |
Editing at 16 bits keeps more room for adjustments. Each change rounds values, and rounding errors add up. More levels mean smaller errors.
Banding: where 8 bits runs out
With only 256 levels, a very smooth gradient has to jump between neighbouring values. Where the gradient is long and subtle, such as a dark sky, you may see visible stripes. This is called banding.
You can cause it yourself. Brightening a dark 8-bit image stretches a small range of values over a larger one, and the gaps become visible. If you plan heavy adjustments, work in a higher bit depth and reduce to 8 bits at the end.
Converting between channel values and fractions
Other tools describe a channel as a fraction from 0 to 1 instead of 0 to 255. The conversion is a division or multiplication by 255.
def to_fraction(value_8bit):
return value_8bit / 255
def to_8bit(fraction):
return round(fraction * 255)
print(to_fraction(136)) # 0.5333333333333333
print(to_8bit(0.5)) # 128 (127.5 rounds to the even number in Python)Notice that 0.5 does not map to a whole number. 127.5 has to be rounded, and different tools round differently. This is one reason the same colour can differ by 1 between programs.
Common errors
- Values out of range. 256 and -1 are not valid 8-bit channel values. Clamp results of calculations to 0–255.
- Confusing 0–255 with 0–1. Some APIs, such as many graphics libraries, expect fractions. Passing 255 where 1 is expected gives a fully saturated, wrong colour.
- Assuming 128 is half the light. The number 128 is the midpoint of the code range, but it does not produce half the light. Channel values are non-linear, a topic covered in the sRGB and gamma chapter.
- Dropping the order. Swapping to BGR, which some libraries use, turns orange into a blue-ish colour.
A quick diagram of the idea
Summary
Each RGB channel is usually one byte, so it holds 0 to 255. Three channels give about 16.7 million colours. More bits per channel reduce banding and rounding error. Always check whether a tool expects 0–255 or 0–1.
Exercise
A colour is written as rgb(0 128 255). Describe it in words, then state how many distinct colours an 8-bit-per-channel image can represent in total.
Show answer
It has no red, about half of the green range and full blue, so it is a bright blue with a slight green tint, like an azure. An 8-bit-per-channel image can represent 256 × 256 × 256 = 16,777,216 colours.
Exercise
A library expects channel values from 0 to 1. Convert the 8-bit orange rgb(255 136 0) to that form, rounded to two decimals.
Show answer
Divide each channel by 255: red 255/255 = 1.00, green 136/255 ≈ 0.53, blue 0/255 = 0.00. The result is (1.00, 0.53, 0.00).