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

762<br />

being either open or closed wouldn’t give us much logic to work with, but if you take all<br />

the transistors on a CPU, <strong>and</strong> begin coding logic based on series of these gates being open<br />

or closed, well, the possibilities are limitless. And since the core processing logic at the<br />

chip level is based on this binary (zeros <strong>and</strong> ones) system, bitwise operations (also based<br />

on a binary system) are really efficient, which means large amounts of data can be<br />

processed more quickly. This speed benefit is the main reason it’s worth learning to<br />

directly manipulate bits with regard to graphics computing.<br />

Color data structure<br />

Besides the silicon story, there is another practical reason why bitwise operations work<br />

well with color, <strong>and</strong> it relates to how color is stored <strong>and</strong> structured in memory.<br />

Color information is stored in what’s referred to as a packed 32-bit integer, representing<br />

alpha, red, green, <strong>and</strong> blue. The reason the integer is referred to as “packed” is because<br />

the components—alpha (A), red (R), green (G), <strong>and</strong> blue (B)—are divided up into distinct,<br />

delineated 8-bit sections. The following line shows how the color is actually stored as an<br />

integer (the letter of the color is used to show the place in the integer):<br />

AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB<br />

In actuality, of course, the values of each of the bits can only be 0 or 1. Remember, a 32bit<br />

integer is just a binary (base 2) number to 32 places. Also, alpha by default is set to<br />

100% when you create a color (which equals all ones for its eight bits). So, for example, the<br />

color purple at 100 percent alpha would be represented like this: 11111111 11111111<br />

00000000 11111111. (I separated the 32 bits into four 8-bit groups just to help you visualize<br />

where each of the components is stored in the continuous 32-bit string). Likewise, the<br />

color red would be 11111111 11111111 00000000 00000000, <strong>and</strong> blue would be 11111111<br />

00000000 00000000 11111111. If this still isn’t clear, maybe this list will help (please note<br />

that the bits are counted from right to left):<br />

Alpha: Bits 25 through 32<br />

Red: Bits 17 through 24<br />

Green: Bits 9 through 16<br />

Blue: Bits 1 through 8<br />

What is confusing about the color integer (at first glance) is that the 8 bits controlling<br />

alpha seem to be on the wrong side of the bit string, since alpha is normally specified as<br />

the fourth argument when you create an (RGBA) color, as in color c = color(255, 127,<br />

0, 255). However, in the integer, alpha is specified in the first 8 bits. I’m also using the<br />

default value for alpha, which is 100 percent, which is why all the places (where alpha is<br />

specified) are 1.<br />

OK, but that still doesn’t explain why the value of eight ones is equal to 100 percent alpha?<br />

The trick to converting a binary value (like eight ones) back to our more familiar decimal<br />

(base 10) system is to think about how numbers are represented, which is easiest for us to<br />

do in our base 10 system. For example, what’s the difference between the numbers 52,<br />

479, <strong>and</strong> 100,000? The key to answering this question is found in the number of places in

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

Saved successfully!

Ooh no, something went wrong!