+ // determine the number of colors and the bits-per-color
+ _bpc = 2; // XXX THIS SHOULD BE A USER OPTION
+ assert(_bpc >= 1);
+ _ncolors = 1 << (_bpc * 3);
+
+ if (_ncolors > 1 << depth) {
+ fprintf(stderr,
+ _("PseudoRenderControl: Invalid colormap size. Resizing.\n"));
+ _bpc = 1 << (depth/3) >> 3;
+ _ncolors = 1 << (_bpc * 3);
+ }
+
+ // build a color cube
+ _colors = new XColor[_ncolors];
+int tr, tg, tb;
+ int cpc = 1 << _bpc; // colors per channel
+ for (int n = 0,
+ r = 0; r < cpc; r++)
+ for (int g = 0; g < cpc; g++)
+ for (int b = 0; b < cpc; b++, n++) {
+ tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
+ tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
+ tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
+ _colors[n].red = tr | tr << 8;
+ _colors[n].green = tg | tg << 8;
+ _colors[n].blue = tb | tb << 8;
+ _colors[n].flags = DoRed|DoGreen|DoBlue; // used to track allocation
+ }
+
+ // allocate the colors
+ for (int i = 0; i < _ncolors; i++)
+ if (!XAllocColor(**display, info->colormap(), &_colors[i]))
+ _colors[i].flags = 0; // mark it as unallocated
+
+ // try allocate any colors that failed allocation above
+
+ // get the allocated values from the X server (only the first 256 XXX why!?)
+ XColor icolors[256];
+ int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
+ for (int i = 0; i < incolors; i++)
+ icolors[i].pixel = i;
+ XQueryColors(**display, info->colormap(), icolors, incolors);