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

From Medien Wiki
< GMU:Processing im Park‎ | Emilio Aguas
Revision as of 16:09, 1 April 2016 by EmilioAguas (talk | contribs) (Created page with "====--- Processing3.0 Code--- ==== Moving the mouse changing the brightness that looks like lightness <source lang="java"> int photos = 2; //maximum of photos int index = 0; ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

--- Processing3.0 Code---

Moving the mouse changing the brightness that looks like lightness

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

void setup() {
  size (800, 600);
  //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 throught the pixels in two dimensions
  for (int x =0; x < width; x++) {
    for (int y =0; y < 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 mouse position
      float changeBrightness = ((float) mouseY / height) * 5.0;
      red *= changeBrightness;
      green *= changeBrightness;
      blue *= changeBrightness;

      // 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;
    }
  }
  //dispaly the pixels
  updatePixels();
}

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