Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Part 1b: Color Changing Methods

Background: In all of our images, whether color or grayscale, each individual pixel has:

  • a redness level (0-255, where 0=no redness and 255=maximum redness)
  • a blueness level (0-255)
  • a greenness level (0-255)
  • an opacity or opaqueness (0-255, where 0=completely transparent and 255=totally opaque).

We will only use the red, green, and blue components; opacity will always be 255. Shades of gray arise when you have equal amounts of red, green, and blue, with black containing 0 for all three and white containing 255 for all three.

The grayscale2 and grayscale methods convert from RGB to gray by computing the average of the three. They also show how to use many of the methods in the Picture class (for example, the constructor to copy an image) and in the Pixel class, e.g., the way to access individual components of a Pixel or to access a whole Color object that, in turn, has all of the color components needed.

You will implement the methods whose signatures appear below. To implement the first six methods, adapt one of the two approaches to computing grayscale, either grayscale2 or grayscale (with helper methods). Be sure to try out your image transformations in the graphical user interface (GUI). Also, be sure to test your implementations with PictureTest_ColorChange.java.

It is really important within this set of methods to reuse as much code as possible! If you find yourself with many copies and pasted versions of the same code – you should look for a way to avoid this duplicated code (Note – because these methods – other than negate()– are very similar to grayscale, almost all of the points within this section are allocated to avoiding this duplication of code using a helper method! For lighten, darken, addBlue, addGreen, and addRed, you shouldnt need to copy and paste the for loops!)

Here are the signatures of these methods that you’ll implement:

  • public Picture negate()
    should return the ”photo inverse” of this Picture. You’ll essentially do newred = 255 – red, newblue = 255 – blue and newgreen = 22 – green
  • public Picture lighten(int lightenAmount)
    returns a new Picture that is lighter in red, green, and blue by lightenAmount (maxing at 255)
  • public Picture darken(int darkenAmount)
    similar for creating a darker version of thisPicture
  • public Picture addBlue(int amount)
    should return a new Picture with only the blue channel changed (keeping r,g,b values between 0 and 255, inclusive)
  • public Picture addGreen(int amount)
    should return a new Picture with only the green channel changed (keeping r,g,b values between 0 and 255, inclusive)
  • public Picture addRed(int amount)
    should return a new Picture with only the red channel changed (keeping r,g,b values between 0 and 255, inclusive)
  • private int luminosityOfPixel(int x, int y)
    returns the luminosity of this Picture at Pixel(x,y). Implement this before luminosity().

Here is a visual example showing the difference between uniform averaging and luminosity. Luminosity is a weighted average of red, green, and blue in which the weights take into account that human vision has much better visual acuity within some color frequencies (e.g., greens) than in others (e.g., blues): luminosity = (int)(0.21 * redness + 0.72 * greenness + 0.07 * blueness);

Warning! The order in which you perform the luminosity calculation, above, matters! Be sure to use the order above (adding red, then green, then blue) with the coefficients shown. Small floating-point errors can accumulate and, once in a while, they will change a pixel value by 1 gray level – not enough to see, but enough to cause a test to fail…

  • public Picture luminosity()
    should return a different grayscale image, using the luminosity calculation described above.

Tests

When you complete this part, all of the tests from PictureTest_ColorChange.java should pass. You might find the method printLuminosity() helpful because you can compare the output to the correct output shown in class.