# How to check for a color in an area

**URL:** <https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616>\
**Category:** Beginners\
**Created:** [February 7, 2020, 9:28pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616 "2020-02-07T21:28:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://discourse.processing.org/u/Yannik)\
**Post date:** [February 7, 2020, 9:28pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/1 "2020-02-07T21:28:25Z")

</div>

How can I check, if there is a specific color in an area?  
Like:

```auto
If(color in area(10,100,50,600) == color(0,0,255)){
  run code
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 7, 2020, 9:33pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/2 "2020-02-07T21:33:38Z")

</div>

Err, get(x,y) gives you one point

get with 4 parameters gives you a rectangle that you could loop over to check all points in it

---

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://discourse.processing.org/u/Yannik)\
**Post date:** [February 8, 2020, 11:30am UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/3 "2020-02-08T11:30:32Z")

</div>

If I use get with 4 parameters it says: Type mismatch,“processing.core.PImage” does not match with “int”

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 8, 2020, 11:37am UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/4 "2020-02-08T11:37:39Z")

</div>

Yeah, using it with 4 parameters gives you an image (the section of the big image)

PImage section = get(…); // 4 parameters

Look at loadPixels and pixels to see how to for loop over it to check single points

---

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://discourse.processing.org/u/Yannik)\
**Post date:** [February 8, 2020, 11:55am UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/5 "2020-02-08T11:55:36Z")

</div>

With a for loop its too slow. I want to create a PacMan game where it should get checked fast to stop the character if a blue pixel is x+24 pixels next to it.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 8, 2020, 12:02pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/6 "2020-02-08T12:02:02Z")

</div>

When you know it’s x+24 then use get with 2 parameters to get the color of this point

color testColor=get(x+24,y);

Test it against the color blue

---

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://discourse.processing.org/u/Yannik)\
**Post date:** [February 8, 2020, 12:15pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/7 "2020-02-08T12:15:37Z")

</div>

can I also check if the color blue is in the area (x+24,y-24,x+24,y+24)?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 8, 2020, 12:29pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/8 "2020-02-08T12:29:37Z")

</div>

Yes.

Theoretically

- if he goes left check left,
- when he goes right check right side.

so when you have the direction stored (`int dir=0` or 1 or 2… for north, east, south, west) make the check accordingly.

Otherwise you have to write in full length:

```auto
if( get(x+24,y) == BLUE || 
   get(x-24,y) == BLUE ||
    .... ) { 
    // reached a wall
}

```

(|| means logical OR, && means logical AND)

This doesn’t make much sense because it would nearly always give you that PacMan reached a wall. So he wouldn’t be able to move anyway.

Instead, when you hit a cursor (or wasd keys) to make PacMan move, check only the direction for that key and allow the move or disallow move.

Chrisir

[EDITED]

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 21, 2020, 11:01am UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/9 "2020-12-21T11:01:47Z")

</div>

so is this the only way to set pixels?

```auto
loadPixels()
pixels[x+y*w] = color(255)
updatePixels()

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 21, 2020, 3:16pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/10 "2020-12-21T15:16:40Z")

</div>

> [@Chrisir](#):
>
> `r=0` or 1 or 2… for north, east, south, west) make the check accordingly.

… or use `point()` command

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 21, 2020, 5:01pm UTC](https://discourse.processing.org/t/how-to-check-for-a-color-in-an-area/17616/11 "2020-12-21T17:01:40Z")

</div>

> [@CodeMasterX](#):
>
> so is this the only way to set pixels?

> **[set() / Reference](https://processing.org/reference/set_.html)**
>
> Changes the color of any pixel or writes an image directly into the display window. The x and y parameters specify the pixel to change and the color parameter specifies…
