Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement