PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

RGB colors

Surface contains pixels in uint16_t* m_pixels,
and every pixel is uint16_t. How can I get R G B value from uint16_t?
Since it's a 16-bit color, aka 565, that means Red is the first 5 bits, green the middle 6 bits, and blue is the last 5 bits.

So to get their color values, here's an example:
Code:

Pixel cPixel = pDisplay->GetPixel(x, y);
int iRed = ((cPixel & RED_MASK) >> (RED_SHIFT - (8 - RED_BITS)));
int iGreen = ((cPixel & GREEN_MASK) >> (GREEN_SHIFT - (8 - GREEN_BITS)));
int iBlue = ((cPixel & BLUE_MASK) << (8 - BLUE_BITS));

// where RED_MASK, RED_SHIFT, etc are defined in PF's defs.h file

const Pixel RED_MASK = 0x1F << 11;
const Pixel GREEN_MASK = 0x3F << 5;
const Pixel BLUE_MASK = 0x1F;
const int RED_SHIFT = 11;
const int GREEN_SHIFT = 5;
const int BLUE_SHIFT = 0;
const int RED_BITS = 5;
const int GREEN_BITS = 6;
const int BLUE_BITS = 5;
Thanks a lot. I've failed in my attempts to extrapolate from PHAL source, these bit operatins are real headache for a newbie.