Welcome to CS

Section26.4Posterizing

As one last example of selectively modifying the colors in an image, we are going to posterize an image. Posterizing is the process of changing an image so it only uses a small number of colors. (Old posters had to be printed with a small number of colors - the technology didn’t allow for complex blending of colors).
This time, instead of making a function to answer a question like “Is this green?”, we need to make a function that will force a color value to become one of a few specific options.

Checkpoint26.4.1.

Write code for posterize to return 0 if colorValue is less than 50, 125 if it is 50-124, and 255 otherwise.
Now that you have that function, add it to this program to see it in action:
Rewrite the code for posterizing an image using if and else rather than multiple ifs. Test that it still works correctly.

Checkpoint26.4.2.

How many different colors are possible in our image after we posterize it using this function?
Hint.
There are three color channels. How many values can they each take on?
• 27
• Each of the three color channels (red, green, and blue) has 3 possible values (0, 125, or 255). That gives us 3 raised to 3rd power, or 27, combinations.
• 3
• Three values of each of red, green, and blue is more than 3.
• 9
• Each of the 3 color channels (red, green, and blue) has 3 possible values (0, 125, or 255). There are more than 9 total combinations that can be made with those.
• 16,777,216 (= 256 * 256 * 256)
• That’s the total number of colors possible if each channel can have one of 256 different values. But our code makes each color only have 3 possible values.