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.
// 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:
// 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;
}
|
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 |
|
|
|
|
|
|
For this lab assignment, you are to write a java program that will complete the following:
Submission of the Lab
The lab must be submitted electronically to the Assignment Link for Lab 8 inside of Blackboard. You will only need to submit the java source code file (the ".java" file). Please only submit source code file (the .java file, not the .class).