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

424<br />

Gradients<br />

I’ll begin with some simple pixel gradients <strong>and</strong> then play with some images. This first<br />

sketch, shown in Figure 10-17, creates a linear gradient <strong>and</strong> utilizes <strong>Processing</strong>’s set()<br />

function. This function allows me to change the color of a pixel, as well as load an image<br />

into the display window. For now, I’ll just show you how to change some pixels. The function<br />

expects three arguments: set(x, y, color).<br />

// Simple Linear Gradient<br />

// January 12, 2006<br />

// x <strong>and</strong> y axis<br />

// constants<br />

int Y_AXIS = 1;<br />

int X_AXIS = 2;<br />

void setup(){<br />

size(400, 400);<br />

// create some gradients<br />

// background<br />

color b1 = color(190, 190, 190);<br />

color b2 = color(20, 20, 20);<br />

setGradient(0, 0, width, height, b1, b2, Y_AXIS);<br />

//center squares<br />

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

color c2 = color(10, 45, 255);<br />

color c3 = color(10, 255, 15);<br />

color c4 = color(125, 2, 140);<br />

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

color c6 = color(25, 255, 200);<br />

setGradient(50, 50, 150, 150, c1, c2, Y_AXIS);<br />

setGradient(200, 50, 150, 150, c3, c4, X_AXIS);<br />

setGradient(50, 200, 150, 150, c2, c5, X_AXIS);<br />

setGradient(200, 200, 150, 150, c4, c6, Y_AXIS);<br />

}<br />

void setGradient(int x, int y, float w, float h, ➥<br />

color c1, color c2, int axis ){<br />

// calculate differences between color components<br />

float deltaR = red(c2)-red(c1);<br />

float deltaG = green(c2)-green(c1);<br />

float deltaB = blue(c2)-blue(c1);<br />

// choose axis<br />

if(axis == Y_AXIS){<br />

/*nested for loops set pixels<br />

in a basic table structure */

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

Saved successfully!

Ooh no, something went wrong!