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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

448<br />

Figure 10-32. Contrast sketch (using bitwise operations)<br />

This is a fairly simple contrast implementation that calculates the difference between the<br />

color component value <strong>and</strong> some threshold—in this case, the midpoint of the 255-value<br />

color range (127.5). This value is multiplied by a contrast value <strong>and</strong> then added to 1;<br />

the color component is then multiplied by this number. So, for example, if the threshold/<br />

contrast expression value evaluates to 1.2, <strong>and</strong> the color component value is 150, it will<br />

become 180. The reason I subtract the threshold from the component is to generate a<br />

nice gradient of values so that the contrast shift will be smooth. For example, a color in the<br />

mid-range (e.g., 129) would be affected very little (multiplied by 1.00225). However, colors<br />

approaching 0 or 255 would be more intensely affected (e.g., an initial component value<br />

of 240, using .15 as the value of the contrast variable, would become 280.5). The difference<br />

in values between the most minimal changes <strong>and</strong> the most severe would be across a<br />

gradient proportional to the difference between the component’s original value <strong>and</strong> the<br />

threshold.<br />

In the example, I was also forced to convert the calculated contrast values from type float<br />

to type int so that I could assign them back to the color component variables, declared as<br />

type int. Finally, I used <strong>Processing</strong>’s constrain() function, which sets a minimum <strong>and</strong><br />

maximum for a value. Since my component values needed to stay within the range of 0 to<br />

255, I clipped them if they exceeded these limits. With a starting value of 240 <strong>and</strong> a contrast<br />

value of .15, the calculated value 280.5 would get constrained to 255. You can try<br />

commenting out the constrain() function calls to see what happens when the values do<br />

go out of range; it can be visually interesting. I’ll finish up this section by looking some<br />

more at <strong>Processing</strong>’s built-in imaging <strong>and</strong> filtering capabilities.<br />

Imaging filters<br />

<strong>Processing</strong>’s PImage class has a method called mask() that allows you to use either an<br />

image or a pixel array to mask out part of an image. The mask image should be grayscale.<br />

(You can use an RGB image, but only the blue channel of the image will be used as the<br />

mask.) My suggestion is to convert your mask image to grayscale before using it.

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

Saved successfully!

Ooh no, something went wrong!