Section 6.4. Highlighting Extremes


[Page 193 (continued)]

6.4. Highlighting Extremes

What if we want to highlight the lightest and darkest areas of a picture? Would we highlight areas that are less than some amount from white and less than the same amount from black? Is there any color that is close to both white and black? No, we would want to replace the color at all pixels that have a distance from white or a distance from black less than some amount. We used '&&' to mean 'and' in the last program. In this program we will use '||' to mean 'or'.

Program 41. Highlight Extremes

/**  * Method to replace the pixel colors in the current  * picture object that have a color distance less than  * the passed amount to white or black with the passed  * replacement color  * @param replacementColor the new color to use  */ public void highlightLightAndDark(double amount,                                   Color replacementColor) {  Pixel pixel = null;  // loop through all the pixels in the x direction  for (int x = 0; x < getWidth(); x++) {     // loop through all the pixels in the y direction     for (int y = 0; y < getHeight(); y++) {       // get the current pixel       pixel = getPixel(x,y);       // if the distance from white or black is less than the       // passed amount use the replace color instead       if (pixel.colorDistance(Color.white) < amount ||           pixel.colorDistance(Color.black) < amount) {         pixel.setColor(replacementColor);       }     }   } }



[Page 194]

You can use this as follows and see the result in Figure 6.12:

> String fileName = Picture.getMediaPath("butterfly1.jpg"); > Picture picture = new Picture(fileName); > picture.explore(); > picture.highlightLightAndDark(50.0,java.awt.Color.yellow); > picture.explore();


Figure 6.12. Original picture (left) and light or dark areas highlighted (right).




Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net