Search code examples
cx11rgbpixelxlib

X11: Convert Pixel value to RGB


I'm relatively new to X11 and extending an X11 application. I need to convert a color in a variable of type Pixel into its RGB components. My understanding is that this depends on the application's visual. For TrueColor and DirectColor visuals I can look at the XVisualInfo and use the bits_per_rgb and {red,green,blue}_mask members.

But what to do for PseudoColor or other visuals? What I need is a function such as

void PixelToRGB(Pixel p, XVisualInfo *v, uint16_t *r, uint16_t *g, uint16_t *b)

I'm reading Xlib programming manuals but could not yet find information how to implement such a function.


Solution

  • I haven't done Xlib programming so this is educated guesswork, but you should be able to get the ColorMap from the XWindowAttributes of the window in question, and the pixel value will be an index into the color map. Apparently the lookup is done by stuffing the pixel into an XColor and calling XQueryColor with it, which will populate the XColor's red, green, and blue fields. I believe this is also supposed to be done even with DirectColor and TrueColor visuals, as the colormap can contain things like gamma corrections, and perhaps odder things (weird color-cycling effects?) in the case of a DirectColor visual.