import javax.swing.JFileChooser; PImage img; final int HEIGHT = 500; final int WIDTH = 800; /** image height and width */ int iw = WIDTH/3 - 20; int ih = 200; /** red, green, blue components */ PImage ired, igreen, iblue; PImage redcount, greencount, bluecount; int rx, ry, bx, by, gx, gy, ox, oy; final int MOVESPEED = 2; int tintval; final int TINTSPEED = 3; /** milliseconds to wait after loading the image */ final int WAIT_AFTER_LOAD = 2000; int wait; int lastSecond=0; PFont font; int state; final int STATE_WAIT_SEPARATE = 1, STATE_SEPARATING = 2, STATE_WAIT_COMPACT = 3, STATE_COMPACTING = 4, STATE_DONE=5; void setup() { size(WIDTH, HEIGHT, JAVA2D); frameRate(30); smooth(); font = loadFont("Arab-24.vlw"); JFileChooser fc = new JFileChooser(); if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)) { openImage(fc.getSelectedFile().getPath()); } else { openImage("niesen.jpg"); } } void draw() { background(0); textFont(font, 20); fill(0xFF55FF55); smooth(); textAlign(LEFT); text("e-recycling 2.0", 10, 50); noSmooth(); switch(state) { case STATE_WAIT_SEPARATE: image(igreen,gx,gy); blend(ired ,0,0,iw,ih,rx,ry,iw,ih,ADD); blend(iblue ,0,0,iw,ih,bx,by,iw,ih,ADD); textFont(font, 16); fill(0xFFFFFFFF); smooth(); textAlign(CENTER); text("separating colors", width/2, gy+ih+20); noSmooth(); if (millis() >= wait) { state = STATE_SEPARATING; } break; case STATE_SEPARATING: image(img,ox,oy); image(igreen,gx,gy); blend(ired ,0,0,iw,ih,rx,ry,iw,ih,ADD); blend(iblue ,0,0,iw,ih,bx,by,iw,ih,ADD); textFont(font, 16); fill(0xFFFFFFFF); smooth(); textAlign(CENTER); text("separating colors", width/2, gy+ih+20); noSmooth(); if (oy > gy - ih - 20) oy-=MOVESPEED; rx-=MOVESPEED; bx+=MOVESPEED; if (rx <= 10 || bx >= iw+iw+30) { state = STATE_WAIT_COMPACT; wait = millis() + WAIT_AFTER_LOAD; } break; case STATE_WAIT_COMPACT: image(img,ox,oy); image(igreen,gx,gy); image(ired ,rx,ry); image(iblue ,bx,by); textFont(font, 16); fill(0xFFFFFFFF); smooth(); textAlign(CENTER); text("colors separated, compacting them", width/2, gy+ih+20); noSmooth(); if(millis() >= wait) { state = STATE_COMPACTING; } break; case STATE_COMPACTING: tint(255,255); image(img,ox,oy); tint(255,255-tintval); float scalefactor = (255-tintval)/255.0; image(igreen,gx,gy,greencount.width+(int)(iw-greencount.width)*scalefactor, greencount.height+(int)(ih-greencount.height)*scalefactor); image(ired ,rx,ry,redcount.width +(int)(iw-redcount.width) *scalefactor, redcount.height+(int)(ih-redcount.height)*scalefactor); image(iblue ,bx,by,bluecount.width +(int)(iw-bluecount.width) *scalefactor, bluecount.height+(int)(ih-bluecount.height)*scalefactor); tintval += TINTSPEED; tint(255,tintval); image(greencount,gx,gy); image(redcount ,rx,ry); image(bluecount ,bx,by); if (tintval >= 255) { state = STATE_DONE; smooth(); } textFont(font, 16); fill(0xFFFFFFFF); smooth(); textAlign(CENTER); text("colors separated, compacting them", width/2, gy+ih+20); noSmooth(); break; case STATE_DONE: image(img,ox,oy); image(greencount,gx,gy); image(redcount ,rx,ry); image(bluecount ,bx,by); textFont(font, 16); fill(0xFFFFFFFF); smooth(); textAlign(CENTER); text("image is now reduced to its primary colors. please bring the components to a recycling plant near you!", width/2, gy+ih+20); noSmooth(); break; } noFill(); stroke(0xFFFFFFFF); rect(9,gy-1,iw+1,ih+1); rect(iw+20,gy-1,iw+1,ih+1); rect(iw+iw+30,gy-1,iw+1,ih+1); /* if (millis()/1000 > lastSecond) { println("fps: "+frameRate); lastSecond = millis()/1000; } */ } void openImage(String name) { img = loadImage(name); img.resize(iw, 0); if (img.height > ih) { img = img.get(0,0,iw,ih); } else { ih = img.height; } ired = createColoredImage(0xFFFF0000, img.width, img.height); ired.blend(img, 0, 0, iw, ih, 0, 0, iw, ih, DARKEST); redcount = sumImage(ired, 16); igreen = createColoredImage(0xFF00FF00, img.width, img.height); igreen.blend(img, 0, 0, iw, ih, 0, 0, iw, ih, DARKEST); greencount = sumImage(igreen, 8); iblue = createColoredImage(0xFF0000FF, img.width, img.height); iblue.blend(img, 0, 0, iw, ih, 0, 0, iw, ih, DARKEST); bluecount = sumImage(iblue, 0); ox = rx = gx = bx = iw + 20; oy = ry = gy = by = height - ih - 40; wait = millis() + WAIT_AFTER_LOAD; state = STATE_WAIT_SEPARATE; } PImage sumImage(PImage img, int move) { img.loadPixels(); int x = 0; int c = 0; for(int i=0; i> move & 0xFF); if (x > 255) { c++; x -= 255; } } int w = c/ih; int h = c/iw; return createColoredImage(0xFF000000 | 0xFF << move, w, h); } PImage createColoredImage(int c, int width, int height) { PImage img = createImage(width, height, RGB); img.loadPixels(); for(int i=0; i