📄 colormap.c
字号:
} /* cycle through the cmaps */ while (c_nr < 0) c_nr += max_cmap; while (c_nr >= max_cmap) c_nr -= max_cmap; if (verbose == 1) warn (" using : \"cmap=rgb%i\"", c_nr);if(depth<=8){ /* determine beginning and ending pixels in contiguous range */ bpixel = xGetFirstPixel(dpy); epixel = xGetLastPixel(dpy); if (epixel<=bpixel) return None; /* 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,&pixel[i],1,0); XAllocColor(dpy,cmap,&color); } } /* build scale in contiguous cells in new colormap */ npixels = epixel-bpixel+1; half = npixels / 2; /* Build the 1st ramp */ for (ih = 0; ih < half; ++ih) { color.pixel = bpixel + ih; color.red = c_rgb[c_nr][0][0] + (c_rgb[c_nr][1][0] - c_rgb[c_nr][0][0]) * ((float) ih)/((float) half); color.green = c_rgb[c_nr][0][1] + (c_rgb[c_nr][1][1] - c_rgb[c_nr][0][1]) * ((float) ih)/((float) half); color.blue = c_rgb[c_nr][0][2] + (c_rgb[c_nr][1][2] - c_rgb[c_nr][0][2]) * ((float) ih)/((float) half); color.red *= 257.0; color.green *= 257.0; color.blue *= 257.0; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* Build the 2nd ramp */ for (ih=half; ih<npixels; ++ih) { color.pixel = bpixel+ih; color.red = c_rgb[c_nr][1][0] + (c_rgb[c_nr][2][0] - c_rgb[c_nr][1][0]) * ((float) (ih-half))/((float) half); color.green = c_rgb[c_nr][1][1] + (c_rgb[c_nr][2][1] - c_rgb[c_nr][1][1]) * ((float) (ih-half))/((float) half); color.blue = c_rgb[c_nr][1][2] + (c_rgb[c_nr][2][2] - c_rgb[c_nr][1][2]) * ((float) (ih-half))/((float) half); color.red *= 257.0; color.green *= 257.0; color.blue *= 257.0; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* return colormap */ return cmap;}else{ /* Build the 1st ramp */ for (ih = 0; ih < 128; ++ih) { color.red = c_rgb[c_nr][0][0] + (c_rgb[c_nr][1][0] - c_rgb[c_nr][0][0]) * ((float) ih)/((float) 128); color.green = c_rgb[c_nr][0][1] + (c_rgb[c_nr][1][1] - c_rgb[c_nr][0][1]) * ((float) ih)/((float) 128); color.blue = c_rgb[c_nr][0][2] + (c_rgb[c_nr][1][2] - c_rgb[c_nr][0][2]) * ((float) ih)/((float) 128); color.red *= 257.0; color.green *= 257.0; color.blue *= 257.0; XAllocColor(dpy,wcmap,&color); truecolor_pixel[ih]=(unsigned long)color.pixel; } /* Build the 2nd ramp */ for (ih=128; ih<256; ++ih) { color.red = c_rgb[c_nr][1][0] + (c_rgb[c_nr][2][0] - c_rgb[c_nr][1][0]) * ((float) (ih-128))/((float) 128); color.green = c_rgb[c_nr][1][1] + (c_rgb[c_nr][2][1] - c_rgb[c_nr][1][1]) * ((float) (ih-128))/((float) 128); color.blue = c_rgb[c_nr][1][2] + (c_rgb[c_nr][2][2] - c_rgb[c_nr][1][2]) * ((float) (ih-128))/((float) 128); color.red *= 257.0; color.green *= 257.0; color.blue *= 257.0; XAllocColor(dpy,wcmap,&color); truecolor_pixel[ih]=(unsigned long)color.pixel; } return wcmap;}}#include "cwpcmaps.h"Colormap xCreateHSVColormap (Display *dpy, Window win, char *str_cmap, int verbose){ Screen *scr=XDefaultScreenOfDisplay(dpy); /* Window root=XRootWindowOfScreen(scr); --unused? */ Colormap cmap,wcmap; XColor color; XWindowAttributes wa; int i,ncells; unsigned long npixels; unsigned long bpixel,epixel,pixel[4096];/*# define HSV_BLACK { 0.0, 0.00, 0.00}# define HSV_GRAY { 0.0, 0.00, 0.50}# define HSV_WHITE { 0.0, 0.00, 1.00}# define HSV_HUE1 {240.0, 1.00, 0.50}# define HSV_HUE2 {120.0, 1.00, 0.50}# define HSV_HUE3 { 0.0, 1.00, 0.50}# define HSV_DRED { 0.0, 1.00, 0.50}# define HSV_BROWN { 30.0, 1.00, 0.30}# define HSV_GREEN {140.0, 1.00, 0.50}# define HSV_BLUE {240.0, 1.00, 0.70}# define HSV_YELLOW { 70.0, 1.00, 0.50} float c_hsv [][3][3] = { { HSV_WHITE, HSV_GRAY, HSV_BLACK }, { HSV_HUE1, HSV_HUE2, HSV_HUE3 }, { HSV_HUE3, HSV_HUE2, HSV_HUE1 }, { HSV_BROWN, HSV_GREEN, HSV_BLUE }, { HSV_DRED, HSV_WHITE, HSV_BLUE }, { HSV_BLUE, HSV_WHITE, HSV_DRED }, { HSV_WHITE, HSV_DRED, HSV_BLUE }, { HSV_WHITE, HSV_GREEN, HSV_BLUE }, { HSV_BLUE, HSV_DRED, HSV_WHITE }, { HSV_BLUE, HSV_GREEN, HSV_WHITE }, { HSV_BLUE, HSV_WHITE, HSV_GREEN }, { HSV_YELLOW, HSV_DRED, HSV_BROWN }, { HSV_BROWN, HSV_DRED, HSV_YELLOW }, { HSV_DRED, HSV_YELLOW, HSV_BROWN } };*/ static int c_nr = -1; unsigned long max_cmap, half, ih; float r,g,b, h,s,v; /* Red,Green,Blue, Hue,Sat,Value */ unsigned int depth, sr; sr=DefaultScreen(dpy); depth=(unsigned int)DefaultDepth(dpy,sr); /* determine window's current colormap */ XGetWindowAttributes(dpy,win,&wa); wcmap = wa.colormap; max_cmap = sizeof (c_hsv) / sizeof (float[3][3]); /* We got the specific number of the cmap from the string */ if (STREQ (str_cmap, "hsv_up")) c_nr++; else if (STREQ (str_cmap, "hsv_down")) c_nr--; else { if (strlen (str_cmap) > 3) { str_cmap[0] = str_cmap[1] = str_cmap[2] = ' '; c_nr = atoi (str_cmap); if (c_nr < 0 || c_nr >= max_cmap) { warn ("\"cmap=hsv%i\" not installed !", c_nr); c_nr = 0; warn (" using : \"cmap=hsv%i\"", c_nr); } } } /* cycle through the cmaps */ while (c_nr < 0) c_nr += max_cmap; while (c_nr >= max_cmap) c_nr -= max_cmap; if (verbose == 1) warn (" using : \"cmap=hsv%i\"", c_nr);if(depth<=8){ /* determine beginning and ending pixels in contiguous range */ bpixel = xGetFirstPixel(dpy); epixel = xGetLastPixel(dpy); if (epixel<=bpixel) return None; /* 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,&pixel[i],1,0); XAllocColor(dpy,cmap,&color); } } /* build scale in contiguous cells in new colormap */ npixels = epixel-bpixel+1; half = npixels / 2; /* Build the 1st ramp */ for (ih = 0; ih < half; ++ih) { color.pixel = bpixel + ih; h = c_hsv[c_nr][0][0] + (c_hsv[c_nr][1][0] - c_hsv[c_nr][0][0]) * ((float) ih) /((float) half); s = c_hsv[c_nr][0][1] + (c_hsv[c_nr][1][1] - c_hsv[c_nr][0][1]) * ((float) ih) / ((float) half); v = c_hsv[c_nr][0][2] + (c_hsv[c_nr][1][2] - c_hsv[c_nr][0][2]) * ((float) ih) / ((float) half); hsv2rgb (h, s, v, &r, &g, &b); color.red = 65535.0 * r; color.green = 65535.0 * g; color.blue = 65535.0 * b; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* Build the 2nd ramp */ for (ih = half; ih < npixels; ++ih) { color.pixel = bpixel + ih; h = c_hsv[c_nr][1][0] + (c_hsv[c_nr][2][0] - c_hsv[c_nr][1][0]) * ((float) (ih-half))/((float) half); s = c_hsv[c_nr][1][1] + (c_hsv[c_nr][2][1] - c_hsv[c_nr][1][1]) * ((float) (ih-half))/((float) half); v = c_hsv[c_nr][1][2] + (c_hsv[c_nr][2][2] - c_hsv[c_nr][1][2]) * ((float) (ih-half))/((float) half); hsv2rgb (h, s, v, &r, &g, &b); color.red = 65535.0 * r; color.green = 65535.0 * g; color.blue = 65535.0 * b; color.flags = DoRed|DoGreen|DoBlue; XStoreColor(dpy,cmap,&color); } /* return colormap */ return cmap;}else{ /* Build the 1st ramp */ for (ih = 0; ih < 128; ++ih) { h = c_hsv[c_nr][0][0] + (c_hsv[c_nr][1][0] - c_hsv[c_nr][0][0]) * ((float) ih) /((float) 128); s = c_hsv[c_nr][0][1] + (c_hsv[c_nr][1][1] - c_hsv[c_nr][0][1]) * ((float) ih) / ((float) 128); v = c_hsv[c_nr][0][2] + (c_hsv[c_nr][1][2] - c_hsv[c_nr][0][2]) * ((float) ih) / ((float) 128); hsv2rgb (h, s, v, &r, &g, &b); color.red = 65535.0 * r; color.green = 65535.0 * g; color.blue = 65535.0 * b; XAllocColor(dpy,wcmap,&color); truecolor_pixel[ih]=(unsigned long)color.pixel; } /* Build the 2nd ramp */ for (ih = 128; ih < 256; ++ih) { h = c_hsv[c_nr][1][0] + (c_hsv[c_nr][2][0] - c_hsv[c_nr][1][0]) * ((float) (ih-128))/((float) 128); s = c_hsv[c_nr][1][1] + (c_hsv[c_nr][2][1] - c_hsv[c_nr][1][1]) * ((float) (ih-128))/((float) 128); v = c_hsv[c_nr][1][2] + (c_hsv[c_nr][2][2] - c_hsv[c_nr][1][2]) * ((float) (ih-128))/((float) 128); hsv2rgb (h, s, v, &r, &g, &b); color.red = 65535.0 * r; color.green = 65535.0 * g; color.blue = 65535.0 * b; XAllocColor(dpy,wcmap,&color); truecolor_pixel[ih]=(unsigned long)color.pixel; } /* return colormap */ return wcmap;}}/* internal functions to convert HSV to RGB */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;}/* * variable range * -------- ------------ * h 0.0 .. 360.0 * s 0.0 .. 1.0 * v 0.0 .. 1.0 */static void hsv2rgb (float h, float s, float v, float *r, float *g, float *b){ float m1,m2; /* float rgbvalue (float,float,float);*/ if (v <= 0.5) m2 = v * (1.0 + s); else m2 = v + s - v * s; m1 = 2 * v - m2; if (s == 0.0) { *r = *g = *b = v; } else { *r = rgbvalue(m1, m2, h + 120.0); *g = rgbvalue(m1, m2, h); *b = rgbvalue(m1, m2, h - 120.0); if (*r > 1.0) *r = 1.0; if (*g > 1.0) *g = 1.0; if (*b > 1.0) *b = 1.0; }} /* test program - compile with "cc -DTEST colormap.c $INCS $LIBS ..." */#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 = xCreateRGBColormap(dpy,win, "rgb0", 1); 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 + -