Java:Applying colors of your choice to images

Sai Pitchuka
3 min readOct 20, 2018

When you want to apply a specific color to an image using java,it can be done using the BufferedImage class.

As we know that images are made up of pixels and each pixel is represented by it’s red,green,blue values which range from 0 to 255.So,to represent a color in each pixel these red,green,blue values has to be mixed in the appropriate proportion required to obtain the desired color to the pixel.

An example program to achieve the effect of applying a color layer to an image is given below:

package com.example.demo;

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.imageio.ImageIO;

public class ImageColorFilter{
public static void main(String args[])throws IOException{

Scanner commandLineScanner = new Scanner(System.in);

System.out.println("Select the number of the color to apply for the image:");
System.out.println("1.Blue");
System.out.println("2.Red");
System.out.println("3.Green");
System.out.println("4.Purple");
System.out.println("5.Orange");
System.out.println("6.Yellow");
System.out.println("7.Gold");
System.out.println("8.Pink");
System.out.println("9.Turquoise");
System.out.println("10.Silver");
System.out.println("11.Other");

int selectedOption = commandLineScanner.nextInt();

int redPercent = 0;
int greenPercent = 0;
int bluePercent = 0;

switch (selectedOption){
case 1:bluePercent=100;
break;
case 2:redPercent=100;
break;
case 3:greenPercent=100;
break;
case 4:redPercent=50;
bluePercent=50;
break;
case 5:redPercent=100;
greenPercent=65;
break;
case 6:redPercent=100;
greenPercent=100;
break;
case 7:redPercent=100;
greenPercent=84;
break;
case 8:redPercent=100;
greenPercent=75;
bluePercent=80;
break;
case 9:redPercent=25;
greenPercent=88;
bluePercent=82;
break;
case 10:redPercent=75;
greenPercent=75;
bluePercent=75;
break;
case 11:System.out.println("Enter Red %:");
redPercent = commandLineScanner.nextInt();

System.out.println("Enter Green %:");
greenPercent = commandLineScanner.nextInt();

System.out.println("Enter Blue %:");
bluePercent = commandLineScanner.nextInt();
break;
}

applyColorFilter(redPercent, greenPercent, bluePercent);
}

private static void applyColorFilter(int redPercent, int greenPercent, int bluePercent) throws IOException {
File inputImage = new File("/home/sai/Desktop/aishwarya.jpg");//File path for input image
BufferedImage image = ImageIO.read(inputImage);

for(int y = 0; y < image.getHeight(); y++){
for(int x = 0; x < image.getWidth(); x++){
int pixel = image.getRGB(x,y);

int alpha = (pixel>>24)&0xff;
int red = (pixel>>16)&0xff;
int green = (pixel>>8)&0xff;
int blue = pixel&0xff;

pixel = (alpha<<24) | (redPercent*red/100<<16) | (greenPercent*green/100<<8) | (bluePercent*blue/100);

image.setRGB(x, y, pixel);
}
}

ImageIO.write(image, "jpg", new File("/home/sai/Desktop/output.jpg")); //File path to store resulting image.
}
}

For the sake of running this example,I have taken the image of Aishwarya Rai

Original Image

So,let’s proceed to run the program.The program asks to choose the color to apply from a list of some popular colors.Example run will be like below.

selected 5 for Orange

I have chosen option no 5 which is orange and the program creates an image with orange color applied to Aishwarya Rai.

Orange color

Not only the colors that are listed in the command line,you can also apply any color of your choice but only thing is that you need to know the red,green,blue percentage proportions of the desired color.

Let us take an example of color brown.The Red,Green,Blue values for Brown is 165,42,42 respectively.The percentage values of red,green,blue values will be (165/255)*100,(42/255)*100,(42/255)*100 i.e 65,16,16 respectively.

Running the program again to apply brown color:

Command Line values for brown color

The result after running the program is a brown version of the image.

Brown

Like this,one can apply any desired color to the image.

The program re-calculates the r,g,b value for each pixel in order to apply a specific color to the image by multiplying the actual r,g,b values of each pixel in the image with the percentage of r,g,b values of the target color.

Please visit my YouTube channel https://youtube.com/channel/UC-8tqCyhtt6Lt5-n2UMZB_A

--

--