<< Chapter < Page | Chapter >> Page > |
A comparison
If you compare the top half of the bottom image in Figure 1 with the top half of the other two images, you can see the dramatic effect of color inversion.However, all that is required to exactly restore the original colors is to run the inverted color pixels through the inversion process again.
Because of these characteristics, some major software products use color inversion to change the colors in an image that has been selected forprocessing to provide a visual indication that it has been selected.
The color inversion algorithm
To invert the color of a pixel, you simply subtract the red, green, and blue color values from 255 without modifying the alpha value. To reverse the process,you simply subtract the inverted color values from 255 again, which produces the original color values.
Beginning of the invert method
The invert method begins in Listing 15.
private function invert(bitmap:Bitmap):void{
//Get the BitmapData object.var bitmapData:BitmapData = bitmap.bitmapData;
//Get a one-dimensional byte array of pixel data// from the top half of the bitmapData object
var rawBytes:ByteArray = new ByteArray();rawBytes = bitmapData.getPixels(new Rectangle(
0,0,bitmapData.width,bitmapData.height/2));
The code in Listing 15 gets the BitmapData object encapsulated in the incoming Bitmap object and extracts the pixel data from a rectangle that comprises the entire top half of the bitmapdata into a ByteArray object.
Apply the inversion algorithm
Listing 16 applies the color inversion algorithm to all of the pixel data in the ByteArray object by subtracting each color value from 255 and storing the result back into the same element of the ByteArray object.
var cnt:uint = 1;
while(cnt<rawBytes.length){
rawBytes[cnt]= 255 - rawBytes[cnt];rawBytes[cnt + 1] = 255 - rawBytes[cnt + 1];
rawBytes[cnt + 2]= 255 - rawBytes[cnt + 2];cnt += 4;//increment the counter
}//end while loop
Put the modified pixel data back into the BitmapData object
Listing 17 calls the setPixels method to put the modified pixel data back into the BitmapData object producing the final output shown in the bottom image of Figure 1.
rawBytes.position = 0;//this is critical
bitmapData.setPixels(new Rectangle(0,0,bitmapData.width,bitmapData.height/2),
rawBytes);} //end invert method//--------------------------------------------------//} //end class
} //end package
This is a case where the new pixel color values depend on the original color values. Therefore, it was necessary to get and use the old color values tocompute the new color values.
An interesting side note
If you compare the two color bars in the upper-left corner of the middle and bottom images in Figure 1, you will see that they appear to have swappedpositions. This is because the numeric value of magenta is the inverse of the numeric value of cyan and vice versa. The same is true of the cyan and magentaborders.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?