--- highlight.c.previous Fri Mar 17 16:09:50 2000 +++ highlight.c Sat Mar 25 01:35:37 2000 @@ -1510,18 +1510,38 @@ */ static Pixel allocColor(Widget w, char *colorName) { - XColor colorDef; + XColor colorDef , screenDef; Display *display = XtDisplay(w); + double tred, tgreen, tblue, dist, small = 1.0e9; + unsigned long i, found; /* pixel value */ int screenNum = XScreenNumberOfScreen(XtScreen(w)); + Visual *visual = DefaultVisual( display, screenNum); Colormap cMap = DefaultColormap(display, screenNum); Pixel foreground; /* Allocate and return the color cell, or print an error and fall through */ - if (XParseColor(display, cMap, colorName, &colorDef)) { + if (XParseColor(display, cMap, colorName, &colorDef)) { if (XAllocColor(display, cMap, &colorDef)) return colorDef.pixel; - else - fprintf(stderr, "NEdit: Can't allocate color: %s\n", colorName); + /* even if out of memory, share an available color */ + for (i = 0; i < visual->map_entries; i++ ) + { + screenDef.pixel = i; + XQueryColor( display, cMap, &screenDef); + tred = (double)(screenDef.red >> 8) - (double)(colorDef.red >> 8); + tgreen = (double)(screenDef.green >> 8) + - (double)(colorDef.green >> 8); + tblue = (double)(screenDef.blue >> 8) + - (double)(colorDef.blue >> 8); + /* use square euclidien distance */ + dist = tred * tred + tgreen * tgreen + tblue * tblue; + if ( dist < small ) + { + found = i; + small = dist; + } + } + return found; } else fprintf(stderr, "NEdit: Color name %s not in database\n", colorName);