posted on: 2008-11-15 10:21:34
Here is a short example of how to unpack a 24bit RGB color stored as an integer using bitwise operations

>Here I will pack a color and unpack it.

red = 156         # 0x9c
green = 64        # 0x40
blue = 100        # 0x64

RGB = (red<<16) + (green<<8) + blue
#RGB = 10240100
#RGB = 0x9c4064

unpacked_red = (RGB & 0xff0000)>>16
unpacked_green = (RGB & 0x00ff00)>>8
unpacked_blue = (RGB & 0x0000ff)

This shows how to pack and unpack the numbers, notice that at any point I could have used hex or base 10 to represent the numbers but the hex is consistent through out.

(RGB & 65280)>>8 

doesn't quite have the same meaning as 0x00ff00 then you see specifically what digits you want.

Comments

Name: