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.

Writing code in short form instead writing multiple statements. Multiple line of code

Replace all above statements with the following line will work in a same way.

Here is a complete method. I wrote in MyPicture class below.

From main method i added following line to test increaseRed method with amount 0.6. The output reflect on amount given in main method.

Left side is original picture and on right side after calling method increaseRed()

Left side is original picture and on right side after calling method increaseRed(0.6)

To clear red color just passed 0.0 in increaseRed method.

Effects on after clearing red

Effects on after clearing red

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.

Method to change Green color

Clear Green

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

Method to change Blue color

Method to clear Blue color

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

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

To clear green call the above method with MyPicture class instance by passing red = 1, green =0, blue =1

No Responses