📄 xcolor.c
字号:
XBWindow *XBWin;PixVal *white_pix, *black_pix;{ *white_pix = XBWin->cmapping[WHITE]; *black_pix = XBWin->cmapping[BLACK]; return 0;}/* Set up a color map, using uniform separation in hue space. Map entries are Red, Green, Blue. Values are "gamma" corrected. *//* Gamma is a monitor dependent value. The value here is an approximate that gives somewhat better results than Gamma = 1. */static double Gamma = 2.0;#include <math.h>int XBSetGamma( g )double g;{ Gamma = g; return 0;}int XBSetCmapHue( red, green, blue, mapsize )int mapsize;unsigned char *red, *green, *blue;{ int i, hue, lightness, saturation; double igamma = 1.0 / Gamma; red[0] = 0; green[0] = 0; blue[0] = 0; hue = 0; /* in 0:359 */ lightness = 50; /* in 0:100 */ saturation = 100; /* in 0:100 */ for (i = 1; i < mapsize-1; i++) { XBHlsToRgb( hue, lightness, saturation, red + i, green + i, blue + i ); red[i] = floor( 255.999 * pow( ((double) red[i])/255.0, igamma ) ); blue[i] = floor( 255.999 * pow( ((double) blue[i])/255.0, igamma ) ); green[i] = floor( 255.999 * pow( ((double)green[i])/255.0, igamma ) ); hue += (359/(mapsize-2)); } red [mapsize-1] = 255; green[mapsize-1] = 255; blue [mapsize-1] = 255; return 0;}/* * This algorithm is from Foley and van Dam, page 616 * given * (0:359, 0:100, 0:100). * h l s * set * (0:255, 0:255, 0:255) * r g b */int XBHlsHelper( h, n1, n2 )int h, n1, n2;{while (h > 360) h = h - 360;while (h < 0) h = h + 360;if (h < 60) return n1 + (n2-n1)*h/60;if (h < 180) return n2;if (h < 240) return n1 + (n2-n1)*(240-h)/60;return n1;}int XBHlsToRgb( h, l, s, r, g, b )int h, l, s;unsigned char *r, *g, *b;{int m1, m2; /* in 0 to 100 */if (l <= 50) m2 = l * ( 100 + s ) / 100 ; /* not sure of "/100" */else m2 = l + s - l*s/100;m1 = 2*l - m2;if (s == 0) { /* ignore h */ *r = 255 * l / 100; *g = 255 * l / 100; *b = 255 * l / 100; }else { *r = (255 * XBHlsHelper( h+120, m1, m2 ) ) / 100; *g = (255 * XBHlsHelper( h, m1, m2 ) ) / 100; *b = (255 * XBHlsHelper( h-120, m1, m2 ) ) / 100; }return 0;}/* This routine returns the pixel value for the specified color Returns 0 on failure, <>0 otherwise. */int XBFindColor( XBWin, name, pixval )XBWindow *XBWin;char *name;PixVal *pixval;{ XColor colordef; int st; st = XParseColor( XBWin->disp, XBWin->cmap, name, &colordef ); if (st) { st = XAllocColor( XBWin->disp, XBWin->cmap, &colordef ); if (st) *pixval = colordef.pixel; } else printf( "did not find color %s\n", name ); return st;}/* When there are several windows being displayed, it may help to merge their colormaps together so that all of the windows may be displayed simultaneously with true colors. These routines attempt to accomplish this *//* * The input to this routine is RGB, not HLS. * X colors are 16 bits, not 8, so we have to shift the input by 8. * This is like XBCmap, except that it APPENDS to the existing * colormap. */int XBAddCmap( red, green, blue, mapsize, XBWin )int mapsize;unsigned char red[], green[], blue[];XBWindow *XBWin;{ int i, err; XColor colordef; int cmap_start; if (mapsize + XBWin->maxcolors > XBWin->numcolors) mapsize = XBWin->numcolors - XBWin->maxcolors; cmap_start = XBWin->maxcolors; XBWin->maxcolors += mapsize; err = 0; for (i=0; i<mapsize; i++) { colordef.red = ((int)red[i] * 65535) / 255; colordef.green = ((int)green[i] * 65535) / 255; colordef.blue = ((int)blue[i] * 65535) / 255; colordef.flags = DoRed | DoGreen | DoBlue; if (!XAllocColor( XBWin->disp, XBWin->cmap, &colordef )) err = 1; XBWin->cmapping[cmap_start+i] = colordef.pixel; /* printf( "pixel value for %d is %d\n\r", i, XBWin->cmapping[i] ); */ } return err;}/* Another real need is to assign "colors" that make sense for a monochrome display, without unduely penalizing color displays. This routine takes a color name, a window, and a flag that indicates whether this is "background" or "foreground". In the monchrome case (or if the color is otherwise unavailable), the "background" or "foreground" colors will be chosen */PixVal XBGetColor( XBWin, name, is_fore )XBWindow *XBWin;char *name;int is_fore;{ PixVal pixval; if (XBWin->numcolors == 2 || !XBFindColor( XBWin, name, &pixval )) pixval = is_fore ? XBWin->cmapping[WHITE] : XBWin->cmapping[BLACK]; return pixval;}/* This routine takes a named color and returns a color that is either lighter or darker */PixVal XBSimColor( XBWin, pixel, intensity, is_fore )XBWindow *XBWin;PixVal pixel;int intensity, is_fore;{ XColor colordef, colorsdef; char RGBcolor[20]; PixVal red, green, blue; /* int st; */ colordef.pixel = pixel; XQueryColor( XBWin->disp, XBWin->cmap, &colordef );/* Adjust the color value up or down. Get the RGB values for the color */ red = colordef.red; green = colordef.green; blue = colordef.blue;#define min(a,b) ((a)<(b) ? a : b)#define max(a,b) ((a)>(b) ? a : b)#define WHITE_AMOUNT 5000 if (intensity > 0) { /* Add white to the color */ red = min(65535,red + WHITE_AMOUNT); green = min(65535,green + WHITE_AMOUNT); blue = min(65535,blue + WHITE_AMOUNT); } else { /* Subtract white from the color */ red = (red < WHITE_AMOUNT) ? 0 : red - WHITE_AMOUNT; green = (green < WHITE_AMOUNT) ? 0 : green - WHITE_AMOUNT; blue = (blue < WHITE_AMOUNT) ? 0 : blue - WHITE_AMOUNT; } sprintf( RGBcolor, "rgb:%4.4x/%4.4x/%4.4x", (unsigned int)red, (unsigned int) green, (unsigned int) blue ); XLookupColor( XBWin->disp, XBWin->cmap, RGBcolor, &colordef, &colorsdef ); /* st = XLookupColor( XBWin->disp, XBWin->cmap, RGBcolor, &colordef, &colorsdef ); */ return colorsdef.pixel;}/* XBUniformHues - Set the colormap to a uniform distribution Input parameters:. XBwin - window to set colors for. ncolors - number of colors Note: This routine sets the colors in the current colormap, if the default colormap is used. The Pixel values chosen are in the cmapping structure; this is used by routines such as the XB contour plotter. */ void XBUniformHues( XBwin, ncolors )XBWindow *XBwin;int ncolors;{ unsigned char *red, *green, *blue; red = (unsigned char *)MALLOC( 3 * ncolors * sizeof(unsigned char) ); /* CHKPTR(red); */ green = red + ncolors; blue = green + ncolors; XBSetCmapHue( red, green, blue, ncolors ); XBCmap( red, green, blue, ncolors, XBwin ); FREE( red );}/* XBSetCmapLight - Create rgb values from a single color by adding white Input Parameters:. mapsize - number of values Output Parameters:. red - red values. green - green values. blue - blue values Note: The initial color is (red[0],green[0],blue[0]). */void XBSetCmapLight( red, green, blue, mapsize ) int mapsize; unsigned char *red, *green, *blue;{ int i ; for (i = 1; i < mapsize-1; i++) { blue[i] = i*(255-(int)blue[0])/(mapsize-2)+(int)blue[0] ; green[i] = i*(255-(int)green[0])/(mapsize-2)+(int)green[0] ; red[i] = i*(255-(int)red[0])/(mapsize-2)+(int)red[0] ; } red[mapsize-1] = green[mapsize-1] = blue[mapsize-1] = 255;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -