📄 wshades.c
字号:
/*---------------------------------------------------------------------- File : wshades.c Contents: Windows functions for color and grey shades allocation Author : Christian Borgelt History : 28.01.2000 file created----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <assert.h>#include "wshades.h"/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define RGB_MAX 255 /* 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 */ assert(shds); /* check the function argument */ free(shds->pixels[0]); /* delete the pixel vectors */ free(shds); /* and the base structure */} /* shd_delete() *//*--------------------------------------------------------------------*/SHADES* shd_create (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 */ PIXEL **pp, *p; /* to traverse the pixels */ 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*)malloc(sizeof(SHADES) +(k-1) *sizeof(PIXEL*)); if (!shds) return NULL; /* allocate the base structure */ shds->pixels[0] = p = (PIXEL*)calloc(k *shdcnt, sizeof(PIXEL)); if (!p) { free(shds); return NULL; } for (pp = shds->pixels; --k > 0; ) *++pp = p += shdcnt; /* allocate and organize vectors */ shds->coloff = coloff; /* and initialize other fields */ 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; *--p = RGB(k, k, k); /* if to shade from white, invert */ } /* and store the computed grey */ 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++, pp++) { 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 (p = *pp +(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 */ *--p = RGB((WORD)floor(red *t), (WORD)floor(green *t), (WORD)floor(blue *t)); } else { /* if shading from white */ *--p = RGB(RGB_MAX -(WORD)floor((1 -red) *t), RGB_MAX -(WORD)floor((1 -green) *t), RGB_MAX -(WORD)floor((1 -blue) *t)); } /* compute the RGB value and */ } /* store it in the color vector */ } return shds; /* return the created color shades */} /* shd_create() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -