Increase and decrease RGB colors in Java
RGB color system
RGB color space or RGB color system, constructs all color with the combination of Red, Green and Blue having an integer value between 0 – 255. Let’s have a quick look, how to change these colors.
Increasing / Decreasing Red Color
In our previous example we learnt the way to decrease red values. This method can be implemented in several ways. Such as by passing percentage of increasing or decreasing color and increasing/decreasing exact passed amount of color. Even all manipulation can be reduced to one line as well. Below is an example that takes color amount and change color accordingly. Following method header will take a double value.
public void increaseRed(double amount)
Writing code in short form instead writing multiple statements. Multiple line of code
// get the current pixel pixel = pixelArray[index]; // get the value value = pixel.getRed(); // change the red value by given amount value = (int) (value * amount); // set the red value of the current pixel to the new value pixel.setRed(value);
Replace all above statements with the following line will work in a same way.
pixelArray[index].setRed((int)(pixelArray[index].getRed()*amount));
Here is a complete method. I wrote in MyPicture class below.
public void increaseRed(double amount) { //Get all pixel Pixel[] pixelArray = this.getPixels(); //Iterate through all pixel for(int index =0; index < pixelArray.length; index++ ){ //get current pixel and red value, increase the amount passed in above method //set the current pixel value to new value pixelArray[index].setRed((int)(pixelArray[index].getRed()*amount)); } }
From main method i added following line to test increaseRed method with amount 0.6. The output reflect on amount given in main method.
pic.increaseRed(0.6);
To clear red color just passed 0.0 in increaseRed method.
Increasing or Decreasing Green
We can use similar method mention above with a small change. We need to change just this line to Green or Blue respectively.
pixelArray[index].setGreen((int)(pixelArray[index].getGreen()*amount));
Method to change Green color
public void changeGreen(double amount) { //Get all pixel Pixel[] pixelArray = this.getPixels(); //Iterate through all pixel for(int index =0; index < pixelArray.length; index++ ){ //get current pixel and red value, increase the amount passed in above method //set the current pixel value to new value pixelArray[index].setGreen((int)(pixelArray[index].getGreen()*amount)); } }

The above method with value 0 will change like this. On left side is original image whereas on right image shows result of clearing green from image
pixelArray[index].setBlue((int)(pixelArray[index].getBlue()*amount));
Method to change Blue color
public void changeBlue(double amount) { //Get all pixel Pixel[] pixelArray = this.getPixels(); //Iterate through all pixel for(int index =0; index < pixelArray.length; index++ ){ //get current pixel and red value, increase the amount passed in above method //set the current pixel value to new value pixelArray[index].setBlue((int)(pixelArray[index].getBlue()*amount)); } }
Method to clear Blue color
public void clearBlue() { // Get all pixel Pixel[] pixelArray = this.getPixels(); // Iterate through all pixel for (int index = 0; index < pixelArray.length; index++) // get the current pixel and set it to 0 pixelArray[index].setBlue(0); }

The above method with value 0 will change like this. On left side is original image whereas on right image shows result of clearing Blue from image
Change All color in one method
public void changeAllColors(double red, double green, double blue) { //Get all pixel Pixel[] pixelArray = this.getPixels(); //Iterate through all pixel for(int index =0; index < pixelArray.length; index++ ){ //Change pixel color value to red, green and blue pixelArray[index].setRed((int)(pixelArray[index].getRed()*red)); pixelArray[index].setGreen((int)(pixelArray[index].getGreen()*green)); pixelArray[index].setBlue((int)(pixelArray[index].getBlue()*blue)); } }
To clear green call the above method with MyPicture class instance by passing red = 1, green =0, blue =1
pic.changeAllColors(1, 0, 1);
No Responses