Move cursor left-right to control speed / click on window for new palette.
xxxxxxxxxx
// An analogous palette generator using "fish" particles.
// http://www.tigercolor.com/color-lab/color-theory/color-harmonies.htm
// Move cursor left-right to control speed / click on window for new palette.
// array, noise, particle, particle system, class, random, map, line, palette, string
int num;
Particle[] particles;
int[] palette;
String s0, s1, s2, s3, s4;
void setup() {
size(800, 400);
background(50);
colorMode(HSB, 360, 100, 100);
smooth();
setPalette();
setParticles();
}
void draw() {
noStroke();
fill(0, 5);
rect(0, 0, width, height);
for (int i = 0; i < num; i++) {
particles[i].move();
}
displayInfo();
}
// %%%%%%%% class and helper functions %%%%%%%%%%%%%%%%
void setPalette() {
palette = new int[5];
int dst = 20, sat;
int h0 = int(random(0, 360));
int h1 = (h0 + dst) % 360;
int h2 = (h0 + 2*dst) % 360;
int h3 = (h0 + 3*dst) % 360;
int h4 = (h0 + 4*dst) % 360;
sat = int(random(70, 100));
s0 = "H:" + h0 + " S:" + sat + " B:" + "100";
palette[0] = color(h0, sat, 100);
sat = int(random(20, 80));
s1 = "H:" + h1 + " S:" + sat + " B:" + "100";
palette[1] = color(h1, sat, 100);
sat = int(random(20, 80));
s2 = "H:" + h2 + " S:" + sat + " B:" + "100";
palette[2] = color(h2, sat, 100);
sat = int(random(20, 80));
s3 = "H:" + h3 + " S:" + sat + " B:" + "100";
palette[3] = color(h3, sat, 100);
sat = int(random(20, 80));
s4 = "H:" + h4 + " S:" + sat + " B:" + "100";
palette[4] = color(h4, sat, 100);
};
void setParticles() {
// background(255);
int cols = 16;
int rows = 16;
float cw = width/cols;
float rh = height/rows;
num = (cols+1) * (rows+1);
particles = new Particle[num];
float u = 0;
int j = 0;
float sze = 30;
for (float y=0; y<=height; y+=rh) {
for (float x=0; x<=width; x+=cw) {
particles[j] = new Particle(x, y, sze, u);
u+= .02;
j++;
}
}
}
void mousePressed() {
setPalette();
setParticles();
}
class Particle {
int c, index;
float s, speed, max, incr, incr2, ns, x, y, x2, y2, setX, setY;
Particle(float xIn, float yIn, float sIn, float incr2In) {
x = xIn;
y = yIn;
s = max = sIn;
incr2 = incr2In;
setX = random(width);
setY = random(height);
index = int(random(0, 5));
c = palette[index];
incr = -random (.1, .4);
}
void move() {
// update position and control speed
speed = map(mouseX, 0, width, 0, 2);
ns = ((noise(incr2, x/setX, y/setY)-0.5)*setX) * (TWO_PI/360);
x+=cos(ns)*speed;
y+=sin(ns)*speed;
incr2 += .01;
// display
s += incr;
if (s <= 0) {
s = 0;
incr *= -1;
// s += incr;
} else if (s >= max) {
s = max;
incr *= -1;
}
x2 = x;
y2 = y;
stroke(c);
strokeWeight(s);
line(x2, y2, x, y);
// wrap
if (x < -s) {
x = x2 = width + s;
} else if (x>width+s) {
x = x2= -s;
}
if (y < -s) {
y = y2 = height + s;
} else if (y > height + s) {
y = y2 = -s;
}
}
}
void displayInfo() {
fill(50);
stroke(#959494);
strokeWeight(2);
rect(10, height-40, width-20, 30, 10);
textAlign(CENTER);
textSize(15);
fill(palette[0]);
text(s0, width/2-315, height-20);
fill(palette[1]);
text(s1, width/2-160, height-20);
fill(palette[2]);
text(s2, width/2, height-20);
fill(palette[3]);
text(s3, width/2+160, height-20);
fill(palette[4]);
text(s4, width/2+315, height-20);
}