28 Apr 2016
Lightening and Darkening picture in Java
As described in previous articles, we need to change color component to darken or lighten a picture. Below is the method to lighten the picture.
public void brighter() { // Get all pixel Pixel[] pixelArray = this.getPixels(); // Iterate through all pixel for (int index = 0; index < pixelArray.length; index++) // get the current pixel color, extract brighter color and set it pixelArray[index].setColor(pixelArray[index].getColor().brighter()); }
Darken Picture Color
public void darker() { // Get all pixel Pixel[] pixelArray = this.getPixels(); // Iterate through all pixel for (int index = 0; index < pixelArray.length; index++) // get the current pixel color, extract darker color and set it pixelArray[index].setColor(pixelArray[index].getColor().darker()); }
You can also write the above code in many steps. If you are confused how this is happening. Let me to elaborate it further.
To get the current pixel color, use the following line of code.
Color color = pixelArray[index].getColor();
To get brighter color on current color, use the following line of code
color = color.brighter();
To get brighter color on current color, use the following line of code
color = color.darker();
Replace current color with a new color value
pixelArray[index].setColor(color);
No Responses