# How to check pixels color

**URL:** https://discourse.processing.org/t/how-to-check-pixels-color/1316
**Category:** Coding Questions
**Created:** [June 27, 2018, 6:42pm UTC](https://discourse.processing.org/t/how-to-check-pixels-color/1316 "2018-06-27T18:42:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![albertohilal](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@albertohilal](https://discourse.processing.org/u/albertohilal)
#### Post date: [June 27, 2018, 6:42pm UTC](https://discourse.processing.org/t/how-to-check-pixels-color/1316/1 "2018-06-27T18:42:11Z")

</div>

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!!”

```auto
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);
    }
      
  }
  } 
}

```

---

<div class="post-metadata">

### Author: ![nicolaskellum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/nicolaskellum/32/15860_2.png) [@nicolaskellum](https://discourse.processing.org/u/nicolaskellum)
#### Post date: [June 28, 2018, 6:24pm UTC](https://discourse.processing.org/t/how-to-check-pixels-color/1316/2 "2018-06-28T18:24:01Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

### Author: ![albertohilal](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@albertohilal](https://discourse.processing.org/u/albertohilal)
#### Post date: [June 30, 2018, 6:45pm UTC](https://discourse.processing.org/t/how-to-check-pixels-color/1316/3 "2018-06-30T18:45:02Z")

</div>

thank you very much!!
