Accessing the r,g,b fields of an RGB LED

The Adafruit NeoPixel library provides the means of controlling NeoPixel RGB LEDS.

It includes a means of constructing the color of a pixel using the Color() method, e.g.

#define COLOR_YELLOW MyPixels.Color(255,255,0)

Is there any way to access or retrieve the individual Red, Green and Blue values of an existing color?

For example, if I have :

MyPixels.setPixelColor(LightNumber, COLOR_YELLOW);

is there any way to extract the red, green, and blue values from that pixel?

If I do something like:

** uint32_t ColorOfThePixel = MyPixels.getPixelColor(12);**

Is there any way to get the red, green, blue components out of the ColorOfThePixel variable?

1 Like

Yes.
The value returned is an unsigned 32 integer (4 bytes). The first byte is unused, second is red, third is green and fourth is blue. Looking that the number in hex makes the layout easier to see 0x00rrggbb

To extract individual colours...

uint8_t r = ColorOfThePixel >> 16;
uint8_t g = ColorOfThePixel >> 8;
uint8_t b = ColorOfThePixel;
1 Like