+ status = XParseColor (display, screen_colormap, color, color_def);
+ if (status && alloc)
+ {
+ status = XAllocColor (display, screen_colormap, color_def);
+ if (!status)
+ {
+ /* If we got to this point, the colormap is full, so we're
+ going to try and get the next closest color.
+ The algorithm used is a least-squares matching, which is
+ what X uses for closest color matching with StaticColor visuals. */
+
+ XColor *cells;
+ int no_cells;
+ int nearest;
+ long nearest_delta, trial_delta;
+ int x;
+
+ no_cells = XDisplayCells (display, XDefaultScreen (display));
+ cells = (XColor *) alloca (sizeof (XColor) * no_cells);
+
+ for (x = 0; x < no_cells; x++)
+ cells[x].pixel = x;
+
+ XQueryColors (display, screen_colormap, cells, no_cells);
+ nearest = 0;
+ /* I'm assuming CSE so I'm not going to condense this. */
+ nearest_delta = ((((color_def->red >> 8) - (cells[0].red >> 8))
+ * ((color_def->red >> 8) - (cells[0].red >> 8)))
+ +
+ (((color_def->green >> 8) - (cells[0].green >> 8))
+ * ((color_def->green >> 8) - (cells[0].green >> 8)))
+ +
+ (((color_def->blue >> 8) - (cells[0].blue >> 8))
+ * ((color_def->blue >> 8) - (cells[0].blue >> 8))));
+ for (x = 1; x < no_cells; x++)
+ {
+ trial_delta = ((((color_def->red >> 8) - (cells[x].red >> 8))
+ * ((color_def->red >> 8) - (cells[x].red >> 8)))
+ +
+ (((color_def->green >> 8) - (cells[x].green >> 8))
+ * ((color_def->green >> 8) - (cells[x].green >> 8)))
+ +
+ (((color_def->blue >> 8) - (cells[x].blue >> 8))
+ * ((color_def->blue >> 8) - (cells[x].blue >> 8))));
+ if (trial_delta < nearest_delta)
+ {
+ XColor temp;
+ temp.red = cells[x].red;
+ temp.green = cells[x].green;
+ temp.blue = cells[x].blue;
+ status = XAllocColor (display, screen_colormap, &temp);
+ if (status)
+ {
+ nearest = x;
+ nearest_delta = trial_delta;
+ }
+ }
+ }
+ color_def->red = cells[nearest].red;
+ color_def->green = cells[nearest].green;
+ color_def->blue = cells[nearest].blue;
+ status = XAllocColor (display, screen_colormap, color_def);
+ }
+ }