int i;
unsigned int i;
CreateColors(display, attributes, colors, ncolors, image_pixels, mask_pixels,             mask_pixel_index, alloc_pixels, nalloc_pixels,             used_pixels, nused_pixels)    Display *display;    XpmAttributes *attributes;    XpmColor *colors;    unsigned int ncolors;    Pixel *image_pixels;    Pixel *mask_pixels;    unsigned int *mask_pixel_index;    Pixel *alloc_pixels;    unsigned int *nalloc_pixels;    Pixel *used_pixels;    unsigned int *nused_pixels;{    /* variables stored in the XpmAttributes structure */    Visual *visual;    Colormap colormap;    XpmColorSymbol *colorsymbols = NULL;    unsigned int numsymbols;    XpmAllocColorFunc allocColor;    void *closure;    char *colorname;    unsigned int color, key;    Bool pixel_defined;    XpmColorSymbol *symbol = NULL;    char **defaults;    int ErrorStatus = XpmSuccess;    char *s;    int default_index;    XColor *cols = NULL;    unsigned int ncols = 0;    /*     * retrieve information from the XpmAttributes     */    if (attributes && attributes->valuemask & XpmColorSymbols) {	colorsymbols = attributes->colorsymbols;	numsymbols = attributes->numsymbols;    } else	numsymbols = 0;    if (attributes && attributes->valuemask & XpmVisual)	visual = attributes->visual;    else	visual = XDefaultVisual(display, XDefaultScreen(display));    if (attributes && (attributes->valuemask & XpmColormap))	colormap = attributes->colormap;    else	colormap = XDefaultColormap(display, XDefaultScreen(display));    if (attributes && (attributes->valuemask & XpmColorKey))	key = attributes->color_key;    else	key = xpmVisualType(visual);    if (attributes && (attributes->valuemask & XpmAllocColor))	allocColor = attributes->alloc_color;    else	allocColor = AllocColor;    if (attributes && (attributes->valuemask & XpmColorClosure))	closure = attributes->color_closure;    else	closure = NULL;#ifndef FOR_MSW    if (USE_CLOSECOLOR) {	/* originally from SetCloseColor */#if 0	if (visual->class == DirectColor) {	    /*	     * TODO: Implement close colors for DirectColor visuals. This is	     * difficult situation. Chances are that we will never get here,	     * because any machine that supports DirectColor will probably	     * also support TrueColor (and probably PseudoColor). Also,	     * DirectColor colormaps can be very large, so looking for close	     * colors may be too slow.	     */	} else {#endif	    int i;#ifndef AMIGA	    ncols = visual->map_entries;#else	    ncols = colormap->Count;#endif	    cols = (XColor *) XpmCalloc(ncols, sizeof(XColor));	    for (i = 0; i < ncols; ++i)		cols[i].pixel = i;	    XQueryColors(display, colormap, cols, ncols);#if 0	}#endif    }#endif /* ndef FOR_MSW */    switch (key) {    case XPM_MONO:	default_index = 2;	break;    case XPM_GRAY4:	default_index = 3;	break;    case XPM_GRAY:	default_index = 4;	break;    case XPM_COLOR:    default:	default_index = 5;	break;    }    for (color = 0; color < ncolors; color++, colors++,					 image_pixels++, mask_pixels++) {	colorname = NULL;	pixel_defined = False;	defaults = (char **) colors;	/*	 * look for a defined symbol	 */	if (numsymbols) {	    unsigned int n;	    s = defaults[1];	    for (n = 0, symbol = colorsymbols; n < numsymbols; n++, symbol++) {		if (symbol->name && s && !strcmp(symbol->name, s))		    /* override name */		    break;		if (!symbol->name && symbol->value) {	/* override value */		    int def_index = default_index;		    while (defaults[def_index] == NULL)	/* find defined							 * colorname */			--def_index;		    if (def_index < 2) {/* nothing towards mono, so try					 * towards color */			def_index = default_index + 1;			while (def_index <= 5 && defaults[def_index] == NULL)			    ++def_index;		    }		    if (def_index >= 2 && defaults[def_index] != NULL &&			!xpmstrcasecmp(symbol->value, defaults[def_index]))			break;		}	    }	    if (n != numsymbols) {		if (symbol->name && symbol->value)		    colorname = symbol->value;		else		    pixel_defined = True;	    }	}	if (!pixel_defined) {		/* pixel not given as symbol value */	    unsigned int k;	    if (colorname) {		/* colorname given as symbol value */		if (!SetColor(display, colormap, visual, colorname, color,			      image_pixels, mask_pixels, mask_pixel_index,			      alloc_pixels, nalloc_pixels, used_pixels,			      nused_pixels, attributes, cols, ncols,			      allocColor, closure))		    pixel_defined = True;		else		    ErrorStatus = XpmColorError;	    }	    k = key;	    while (!pixel_defined && k > 1) {		if (defaults[k]) {		    if (!SetColor(display, colormap, visual, defaults[k],				  color, image_pixels, mask_pixels,				  mask_pixel_index, alloc_pixels,				  nalloc_pixels, used_pixels, nused_pixels,				  attributes, cols, ncols,				  allocColor, closure)) {			pixel_defined = True;			break;		    } else			ErrorStatus = XpmColorError;		}		k--;	    }	    k = key + 1;	    while (!pixel_defined && k < NKEYS + 1) {		if (defaults[k]) {		    if (!SetColor(display, colormap, visual, defaults[k],				  color, image_pixels, mask_pixels,				  mask_pixel_index, alloc_pixels,				  nalloc_pixels, used_pixels, nused_pixels,				  attributes, cols, ncols,				  allocColor, closure)) {			pixel_defined = True;			break;		    } else			ErrorStatus = XpmColorError;		}		k++;	    }	    if (!pixel_defined) {		if (cols)		    XpmFree(cols);		return (XpmColorFailed);	    }	} else {	    /* simply use the given pixel */	    *image_pixels = symbol->pixel;	    /* the following makes the mask to be built even if none	       is given a particular pixel */	    if (symbol->value		&& !xpmstrcasecmp(symbol->value, TRANSPARENT_COLOR)) {		*mask_pixels = 0;		*mask_pixel_index = color;	    } else		*mask_pixels = 1;	    used_pixels[(*nused_pixels)++] = *image_pixels;	}    }    if (cols)	XpmFree(cols);    return (ErrorStatus);}