Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. // This produces "Pop Art" in the style of Andy Warhol
  2. // Try out your own images!
  3. // Left and right click to modify the fading/colors.
  4. // Press 1-9 keys to change a single square at a time.
  5.  
  6. PImage img;
  7.  
  8. float[] bgs;
  9. float[] fills;
  10.  
  11. int w = 100;
  12. int h = w;
  13.  
  14. int n;
  15.  
  16. float fade = 1; // goes from 0 to 1 and back
  17. boolean fadeOut = false;
  18. boolean hold = false;
  19.  
  20. String[] urls = new String[]{
  21. // 0 - PLANE
  22. "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Airplane_silhouette.svg/1024px-Airplane_silhouette.svg.png",
  23. // 1 - PIKACHU
  24. "https://s-media-cache-ak0.pinimg.com/originals/95/d5/cd/95d5cded00f3a3e8a98fb1eed568aa9f.png",
  25. // 2 - DRAGON:
  26. "http://orig00.deviantart.net/e271/f/2012/305/7/6/dragon_silhouette_by_xkeren-d5jofma.png",
  27. // 3 - SKATER:
  28. "https://s-media-cache-ak0.pinimg.com/originals/5f/83/ac/5f83acde678ef321c9c858ed2a71bc11.png",
  29. // 4 - SKATER:
  30. "https://openclipart.org/image/800px/svg_to_png/221642/Ice-Skating-Woman-Silhouette.png",
  31. // CUSTOM:
  32. ""
  33. };
  34.  
  35. void setup() {
  36. size(600, 600);
  37. colorMode(HSB, 360, 100, 100, 100);
  38. // 4th # is for opacity maximum (opposite of transparency)
  39. noStroke();
  40.  
  41. // make sure width & height are perfectly divisible
  42. n = (width / w) * (height / h);
  43. bgs = new float[n];
  44. fills = new float[n];
  45.  
  46. // You can change the index to use different images, or add your own!
  47. img = loadImage(urls[4]);
  48.  
  49. // If your image is all black, it will not be tinted
  50. // with color; all white is fully tinted.
  51. // Use this "filter" to invert (flip) colors if you want, or
  52. // comment it out otherwise.
  53. img.filter(INVERT);
  54.  
  55. randomColors();
  56. }
  57.  
  58. void draw() {
  59. drawPopArt();
  60.  
  61. if (!hold) {
  62. // fade to white if not holding mouse
  63. fill(0, 0, 100, fade * 100); // white, transparency goes down as fade goes up
  64. rect(0, 0, width, height);
  65.  
  66. if (fadeOut) {
  67. fade += 0.03;
  68.  
  69. // when it's faded out completely, reset colors
  70. if (fade >= 1) {
  71. fadeOut = false;
  72. randomColors();
  73. }
  74. }
  75. else {
  76. fade -= 0.03;
  77. if (fade <= 0) {
  78. fadeOut = true;
  79. }
  80. }
  81. }
  82. }
  83.  
  84. void drawPopArt() {
  85. // top left square
  86. int x = 0;
  87. int y = 0;
  88.  
  89. for (int i = 0; i < n; i++) {
  90. // draw square
  91. fill(bgs[i], 100, 100);
  92. rect(x, y, w, h);
  93.  
  94. tint(fills[i], 100, 100);
  95. image(img, x, y, w, h);
  96.  
  97. // move to right
  98. x += w;
  99.  
  100. // or move to beginning of next row
  101. if (x >= width) {
  102. x = 0;
  103. y += h;
  104. }
  105. }
  106. }
  107.  
  108. void randomColors() {
  109. for (int i = 0; i < n; i++) {
  110. bgs[i] = random(0, 360);
  111. fills[i] = random(0, 360);
  112. }
  113. }
  114.  
  115. // sets colors evenly distributed along rainbow
  116. void rainbowColors() {
  117. float huePerSquare = 360 / n;
  118. float h = random(0, 360); // random starting point
  119. for (int i = 0; i < n; i++) {
  120. // % 360 ensures colors stay within 0-360
  121. bgs[i] = h % 360;
  122. // fill color is 180 degrees (opposite) bg color
  123. fills[i] = (h + 180) % 360;
  124.  
  125. h += huePerSquare;
  126. }
  127. }
  128.  
  129. void mousePressed() {
  130. if (mouseButton == RIGHT) {
  131. randomColors();
  132. }
  133. else {
  134. hold = true;
  135. }
  136. }
  137.  
  138. void mouseReleased() {
  139. hold = false;
  140. fade = 0; // reset fade after holding
  141. }
  142.  
  143. void keyPressed() {
  144. if ('1' <= key && key <= '9') {
  145. int num = key - '0'; // translate char data to int data
  146. int i = num - 1;
  147.  
  148. bgs[i] = random(0, 360);
  149. fills[i] = random(0, 360);
  150. }
  151. else if (key == ' ') {
  152. randomColors();
  153. }
  154. else if (key == 'r') {
  155. rainbowColors();
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement