// BoxFit03 (jiggly colored)
// David Bollinger, July 2006
// http://www.davebollinger.com
// for Processing 0109 beta
// (0115 has trouble loading fasa300.jpg from applet)
//
/**
BoxFit03 - "JigglyColoredBoxFitter" (derived class)
Assigns color to areas based on an underlying texture.
Jiggles (rotates) those areas based on hue/saturation/brightness.
Press 'h' to jiggle by hue (default).
Press 's' to jiggle by saturation.
Press 'b' to jiggle by brightness.
Click to advance early to next pattern.
*/
JigglyColoredBoxFitter fitter;
int currentSeed = 0;
int nextwait = 0;
PImage texture;
void setup() {
size(300,300);
smooth();
texture = loadImage("fasa300.jpg");
fitter = new JigglyColoredBoxFitter(texture,4,4,8);
framerate(30);
next();
}
void next() {
background(color(255,255,255));
fitter.make(++currentSeed);
}
void draw() {
if (nextwait > 0) {
if (--nextwait <= 0) {
next();
}
} else {
fitter.drawone();
if (fitter.at00())
nextwait = 5*30;
}
}
void keyPressed() {
if (key=='h') fitter.rotby = fitter.ROT_BY_H;
if (key=='s') fitter.rotby = fitter.ROT_BY_S;
if (key=='b') fitter.rotby = fitter.ROT_BY_B;
if (key=='`') saveFrame("frame.tga");
}
void mousePressed() {
next();
}
class JigglyColoredBoxFitter extends BoxFitter{
PImage texture;
static final int ROT_BY_H = 0;
static final int ROT_BY_S = 1;
static final int ROT_BY_B = 2;
int rotby;
JigglyColoredBoxFitter(PImage _texture, int _divx, int _divy, int _maxsizer) {
super(_texture.width, _texture.height, _divx, _divy, _maxsizer);
texture = _texture;
rotby = ROT_BY_H;
}
// get the representative color from texture for specified region
int getRegionColor(int x1, int y1, int wid, int hei) {
// "easiest" method would be to just return center pixel...
// return texture.get(x1+wid/2, y1+hei/2);
//
// "best" (?) method would be to get() the entire area and average ALL pixels
// [overkill]
//
// the "compromise" adopted is to average the 4 pixels at center of quads
color q1 = texture.get(x1+wid/4, y1+hei/4);
color q2 = texture.get(x1+wid*3/4, y1+hei/4);
color q3 = texture.get(x1+wid/4, y1+hei*3/4);
color q4 = texture.get(x1+wid*3/4, y1+hei*3/4);
float r = (red(q1)+red(q2)+red(q3)+red(q4))/4.0;
float g = (green(q1)+green(q2)+green(q3)+green(q4))/4.0;
float b = (blue(q1)+blue(q2)+blue(q3)+blue(q4))/4.0;
return color(r,g,b);
}
void render(int x1, int y1, int w, int h) {
color c = getRegionColor(x1,y1,w,h);
fill(c);
stroke(color(red(c)/2,green(c)/2,blue(c)/2,128));
float theta = 0.0;
switch(rotby) {
case ROT_BY_H : theta = hue(c) / 255.0 * HALF_PI; break;
case ROT_BY_S : theta = saturation(c) / 255.0 * HALF_PI; break;
case ROT_BY_B : theta = brightness(c) / 255.0 * HALF_PI; break;
}
pushMatrix();
translate(x1+w/2,y1+h/2);
rotate(theta);
rectMode(CENTER);
rect(0,0,w,h);
popMatrix();
}
}