📄 colormaps.c
字号:
/* define hue, saturation, lightness values */
#include "../include/xplot.h"
# 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 }
};
/* define red, green, blue values */
# define RGB_BLACK {0x00, 0x00, 0x00}
# define RGB_WHITE {0xff, 0xff, 0xff}
# define RGB_GRAY {0x80, 0x80, 0x80}
# define RGB_ORANGE {0xff, 0x80, 0x00}
# define RGB_RED {0xe0, 0x00, 0x50}
# define RGB_BLUE {0x00, 0x40, 0xc0}
# define RGB_GREEN {0x06, 0x5b, 0x3f}
# define RGB_BROWN {0x72, 0x5b, 0x3f}
# define RGB_REDBROWN {0xa0, 0x40, 0x00}
# define RGB_GRAY2 {0xb0, 0xb0, 0xb0}
# define RGB_LGRAY {0xf0, 0xf0, 0xf0}
# define RGB_LBLUE {0x55, 0x9c, 0xe0}
# define RGB_YELLOW {0xd0, 0xb0, 0x20}
float c_rgb [][3][3] = {
{ RGB_BLACK, RGB_GRAY, RGB_WHITE },
{ RGB_RED, RGB_LGRAY, RGB_BLUE },
{ RGB_RED, RGB_LGRAY, RGB_GREEN },
{ RGB_BROWN, RGB_LGRAY, RGB_BLUE },
{ RGB_BROWN, RGB_LGRAY, RGB_GREEN },
{ RGB_REDBROWN, RGB_LGRAY, RGB_BLUE },
{ RGB_REDBROWN, RGB_LGRAY, RGB_GREEN },
{ RGB_ORANGE, RGB_LGRAY, RGB_BLUE },
{ RGB_ORANGE, RGB_LGRAY, RGB_GREEN },
{ RGB_BROWN, RGB_GRAY2, RGB_GREEN },
{ RGB_BROWN, RGB_GRAY2, RGB_BLUE },
{ RGB_BROWN, RGB_YELLOW, RGB_BLUE }
};
/*********************** self documentation **********************/
/*****************************************************************************
COLORMAP - Functions to manipulate X colormaps:
xCreateRGBDefaultMap create XA_RGB_DEFAULT_MAP property of root window if
it does not already exist
xGetFirstPixel return first pixel in range of contiguous pixels in
XA_RGB_DEFAULT_MAP
xGetLastPixel return last pixel in range of contiguous pixels in
XA_RGB_DEFAULT_MAP
xCreateHSVColormap create a 2 ramp colormap (HSV - Model)
xCreateRGBColormap create a 2 ramp colormap (RGB - Model)
******************************************************************************
Function Prototypes:
Status xCreateRGBDefaultMap (Display *dpy, XStandardColormap *scmap);
unsigned long xGetFirstPixel (Display *dpy);
unsigned long xGetLastPixel (Display *dpy);
Colormap xCreateRGBColormap (Display *dpy, Window win,
char *str_cmap, int verbose)
Colormap xCreateHSVColormap (Display *dpy, Window win,
char *str_cmap, int verbose)
******************************************************************************
xCreateRGBDefaultMap:
Input:
dpy display
Output:
scmap the standard colormap structure
******************************************************************************
xGetFirstPixel, xGetLastPixel:
Input:
dpy display
*****************************************************************************/
/**************** end self doc ********************************/
unsigned long truecolor_pixel[256];
/* functions defined and used internally */
static float rgbvalue (float n1, float n2, float hue);
static void hsv2rgb (float h, float s, float v, float *r, float *g, float *b);
Status xCreateRGBDefaultMap (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.
*****************************************************************************/
{
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];
#ifndef __osf__
/* grab the server */
XGrabServer(dpy);
#endif
/* 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) {
#ifndef __osf__
XUngrabServer(dpy);
#endif
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 */
#ifndef __osf__
XUngrabServer(dpy);
#endif
return 1;
}
unsigned long xGetFirstPixel (Display *dpy)
/*****************************************************************************
return first pixel in range of contiguous pixels in XA_RGB_DEFAULT_MAP
******************************************************************************
Input:
dpy display
******************************************************************************
Notes:
If it does not already exist, XA_RGB_DEFAULT_MAP will be created.
If XA_RGB_DEFAULT_MAP does not exist and cannot be created, then
this function returns 0.
*****************************************************************************/
{
Screen *scr=XDefaultScreenOfDisplay(dpy);
Window root=XRootWindowOfScreen(scr);
XStandardColormap scmap;
/* if XA_RGB_DEFAULT_MAP does not exist, create it */
if (!XGetStandardColormap(dpy,root,&scmap,XA_RGB_DEFAULT_MAP))
if (!xCreateRGBDefaultMap(dpy,&scmap))
return 0;
/* return first pixel in range of contiguous pixels */
return scmap.base_pixel;
}
unsigned long xGetLastPixel (Display *dpy)
/*****************************************************************************
return last pixel in range of contiguous pixels in XA_RGB_DEFAULT_MAP
******************************************************************************
Input:
dpy display
******************************************************************************
Notes:
If it does not already exist, XA_RGB_DEFAULT_MAP will be created.
If XA_RGB_DEFAULT_MAP does not exist and cannot be created, then
this function returns 0.
*****************************************************************************/
{
Screen *scr=XDefaultScreenOfDisplay(dpy);
Window root=XRootWindowOfScreen(scr);
XStandardColormap scmap;
/* if XA_RGB_DEFAULT_MAP does not exist, create it */
if (!XGetStandardColormap(dpy,root,&scmap,XA_RGB_DEFAULT_MAP))
if (!xCreateRGBDefaultMap(dpy,&scmap))
return 0;
/* return last pixel in range of contiguous pixels */
return scmap.base_pixel+
scmap.red_max*scmap.red_mult+
scmap.green_max*scmap.green_mult+
scmap.blue_max*scmap.blue_mult;
}
Colormap xCreateRGBColormap (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];
unsigned int depth,sr;
static int c_nr = -1;
unsigned long max_cmap, half, ih;
sr=DefaultScreen(dpy);
depth=(unsigned int)DefaultDepth(dpy,sr);
if( verbose == 1 ) {
warn("\n in colormap, depth=%d\n",depth);
}
/* determine window's current colormap */
XGetWindowAttributes(dpy,win,&wa);
wcmap = wa.colormap;
max_cmap = sizeof (c_rgb) / sizeof (float[3][3]);
/* We got the specific number of the cmap from the string */
if (STREQ (str_cmap, "rgb_up"))
c_nr++;
else if (STREQ (str_cmap, "rgb_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=rgb%i\" not installed !", c_nr);
c_nr = 0;
warn (" using : \"cmap=rgb%i\"", c_nr);
}
}
}
/* cycle through the cmaps */
while (c_nr < 0)
c_nr += max_cmap;
while (c_nr >= max_cmap)
c_nr -= max_cmap;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -