Background: In all of our images, whether color or grayscale, each individual pixel has:
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:
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…
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.