Friday 13 March 2009

The INVERT filter



Inverted image on left, original on right

The invert filter takes an image from the display screen and inverts its colours. As can be seen from the applet, when the background of an image is black, it is changed to white. Black has colour value 0 and white has value 255. So to invert black 255 is added to the colour value of (0,0,0) to give (255,255,255). The red, green and blue colour values set at 255 (maximum value) gives the colour white. The colour pure red has a colour value of (255,0,0), we would expect the inverse of this to be (0,255,255). We can test this theory in Processing.
psuedo code:
make colour
c=(255,0,0)(red)
set background to c
make colour c=(0,255,255)(cyan)
set fill to newC
draw a rectangle in centre of screen
apply invert filter

Run the following code in processing to produce a cyan square with a red background.
color c, newC;
void setup(){
size(200,200);
c=color(255,0,0);
newC=color(0,255,255);}
void draw(){
background(c);
fill(newC);
rect(width/4,height/4,width/2,height/2);
//filter(INVERT);
}
uncomment the last line and run again.
The colour of the rectangle is now red and the background cyan.
If we set the colour value to (128,128,128) and then invert it we may establish how processing applies the invert filter.
Applying different values we can establish that the invert filter subtracts the colour values from 255 to produce the new colour values.
We can use the following code to prove this by changing the values of the colours and printing out the values after the inverse filter has been applied:

color c, newC;
void setup(){
size(200,200);
c=color(55,55,55);
newC=color(0,255,255);}
void draw(){
background(c);
fill(newC);
rect(width/4,height/4,width/2,height/2);
filter(INVERT);
color getC=get(10,10);
println(red(getC));
println(blue(getC));
println(green(getC));
}






No comments:

Post a Comment