Friday 20 March 2009

Chunk 73. More colour functions

blendColor provides a simple method of combing two colours to create a new one.
For example if we add together a red and green we will see yellow.

This simple example draws three rectangles - one each in red and green, and
one in the color formed by adding them.

We'd expect this colour to be yellow, as you'd get by combining colours normally.


size(400, 400);
smooth();
noStroke();
color red = color(255,0,0);
color green = color(0,255,0);
color red_add_green=blendColor(red,green,ADD);
fill(red);
rect(0,0,width/2,height);
fill(green);
rect(width/2,0,width/2,height);
// background
fill(red_add_green);
rect(width/4, width/4, width/2, height/2);


To understand exactly how these work to create a new colour, consider they way colours are defined.
When we write
color red = color(255,0,0);

We're creating a colour with a red component of 255, a green component of 0 and a blue component of 0.
Not suprisingly the colour is pure red.

When we use blendColor() to ADD a colour, it adds each of the components to create a new colour with those components.
So for our first example, adding red and green, we are adding components (255,0,0) and (0,255,0).
blendColor combines these to form a new colour with the components (255,255,0) which corresponds to yellow.