# Accessing individual channel values of a color var?

**URL:** <https://discourse.processing.org/t/accessing-individual-channel-values-of-a-color-var/23993>\
**Category:** Beginners\
**Created:** [September 21, 2020, 3:46am UTC](https://discourse.processing.org/t/accessing-individual-channel-values-of-a-color-var/23993 "2020-09-21T03:46:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablohotsauce](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pablohotsauce/32/10113_2.png) [@pablohotsauce](https://discourse.processing.org/u/pablohotsauce)\
**Post date:** [September 21, 2020, 3:46am UTC](https://discourse.processing.org/t/accessing-individual-channel-values-of-a-color-var/23993/1 "2020-09-21T03:46:17Z")

</div>

I’m trying to access the individual RGBA channel values of a color variable in Processing.

In p5, it seems like color variables are objects, and they have a property called `levels`, which is an array, which can spit out individual channel values, like so:

```javascript
let myColor = color(128, 64, 255);
console.log(myColor.levels[0]); // prints red channel value (128)

```

But from probing color vars in (Java) Processing, it seems like they’re coded as ints of some kind. I can’t find any documentation on accessing individual color channels in either the Reference or in the Javadoc. Is it possible?

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [September 21, 2020, 4:10am UTC](https://discourse.processing.org/t/accessing-individual-channel-values-of-a-color-var/23993/2 "2020-09-21T04:10:56Z")

</div>

```auto
Float r = red(myColor);
Float g = green(myColor);
Float b = blue(myColor);
Float br = brightness(myColor);

```

---

<div class="post-metadata">

**Author:** ![pablohotsauce](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pablohotsauce/32/10113_2.png) [@pablohotsauce](https://discourse.processing.org/u/pablohotsauce)\
**Post date:** [September 21, 2020, 4:22am UTC](https://discourse.processing.org/t/accessing-individual-channel-values-of-a-color-var/23993/3 "2020-09-21T04:22:33Z")

</div>

Facepalm. Thanks. 😇

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 21, 2020, 8:30am UTC](https://discourse.processing.org/t/accessing-individual-channel-values-of-a-color-var/23993/4 "2020-09-21T08:30:28Z")

</div>

> [@pablohotsauce](#):
>
> , it seems like they’re coded as ints of some kind.

> **[color / Reference](https://processing.org/reference/color_datatype.html)**
>
> Datatype for storing color values. Colors may be assigned with get() and color() or they may be specified directly using hexadecimal notation such as #FFCC00 or 0xFFFFCC00.…

> [@pablohotsauce](#):
>
> I can’t find any documentation on accessing individual color channels

> **[\>\> (right shift) / Reference](https://processing.org/reference/rightshift.html)**
>
> Shifts bits to the right. The number to the left of the operator is shifted the number of places specified by the number to the right. Each shift to the right halves the number, therefore each right s…

> [@pablohotsauce](#):
>
> Is it possible?

Besides the methods **alpha()**, **red()**, **green()** & **blue()**:

> **[alpha() / Reference](https://processing.org/reference/alpha_.html)**
>
> Extracts the alpha value from a color.

> **[red() / 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() fu…

> **[green() / Reference](https://processing.org/reference/green_.html)**
>
> Extracts the green 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 green()

> **[blue() / Reference](https://processing.org/reference/blue_.html)**
>
> Extracts the blue 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 blue() …

```auto
final color
  c = 0x40F0A080, // color as a whole int (32-bit) (4-byte)
  a = (int) alpha(c), // alpha channel
  r = (int) red(c), // red channel
  g = (int) green(c), // green channel
  b = (int) blue(c); // blue channel

println("Color:\t", hex(c)); // 40F0A080
println("Alpha:\t", hex(a, 2)); // 40
println("Red:\t", hex(r, 2)); // F0
println("Green:\t", hex(g, 2)); // A0
println("Blue:\t", hex(b, 2)); // 80

noLoop();

//background(c);
background(r, g, b, a);

```

We can use bitwise operators in order to grab the 4 channels more performantly:

```auto
final color
  c = 0x40F0A080, // color as a whole int (32-bit) (4-byte)
  a = c >>> 030, // alpha channel
  r = c >> 020 & 0xff, // red channel
  g = c >> 010 & 0xff, // green channel
  b = c & 0xff; // blue channel

println("Color:\t", hex(c)); // 40F0A080
println("Alpha:\t", hex(a, 2)); // 40
println("Red:\t", hex(r, 2)); // F0
println("Green:\t", hex(g, 2)); // A0
println("Blue:\t", hex(b, 2)); // 80

noLoop();

//background(c);
background(r, g, b, a);

```

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [September 21, 2020, 4:55pm UTC](https://discourse.processing.org/t/accessing-individual-channel-values-of-a-color-var/23993/5 "2020-09-21T16:55:39Z")

</div>

> [@GoToLoop](#):
>
> We can use bitwise operators in order to grab the 4 channels more performantly:

Sometimes your code is a bit of a mystery, but always concise and to the point! (all good, appreciated!)

[Octal literals](https://www.geeksforgeeks.org/literals-in-java/)  
[Zero fill right shift](https://www.tutorialspoint.com/java/java_basic_operators.htm) (search for it on the page).
