int main(int argc, char**argv) { Display *dpy; Screen *scr; XWindowAttributes attr; Colormap cmap; XColor color; int i; /* connect to X server */ if ((dpy=XOpenDisplay(NULL))==NULL) { fprintf(stderr,"Cannot connect to display %s\n", XDisplayName(NULL)); exit(-1); } scr = XDefaultScreenOfDisplay(dpy); /* determine colormap ID */ if (!XGetWindowAttributes(dpy,RootWindowOfScreen(scr),&attr)) { fprintf(stderr,"Cannot get RootWindow attributes\n"); exit(-1); } cmap = attr.colormap; printf("Root Window Colormap ID = %ld\n",cmap); /* list colors */ for (i=0; i<CellsOfScreen(scr); i++) { color.pixel = i; XQueryColor(dpy,cmap,&color); printf("pixel = %d \tred = %d \tgreen = %d \tblue = %d\n", (int) color.pixel, (int) color.red, (int) color.green, (int) color.blue); } /* close connection to X server */ XCloseDisplay(dpy); return( 0 ); }
int XCellsOfScreen(Screen *s) { return (CellsOfScreen(s)); }
Colormap XtcwpCreateSatColormap (Display *dpy, Window win, float fhue, float lhue, float wfrac, float bright) /***************************************************************************** create a colormap with varying saturations in contiguous cells ****************************************************************************** Input: dpy display win window fhue first hue in colormap (saturation=1) lhue last hue in colormap (saturation=1) wfrac fractional position of white within the colormap (saturation=0) bright brightness ****************************************************************************** Notes: The returned colormap is only created; the window's colormap attribute is not changed, and the colormap is not installed by this function. The returned colormap is a copy of the window's current colormap, but with varying hues (blue to red) allocated in the range of contiguous cells determined by XA_RGB_DEFAULT_MAP. If it does not already exist, XA_RGB_DEFAULT_MAP will be created. ****************************************************************************** Author: Craig Artley, Colorado School of Mines, 11/22/93 *****************************************************************************/ { Screen *scr=XDefaultScreenOfDisplay(dpy); Colormap cmap,wcmap; XColor color; XWindowAttributes wa; unsigned long i,j,ncells,npixels,nfpixels,nlpixels; unsigned long bpixel,epixel,pixel[4096]; long ltemp; float red=0,green=0,blue=0; /* determine beginning and ending pixels in contiguous range */ bpixel = XtcwpGetFirstPixel(dpy); epixel = XtcwpGetLastPixel(dpy); if (epixel<=bpixel) return None; /* determine window's current colormap */ XGetWindowAttributes(dpy,win,&wa); wcmap = wa.colormap; /* create new colormap and allocate all cells read/write */ cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone); ncells = CellsOfScreen(scr); XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int) ncells); /* copy color cells from window's colormap to new colormap */ for (i=0; i<ncells; ++i) { if (i<bpixel || i>epixel) { color.pixel = i; XQueryColor(dpy,wcmap,&color); XFreeColors(dpy,cmap,&i,1,0); XAllocColor(dpy,cmap,&color); } } /* devide colormap into 3 regions: fhues, white, and lhues */ npixels = epixel-bpixel+1; ltemp = (long) (wfrac*((float) npixels)); nfpixels = (ltemp<0) ? 0: ltemp; if (nfpixels>npixels-1) nfpixels = npixels-1; nlpixels = npixels-nfpixels-1; /* pixels from fhue to just under white */ for (i=0; i<nfpixels; ++i) { color.pixel = bpixel+i; hsvrgb(fhue,((float)(nfpixels-i))/((float) nfpixels),bright, &red,&green,&blue); color.red = 65535*red; color.green = 65535*green; color.blue = 65535*blue; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* white pixel */ color.pixel = bpixel+i; hsvrgb(fhue,0.0,bright,&red,&green,&blue); color.red = 65535*red; color.green = 65535*green; color.blue = 65535*blue; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); ++i; /* pixels from just under white to lhue */ for (j=0; j<nlpixels; ++i,++j) { color.pixel = bpixel+i; hsvrgb(lhue,((float)(j+1))/((float) nlpixels),bright,&red,&green,&blue); color.red = 65535*red; color.green = 65535*green; color.blue = 65535*blue; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* return colormap */ return cmap; }
Colormap XtcwpCreateHueColormap (Display *dpy, Window win, float fhue, float lhue, float sat, float bright) /***************************************************************************** create a colormap with varying hues (user-specified) in contiguous cells ****************************************************************************** Input: dpy display win window fhue first hue in colormap lhue last hue in colormap sat saturation bright brightness ****************************************************************************** Notes: The returned colormap is only created; the window's colormap attribute is not changed, and the colormap is not installed by this function. The returned colormap is a copy of the window's current colormap, but with varying hues (blue to red) allocated in the range of contiguous cells determined by XA_RGB_DEFAULT_MAP. If it does not already exist, XA_RGB_DEFAULT_MAP will be created. ****************************************************************************** Author: Dave Hale, Colorado School of Mines, 09/29/90 Modified: Craig Artley, Colorado School of Mines, 11/22/93 Saturation, brightness, and range of hues now user-specified. *****************************************************************************/ { Screen *scr=XDefaultScreenOfDisplay(dpy); Colormap cmap,wcmap; XColor color; XWindowAttributes wa; unsigned long i,ncells,npixels; unsigned long bpixel,epixel,pixel[4096]; float red=0,green=0,blue=0; /* determine beginning and ending pixels in contiguous range */ bpixel = XtcwpGetFirstPixel(dpy); epixel = XtcwpGetLastPixel(dpy); if (epixel<=bpixel) return None; /* determine window's current colormap */ XGetWindowAttributes(dpy,win,&wa); wcmap = wa.colormap; /* create new colormap and allocate all cells read/write */ cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone); ncells = CellsOfScreen(scr); XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int) ncells); /* copy color cells from window's colormap to new colormap */ for (i=0; i<ncells; ++i) { if (i<bpixel || i>epixel) { color.pixel = i; XQueryColor(dpy,wcmap,&color); XFreeColors(dpy,cmap,&i,1,0); XAllocColor(dpy,cmap,&color); } } /* build hues in contiguous cells in new colormap */ npixels = epixel-bpixel+1; for (i=0; i<npixels; ++i) { color.pixel = bpixel+i; hsvrgb(fhue+(lhue-fhue)*((float)i)/((float)(npixels-1)),sat,bright, &red,&green,&blue); color.red = 65535*red; color.green = 65535*green; color.blue = 65535*blue; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* return colormap */ return cmap; }
Colormap XtcwpCreateGrayColormap (Display *dpy, Window win) /***************************************************************************** create a colormap with a gray scale in contiguous cells ****************************************************************************** Input: dpy display win window ****************************************************************************** Notes: The returned colormap is only created; the window's colormap attribute is not changed, and the colormap is not installed by this function. The returned colormap is a copy of the window's current colormap, but with a gray scale (black to white) allocated in the range of contiguous cells determined by XA_RGB_DEFAULT_MAP. If it does not already exist, XA_RGB_DEFAULT_MAP will be created. ****************************************************************************** Author: Dave Hale, Colorado School of Mines, 09/29/90 *****************************************************************************/ { Screen *scr=XDefaultScreenOfDisplay(dpy); Colormap cmap,wcmap; XColor color; XWindowAttributes wa; unsigned long i,ncells,npixels; unsigned long bpixel,epixel,pixel[4096]; /* determine beginning and ending pixels in contiguous range */ bpixel = XtcwpGetFirstPixel(dpy); epixel = XtcwpGetLastPixel(dpy); if (epixel<=bpixel) return None; /* determine window's current colormap */ XGetWindowAttributes(dpy,win,&wa); wcmap = wa.colormap; /* create new colormap and allocate all cells read/write */ cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone); ncells = CellsOfScreen(scr); XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int)ncells); /* copy color cells from window's colormap to new colormap */ for (i=0; i<ncells; ++i) { if (i<bpixel || i>epixel) { color.pixel = i; XQueryColor(dpy,wcmap,&color); XFreeColors(dpy,cmap,&i,1,0); XAllocColor(dpy,cmap,&color); } } /* build gray scale in contiguous cells in new colormap */ npixels = epixel-bpixel+1; for (i=0; i<npixels; ++i) { color.pixel = bpixel+i; color.red = (unsigned short) (65535*i/(npixels-1)); color.green = color.red; color.blue = color.red; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* return colormap */ return cmap; }
Colormap XtcwpCreateRGBColormap (Display *dpy, Window win) /***************************************************************************** create a colormap with an RGB color scale in contiguous cells ****************************************************************************** Input: dpy display win window ****************************************************************************** Notes: The returned colormap is only created; the window's colormap attribute is not changed, and the colormap is not installed by this function. The returned colormap is a copy of the window's current colormap, but with an RGB color scale allocated in the range of contiguous cells determined by XA_RGB_DEFAULT_MAP. If it does not already exist, XA_RGB_DEFAULT_MAP will be created. ****************************************************************************** Author: Dave Hale, Colorado School of Mines, 09/29/90 *****************************************************************************/ { Screen *scr=XDefaultScreenOfDisplay(dpy); Window root=XRootWindowOfScreen(scr); Colormap cmap,wcmap; XStandardColormap scmap; XColor color; XWindowAttributes wa; unsigned long i,ncells,npixels; unsigned long bpixel,epixel,pixel[4096]; /* determine beginning and ending pixels in contiguous range */ bpixel = XtcwpGetFirstPixel(dpy); epixel = XtcwpGetLastPixel(dpy); if (epixel<=bpixel) return None; /* get standard colormap XA_RGB_DEFAULT_MAP */ if (!XGetStandardColormap(dpy,root,&scmap,XA_RGB_DEFAULT_MAP)) if (!XtcwpCreateRGBDefaultMap(dpy,&scmap)) return None; /* determine window's current colormap */ XGetWindowAttributes(dpy,win,&wa); wcmap = wa.colormap; /* create new colormap and allocate all cells read/write */ cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone); ncells = CellsOfScreen(scr); XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int)ncells); /* copy color cells from window's colormap to new colormap */ for (i=0; i<ncells; ++i) { if (i<bpixel || i>epixel) { color.pixel = i; XQueryColor(dpy,wcmap,&color); XFreeColors(dpy,cmap,&i,1,0); XAllocColor(dpy,cmap,&color); } } /* copy RGB color scale from XA_RGB_DEFAULT_MAP to new colormap */ npixels = epixel-bpixel+1; for (i=0; i<npixels; ++i) { color.pixel = bpixel+i; XQueryColor(dpy,scmap.colormap,&color); XStoreColor(dpy,cmap,&color); } /* return colormap */ return cmap; }
Status XtcwpCreateRGBDefaultMap (Display *dpy, XStandardColormap *scmap) /***************************************************************************** create XA_RGB_DEFAULT_MAP property of root window if it does not already exist ****************************************************************************** Input: dpy display Output: scmap the standard colormap structure ****************************************************************************** Notes: This function returns 0 if the XA_RGB_DEFAULT_MAP property does not exist and cannot be created. At least 8 contiguous color cells must be free in the default colormap to create the XA_RGB_DEFAULT_MAP. If created, the red_max, green_max, and blue_max values returned in scmap will be equal. ****************************************************************************** Author: Dave Hale, Colorado School of Mines, 09/29/90 *****************************************************************************/ { Screen *scr=XDefaultScreenOfDisplay(dpy); Window root=XRootWindowOfScreen(scr); Colormap cmap; XColor color; int i,ncells; unsigned long npixels; unsigned long bpixel,epixel,pixel1,pixel2,imax,rmult,gmult,bmult; unsigned long pixel[4096]; /* grab the server */ XGrabServer(dpy); /* if XA_RGB_DEFAULT_MAP does not exist, then */ if (!XGetStandardColormap(dpy,root,scmap,XA_RGB_DEFAULT_MAP)) { /* use default colormap */ cmap = DefaultColormapOfScreen(scr); /* determine largest number of contiguous free color cells */ ncells = CellsOfScreen(scr); while(ncells && !XAllocColorCells(dpy,cmap,True,NULL,0,pixel,ncells)) ncells--; /* determine beginning and ending pixel of contiguous cells */ for (i=1,bpixel=epixel=pixel1=pixel2=pixel[0]; i<ncells; i++) { if (pixel[i]==pixel[i-1]+1) pixel2 = pixel[i]; else pixel1 = pixel2 = pixel[i]; if (pixel2-pixel1>=epixel-bpixel) { bpixel = pixel1; epixel = pixel2; } } /* number of pixels must be at least 8 */ npixels = epixel-bpixel+1; if (npixels<8) { XUngrabServer(dpy); return 0; } /* force number of contiguous cells to be an integer cubed */ for (i=2,imax=0; i*i*i<=npixels; i++,imax++); npixels = (imax+1)*(imax+1)*(imax+1); bpixel = epixel-npixels+1; /* free cells not in contiguous range */ for (i=0; i<ncells; i++) if (pixel[i]<bpixel || pixel[i]>epixel) XFreeColors(dpy,cmap,&pixel[i],1,0); /* store colors in contiguous range of allocated cells */ rmult = (imax+1)*(imax+1); gmult = imax+1; bmult = 1; for (i=0; i<npixels; i++) { color.pixel = bpixel+i; color.red = (unsigned short) (i/rmult); color.green = (unsigned short) ((i-color.red*rmult)/gmult); color.blue = (unsigned short) (i-color.red*rmult-color.green*gmult); color.red *= 65535/imax; color.green *= 65535/imax; color.blue *= 65535/imax; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* set standard colormap */ scmap->colormap = cmap; scmap->red_max = imax; scmap->green_max = imax; scmap->blue_max = imax; scmap->red_mult = rmult; scmap->green_mult = gmult; scmap->blue_mult = bmult; scmap->base_pixel = bpixel; XSetStandardColormap(dpy,root,scmap,XA_RGB_DEFAULT_MAP); } /* ungrab the server before returning */ XUngrabServer(dpy); return 1; }
void fixColormap(ModeInfo * mi, int ncolors, float saturation, Bool mono, Bool install, Bool inroot, Bool inwindow, Bool verbose) { Display *display = MI_DISPLAY(mi); Window window = MI_WINDOW(mi); Screen *scr = MI_SCREENPTR(mi); Colormap cmap = MI_COLORMAP(mi); Colormap dcmap = DefaultColormapOfScreen(scr); XColor xcolor; unsigned char *red = (unsigned char *) NULL, *green = (unsigned char *) NULL, *blue = (unsigned char *) NULL; int colorcount, i, fixed, visualclass; #ifndef COMPLIANT_COLORMAP Bool retry = False; #endif if (mono || CellsOfScreen(scr) <= 2) { if (MI_PIXELS(mi)) return; if ((MI_PIXELS(mi) = (unsigned long *) calloc(2, sizeof (unsigned long))) == NULL) { (void) fprintf(stderr, "could not get the 2 colors for mono\n"); } monoColormap(scr, MI_SCREENINFO(mi), foreground, background); return; } colorcount = ncolors; if ((( red = (unsigned char *) calloc(ncolors, sizeof (unsigned char))) == NULL) || ((green = (unsigned char *) calloc(ncolors, sizeof (unsigned char))) == NULL) || (( blue = (unsigned char *) calloc(ncolors, sizeof (unsigned char))) == NULL)) { (void) fprintf(stderr, "could not get the %d colors\n", ncolors); if (red != NULL) free(red); if (green != NULL) free(green); return; } visualclass = MI_VISUALCLASS(mi); fixed = (visualclass == StaticGray) || (visualclass == StaticColor) || (visualclass == TrueColor); if ( #ifdef USE_DTSAVER dtsaver || /* needs to be in focus without mouse */ #endif inroot || (!install && !fixed) || cmap == None) { cmap = dcmap; } if (cmap != dcmap && MI_PIXELS(mi)) { XFreeColors(display, cmap, MI_PIXELS(mi), MI_NPIXELS(mi), 0); #ifndef COMPLIANT_COLORMAP XFreeColors(display, cmap, &(MI_BLACK_PIXEL(mi)), 1, 0); XFreeColors(display, cmap, &(MI_WHITE_PIXEL(mi)), 1, 0); #endif XFreeColors(display, cmap, &(MI_BG_PIXEL(mi)), 1, 0); XFreeColors(display, cmap, &(MI_FG_PIXEL(mi)), 1, 0); } /* else if (cmap) { (void) printf("cmap: this case is possible?\n"); } */ if (MI_PIXELS(mi)) free(MI_PIXELS(mi)); if ((MI_PIXELS(mi) = (unsigned long *) calloc(ncolors, sizeof (unsigned long))) == NULL) { (void) fprintf(stderr, "could not get the %d colors\n", ncolors); } /* "allocate" the black and white pixels, so that they will be included by XCopyColormapAndFree() if it gets called */ #ifdef COMPLIANT_COLORMAP MI_BLACK_PIXEL(mi) = BlackPixelOfScreen(scr); MI_WHITE_PIXEL(mi) = WhitePixelOfScreen(scr); #else MI_BLACK_PIXEL(mi) = allocPixel(display, cmap, "Black", "Black"); MI_WHITE_PIXEL(mi) = allocPixel(display, cmap, "White", "White"); #endif MI_BG_PIXEL(mi) = allocPixel(display, cmap, background, "White"); MI_FG_PIXEL(mi) = allocPixel(display, cmap, foreground, "Black"); hsbramp(0.0, saturation, 1.0, 1.0, saturation, 1.0, colorcount, red, green, blue); MI_NPIXELS(mi) = 0; for (i = 0; i < colorcount; i++) { xcolor.red = red[i] << 8; xcolor.green = green[i] << 8; xcolor.blue = blue[i] << 8; xcolor.flags = DoRed | DoGreen | DoBlue; if (!XAllocColor(display, cmap, &xcolor)) { #ifdef COMPLIANT_COLORMAP if (!install || cmap != dcmap) break; if ((cmap = XCopyColormapAndFree(display, cmap)) == dcmap) break; if (verbose) (void) fprintf(stderr, "using private colormap\n"); if (!XAllocColor(display, cmap, &xcolor)) break; #else if (verbose) (void) fprintf(stderr, "ran out of colors on colormap\n"); if ((saturation != 1.0 || ncolors != 64) && MI_NPIXELS(mi) < 2) { if (verbose) (void) fprintf(stderr, "retrying with saturation = 1.0 and ncolors = 64\n"); retry = True; } break; #endif } MI_PIXELS(mi)[i] = xcolor.pixel; MI_NPIXELS(mi)++; } free(red); free(green); free(blue); if (verbose) (void) fprintf(stderr, "%d pixel%s allocated\n", MI_NPIXELS(mi), (MI_NPIXELS(mi) == 1) ? "" : "s"); if (MI_NPIXELS(mi) <= 4) { XFreeColors(display, cmap, MI_PIXELS(mi), MI_NPIXELS(mi), 0); #ifndef COMPLIANT_COLORMAP XFreeColors(display, cmap, &(MI_BLACK_PIXEL(mi)), 1, 0); XFreeColors(display, cmap, &(MI_WHITE_PIXEL(mi) ), 1, 0); #endif XFreeColors(display, cmap, &(MI_BG_PIXEL(mi)), 1, 0); XFreeColors(display, cmap, &(MI_FG_PIXEL(mi)), 1, 0); #ifndef COMPLIANT_COLORMAP if (retry) { fixColormap(mi, 64, 1.0, mono, install, inroot, inwindow, verbose); return; } #endif monoColormap(scr, MI_SCREENINFO(mi), foreground, background); MI_COLORMAP(mi) = cmap = DefaultColormapOfScreen(scr); return; } MI_COLORMAP(mi) = cmap; if ((install || fixed) && !inroot && MI_NPIXELS(mi) > 2) { #if 0 (void) XGetWindowAttributes(display, window, &xgwa); if (cmap != xgwa.colormap) #endif #if 1 /* Turn off to simulate fvwm and tvwm */ setColormap(display, window, cmap, inwindow); #endif } #if 0 else { /* white and black colors may not be right for GL modes so lets set them */ MI_BLACK_PIXEL(mi) = BlackPixelOfScreen(scr); MI_WHITE_PIXEL(mi) = WhitePixelOfScreen(scr); /* foreground and background colors may not be right.... */ BlackPixelOfScreen(scr) = MI_BLACK_PIXEL(mi); WhitePixelOfScreen(scr) = MI_WHITE_PIXEL(mi); } #endif }