+ if (dpyinfo->red_bits > 0)
+ {
+ /* For TrueColor displays, we can decompose the RGB value
+ directly. */
+ int i;
+ unsigned int rmult, gmult, bmult;
+ unsigned int rmask, gmask, bmask;
+
+ rmask = (1 << dpyinfo->red_bits) - 1;
+ gmask = (1 << dpyinfo->green_bits) - 1;
+ bmask = (1 << dpyinfo->blue_bits) - 1;
+ /* If we're widening, for example, 8 bits in the pixel value to
+ 16 bits for the separate-color representation, we want to
+ extrapolate the lower bits based on those bits available --
+ in other words, we'd like 0xff to become 0xffff instead of
+ the 0xff00 we'd get by just zero-filling the lower bits.
+
+ We generate a 32-bit scaled-up value and shift it, in case
+ the bit count doesn't divide 16 evenly (e.g., when dealing
+ with a 3-3-2 bit RGB display), to get more of the lower bits
+ correct.
+
+ Should we cache the multipliers in dpyinfo? Maybe
+ special-case the 8-8-8 common case? */
+ rmult = 0xffffffff / rmask;
+ gmult = 0xffffffff / gmask;
+ bmult = 0xffffffff / bmask;
+
+ for (i = 0; i < ncolors; ++i)
+ {
+ unsigned int r, g, b;
+ unsigned long pixel = colors[i].pixel;
+
+ r = (pixel >> dpyinfo->red_offset) & rmask;
+ g = (pixel >> dpyinfo->green_offset) & gmask;
+ b = (pixel >> dpyinfo->blue_offset) & bmask;
+
+ colors[i].red = (r * rmult) >> 16;
+ colors[i].green = (g * gmult) >> 16;
+ colors[i].blue = (b * bmult) >> 16;
+ }
+ return;
+ }
+