📄 colormap.c
字号:
******************************************************************************Notes:The returned colormap is only created; the window's colormap attributeis 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 cellsdetermined 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; int i,ncells,npixels; unsigned long bpixel,epixel,pixel[4096]; /* determine beginning and ending pixels in contiguous range */ bpixel = xGetFirstPixel(dpy); epixel = xGetLastPixel(dpy); if (epixel<=bpixel) return None; /* get standard colormap XA_RGB_DEFAULT_MAP */ if (!XGetStandardColormap(dpy,root,&scmap,XA_RGB_DEFAULT_MAP)) if (!xCreateRGBDefaultMap(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,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;}Colormap xCreateHueColormap (Display *dpy, Window win)/*****************************************************************************create a colormap with varying hues (blue to red) in contiguous cells******************************************************************************Input:dpy displaywin window******************************************************************************Notes:The returned colormap is only created; the window's colormap attributeis 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 contiguouscells 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; XColor color; XWindowAttributes wa; int i,ncells,npixels; unsigned long bpixel,epixel,pixel[4096]; float red,green,blue; void hlsrgb (float, float, float, float*, float*, float*); /* determine beginning and ending pixels in contiguous range */ bpixel = xGetFirstPixel(dpy); epixel = xGetLastPixel(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,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; hlsrgb(240.0-240.0*i/(npixels-1),0.5,1.0,&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 xCreateGrayColormap (Display *dpy, Window win)/*****************************************************************************create a colormap with a gray scale in contiguous cells******************************************************************************Input:dpy displaywin window******************************************************************************Notes:The returned colormap is only created; the window's colormap attributeis 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 contiguouscells 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; XColor color; XWindowAttributes wa; int i,ncells,npixels; unsigned long bpixel,epixel,pixel[4096]; /* determine beginning and ending pixels in contiguous range */ bpixel = xGetFirstPixel(dpy); epixel = xGetLastPixel(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,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 = 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;}/* internal functions to convert HLS to RGB (adapted from Foley & Van Dam) */static float rgbvalue (float n1, float n2, float hue){ while (hue>360.0) hue -= 360.0; while (hue<0.0) hue += 360.0; if (hue<60.0) return n1+(n2-n1)*hue/60.0; else if (hue<180.0) return n2; else if (hue<240.0) return n1+(n2-n1)*(240.0-hue)/60.0; else return n1;}static void hlsrgb (float h, float l, float s, float *r, float *g, float *b){ float m1,m2; float rgbvalue (float,float,float); if (l<=0.5) m2 = l*(1.0+s); else m2 = l+s-l*s; m1 = 2*l-m2; if (s==0.0) { *r = *g = *b = l; } else { *r = rgbvalue(m1,m2,h+120.0); *g = rgbvalue(m1,m2,h); *b = rgbvalue(m1,m2,h-120.0); }} /* test program - compile with "cc -DTEST colormap.c -lX11" */#ifdef TEST#include <stdio.h>#define X 100#define Y 100#define WIDTH 256#define HEIGHT 64main(){ Display *dpy; Window root,win; Colormap cmap; XStandardColormap scmap; XColor color,junk; XImage *image; XEvent event; GC gc; int scr,i; unsigned long black,white,pmin,pmax; char *data; /* connect to X server */ dpy = XOpenDisplay(NULL); if ((dpy=XOpenDisplay(NULL))==NULL) { fprintf(stderr,"Cannot open display!\n"); exit(-1); } scr = DefaultScreen(dpy); root = RootWindow(dpy,scr); black = BlackPixel(dpy,scr); white = WhitePixel(dpy,scr); /* create and map window */ win = XCreateSimpleWindow(dpy,root,X,Y,WIDTH,HEIGHT,4,black,white); cmap = xCreateGrayColormap(dpy,win); XSetWindowColormap(dpy,win,cmap); XMapWindow(dpy,win); /* determine range of contiguous pixels from standard colormap */ if (!xCreateRGBDefaultMap(dpy,&scmap)) { fprintf(stderr,"Cannot create standard colormap!\n"); exit(-1); } pmin = xGetFirstPixel(dpy); pmax = xGetLastPixel(dpy); /* create image */ data = (char*)malloc(WIDTH*HEIGHT); for (i=0; i<WIDTH*HEIGHT; ++i) data[i] = pmin+(pmax-pmin)*(i%WIDTH)/WIDTH; image = XCreateImage(dpy,DefaultVisual(dpy,scr), DefaultDepth(dpy,scr),ZPixmap, 0,data,WIDTH,HEIGHT,BitmapPad(dpy),WIDTH); gc = XCreateGC(dpy,win,0,NULL); XAllocNamedColor(dpy,cmap,"red",&color,&junk); XSetForeground(dpy,gc,color.pixel); /* set event mask */ XSelectInput(dpy,win,ExposureMask); /* loop forever */ XPutImage(dpy,win,gc,image,0,0,0,0,WIDTH,HEIGHT); while(True) { XNextEvent(dpy,&event); while (XCheckTypedEvent(dpy,Expose,&event)); XPutImage(dpy,win,gc,image,0,0,0,0,WIDTH,HEIGHT); XDrawLine(dpy,win,gc,0,0,WIDTH,HEIGHT); } /* close display */ XCloseDisplay(dpy);}#endif /* TEST */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -