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 />

726<br />

Color, rather than being generated on a per-object basis, is part of an overall rendering<br />

state, or graphics context. In <strong>Processing</strong> <strong>and</strong> Java, this graphics context is controlled by a<br />

Graphics object, which sets properties of the rendering state, such as color <strong>and</strong> font, as<br />

well as a few others. Graphics is the base class in Java that encapsulates this process,<br />

allowing you to write relatively simple code, as in the Java example. <strong>Processing</strong>, as you’d<br />

expect, encapsulates this stuff even further, removing the need for explicitly overriding the<br />

paint() method, as in the Java example. The paint() method is the method that a Java<br />

component (a component can be thought of as a panel—like the display window) calls<br />

when it needs to paint something. What I’m actually doing in the Java example is redefining<br />

the paint() method for my component, so when paint() is called normally by the<br />

component, which happens automatically when the program starts, it will use my paint<br />

method, instead of the component’s default one, to do what I want it to do—in this case,<br />

paint a simple blue rectangle <strong>and</strong> circle on the screen. This process of using one method’s<br />

implementation to replace another is referred to as method overriding, <strong>and</strong> is a fundamental<br />

part of OOP.<br />

If you run the <strong>Processing</strong> code, you’ll notice that the sketch includes two blue shapes. This<br />

is because the state of the rendering context hasn’t been changed between the two calls<br />

to the drawing functions. Thus, the fill is not really about those two shapes, but about the<br />

current rendering state of the program at that point. Until you change the rendering state<br />

color with another fill() comm<strong>and</strong> call, every new filled shape will be blue. I’ll shut up<br />

about this now, but if you want to learn more about the Java Graphics class from Sun,<br />

check out http://java.sun.com/j2se/1.4.2/docs/api/index.html. And here’s a link to<br />

some more info about how painting works in Java: http://java.sun.com/products/<br />

jfc/tsc/articles/painting/.<br />

The next sketch is another example of the Color section’s Setting functions in action (see<br />

Figure A-15). One of the functions, colorMode(), deserves a little clarification. Color can<br />

be specified as RGB (red, green, blue—the default mode) or HSB (hue, saturation, brightness).<br />

In addition, you can set the mathematical range to describe the color, or even each<br />

part of the color. In my two calls to colorMode() in the example, the first call,<br />

colorMode(RGB, 1.0), sets the mode to RGB <strong>and</strong> specifies the range of values to be set for<br />

each component between 0.0 <strong>and</strong> 1.0. Thus, (0, 0, 0) would equal black, <strong>and</strong> (.5, .5, 0)<br />

would equal yellow. The second call is pretty odd, <strong>and</strong> I only did it as an extreme example<br />

of what is possible. colorMode(HSB, 100, 10, .1) sets the mode to HSB. It specifies the<br />

hue value range from 0 to 100 (which is what you typically see with HSB values—0 to 100<br />

percent), the saturation range from 0 to 10, <strong>and</strong> the brightness range from 0 to .1 (which<br />

is pretty odd, indeed). However, it all works, <strong>and</strong> may even impress your friends (probably<br />

not). Wikipedia has an interesting discussion on HSB (sometimes also referred to as HSV)<br />

at http://en.wikipedia.org/wiki/HSB_color_space.<br />

/*<br />

Simple Color Example<br />

Ira Greenberg, November 10, 2005<br />

*/<br />

size(200, 200);<br />

background(200, 50, 40);<br />

stroke(45);<br />

fill(200, 200, 0);<br />

rect(25, 50, 50, 50);

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

Saved successfully!

Ooh no, something went wrong!