Manipulating color and pixel in Java


If you are not familiar with BookClass basics. It is highly recommended to read the following tutorials before starting picture manipulation.
Customize and use BookClasses in eclipse
Manipulating Pictures in Java- I
Manipulating Pictures in Java- II

Let’s have a look at pixel, ColorChooser and color class methods before proceeding with color value manipulation.

Pixel class methods :

Methods Description
getColor() This method returns color object
getred(), getGreen(), getBlue() Each method returns color value between 0 to 255.
getX(), getY() Returns pixel position (x and y) of where this pixel is in that picture
setColor(Color color) This method takes Color object and set it for pixel
setRed(int value), setGreen(int value), setBlue(int value) Each method takes a value between 0 to 255 to set value of a pixel
int correctValue(int value) Correct color value.. must be 0 to 255
updatePicture(int alpha, int red, int green, int blue) Update picture based on color value for this pixel
setAlpha(int value) set the alpha (transparency) to a new alpha value
double colorDistance(Color testColor) get the distance between this pixel’s color and the passed color

Color class methods :

Method Description
new Color(int red,int green, int blue) Take three values as red, green and blue. create color object and return it as Color object
darker(),brighter() returns slightly darker or brigher color object

FileChooser class method

Method Description
ColorChooser.pickAColor() Displays a window to choose some colors

Here is an example how to manipulate pixel. Find decreaseRed() method in following code. The method retrieves all pixel and iterate through each pixel, retrieves each red value of a pixel and change red by multiplying each value with 0.6. We are calling an additional method repaint() to update changes.

import java.awt.Color;
import java.awt.image.BufferedImage;

import bookClasses.FileChooser;
import bookClasses.Pixel;
import bookClasses.SimplePicture;

public class MyPicture extends SimplePicture {
	public MyPicture(String fileName) {
		super(fileName);
	}

	public MyPicture(int width, int height) {
		super(width, height);
	}

	public MyPicture(MyPicture copyPicture) {
		super(copyPicture);
	}

	public MyPicture(BufferedImage image) {
		super(image);
	}

	public void adjustToBlack() {
		Pixel[] pixelArray = this.getPixels();
		Pixel pixel = null;
		int index = 0;
		while (index < pixelArray.length) {
			pixel = pixelArray[index];
			pixel.setColor(Color.black);
			index++;
		}
	}

	public void decreaseRed() {
		//Get all pixel
		Pixel[] pixelArray = this.getPixels();
		Pixel pixel = null;
		int value = 0;
		
		//Iterate through all pixel
		for(int index =0; index < pixelArray.length; index++ ){
			
			// get the current pixel
			pixel = pixelArray[index];

			// get the value
			value = pixel.getRed();
			// decrease the red value by 60% 
			value = (int) (value * 0.6);
			
			// set the red value of the current pixel to the new value
			pixel.setRed(value);
			
		}
	}

	public String toString() {
		String output = "Picture, filename " + getFileName() + " height " + getHeight() + " width " + getWidth();
		return output;
	}

	public static void main(String[] args) {
		String fileName = FileChooser.pickAFile();
		MyPicture pic = new MyPicture(fileName);
		//pic.adjustToBlack();
		pic.explore();
		
		//Call descrease Red method in MyPicture class
		pic.decreaseRed();
		
		//call repaint method to update picture
		pic.repaint();
	}
}
Left side is original picture and on right side after calling method decreaseRed()

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