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.

of this part of the API. These mysterious terms relate to bitwise operations. Bitwise operations<br />

are low-level operations useful for squeezing some extra performance out of a<br />

sketch, but they’re also a little (actually more than a little) confusing to use. I cover bitwise<br />

operations further in Appendix B.<br />

<strong>Processing</strong> has a convenient data type called color that encapsulates the 32 bits of information<br />

making up a color (8 bits for alpha, 8 bits for red, 8 bits for green, <strong>and</strong> 8 bits for<br />

blue). You can declare a variable of type color using the following syntax:<br />

color c<br />

In the Image section of the API, which I’ll look at next, is a function named get(). get()<br />

returns a value as a color data type based on a specific pixel’s color. Another way to create<br />

a color from scratch is to use the color() function. (I realize it gets confusing when a<br />

number of different comm<strong>and</strong>s/data types share common names). Finally, you can create<br />

a color by directly assigning a hexadecimal value to a color variable. Following are three<br />

statements that generate a color three different ways: through the use of get(), through<br />

the use of the color() function, <strong>and</strong> through direct assignment using hexadecimal notation.<br />

I also include a fourth direct assignment statement that includes an alpha component<br />

in the hexadecimal notation.<br />

color c1 = get(50, 50);<br />

color c2 = color(200, 100, 50);<br />

color c3 = #FF33DD;<br />

// this last statement explicitly specifies alpha <strong>and</strong> RGB values<br />

color c4 = 0xFFFF33DD;<br />

The Creating & Reading subsection also includes two color-blending functions:<br />

blendColor() <strong>and</strong> lerpColor(). blendColor(), the more complex of the two functions,<br />

utilizes a blending mode, declared as an argument passed to the function, along with the<br />

two colors to blend. lerpColor() uses a simpler numeric ratio for blending two colors.<br />

Here’s an example that uses both functions, including blendColor()’s different blending<br />

modes (see Figure A-16):<br />

/* blendColor <strong>and</strong> lerpColor<br />

Ira Greenberg, October 12, 2006 */<br />

size(210, 490);<br />

background(100);<br />

noStroke();<br />

smooth();<br />

color c1 = color(200, 10, 200);<br />

color c2 = color(200, 200, 10);<br />

//blend<br />

color c_add = blendColor(c1, c2, ADD);<br />

color c_subtract = blendColor(c1, c2, SUBTRACT);<br />

color c_darkest = blendColor(c1, c2, DARKEST);<br />

color c_lightest = blendColor(c1, c2, LIGHTEST);<br />

PROCESSING LANGUAGE API<br />

729<br />

A

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

Saved successfully!

Ooh no, something went wrong!