Home > Back-end >  24-Bit color decoding help (python)
24-Bit color decoding help (python)

Time:01-31

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

CodePudding user response:

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).

  • Related