Skip to content

Instantly share code, notes, and snippets.

@behreajj
Created November 16, 2017 17:31
Show Gist options
  • Select an option

  • Save behreajj/35ba2976882e2b028b0ca1b55fb38291 to your computer and use it in GitHub Desktop.

Select an option

Save behreajj/35ba2976882e2b028b0ca1b55fb38291 to your computer and use it in GitHub Desktop.
Color Gradient V2_11
int halfh;
color startc, stopc;
float w, step0, step1, stepc;
float[] startf, stopf, current = new float[4];
String fmt = "%s\tX %.2f\tY %.2f\tZ %.2f\tW %.2f";
void setup() {
size(512, 256);
halfh = height / 2;
w = float(width);
rndclrs();
}
void draw() {
surface.setTitle(String.format("fps %.2f f %d s %.2f",
frameRate, frameCount, millis() * .001));
background(0xffffffff);
step1 = abs(cos(frameCount * .01));
for (int i = 0; i < width; ++i) {
step0 = i / w;
stepc = step0 * step1;
// Draw Processing gradient.
stroke(lerpColor(startc, stopc, stepc, RGB));
line(i, 0, i, halfh);
// Draw custom gradient.
smootherStepRgb(startf, stopf, stepc, current);
stroke(composeclr(current));
line(i, halfh, i, height);
}
}
void rndclrs() {
// Create random RGB values.
startf = new float[] { random(1.0), random(1.0), random(1.0), 1.0 };
stopf = new float[] { random(1.0), random(1.0), random(1.0), 1.0 };
// Convert to colors for Processing gradient.
startc = composeclr(startf);
stopc = composeclr(stopf);
// Print color values.
println(String.format(fmt,
hex(startc), startf[0], startf[1], startf[2], startf[3]));
println(String.format(fmt,
hex(stopc), stopf[0], stopf[1], stopf[2], stopf[3]));
}
void mousePressed() {
rndclrs();
}
float smootherStep(float st) {
return st * st * st * (st * (st * 6.0 - 15.0) + 10.0);
}
float[] smootherStepRgb(float[] a, float[] b, float st, float[] out) {
float eval = smootherStep(st);
out[0] = a[0] + eval * (b[0] - a[0]);
out[1] = a[1] + eval * (b[1] - a[1]);
out[2] = a[2] + eval * (b[2] - a[2]);
out[3] = a[3] + eval * (b[3] - a[3]);
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment