GMU:Processing im Park/Emilio Aguas/sample code test06

From Medien Wiki
< GMU:Processing im Park‎ | Emilio Aguas
Revision as of 16:36, 1 April 2016 by EmilioAguas (talk | contribs) (Created page with "====--- Processing3.0 Code--- ==== Moving the mouse and show me the image <source lang="java"> int photos = 2; //maximum of photos int index = 0; // first image PImage[] pics ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

--- Processing3.0 Code---

Moving the mouse and show me the image

int photos = 2; //maximum of photos
int index = 0; // first image
PImage[] pics = new PImage[photos]; //images array

void setup() {
  size (800, 600);
  background (150);
  smooth ();
  noStroke();
  //load the images into the array
  for (int i = 0; i < pics.length; i++ ) {
    pics[i] = loadImage ("IMG_024"+ (i+2) + ".jpg");
  }
}

void draw () {
  loadPixels();

  //load the pixels of every image
  pics[index].loadPixels() ;

  //moving through the pixels in two dimensions
  for (int x =0; x <  pics[index].width; x++) {
    for (int y =0; y <  pics[index].height; y++) {

      //position of the pixels in one dimension
      int pixel_Loc = x + (y * pics[index].width);

      //take the r,g.b values from the image pixel by pixel
      float red = red (pics[index].pixels [pixel_Loc]);
      float green = green (pics[index].pixels [pixel_Loc]);
      float blue = blue (pics[index].pixels [pixel_Loc]);

      //change the brightness according to the proximity of the mouse
      float flash = dist (x, y, mouseX, mouseY);//using dist function with the 4 values points in X and Y coordinates
      float flashLightX = (100-flash) / 100;// change the values for filters or the "100" for the size of the flashlight
      float flashLightY = (100-flash) / 100;
      red *= flashLightX;
      green *= flashLightY;
      blue *= flashLightX;

      // Constrain rgb values
      red = constrain (red, 0, 255);
      green = constrain (green, 0, 255);
      blue = constrain (blue, 0, 255);

      //change the pixels for a new value
      color images = color (red, green, blue);
      pixels[pixel_Loc] = images;  
    }
  }
  //display the pixels
  updatePixels();
}

void mousePressed() {
  //change the image every time the mouse is clicked
  index = (index + 1) % pics.length;
}