Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
color-cube.pde
/**
* Bouncing ball modified from http://www.instructables.com/id/Control-a-RepStrap-RepRap-CNCCartesian-Bot-usi/step20/3D-Bouncing-Ball-Processing/
NOTE: 0,0,0 - top left corner (facing the computer monitor)
negative Z axis points 'into' the computer monitor
positive Y axis points down
positive X axis points to the right
*/
int size = 20; // Width of the shape
float xpos, ypos, zpos; // Starting position of shape
float depth;
int cubeSize = 400;
int colorSize = 400;
int marginSize = 200;
float xspeed = 1.5; // Speed of the shape
float yspeed = 1.5; // Speed of the shape
float zspeed = 1.5;
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int zdirection = 1; //front or back
void setup() {
colorMode(HSB, 360, 1, 1);
size(cubeSize + 2 * marginSize + colorSize, 400, P3D);
noStroke();
frameRate(30);
smooth();
// Set the starting position of the shape
xpos = cubeSize/2;
ypos = cubeSize/2;
zpos = -cubeSize/2; //note that Zaxis goes 'into' the screen
depth = -cubeSize;
}
void draw() {
clear();
background(#ffffff);
lights();
//glass box //note: line(x1, y1, z1, x2, y2, z2)
stroke(0);
//back
//line(0,0,depth, cubeSize,0,depth);
fill(0);
textAlign(CENTER);
textSize(32);
text("Cosmic Latte", cubeSize, cubeSize - 50, depth);
text("Blue Beginning", 0, -20, depth);
textSize(22);
text("Red Shift", 0, cubeSize - 20, depth/3);
line(0,cubeSize,depth, cubeSize,cubeSize,depth);
line(0,0,depth, 0,cubeSize,depth);
//line(cubeSize,0,depth, cubeSize,cubeSize,depth);
//corners
//line(0,0,0, 0,0,depth);
line(0,cubeSize, depth/(2*1.41421356237), 0,cubeSize,depth);
//line(cubeSize,0,0, cubeSize,0,depth);
//line(cubeSize,cubeSize,0, cubeSize,cubeSize,depth);
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
zpos = zpos + ( zspeed * zdirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > cubeSize-size || xpos < 0) {
xdirection *= -1;
}
if (ypos > cubeSize-size || ypos < 0) {
ydirection *= -1;
}
if (zpos < -cubeSize-size || zpos > 0) { //note that Zaxis goes 'into' the screen
zdirection *= -1;
}
// Draw the shape
lights();
pushMatrix();
translate(xpos, ypos, zpos);
fill(0);
//stroke(#aaaaaa);
noStroke();
sphere(size);
// 2D color stuff
popMatrix();
noLights();
float transX = xpos;
float transY = cubeSize - ypos;
float transZ = cubeSize - Math.abs(zpos);
color latte = getLatte(transX / cubeSize);
color blue = getBlueBeginning(transY / cubeSize);
color red = getRedShift(transZ / cubeSize);
fill(latte);
rect(marginSize + cubeSize, 50, colorSize/3., colorSize/4.);
fill(blue);
rect(marginSize + cubeSize + colorSize/3., 50, colorSize/3., colorSize/4.);
fill(red);
rect(marginSize + cubeSize + 2*colorSize/3., 50, colorSize/3., colorSize/4.);
color c = calculateColor(transX / cubeSize, transY / cubeSize, transZ / cubeSize);
fill(c);
rect(marginSize + cubeSize, 50 + colorSize/4., colorSize, colorSize/2);
}
/*
* Return a color based on a position in the unit cube
*/
color calculateColor(float x, float y, float z) {
// x - cosmic latte
// y - blue beginning
// z - red-shift
color currentLatte = getLatte(x);
color currentBlueBeginning = getBlueBeginning(y);
color currentRedShift = getRedShift(z);
color blueLatte = lerpColor(currentLatte, currentBlueBeginning, y);
color redLatte = lerpColor(currentLatte, currentRedShift, z);
return lerpColor(blueLatte, redLatte, x);
}
color getLatte(float x) {
float targetSat = .094118;
float satVar = 0.06;
return color(42.5, map(x, 0, 1, targetSat - satVar, targetSat + satVar), 1);
}
color getBlueBeginning(float x) {
float targetSat = .407;
float satVar = 0.2;
return color(212, map(x, 0, 1, targetSat - satVar, targetSat + satVar), 0.992);
}
color getRedShift(float x) {
float targetSat = .38;
float satVar = 0.1;
return color(17, map(x, 0, 1, targetSat - satVar, targetSat + satVar), 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.