Created
June 25, 2017 15:50
-
-
Save behreajj/7e9b22acd47921884527f47435cf98dd to your computer and use it in GitHub Desktop.
Color Gradients 1-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PImage bkg; | |
color px; | |
float centerX; | |
float centerY; | |
float run; | |
float rise; | |
float distSq; | |
float invDist; | |
float hypotSq; | |
float red; | |
float green; | |
float blue; | |
float smooth = 0.013; | |
float invSmooth = 1 - smooth; | |
float scalar = 0.125; | |
void setup() { | |
size(512, 256); | |
bkg = loadImage("corfu.png"); | |
bkg.resize(width, height); | |
hypotSq = scalar * (width * width + height * height); | |
} | |
void draw() { | |
// Move flashlight to mouse. | |
centerX = invSmooth * centerX + smooth * mouseX; | |
centerY = invSmooth * centerY + smooth * mouseY; | |
background(bkg); | |
loadPixels(); | |
for (int y = 0, i = 0, x; y < height; ++y) { | |
for (x = 0; x < width; ++x, ++i) { | |
// Calculate rise and run. | |
rise = centerY - y; | |
run = centerX - x; | |
// Find distance squared and normalize. | |
distSq = (run * run + rise * rise) / hypotSq; | |
invDist = 1 - distSq; | |
// Decompose color. | |
px = pixels[i]; | |
red = (px >> 16 & 0xff) * invDist; | |
green = (px >> 8 & 0xff) * invDist; | |
blue = (px & 0xff) * invDist; | |
// Boundary check new values. | |
red = red < 0 ? 0 : red > 255 ? 255 : red; | |
green = green < 0 ? 0 : green > 255 ? 255 : green; | |
blue = blue < 0 ? 0 : blue > 255 ? 255 : blue; | |
// Composite into hex color. | |
pixels[i] = 0xff000000 | |
| round(red) << 16 | |
| round(green) << 8 | |
| round(blue); | |
} | |
} | |
updatePixels(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment