AP® Computer Science Principles · Big Idea 2 · Topic 2.1

Representing Color with Bits

A screen color is just three numbers, one each for red, green, and blue light, usually 0 to 255. Mixing those three light intensities together, additively, produces every color the screen can show.

Red, green, and blue are the three colors of light that human eyes are built to detect, through three types of cone cells in the retina. A screen has no paint and no ink. It has tiny lights, and it controls color by controlling how bright each of the red, green, and blue lights are at every pixel. Turn all three all the way up and you get white. Turn all three off and you get black. Everything in between comes from different mixes of brightness.

Each pixel stores this as three numbers, one per channel, each usually a byte: 8 bits, giving 256 possible values from 0 to 255. The triple 255, 140, 0 means red fully on, green a little under half on, and blue completely off. Full red plus moderate green plus no blue lands on a warm orange. With three channels of 8 bits each, the total number of colors is 28 times 28 times 28, which is 224, about 16.7 million. More generally, n bits per channel gives 2n values per channel and 23n colors total.

This is why RGB sits at the start of the data unit: it is the same idea as counting in binary, just applied three times over and read as brightness instead of quantity. It also sets up what comes next, because fewer bits per channel means fewer colors available, and squeezing an image down to 2 bits per channel instead of 8 is exactly the kind of compression tradeoff the rest of the unit explores: smaller files, visibly worse pictures.

Key terms

RGB
A color model that specifies a color using three values for red, green, and blue light intensity.
Channel
One of the three separate red, green, or blue components that together make up a pixel's color.
Additive color mixing
Combining colors by adding light wavelengths together, as screens do, where red plus green makes yellow.
Subtractive color mixing
Combining colors by absorbing (subtracting) wavelengths, as paint and ink do, which behaves differently from light.
24 bit color
A color depth using 8 bits for each of the red, green, and blue channels, giving 16,777,216 possible colors.

Watch it explained

  1. How it worksHow do three bytes, one each for red, green, and blue, pick out one exact color?
  2. Worked exampleShow me which color the RGB triple 255, 140, 0 turns out to be and why.
  3. Worked exampleFind how many distinct colors 24 bit color is able to represent.
  4. Common mistakeWhy doesn't mixing red and green paint on a page give you the same color as mixing red and green light on a screen?
  5. Going furtherWhat happens to a photograph when each color channel is cut from 8 bits down to 2?

Check yourself

Your turn1 of 2
An image uses 8 bits per color channel, with three channels (red, green, and blue). How many distinct colors can it represent in total?

End of topic.