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.

Figure 10-30. Tint sketch<br />

Speeding things up with bitwise operations<br />

You might have noticed that this last sketch took some time to render (or maybe your<br />

computer is faster than mine). There is a faster, albeit lower-level, way to manipulate the<br />

individual color components packed inside the color data type. The color data type combines<br />

the individual color components alpha, red, green, <strong>and</strong> blue into 32 bits of data. The<br />

structure of the color data type looks like this: AAAAAAAARRRRRRRRGGGGGGGG-<br />

BBBBBBBB. Of course, the letters would be replaced by zeros <strong>and</strong> ones, depending on the<br />

color specified. For example, run the following sketch to see the bit string for blue:<br />

color c = color(0, 0, 255);<br />

println(binary(c));<br />

In the sketch output, the alpha <strong>and</strong> blue bits are all ones, since I specified maximum value<br />

for blue (255), <strong>and</strong> alpha is 255 by default. Likewise, the red <strong>and</strong> green bits are all zeros,<br />

per the original color value I set—color(0, 0, 255).<br />

Bitwise operations allow you to directly manipulate the bits, shifting <strong>and</strong> even doing<br />

simple mathematical operations on them. Bitwise operations are not for everyone, so<br />

don’t fret if you can’t deal with this approach. However, it’s good to know that the possibility<br />

exists, <strong>and</strong> they are quite speedy. I provide a detailed overview of bitwise operations<br />

in Appendix B, so you might want to read that before trying these next few sketches. If,<br />

however, you just want to use bitwise operations with a limited underst<strong>and</strong>ing of how they<br />

actually work, here’s the highly abridged “recipe” version:<br />

COLOR AND IMAGING<br />

443<br />

10

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

Saved successfully!

Ooh no, something went wrong!