Region with new color

edited June 2016 in Questions about Code

Hi, what I want to do it's to load an image and draw a rectangle that change the image pixels in a different color. If you try to run the program it's all correct but I wish the pixels change in "coloreFC" inside the rect. Please help me! This is the code:

PImage imageOR;
int x1, y1;


void setup() { 
  String nomeImageOR=selectInput("Carica un'immagine");
  imageOR = loadImage(nomeImageOR);
  size(imageOR.width, imageOR.height);
}


void draw() {
  image(imageOR, 0, 0);
  if (mousePressed = true){
    if (mouseButton == RIGHT){
      x1 = mouseX;
      y1 = mouseY;
    }
    noStroke();
    rectMode(CORNER);
    rect(x1, y1, mouseX-x1, mouseY-y1);
  }
}


color FalsoColore() {

  int coloreFC = color (0, 0, 0);
  int locPixel;

  float r;
  float g;
  float b;

  imageOR.loadPixels();

  for (int y = 0; y < imageOR.height; y++ ) {
    for (int x = 0; x < imageOR.width; x++ ) {

      locPixel = x + y*imageOR.width;

      r = red(imageOR.pixels[locPixel]);
      g = green(imageOR.pixels[locPixel]);
      b = blue(imageOR.pixels[locPixel]);

      float gl = ((r+g+b)/3.0);

      if( gl <= 80 ) {
        float c = gl*(255/80);
        coloreFC = color (0, 0, c);
        pixels[locPixel] = coloreFC;
      } else if( gl < 160 || gl > 80) {
        float c = (gl-80)*(255/80);
        coloreFC = color (c, 0, 255);
        pixels[locPixel] = coloreFC;
      } else if( gl < 255 || gl > 160) {
        float c = (gl-160)*(255/95);
        coloreFC = color (255, c, 255);
        pixels[locPixel] = coloreFC;
      }
    }
  }

  return coloreFC;
}
Tagged:

Answers

Sign In or Register to comment.