﻿ CS 111 - Lab 8

## CS 111 - Intro to Computing, Fall 2012

### Posterization

Posterization is a technique that reduces the number of colors used in a picture. During lecture we did examples of posterizing a picture to 2 colors, to 4 colors and to 8 colors.

The code to posterize is often a modification of the black & white (grayScale) code. Once the "grayAmount" has been determined for a pixel, we use that value in if statement(s) to determine which of the posterized colors that pixel will be set. The following shows that code for posterizing into 2 colors: black and white.

• If the grayAmount is in the range from 0 to 127, the color of the pixel becomes black.
• If the gray Amount is in the range from 128 to 255, the color of the pixel becomes white.

 ``` // determine the grayAmount from the color at the pixel int grayAmount = (int) (red * 0.299 + green * 0.587 + blue * 0.114); // determine the final color value of the pixel if ( grayAmount < 128 ) { // set the pixel to black red = 0; green = 0; blue = 0; } else { // set the pixel to white red = 255; green = 255; blue = 255; }```

To posterize to more colors we just need to use nested if statements (often called "else-if" clauses). The following posterizes to 4 colors (black, blue, cyan and white). In this one the ranges for the grayAmount are:

• 0 - 63 for black,
• 64 - 127 for blue,
• 128 - 191 for cyan, and
• 192 - 255 for white.

 ``` // determine the grayAmount from the color at the pixel int grayAmount = (int) (red * 0.299 + green * 0.587 + blue * 0.114); // determine the final color value of the pixel if ( grayAmount < 64 ) { // set the pixel to black red = 0; green = 0; blue = 0; } else if ( grayAmount < 128 ) { // set the pixel to blue red = 0; green = 0; blue = 255; } else if ( grayAmount < 192 ) { // set the pixel to cyan red = 0; green = 255; blue = 255; } else { // set the pixel to white red = 255; green = 255; blue = 255; }```

### Lab Assignment

Due: Wednesday 10/24/2012 by 11:59 pm

For this lab assignment, you are to posterize a picture into 6 colors. We will use the "grayAmount" that was used when creating a black and white (grayscale) pricture to determine which of the six color values will be used.

Since our range for the "grayAmount" can vary from 0 to 255, we need to divide that range into 6 parts to distribute the six colors evenly throughout the picture.

We will use the following shades of green for our 6 colors. The names come from the web page at: http://www.tayloredmktg.com/rgb/.

grayAmount
Range
0 - 42 43 - 84 85 - 127 128 - 170 171 - 212 213 - 255
Color
Value
``` Olive Drab
r = 107  g = 142  b = 35 ```
```Forest Green
r = 34g = 139b = 34```
```Lime Green
r = 50g = 205b = 50```
```Lawn Green
r = 124g = 252b = 0```
```Spring Green
r = 0g = 255b = 127```
```Pale Green
r = 152g = 251b = 152```

For this lab assignment, you are to write a java program that will complete the following:

1. Prompt the user for an picture and open that picture.

2. Call a method that will posterize the picture as described above. The picture opened in step 1 is to sent as a parameter to this method.

3. Display the posterized picture.

4. You must write your programs using good programming style which includes:
• Good variable names
• in-line commenting
• header block commenting for the program and each method written
Be sure to include the following with the header block comment for the program.