Multiple If’s with Images

We can use multiple if’s to reduce the number of colors in an image. Let’s say that if we have a little bit of each of red, green, and blue, we want to make each of them zero. If we have more, we set them to a mid-range value like 120. This is called posterizing because it reduces all the colors in a picture to a small number of colors – like the ones you might use if you were making a poster.

Rewrite the code for posterizing an image using if and else rather than multiple if’s. Test that it still works correctly.

    csp-15-4-1: How many different colors will be in our image when we are done?
  • 8
  • Two possible values of each of red, green, and blue (3 colors) is 2 raised to 3rd power combinations which is 8.
  • 3
  • Two values of each of red, green, and blue is more than 3.
  • 120
  • Far fewer
  • 16,777,216 (= 256 * 256 * 256)
  • That's the total number of colors possible. But this code reduces that.


Discuss topics in this section with classmates.

Show Comments
Next Section - Chapter 15 - Concept Summary