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

114<br />

24-bit color value is literally just a 24-digit number, made up of zeros <strong>and</strong> ones, where the<br />

first 8 bits are used for blue, the next 8 bits are used for green, <strong>and</strong> the last 8 bits are used<br />

for red. If you raise 2 to the 24th power, you get the same answer as before: 16,777,216<br />

possible colors. If you’ve ever used a control panel device to change your screen resolution,<br />

you’ve probably seen 16.7 million colors—or just “millions of colors”—as a choice.<br />

When I first got involved in computer graphics in 1992, it was common to have systems<br />

that couldn’t h<strong>and</strong>le 24-bit color, <strong>and</strong> you were forced to use either 16-bit or even 8-bit<br />

color depths, which only permitted a range of 65,536 colors or 256 colors, respectively.<br />

8-bit displays required fixed palettes composed of only 256 colors. 8-bit color is referred<br />

to as indexed color, while 24-bit color is referred to as true color. Working in 8-bit color<br />

space, we were forced to choose the 256 colors we wanted to use for a project, <strong>and</strong> it was<br />

not easy to change the palette on the fly; it also didn’t help that Windows <strong>and</strong> Mac had<br />

separate palettes. The web safe palette, which some of you might be familiar with, was one<br />

such made-up palette. It only included 216 of the 256 colors common to both Mac <strong>and</strong> PC<br />

platforms. These limited palettes led to pretty dithered images. Dithering occurs when colors<br />

outside of the range of the palette need to be approximated, which is done through<br />

the grouping of adjacent colors—optically mixing the necessary color. Dithering helps create<br />

the illusion of a wider color gamut on an 8-bit system, but usually leads to pretty<br />

grainy looking images.<br />

Although contemporary monitors display 24 bits of information for each pixel, internally it<br />

is possible to pack even more information into a pixel data structure. Photoshop, as an<br />

example, supports 48-bit color, as do some scanners <strong>and</strong> digital cameras, allowing each<br />

pixel to utilize 16 bits for each color component—that’s 65,536 unique values for each<br />

red, green, <strong>and</strong> blue component, or a total of over 281 trillion (not a typo) different colors.<br />

I know 281 trillion colors sounds bizarre, <strong>and</strong> nobody uses (or needs) all those colors,<br />

but the additional color space supports an increased range of visual detail in areas like<br />

shadows. Digital photographers especially benefit from the increased bit depth, as they<br />

can combine numerous exposures into a single image, creating images with a higher<br />

dynamic range than possible with traditional methods. 48-bit images are also utilized in 3D<br />

as radiance maps for rendering composited images, in which a 3D, computer-generated<br />

model is seamlessly blended into a photographic environment. The radiance maps work as<br />

powerful sources of illumination in the rendering process.<br />

Java’s BufferedImage class can h<strong>and</strong>le 32 bits per pixel, <strong>and</strong> adds an additional 8-bit alpha<br />

component to the RGB 24-bit structure. Alpha controls the translucency of the pixel, ranging<br />

from 100 percent, which is opaque, to 0 percent, which is transparent. The integer data<br />

type in Java <strong>and</strong> <strong>Processing</strong> is 32 bits in length, making it very convenient to store pixel values<br />

as individual integers—holding red, blue, green, <strong>and</strong> alpha, commonly abbreviated as<br />

RGBA. If your head isn’t spinning yet, you can see that, in spite of its complexity, the<br />

BufferedImage class really h<strong>and</strong>les a lot. And in spite of the complexity involved in using<br />

it, coding graphics in Java would still be a lot scarier if you had to deal with all the mathematical<br />

calculations <strong>and</strong> color transformations the BufferedImage class h<strong>and</strong>les internally.<br />

In contrast to using the BufferedImage class in Java, in <strong>Processing</strong> you can simply call two<br />

or three functions to get some similar low-level access. Of course, you have less access <strong>and</strong><br />

flexibility than working in pure Java—but most of the stuff you really need or want to use<br />

is available in <strong>Processing</strong>, so you really can have your cake <strong>and</strong> eat it too. As I mentioned<br />

in earlier chapters, as you get deeper into graphics programming, <strong>Processing</strong>’s genius

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

Saved successfully!

Ooh no, something went wrong!