📄 colormap.c
字号:
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;}Colormap XtcwpCreateGrayColormap (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); 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 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 displaywin windowfhue first hue in colormaplhue last hue in colormapsat saturationbright brightness******************************************************************************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/90Modified: 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,green,blue; /* 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 XtcwpCreateSatColormap (Display *dpy, Window win, float fhue, float lhue, float wfrac, float bright)/*****************************************************************************create a colormap with varying saturations in contiguous cells******************************************************************************Input:dpy displaywin windowfhue 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 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: 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,green,blue; /* 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;}static void hsvrgb(float h, float s, float v, float *r, float *g, float *b)/*****************************************************************************convert HSV color space coordinates to RGB color space******************************************************************************Input:h hue (0=1=red, 0.333=green, 0.667=blue)s saturation (0=white, 1=pure color)v value (brightness) (0=black, 1=max intensity)******************************************************************************Output:r red (0=black, 1=max red)g green (0=black, 1=max green)b blue (0=black, 1=max blue)*****************************************************************************/{ float f,p,q,t; if (s==0.0) { *r = v; *g = v; *b = v; } else { while (h<0.0) h += 1.0; while (h>=1.0) h -= 1.0; h *= 6.0; f = h-(int)h; /* fractional part of hue */ p = v*(1.0-s); q = v*(1.0-(s*f)); t = v*(1.0-(s*(1.0-f))); switch ((int)h) { case 0: *r = v; *g = t; *b = p; break; case 1: *r = q; *g = v; *b = p; break; case 2: *r = p; *g = v; *b = t; break; case 3: *r = p; *g = q; *b = v; break; case 4: *r = t; *g = p; *b = v; break; case 5: *r = v; *g = p; *b = q; break; } }} /* 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 = XtcwpCreateGrayColormap(dpy,win); XSetWindowColormap(dpy,win,cmap); XMapWindow(dpy,win); /* determine range of contiguous pixels from standard colormap */ if (!XtcwpCreateRGBDefaultMap(dpy,&scmap)) { fprintf(stderr,"Cannot create standard colormap!\n"); exit(-1); } pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(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 + -