/**
* grid.pde
* version 0.03
* written by d5c
* http://littlesecretsrecords.com/d5c/
* d5c@littlesecretsrecords.com
*
* Flow continues indefinitely.
* There is no user interaction.
* Colors fade out after change.
* Colors fade a little deeper now.
**/
// variables for main
Board myBoard;
void setup() {
//size(500, 500);
size(800, 600);
background(0);
loop();
noCursor();
myBoard = new Board();
}
void draw() {
background(0);
myBoard.sequentialChange();
myBoard.draw();
}
/*******************************************************
******************Class Definitions********************
*******************************************************/
class Space {
// static variables
static public final int spaceWidth = 10; // width of one space in pixels
static public final int spaceHeight = 10; // height of one space in pixels
// local variables
private int x; // x location of this space
private int y; // y location of this space
private int myColor; // color of this space, range 0-9
private int randShift; // tells the Space how to change based on surrounding colors
private float timeLastChanged; // Time of last color change - used to fade out colors
public Space (int x, int y, int initColor) {
this.x = x;
this.y = y;
this.myColor = initColor;
this.randShift = (int)random(999999999);
}
public void change (int newColor) {
// change to a new Color based on surrounding colors and self
// newColor is 9 digits
Random r = new Random(newColor * this.randShift);
int colorResult = r.nextInt(40);
if (colorResult < 10 && colorResult != this.myColor) { // No change for values >= 10 or for same color
this.myColor = colorResult;
this.timeLastChanged = millis();
}
}
public int getColor() {
return this.myColor;
}
private color getRGBcolor() {
switch (this.myColor) {
case 0: return color(0, 0, 0); // black
case 1: return color(165, 42, 42); // brown
case 2: return color(255, 0, 0); // red
case 3: return color(255, 165, 0); // orange
case 4: return color(255, 255, 0); // yellow
case 5: return color(0, 255, 0); // green
case 6: return color(0, 0, 255); // blue
case 7: return color(238, 130, 238); // violet
case 8: return color(190, 190, 190); // grey
case 9: return color(255, 255, 255); // white
default: println("Invalid Color");
return color(0, 0, 0);
}
}
public void draw() {
noStroke();
color baseColor = getRGBcolor();
float fadeAmount = 1 - min(millis()-this.timeLastChanged, 2000)*0.00035; // the last number used to be 0.00025
colorMode(HSB); // change to HSB mode to make this easier
fill(hue(baseColor), saturation(baseColor), brightness(baseColor)*fadeAmount);
colorMode(RGB);
rect(this.x, this.y, Space.spaceWidth, Space.spaceHeight);
}
}
class Board {
// local variables
private int rowSize;
private int colSize;
private Space[][] mySpaces;
public Board() {
this.rowSize = width / Space.spaceWidth;
this.colSize = height / Space.spaceHeight;
// initialize array of spaces with random colors
this.mySpaces = new Space[this.rowSize][this.colSize];
for (int i=0; i wrap around to bottom
if (j == 0) { // left col -> wrap around to right
surColors += this.mySpaces[this.rowSize-1][this.colSize-1].getColor() * pow(10,8);
surColors += this.mySpaces[this.rowSize-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[this.rowSize-1][j+1].getColor() * pow(10,6);
surColors += this.mySpaces[i][this.colSize-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][j+1].getColor() * pow(10,3);
surColors += this.mySpaces[i+1][this.colSize-1].getColor() * pow(10,2);
surColors += this.mySpaces[i+1][j].getColor() * pow(10,1);
surColors += this.mySpaces[i+1][j+1].getColor() * pow(10,0);
} else if (j == this.colSize-1) { // right col -> wrap around to left
surColors += this.mySpaces[this.rowSize-1][j-1].getColor() * pow(10,8);
surColors += this.mySpaces[this.rowSize-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[this.rowSize-1][0].getColor() * pow(10,6);
surColors += this.mySpaces[i][j-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][0].getColor() * pow(10,3);
surColors += this.mySpaces[i+1][j-1].getColor() * pow(10,2);
surColors += this.mySpaces[i+1][j].getColor() * pow(10,1);
surColors += this.mySpaces[i+1][0].getColor() * pow(10,0);
} else {
surColors += this.mySpaces[this.rowSize-1][j-1].getColor() * pow(10,8);
surColors += this.mySpaces[this.rowSize-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[this.rowSize-1][j+1].getColor() * pow(10,6);
surColors += this.mySpaces[i][j-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][j+1].getColor() * pow(10,3);
surColors += this.mySpaces[i+1][j-1].getColor() * pow(10,2);
surColors += this.mySpaces[i+1][j].getColor() * pow(10,1);
surColors += this.mySpaces[i+1][j+1].getColor() * pow(10,0);
}
} else if (i == this.rowSize-1) { // bottom row -> wrap around to top
if (j == 0) { // left col -> wrap around to right
surColors += this.mySpaces[i-1][this.colSize-1].getColor() * pow(10,8);
surColors += this.mySpaces[i-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[i-1][j+1].getColor() * pow(10,6);
surColors += this.mySpaces[i][this.colSize-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][j+1].getColor() * pow(10,3);
surColors += this.mySpaces[0][this.colSize-1].getColor() * pow(10,2);
surColors += this.mySpaces[0][j].getColor() * pow(10,1);
surColors += this.mySpaces[0][j+1].getColor() * pow(10,0);
} else if (j == this.colSize-1) { // right col -> wrap around to left
surColors += this.mySpaces[i-1][j-1].getColor() * pow(10,8);
surColors += this.mySpaces[i-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[i-1][0].getColor() * pow(10,6);
surColors += this.mySpaces[i][j-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][0].getColor() * pow(10,3);
surColors += this.mySpaces[0][j-1].getColor() * pow(10,2);
surColors += this.mySpaces[0][j].getColor() * pow(10,1);
surColors += this.mySpaces[0][0].getColor() * pow(10,0);
} else {
surColors += this.mySpaces[i-1][j-1].getColor() * pow(10,8);
surColors += this.mySpaces[i-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[i-1][j+1].getColor() * pow(10,6);
surColors += this.mySpaces[i][j-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][j+1].getColor() * pow(10,3);
surColors += this.mySpaces[0][j-1].getColor() * pow(10,2);
surColors += this.mySpaces[0][j].getColor() * pow(10,1);
surColors += this.mySpaces[0][j+1].getColor() * pow(10,0);
}
} else {
if (j == 0) { // left col -> wrap around to right
surColors += this.mySpaces[i-1][this.colSize-1].getColor() * pow(10,8);
surColors += this.mySpaces[i-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[i-1][j+1].getColor() * pow(10,6);
surColors += this.mySpaces[i][this.colSize-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][j+1].getColor() * pow(10,3);
surColors += this.mySpaces[i+1][this.colSize-1].getColor() * pow(10,2);
surColors += this.mySpaces[i+1][j].getColor() * pow(10,1);
surColors += this.mySpaces[i+1][j+1].getColor() * pow(10,0);
} else if (j == this.colSize-1) { // right col -> wrap around to left
surColors += this.mySpaces[i-1][j-1].getColor() * pow(10,8);
surColors += this.mySpaces[i-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[i-1][0].getColor() * pow(10,6);
surColors += this.mySpaces[i][j-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][0].getColor() * pow(10,3);
surColors += this.mySpaces[i+1][j-1].getColor() * pow(10,2);
surColors += this.mySpaces[i+1][j].getColor() * pow(10,1);
surColors += this.mySpaces[i+1][0].getColor() * pow(10,0);
} else {
surColors += this.mySpaces[i-1][j-1].getColor() * pow(10,8);
surColors += this.mySpaces[i-1][j].getColor() * pow(10,7);
surColors += this.mySpaces[i-1][j+1].getColor() * pow(10,6);
surColors += this.mySpaces[i][j-1].getColor() * pow(10,5);
surColors += this.mySpaces[i][j].getColor() * pow(10,4);
surColors += this.mySpaces[i][j+1].getColor() * pow(10,3);
surColors += this.mySpaces[i+1][j-1].getColor() * pow(10,2);
surColors += this.mySpaces[i+1][j].getColor() * pow(10,1);
surColors += this.mySpaces[i+1][j+1].getColor() * pow(10,0);
}
}
this.mySpaces[i][j].change(surColors);
}
}
}
public void randomChange() {
}
public void draw() {
for (int i=0; i