Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. PImage bkg;
  2.  
  3. color px;
  4.  
  5. float centerX;
  6. float centerY;
  7. float run;
  8. float rise;
  9. float distSq;
  10. float invDist;
  11. float hypotSq;
  12. float red;
  13. float green;
  14. float blue;
  15. float smooth = 0.013;
  16. float invSmooth = 1 - smooth;
  17. float scalar = 0.125;
  18.  
  19. void setup() {
  20. size(512, 256);
  21. bkg = loadImage("corfu.png");
  22. bkg.resize(width, height);
  23. hypotSq = scalar * (width * width + height * height);
  24. }
  25.  
  26. void draw() {
  27.  
  28. // Move flashlight to mouse.
  29. centerX = invSmooth * centerX + smooth * mouseX;
  30. centerY = invSmooth * centerY + smooth * mouseY;
  31.  
  32. background(bkg);
  33. loadPixels();
  34. for (int y = 0, i = 0, x; y < height; ++y) {
  35. for (x = 0; x < width; ++x, ++i) {
  36.  
  37. // Calculate rise and run.
  38. rise = centerY - y;
  39. run = centerX - x;
  40.  
  41. // Find distance squared and normalize.
  42. distSq = (run * run + rise * rise) / hypotSq;
  43. invDist = 1 - distSq;
  44.  
  45. // Decompose color.
  46. px = pixels[i];
  47. red = (px >> 16 & 0xff) * invDist;
  48. green = (px >> 8 & 0xff) * invDist;
  49. blue = (px & 0xff) * invDist;
  50.  
  51. // Boundary check new values.
  52. red = red < 0 ? 0 : red > 255 ? 255 : red;
  53. green = green < 0 ? 0 : green > 255 ? 255 : green;
  54. blue = blue < 0 ? 0 : blue > 255 ? 255 : blue;
  55.  
  56. // Composite into hex color.
  57. pixels[i] = 0xff000000
  58. | round(red) << 16
  59. | round(green) << 8
  60. | round(blue);
  61. }
  62. }
  63. updatePixels();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement