📄 xshades.c
字号:
/*---------------------------------------------------------------------- File : xshades.c Contents: X11 functions for color and grey shades allocation Author : Christian Borgelt History : 07.01.2000 file created 09.01.2000 color offset saved in structure 10.01.2000 grey shades added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <assert.h>#include "xshades.h"/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define RGB_MAX 65535 /* maximum RGB value *//*---------------------------------------------------------------------- Auxiliary Functions----------------------------------------------------------------------*/static double intensity (double col){ /* --- get rgb color intensity */ assert((col >= -2) && (col <= 3)); /* check the function argument */ if (col <= 0) return 0; /* intensity curve: */ if (col < 0.5) return 2*col; /* 0.5 1.5 */ if (col <= 1.5) return 1; /* _____ 1 */ if (col < 2) return 4 -2*col; /* / \_____ 0 */ return 0; /* 0 1 2 3 */} /* intensity() *//*---------------------------------------------------------------------- Main Functions----------------------------------------------------------------------*/void shd_delete (SHADES *shds){ /* --- delete color shades */ int i; /* loop variable */ PIXEL **pp; /* to delete the pixel vectors */ assert(shds); /* check the function argument */ i = (shds->colcnt > 0) ? shds->colcnt : 1; for (pp = shds->pixels; (--i >= 0) && *pp; pp++) XFreeColors(shds->disp, shds->colmap, *pp, shds->shdcnt, 0); free(shds->pixels[0]); /* delete the pixel vector */ free(shds); /* and the base structure */} /* shd_delete() *//*--------------------------------------------------------------------*/SHADES* shd_create (Display *disp, Colormap colmap, double coloff, int colcnt, int shdbase, int shdcnt){ /* --- create color shades */ int i, k; /* loop variables, buffers */ SHADES *shds; /* created color shades */ double col, dc; /* color and color difference */ double t; /* temporary buffer */ double red, green, blue; /* RGB intensities */ XColor xc; /* buffer for X color allocation */ PIXEL **pp, *p; /* to traverse the pixels */ assert(disp && colmap); /* check the function arguments */ if (shdcnt <= 0) shdcnt = 1; /* check and adapt number of shades */ if (shdcnt > SHD_MAXCNT) shdcnt = SHD_MAXCNT; coloff = fmod(coloff, 360); /* compute color offset and */ if (coloff < 0) coloff += 360;/* ensure that it is positive */ k = (colcnt > 0) ? colcnt : 1; shds = (SHADES*)calloc(1, sizeof(SHADES) +(k-1) *sizeof(PIXEL*)); if (!shds) return NULL; /* allocate the base structure */ shds->pixels[0] = p = (PIXEL*)malloc(k *shdcnt *sizeof(PIXEL)); if (!p) { free(shds); return NULL; } shds->disp = disp; /* and initialize other fields */ shds->colmap = colmap; shds->coloff = coloff; shds->colcnt = (colcnt <= 0) ? 0 : colcnt; shds->shdbase = (shdbase == SHD_BLACK) ? SHD_BLACK : SHD_WHITE; shds->shdcnt = shdcnt; /* --- grey shades --- */ if (colcnt <= 0) { /* if no color shades requested */ for (p += i = shdcnt; --i >= 0; ) { t = (shdcnt > 1) /* compute the intensity value */ ? ((double)i/(shdcnt-1)) *(RGB_MAX+0.99) : (RGB_MAX +0.99); k = (int)floor(t); /* compute RGB values of grey shade */ if (shdbase != SHD_BLACK) k = RGB_MAX -k; xc.red = xc.green = xc.blue = (unsigned short)k; if (XAllocColor(disp, colmap, &xc) == 0) { if ((k = shdcnt -i -1) > 0) XFreeColors(disp, colmap, p, k, 0); free(shds->pixels[0]); free(shds); return NULL; /* allocate the computed grey */ } /* and check for success */ *--p = xc.pixel; /* store the pixel value */ } return shds; /* return the created grey shades */ } /* --- color shades --- */ coloff /= 120; /* compute color offset from angle */ dc = 3.0/colcnt; /* and color difference as a fraction */ for (pp = shds->pixels, k = 0; k < colcnt; k++, p += shdcnt) { col = k *dc +coloff; /* traverse the colors */ col -= t = col -floor(col); col = (((2*t -3)*t +2)*t +fmod(col, 3)); red = intensity((col >= 2) ? col -2 : col +1); green = intensity(col); /* transform the color and */ blue = intensity(col -1); /* compute the RGB intensities */ for (*pp++ = p, p += i = shdcnt; --i >= 0; ) { t = (shdcnt > 1) /* compute the scaling factor */ ? ((double)i/(shdcnt-1)) *(RGB_MAX+0.99) : (RGB_MAX +0.99); if (shdbase == SHD_BLACK){/* if shading from black */ xc.red = (unsigned short)floor(red *t); xc.green = (unsigned short)floor(green *t); xc.blue = (unsigned short)floor(blue *t); } else { /* if shading from white */ xc.red = RGB_MAX -(unsigned short)floor((1 -red) *t); xc.green = RGB_MAX -(unsigned short)floor((1 -green) *t); xc.blue = RGB_MAX -(unsigned short)floor((1 -blue) *t); } /* compute integer RGB values */ if (XAllocColor(disp, colmap, &xc) == 0) { if ((k = shdcnt -i -1) > 0) XFreeColors(disp, colmap, p, k, 0); shd_delete(shds); return NULL; } /* allocate the computed color */ *--p = xc.pixel; /* store the pixel value */ } } return shds; /* return the created color shades */} /* shd_create() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -