-1

Working with led strips from my end I use function Color(0-255, 0-255, 0-255) for rgb, the function encodes to 24-bit color and is written below

Def Color(red, green, blue, white = 0):
    """Convert the provided red, green, blue color to a 24-bit color value.
       Each color component should be a value 0-255 where 0 is the lowest intensity
       and 255 is the highest intensity."""
    Return (white << 24) | (red << 16) | (green << 8) | blue

Later on I retrieve this 24-bit color with another function, I need to decode it back into (0-255, 0-255, 0-255) I have no clue how to do this...

I used a print function to see what's happening and

a pixel (red 0, green 255, blue 0) returns 16711680

a pixel (red 255, green 0, blue 0) returns 65280

a pixel (red 0, green 0, blue 255) returns 255 which makes sense to me

How can I decode this efficiently/quickly (running on rpi zero w) must be in python

HaZZar
  • 11
  • 2
  • Why are you talking about 24-bit RGB, when you are using a 32-bit RGBW? And what is the meaning of white here? – zvone Dec 11 '20 at 09:41
  • Your red and green might be confused, I think. The value 16711680 == 0xff0000 in hexadecimal. Here, every two hex digits represents a colour, and going by your values, you have GRB. As Danny rightly points out in his answer, you need to look at bit shifts and bit masking to get the values you are looking for. – Pam Dec 11 '20 at 09:46
  • Hey, the function isn't my code, I'm guessing its for if the led strip has RGBW, however my strip does not so it is just RGB – HaZZar Dec 11 '20 at 09:50

1 Answers1

0

Full color pixels have red, green, blue (24bit) or alpha, red, green, blue (32bit) (sub-pixel order may differ!), not white, red, green, blue. Alpha = opacity.

To decode RGB 24bit (in this order):

B = pixel & 0xff
G = (pixel >> 8) & 0xff
R = (pixel >> 16) & 0xff

where 0xff = 255, & = bitwise and, >> = shift n bits right (or divide by 2^n).

For other pixel encodings the order will change (and you may have alpha too).

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • Thanks, I'm quite new to this and do not understand much about these functions but this works great. In my case, there will never be alpha (white) and I am aware that RGB may be out of order, I believe my strip is GRB however that's not an issue. Thanks again! – HaZZar Dec 11 '20 at 10:02