How to check pixels color

How to check pixels colors
if the rectangle
it only has white return “Incompleto !!”
has white and yellow return “Incompleto !!”
only has yellow return “Amarillos!!”

color a = color(255,255,0);
color b = color(255,255,255);
color r = color(255,0,0);



void setup(){

//Se define el tamaño de la ventana según el tamaño de la imagen
size(750,607);
//rectangulo blanco para pintar

  stroke(255);
  strokeWeight(1);
  fill(b);
  rect(200, 260, 200, 200);
     
}

void draw(){

  stroke(10);
  strokeWeight(2);
  noFill();
  
  if((mouseX>pmouseX)&&(mouseX>200)&&(mouseX<400)&&(mouseY>260)&&(mouseY<460)){ 
   stroke(255, 255,0); 
   strokeWeight(60);
   line(mouseX, mouseY, pmouseX, pmouseY);
  }  
  
  }  
    
  void mousePressed(){
   
  
    loadPixels();
    // Two loops allow us to visit every column (x) and every row (y).

// Loop through every pixel column
    for (int x = 200; x < 440; x++ ) {
  // Loop through every pixel row
    for (int y = 249; y < 320; y++ ) {

    // Use the formula to find the 1D location
    int loc = x + y * width; // The location in the pixel array is calculated via our formula: 1D pixel location = x + y * width

      if (pixels[loc] == a || pixels[loc] == b)  { // si los pixeles son amarillos o blancos
      textSize(32);
      fill(255,0,0);
      text("Incompleto!", 270, 300);
      // Si tiene el color amarillo
    }else if (pixels[loc] == b)  { 
      textSize(32);
      fill(255,0,0);
      text("Amarillos!!", 270, 450);
    }
      
  }
  } 
}

Hey @albertohilal!

In this case, I would make a boolean to represent if the rectangle is covered in yellow or not. Once you have done that, you can set it to true, then run through your for loops, checking the pixels. If you see even one pixel that is still white, that means it isn’t complete so you can set your boolean back to false. After you are done looping through those, you can then use that boolean in your if statement. If the boolean is true, then you can display “Amarillos!” and if it is still false you can display “Incompleto!”.

I have done this with your code, and I have also changed a couple of things:

I removed (mouseX>pmouseX) from your if statement, it did not seem necessary.

For the x value, since your rectangle is drawn at 200 and is 200 pixels wide, I changed it to for (int x = 200; x < 400; x++ ).
For the y value, since your rectangle is drawn at 260 and is 200 pixels high, I changed it to for (int y = 260; y < 460; y++ ).

Here is the result:

color blanco = color(255, 255, 255);
boolean completo = true;



void setup() {

  //Se define el tamaño de la ventana según el tamaño de la imagen
  size(750, 607);
  //rectangulo blanco para pintar

  stroke(255);
  strokeWeight(1);
  fill(blanco);
  rect(200, 260, 200, 200);
}

void draw() {

  stroke(10);
  strokeWeight(2);
  noFill();

  if ((mouseX>200)&&(mouseX<400)&&(mouseY>260)&&(mouseY<460)) { 
    stroke(255, 255, 0); 
    strokeWeight(60);
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
}  

void mousePressed() {


  loadPixels();
  // Two loops allow us to visit every column (x) and every row (y).
  completo = true;

  // Loop through every pixel column
  for (int x = 200; x < 400; x++ ) {
    // Loop through every pixel row
    for (int y = 260; y < 460; y++ ) {

      // Use the formula to find the 1D location
      int loc = x + y * width; // The location in the pixel array is calculated via our formula: 1D pixel location = x + y * width
      if (pixels[loc] == blanco) completo = false;
    }
  }
  if (!completo) { // si los pixeles son amarillos o blancos
    textSize(32);
    fill(255, 0, 0);
    text("Incompleto!", 100, 200);
    // Si tiene el color amarillo
  } else { 
    textSize(32);
    fill(255, 0, 0);
    text("Amarillos!!", 300, 200);
  }
}

Let me know if you have any questions!

1 Like

thank you very much!!