04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

728<br />

Creating & Reading<br />

As discussed previously, color is specified as three components, for red, green, <strong>and</strong> blue<br />

(RGB). (You can also specify a fourth component, alpha, for controlling transparency.)<br />

Most typically on the computer, color is referred to as a hexadecimal value. The<br />

hexadecimal system uses 16 as the base, instead of 10, as in the more common decimal<br />

system. To account for 16 unique numbers, the letters A, B, C, D, E, <strong>and</strong> F are used along<br />

with the numbers 0 through 9. Also remember that 0 counts as a digit, so F is equal to 15,<br />

while B equals 11. Numbers larger than 15 are created by using additional places, just as in<br />

the decimal system. However, the values are raised by 16 instead of 10. So, the number FF<br />

would be equal to 15 * 16 + 15, which equals 255, <strong>and</strong> the number AFB would equal (10 *<br />

16 * 16) + (15 * 16) + 11, which equals 2,811. To allow the computer to know that you<br />

want it to perform calculations using base 16 instead of base 10, you write 0x before the<br />

value. So, AFB would be specified as 0xAFB.<br />

Try running this one-line sketch, which should output 2811:<br />

println(0xAFB);<br />

In <strong>Processing</strong>, you can also use also the # symbol to specify a color value, as in the following<br />

line:<br />

color orange = #FF7700;<br />

Each color is specified as a six-digit hexadecimal value, where the first two numbers specify<br />

the red component, the next two the green, <strong>and</strong> the final two the blue. For example,<br />

the colors red, green, <strong>and</strong> blue are specified as #FF0000, #00FF00, <strong>and</strong> #0000FF. Of course,<br />

you can any put legal hexadecimal characters (0 through 9 <strong>and</strong> A through F) in any of the<br />

six slots to create up to 16,777,215 different colors.<br />

The seven functions red(), brightness(), blue(), saturation(), green(), hue(), <strong>and</strong><br />

alpha() extract specific values out of a color. For example, if I want the blue component<br />

of the color 0xFFE56D, I can use this simple code:<br />

color c = 0xFFE56D;<br />

print(blue(c));<br />

I can use the exact same structure for most of the functions within this section. Here’s<br />

code to extract the saturation of a color:<br />

color c = 0xFFE56D;<br />

print(saturation(c));<br />

Curiously, if you look at what the API has to say about the red() function, there’s a mysterious<br />

blurb in the Description section: “The red() function is easy to use <strong>and</strong> underst<strong>and</strong>,<br />

but is slower than another technique. To achieve the same results when working in<br />

colorMode(RGB, 255), but with greater speed, use the >> (right shift) operator with a bit<br />

mask” (see http://processing.org/reference/red_.html).<br />

Right shift operator, bit mask—what is this stuff? Unfortunately, there’s not much else to<br />

go on in the API. It seems there may be some pretty scary stuff lurking beneath the surface

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!