﻿ CS 101 - Lab 8

## CS 101 - Intro to Computing, Spring 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, green and white). In this one the ranges for the grayAmount are:

• 0 - 63 for black,
• 64 - 127 for blue,
• 128 - 191 for green, 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 green red = 0; green = 255; blue = 0; } else { // set the pixel to white red = 255; green = 255; blue = 255; }```

### Lab Assignment

Due: Wednesday 3/7/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 blue 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
``` Midnight Blue
r = 25  g = 25  b = 112 ```
```Dark Slate Blue
r = 72g = 61b = 139```
```Royal Blue
r = 65g = 105b = 225```
```Deep Sky Blue
r = 0g = 191b = 255```
```Pale Torquoise
r = 175g = 238b = 238```
```Light Cyan
r = 224g = 255b = 255```

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.