Sub topic 6
Color depth and color modes
This chapter assumes you know that a raster image is a grid of pixels and that each pixel holds a color value. It also assumes you can read short Python snippets. Here we look at how many bits describe one pixel's color (color depth) and which color system those bits are interpreted in (color mode). Both choices change file size, visual quality, and whether an image prints correctly.
Bits per channel and bits per pixel
A pixel's color is stored as one or more numbers called channels. In an RGB image the channels are red, green, and blue. Each channel has a fixed number of bits. That number is the bit depth per channel.
Bits per pixel (bpp) is the bits per channel multiplied by the number of channels. A bit has two states, so n bits give 2^n distinct values.
| Bits per channel | Levels per channel | Colors in an RGB pixel | Bits per pixel (RGB) |
|---|---|---|---|
| 1 | 2 | 8 | 3 |
| 5 | 32 | 32,768 | 15 |
| 8 | 256 | 16,777,216 | 24 |
| 16 | 65,536 | about 281 trillion | 48 |
"8-bit color" can mean two different things. Photo editors usually mean 8 bits per channel, which is 24 bits per pixel and about 16.7 million colors. Older systems and the GIF format use "8-bit color" to mean 8 bits per pixel in total, which allows only 256 colors. Check which one a source means.
def color_count(bits_per_channel, channels=3):
return 2 ** (bits_per_channel * channels)
for bpc in (1, 5, 8, 16):
print(f"{bpc:>2} bits/channel -> {color_count(bpc):,} colors")Running this prints 8, 32,768, 16,777,216, and 281,474,976,710,656 colors.
Why 8 bits per channel is the default
The human eye can distinguish roughly a few million to ten million colors under good conditions. 256 levels per channel is close to the threshold where neighboring levels look the same on a typical display. That makes 24-bit color enough for most screen delivery.
The limit shows up in smooth gradients, such as a sky or a studio backdrop. With only 256 levels, a slow gradient across a wide area gets visible steps. This is called banding. Heavy editing makes it worse, because every tonal adjustment rounds values to whole numbers and discards some of the 256 levels.
Common pixel layouts
Several layouts trade file size against color fidelity.
- 1-bit (bitmap): each pixel is black or white. Used for line art scans and fax-style output.
- Grayscale: one channel, usually 8 bits, giving 256 shades. It stores a third of the data of RGB.
- Indexed color: each pixel stores a number that points into a palette of up to 256 colors. GIF and 8-bit PNG work this way. It suits flat-color graphics and fails on photographs, because 256 colors cannot cover smooth tones without dithering (mixing dots of palette colors to imitate others).
- RGB (24-bit): three 8-bit channels. This is the standard for photos on screens.
- RGBA (32-bit): RGB plus an 8-bit alpha channel that stores transparency per pixel. Alpha 0 is fully transparent, and alpha 255 is fully opaque. Intermediate values give soft edges and shadows.
- 16 bits per channel (48-bit RGB): used for editing, raw photo processing, and scientific or medical images. It gives 65,536 levels per channel, which prevents banding during heavy adjustment.
The storage cost follows directly from bits per pixel. Here is the 1920 × 1080 example from earlier, extended to each layout:
width, height = 1920, 1080
pixels = width * height
layouts = {
"grayscale 8-bit": 8,
"indexed 8-bit": 8,
"RGB 24-bit": 24,
"RGBA 32-bit": 32,
"RGB 48-bit": 48,
}
for name, bpp in layouts.items():
size = pixels * bpp // 8
print(f"{name:<16} {size:>10,} bytes ({size / 1024**2:.1f} MiB)")These are uncompressed sizes. Real files are usually smaller, because the compression covered in the next chapter reduces them. An indexed image also carries a palette of up to 256 × 3 = 768 bytes, which is small next to the pixel data.
High dynamic range
Bit depth also relates to dynamic range, the span from the darkest to the brightest tone an image can hold. Standard 8-bit images map values to a limited brightness range. High dynamic range (HDR) formats use 10, 12, or 16 bits per channel, or floating-point values, so they can store very bright highlights and deep shadows in the same file. Only use these for sources and editing. Most web delivery and print workflows still end in 8-bit images.
Color modes
Color depth says how many bits each channel gets. A color mode says what the channels mean. The same three bytes (42, 111, 219) are a blue in RGB. They would be nonsense if a program read them as another system.
RGB: additive color
RGB is an additive model. Screens start black and add light: red, green, and blue emitters at full strength make white, and all off make black. It fits any device that emits light, such as monitors, phones, and projectors.
CMYK: subtractive color
CMYK is a subtractive model. Printing starts with white paper and adds ink that absorbs (subtracts) light. The channels are cyan, magenta, yellow, and key (black). Black ink is added because mixing C, M, and Y inks gives a muddy brown rather than a true black, and it is cheaper than using three inks for text. At 8 bits per channel a CMYK pixel takes 32 bits. Channel values are usually written as percentages of ink coverage, from 0% to 100%.
Grayscale, indexed, and others
Grayscale and indexed are also modes. Some editors offer Lab, which describes color the way a human perceives it and does not depend on a device. It is mostly used as an intermediate step when converting between other modes. Other models, such as HSL and HSV, are the same RGB data written in terms of hue, saturation, and lightness. Designers use them because they are easier to adjust by hand. They do not change the gamut.
| Mode | Model | Typical use | Channels |
|---|---|---|---|
| RGB | Additive light | Screens, web, video | 3 (+ alpha) |
| CMYK | Subtractive ink | Commercial print | 4 |
| Grayscale | Single brightness | Black and white photos, scans | 1 |
| Indexed | Palette lookup | GIF, simple graphics | 1 index + palette |
Gamut and color spaces
A gamut is the range of colors a device or system can reproduce. The numbers in a mode are only meaningful inside a color space, which defines exactly which color each number triple represents.
- sRGB is the standard color space for the web. If a file has no profile, browsers assume sRGB.
- Adobe RGB and Display P3 are wider RGB spaces. They hold more saturated greens and cyans, and P3 is common on modern phones and laptops.
- CMYK gamuts depend on the press, ink, and paper. Examples are SWOP and FOGRA profiles.
A saturated, bright RGB color, such as a vivid cyan or electric orange, can fall outside a CMYK gamut. Converting it forces the closest printable color, so it looks duller. This is called being out of gamut.
An ICC profile is a small block of data embedded in a file. It names the color space so software can convert faithfully between spaces. Without it, the numbers are ambiguous.
A naive RGB to CMYK conversion
A simple formula shows the relationship between the two models. It is not what professional software does, because real conversions use ICC profiles and account for ink behavior. It still shows why the models are different.
def rgb_to_cmyk(r, g, b):
"""Naive conversion. Inputs 0-255, outputs 0.0-1.0."""
r, g, b = r / 255, g / 255, b / 255
k = 1 - max(r, g, b)
if k == 1:
return 0.0, 0.0, 0.0, 1.0
c = (1 - r - k) / (1 - k)
m = (1 - g - k) / (1 - k)
y = (1 - b - k) / (1 - k)
return c, m, y, k
c, m, y, k = rgb_to_cmyk(42, 111, 219) # #2a6fdb
print(tuple(round(v, 3) for v in (c, m, y, k)))This prints approximately (0.808, 0.493, 0.0, 0.141). That means about 81% cyan, 49% magenta, 0% yellow, and 14% black. A profile-based conversion would give different numbers, usually with a duller blue, because the printer cannot reach the same saturation as the screen.
Color in vector graphics
Vector graphics also use color modes, but color is attached to shapes instead of pixels. A fill such as #2a6fdb in SVG is a single RGB value that applies to a whole shape, so there is no per-pixel storage and no bit-depth cost to speak of. Gradients store color stops and let the renderer compute the in-between values at display resolution, so a vector gradient does not band in the same way. SVG is RGB-based. PDF and EPS can hold CMYK and spot colors, which makes them the usual choice when a vector file goes to a commercial printer.
Practical decision rules
- Choose the mode by destination, not by source. Screen means RGB. Commercial press means CMYK, and desktop printers usually accept RGB.
- Edit in 16 bits per channel when you plan heavy tonal changes, then export at 8 bits.
- Use indexed color only for flat graphics with few colors, and watch for dithering artifacts.
- Use an alpha channel only where you need transparency. It adds a quarter to the raw size of an RGB image.
- Embed a profile when the file will leave your control, so other software interprets the numbers correctly.
Exercise
A 4000 × 3000 pixel image is stored uncompressed as 16-bit-per-channel RGB with no alpha channel. How many bytes does it take, and how much smaller would it be as 8-bit-per-channel RGB?
Show answer
At 16 bits per channel, there are 3 channels, so 48 bits or 6 bytes per pixel. 4000 × 3000 = 12,000,000 pixels, so 12,000,000 × 6 = 72,000,000 bytes (about 68.7 MiB). At 8 bits per channel it is 3 bytes per pixel, or 36,000,000 bytes, which is exactly half the size.
Exercise
A designer builds a poster in RGB using a vivid electric blue, then sends it to a commercial printer that works in CMYK. The printed blue looks duller than the screen. Explain why, and name one way to reduce the surprise.
Show answer
The vivid blue lies outside the CMYK gamut of the press, so conversion replaces it with the nearest printable color, which is less saturated. To reduce the surprise, soft-proof the file with the printer's CMYK profile before sending it, and choose colors that are inside that gamut. Converting to CMYK at the end of the workflow, while keeping an RGB master, also helps.