# How to know if certain pixel is red, or blue, or green

**URL:** <https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802>\
**Category:** Beginners\
**Created:** [November 28, 2021, 4:58pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802 "2021-11-28T16:58:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [November 28, 2021, 4:58pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/1 "2021-11-28T16:58:00Z")

</div>

I want to change colors from of a certain image, so all the reds will be certain blue,  
and all the blues will be certain blue.

in order to do that, I am looking for some logic in the colors numbers, so I can use a simple if.

for example, all red have H150 or more, and G100 or less and B100 or less.

Any idea ?

---

<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:** [November 28, 2021, 5:10pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/2 "2021-11-28T17:10:49Z")

</div>

Hello,

Take a look here:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts…

There is a _search_ on the upper right… search for related keywords.

`:)`

---

<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:** [November 28, 2021, 5:28pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/3 "2021-11-28T17:28:03Z")

</div>

you could use red(), green(), blue(), pixels array

> **[Reference](https://processing.org/reference/red_.html)**
>
> Extracts the red value from a color, scaled to match current colorMode(). The value is always returned as a float, so be careful not to assign it to an int value. The red() …

> **[pixels\[\] / Reference](https://processing.org/reference/pixels.html)**
>
> The pixels\[\] array contains the values for all the pixels in the display window. These values are of the color datatype. This array is defined by the size of the display window. For example, if…

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [November 28, 2021, 10:44pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/4 "2021-11-28T22:44:12Z")

</div>

I already extract the R, G and B of the pixel.

Now, I want to check if it is some kind of red (from few thousands shades of red).

This is what I’m looking for

---

<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:** [November 28, 2021, 10:52pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/5 "2021-11-28T22:52:14Z")

</div>

Hello,

> [@yalaniv](#):
>
> I want to check if it is some kind of red (from few thousands shades of red).

Search for `color` on the Processing website.

There is also `hue`, `saturation` and `brightness` that you can work with.

`:)`

---

<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:** [November 30, 2021, 7:41am UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/6 "2021-11-30T07:41:17Z")

</div>

> [@yalaniv](#):
>
> and G100 or less and B100

if you got the RED, GREEN and BLUE values, and the requirement to turn red would be R\>150, G\>100, B\>100. So just put that into an if statement. Repeat for all oolors.

---

<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:** [November 30, 2021, 11:56am UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/7 "2021-11-30T11:56:41Z")

</div>

Hello,

Processing has a _Color Selector…_ that is in the _Tools_ menu:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cbcced8d75b0f0695a43d4c4b0d14732f66e429c.png)

`:)`

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [November 30, 2021, 12:15pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/8 "2021-11-30T12:15:34Z")

</div>

The main idea is to analyze an image, so I can change it’s color to a dozen color,  
so, I’m looking for an easy way to the check the red, green and blue of a color, and determine which color is it,  
of example - I want to mark as a red all the variation from the top right corner of the image you’ve paste.

So, I basically looking for a formula, because it happen to be more than 100k different shades in a 1200X1200 pixel image

---

<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:** [November 30, 2021, 8:13pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/9 "2021-11-30T20:13:49Z")

</div>

well a simple solution would be to search the distance on a 3D color cube. If a color must be either Red, green or blue, you can just find the closest point. Posterization filters work in a similar way (can work).

> [@My projects library #4! Gallery Megathread](https://discourse.processing.org/t/my-projects-library-4-gallery-megathread/31103/20):
>
> #55 Simple posterization filter Posterization filter sets the maximum amount of colors used. All colors on the screen get converted into those. [explanation by josephh](https://discourse.processing.org/t/posterization-filter-explained/32001/3)
> 
> > **Code**
> >
> > int clrs = 3; //end has clrs^3 colors, (1-1, 2-8, 3-27, 4-64, 5-125, 6-216,...) color clr[]; PImage img; void setup() { size(640,642); clr = generateColors(clrs); img = loadImage("house2.jpg"); println(img.width,img.height); } void draw() { image(img,0,0); noLoop(); loadPixels(); for(int i =…

here is a project that incorporates it.

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [November 30, 2021, 8:14pm UTC](https://discourse.processing.org/t/how-to-know-if-certain-pixel-is-red-or-blue-or-green/33802/10 "2021-11-30T20:14:30Z")

</div>

> [@yalaniv](#):
>
> I’m looking for an easy way to the check the red, green and blue of a color, and determine which color is it …

Below is some p5.js code that might accomplish what you are trying to do. Note what the `color_checker` function does. Pass it a color along with ranges for the red, green, blue, and alpha components, and it will return `true` if all components fall within their designated ranges, and `false` otherwise.

The code below draws a bluish square, then checks the colors of all the pixels. The bluish pixels are turned to red. It can easily be translated to Java.

```auto
// p5.js code
function setup() {
  createCanvas(200, 200);
  frameRate(1); // enable observer to see color change
}

function draw() {
  background(255, 255, 0);
  noStroke();
  fill(0, 0, 216); // note that blue component is 216
  rect(width / 4, height / 4, width / 2, height / 2);
  if (frameCount == 2) {
    for (let x = 0; x <= width; x++) {
      for (y = 0; y <= height; y++) {
        c = get(x, y);
        // note range for blue
        if (color_checker(c, 0, 255, 0, 255, 210, 220, 0, 255)) {
          fill(255, 0, 0);
          square(x, y, 1);
        }
      }
    }
    noLoop();
  }
}

function color_checker(c, r_lo, r_hi, g_lo, g_hi, b_lo, b_hi, a_lo, a_hi) {
  /*
  given a color object, and low and high values (inclusive), for r, g, b, and a,
   return:
   true if all color components are within the specifed ranges,
   otherwise false
  */
  let r = red(c);
  let g = green(c);
  let b = blue(c);
  let a = alpha(c);
  return (
    (r >= r_lo) && (r <= r_hi) &&
    (g >= g_lo) && (g <= g_hi) &&
    (b >= b_lo) && (b <= b_hi) &&
    (a >= a_lo) && (a <= a_hi)
  );
}

```
