Skip to content

Instantly share code, notes, and snippets.

@grifdail
Last active May 19, 2017 11:49
Show Gist options
  • Save grifdail/b561ff42d461057978f4f33da6836b0e to your computer and use it in GitHub Desktop.
Save grifdail/b561ff42d461057978f4f33da6836b0e to your computer and use it in GitHub Desktop.
Use a color to create a gradient
class TextureGradient {
public color[] pixels;
TextureGradient(String name) {
PImage background = loadImage(name);
background.loadPixels();
pixels = background.pixels;
}
color get(float t) {
int currentMode = g.colorMode;
colorMode(RGB);
t*=pixels.length;
color tMin = pixels[max(min(int(t),pixels.length-1),0)];
color tMax = pixels[min(int(t+1),pixels.length-1)];
color result = lerpColor(tMin, tMax, t%1);
colorMode(currentMode);
return result;
}
color getLinear(float t) {
t*=backgroundColors.length;
return pixels[max(min(int(t),pixels.length-1),0)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment