30 Apr 2016
Create a negative image
Creating negative of a an image is pretty simple. Let’s have a look at each color component values. Each color component value lies between 0-255. We want opposite value of each color component. Let’s say a color component e.g. red / green / blue has a value of 255 and we want its opposite that is 0. if color component value is 100 then we want a color component value 100 lesser then the full color that is 255-100 = 155.
Retrieve red color component and opposite current value
int oppositRed = 255-pixelArray[index].getRed();
Retrieve green color component and opposite current value
int oppositGreen = 255-pixelArray[index].getGreen();
Retrieve Blue color component and opposite current value
int oppositBlue = 255-pixelArray[index].getBlue();
Here is the method that really works and have a look at images displayed below. It really works
// Create negative of original image public void createNegative() { // Get all pixel Pixel[] pixelArray = this.getPixels(); // Iterate through all pixel for (int index = 0; index < pixelArray.length; index++) { // get the current pixel color, opposite color and create a new color object Color color = new Color(255 - pixelArray[index].getRed(), 255 - pixelArray[index].getGreen(), 255 - pixelArray[index].getBlue()); // set new color pixelArray[index].setColor(color); } }