/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://playgramming.sketchpad.cc/sp/pad/view/ro.kD5Z$vPuAWW/rev.290
*
* authors:
* Devon Scott-Tunkin
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
color red = color(255, 0, 0);
color black = color(0, 0, 255);
int Y_AXIS = 1;
int X_AXIS = 2;
void setup() { // this is run once.
size(300, 300);
}
void draw() { // this is run repeatedly.
for (int x = 0; x < width; x = x + 1) {
strokeWeight(1);
float percent = x / width;
stroke(lerpColor(red, black, percent));
line(0, x, width, x);
setGradient(x, x, 100, 100, black, red, X_AXIS);
}
}
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {
noFill();
if (axis == Y_AXIS) { // Top to bottom gradient
for (int i = y; i <= y+h; i++) {
float inter = map(i, y, y+h, 0, 1);
color c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x+w, i);
}
}
else if (axis == X_AXIS) { // Left to right gradient
for (int i = x; i <= x+w; i++) {
float inter = map(i, x, x+w, 0, 1);
color c = lerpColor(c1, c2, inter);
stroke(c);
line(i, y, i, y+h);
}
}
}