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 are comfortable with binary and decimal numbers. Hex notation gets its own chapter, so here we only touch on it. We cover what a channel value is, why the range is 0 to 255, what bit depth means, and the errors that come from treating those numbers as more than they are.
What a channel value is
An RGB colour is stored as three numbers, one per channel. Each number says how much of that primary light the pixel emits. A value of 0 means that channel is off. The maximum value means it is fully on.
(0, 0, 0)is all channels off, which is black on a screen.(255, 255, 255)is all channels at maximum, which is white.(255, 0, 0)is only red, at full strength.(128, 128, 128)is a mid grey, with the caveat covered in the gamma section below.
A channel value is a code, not a physical measurement. The number 128 does not mean "128 units of light". It is one of a fixed list of steps between "off" and "full". How those steps map to real light output depends on the colour space, which later chapters cover.
Why 0 to 255
The range comes from bit depth, the number of binary digits (bits) used to store one channel. With n bits you can store 2^n different values.
| Bits per channel | Distinct values | Range | Total colours (3 channels) |
|---|---|---|---|
| 1 | 2 | 0 to 1 | 8 |
| 4 | 16 | 0 to 15 | 4,096 |
| 8 | 256 | 0 to 255 | 16,777,216 |
| 10 | 1,024 | 0 to 1023 | about 1.07 billion |
| 16 | 65,536 | 0 to 65535 | about 281 trillion |
Eight bits is one byte, and a byte holds 256 values. Counting from 0, the highest is 255. That is why you see 255 so often in colour work. The count of 256 values is the real quantity, and 255 is the largest code.
The total number of colours is the number of values per channel, cubed: 256 × 256 × 256 = 16,777,216. Designers often call 8 bits per channel "24-bit colour" because 3 channels × 8 bits = 24 bits per pixel. You may also see "32-bit colour". That is usually 24 bits of colour plus an 8-bit alpha channel for transparency, not extra colour precision.
Normalised values
Many tools do not use 0 to 255. Shaders, image-processing libraries and CSS functions often use a range of 0.0 to 1.0, or a percentage. The conversion is a division or multiplication by 255.
def to_unit(value_8bit):
return value_8bit / 255
def to_8bit(unit):
return round(unit * 255)
print(to_unit(136)) # 0.5333333333333333
print(to_8bit(0.5333)) # 136
print(to_8bit(0.5)) # 128 (127.5 rounds to 128 in this call; see note)Two details matter here. First, the 8-bit values map onto the unit range with 255 as the divisor, not 256, so that 255 maps to exactly 1.0. Second, rounding is a choice. Python's round uses banker's rounding (halves go to the nearest even number), so round(127.5) gives 128, but round(126.5) gives 126. Other languages round halves upward. If two tools round differently, the same colour can differ by one step.
CSS accepts all three forms. These declarations describe the same orange:
.swatch {
background-color: rgb(255 136 0);
border-color: rgb(100% 53.3% 0%);
color: color(srgb 1 0.533 0);
}The percentage and unit forms are not more precise than the integer form once the value is stored in an 8-bit pixel. The browser will quantise them to the nearest of 256 steps.
Quantisation: what 8 bits cannot store
Quantisation is the process of snapping a continuous value to the nearest available step. Storing a colour at 8 bits per channel means every value is rounded to one of 256 levels. Most of the time you cannot see the error, because the steps are small. There are two cases where you can.
The first is banding. A smooth gradient across a large area, such as a sky or a dark vignette, may use only a few dozen distinct values along its length. Neighbouring bands then differ by one step and the eye picks out the edges. The second is repeated editing. Each edit that rounds to 8 bits loses a little information, and many edits can compound.
You can see the step size by counting how many distinct grey levels a gradient has:
def distinct_levels(width_px):
"""Number of distinct 8-bit values in a black-to-white gradient of this width."""
values = {round(x / (width_px - 1) * 255) for x in range(width_px)}
return len(values)
print(distinct_levels(100)) # 100: narrower than 256 so every pixel differs
print(distinct_levels(1000)) # 256: wider than 256 so pixels must share valuesA full black-to-white ramp can never show more than 256 steps, however wide it is. A gradient between two close colours has far fewer. A ramp from (40, 40, 40) to (60, 60, 60) has only 21 distinct values, no matter how wide it is drawn.
Two common fixes exist. Dithering adds a small amount of noise so that neighbouring pixels alternate between two adjacent values, which hides the edges. Working at 16 bits per channel during editing keeps more precision until the final export to 8 bits.
Bit depth per channel versus per pixel
The term "bit depth" is used in two ways, and mixing them up causes confusion.
| Term | Meaning | Example |
|---|---|---|
| Bits per channel (bpc) | Precision of each of R, G and B | 8 bpc, 16 bpc |
| Bits per pixel (bpp) | Total storage for one pixel | 24 bpp for 8-bit RGB, 32 bpp with alpha |
Photo editors usually mean bits per channel when they say "8-bit" or "16-bit". Some file-format documentation means bits per pixel. When a spec says "24-bit", check which one it means. A 24-bit image with three channels is 8 bpc. A 24-bit grey image with one channel would be an unusual 24 bpc.
Higher depths appear in specific places:
- 10-bit panels and video formats, such as HDR video, use 1,024 levels per channel to reduce banding over a wide brightness range.
- 16-bit TIFF and PNG files are used for editing, since they survive many adjustments.
- Floating-point formats, such as OpenEXR, store values as decimals and can hold values above 1.0, which is useful for light that is brighter than white in 3D rendering.
Channel values are not proportional to light
An 8-bit value of 128 is not half as bright as 255. Most RGB images use sRGB encoding, which stores values on a curve so that more steps are given to dark tones, where the eye is more sensitive. The curve is roughly a power of 2.2.
def srgb_to_linear(value_8bit):
c = value_8bit / 255
if c <= 0.04045:
return c / 12.92
return ((c + 0.055) / 1.055) ** 2.4
print(srgb_to_linear(128)) # about 0.2158, not 0.5A stored 128 corresponds to about 22% of full light intensity. This matters in two situations. If you average two pixel values directly, such as (0 + 255) / 2 = 127.5, the result is not the colour you would get from mixing the two lights in equal amounts. That is why blurs and resizing done on raw sRGB values often look slightly too dark. For accurate blending, convert to linear light, do the arithmetic, and convert back. The sRGB chapter gives the full treatment. For now, remember that a channel value is a position on an encoding curve.
Common errors
- Treating 255 as the number of values. There are 256 values. Off-by-one mistakes appear when you build a lookup table with
range(255), which stops at 254. Userange(256). - Dividing by 256. Converting to the unit range with
/ 256means 255 maps to 0.996, not 1.0. White then comes out slightly grey. - Out-of-range values. Arithmetic can push a channel below 0 or above 255. Brightening
(200, 100, 50)by 80 gives(280, 180, 130). Clamp each channel to the valid range, or the value will wrap around or throw an error depending on the language. - Truncating instead of rounding. Using integer truncation turns 127.9 into 127. Over many operations this darkens the image slightly.
- Assuming a value is the same colour everywhere.
(255, 136, 0)means the same thing only within a given colour space. Without a profile, the same numbers can display differently on different screens.
Here is a small helper that avoids the first four errors:
def clamp8(value):
"""Round to the nearest integer and limit to the 0-255 range."""
return max(0, min(255, int(round(value))))
def brighten(rgb, amount):
return tuple(clamp8(c + amount) for c in rgb)
print(brighten((200, 100, 50), 80)) # (255, 180, 130)Quick reference
| You want | Do this |
|---|---|
| Number of values per channel | 2 raised to the bit depth, so 256 at 8 bits |
| Highest 8-bit code | 255 |
| Convert 8-bit to unit range | Divide by 255 |
| Convert unit range to 8-bit | Multiply by 255, then round |
| Total 8-bit RGB colours | 256 cubed, or 16,777,216 |
| Smooth edits across many steps | Work at 16 bits per channel, export at 8 |
| Average or blur accurately | Convert to linear light first |
Exercise
A designer brightens the colour (230, 120, 40) by adding 40 to every channel, then converts the result to the unit range by dividing by 255. What are the final unit values, to three decimal places, and what error would occur if the designer skipped the clamping step before converting?
Show answer
Adding 40 gives (270, 160, 80). Clamping the red channel to 255 gives (255, 160, 80). Dividing by 255 gives (1.000, 0.627, 0.314). Without clamping, red would be 270 / 255 = 1.059, which is outside the valid 0 to 1 range. Depending on the tool, it would be clipped later, wrap around to a dark value, or raise an error.
Exercise
A gradient runs from (100, 100, 100) to (120, 120, 120) across a 1,200-pixel-wide banner at 8 bits per channel. How many distinct grey levels does it contain, and what is the visual risk? Name one fix.
Show answer
It contains 21 distinct levels (100 through 120 inclusive), so each level fills roughly 57 pixels wide. The risk is visible banding, with flat stripes and sharp edges between them. Fixes include adding dither noise to the gradient or editing at 16 bits per channel, then exporting with dithering.