CS 101 - Intro to Computing, Spring 2012

Lab 7

Images Using a GrayScale

A black and white picture uses various shades of gray. These shades of grey all have the same amount of red, green and blue color. The higher the color value, the lighter the shade of gray is. The lower the color value, the darker the shade of gray is. For example, look at the following squares for various shades of gray. The amount of red, green and blue for the color is displayed as text in each square. Note that the amount of red, green and blue is the same in each square. Also note that the larger the amount of color, the lighter the color of gray.


r = 0
g = 0
b = 0

r = 63
g = 63
b = 63

r = 127
g = 127
b = 127

r = 191
g = 191
b = 191

r = 255
g = 255
b = 255

Changing the color in a pixel

The color in a pixel can be changed by using the setRed(), setGreen() and setBlue() functions in the Pixel class as part of the Java bookClasses.

Determining the amount of gray

One method to determine which grayscale color to use for a pixel when creating a black and white picture from a color picture is to average the amount of red, green and blue color the corresponding pixel in the colored picture. This method works fairly well and is fairly easy to understand and use. This way uses the intensity of each color to determine the grayscale value.

Another method is to take a weighted average of the amount of red, green and blue color instead of an evenly-weighted average. A weighted average would give more weight to certain values and less weight to other values. For example, a weighted average will be used when determining the grade for this course. Exams have a higher weight (i.e. more impact on the final grade), while lab assignments have a lower weight (i.e. less impact on the final grade). For determining the amount of color for the graycolor color, the original amount of green is often given a higher weight while the original amount of blue is given a lesser weight. The percent of each color that should be used according to some research done on luminance is:
  • Red
  • Green
  • Blue
 : 
 : 
 : 
29.9%
58.7%
11.4%
Using this weighted value is what was done for the code written in class for Lect1006g.java.

Lab Assignment 7

Due: Wednesday 2/29/2012 by 11:59 pm

Write two Java programs that will do the following:

  1. Program 1: Make Black And Green

  2. Program 2: Make Green And White

    Some of the resulting colors will appear as:
    makeBlackAndWhite()

    r = 0
    g = 0
    b = 0

    r = 63
    g = 63
    b = 63

    r = 127
    g = 127
    b = 127

    r = 191
    g = 191
    b = 191

    r = 255
    g = 255
    b = 255
    makeBlackAndGreen()

    r = 0
    g = 0
    b = 0

    r = 0
    g = 63
    b = 0

    r = 0
    g = 127
    b = 0

    r = 0
    g = 191
    b = 0

    r = 0
    g = 255
    b = 0
    makeGreenAndWhite()

    r = 0
    g = 255
    b = 0

    r = 63
    g = 255
    b = 63

    r = 127
    g = 255
    b = 127

    r = 191
    g = 255
    b = 191

    r = 255
    g = 255
    b = 255

  3. You must write your programs using good programming style which includes:

  4. You are to submit both Java files electronically via the assignment link in Blackboard for Lab 7.

    Please only submit source code files (the .java file, not the .class or the .java~).

    Also, if you have any comments about your program, please write it down in the header block comment of the .java file; please do NOT write in the "Comments" field on the submission page (this will go into a different file and will be easily missed).

The following are some images created based on the ideas in this lab. The following image is the original full color picture.

The following image is the black and white version of the original image.

The following image is the black and green version of the original image.

The following image is the green and white version of the original image.