Actual source code: hue.c
petsc-3.3-p7 2013-05-11
2: #include <petscsys.h> /*I "petscsys.h" I*/
4: /*
5: Set up a color map, using uniform separation in hue space.
6: Map entries are Red, Green, Blue.
7: Values are "gamma" corrected.
8: */
10: /*
11: Gamma is a monitor dependent value. The value here is an
12: approximate that gives somewhat better results than Gamma = 1.
13: */
14: static PetscReal Gamma = 2.0;
18: PetscErrorCode PetscDrawUtilitySetGamma(PetscReal g)
19: {
21: Gamma = g;
22: return(0);
23: }
26: /*
27: * This algorithm is from Foley and van Dam, page 616
28: * given
29: * (0:359, 0:100, 0:100).
30: * h l s
31: * set
32: * (0:255, 0:255, 0:255)
33: * r g b
34: */
37: static PetscErrorCode PetscDrawUtilityHlsHelper(int h,int n1,int n2)
38: {
40: while (h > 360) h = h - 360;
41: while (h < 0) h = h + 360;
42: if (h < 60) PetscFunctionReturn(n1 + (n2-n1)*h/60);
43: if (h < 180) PetscFunctionReturn(n2);
44: if (h < 240) PetscFunctionReturn(n1 + (n2-n1)*(240-h)/60);
45: PetscFunctionReturn(n1);
46: }
50: static PetscErrorCode PetscDrawUtilityHlsToRgb(int h,int l,int s,unsigned char *r,unsigned char *g,unsigned char *b)
51: {
52: int m1,m2; /* in 0 to 100 */
55: if (l <= 50) m2 = l * (100 + s) / 100 ; /* not sure of "/100" */
56: else m2 = l + s - l*s/100;
58: m1 = 2*l - m2;
59: if (!s) {
60: /* ignore h */
61: *r = 255 * l / 100;
62: *g = 255 * l / 100;
63: *b = 255 * l / 100;
64: } else {
65: *r = (255 * PetscDrawUtilityHlsHelper(h+120,m1,m2)) / 100;
66: *g = (255 * PetscDrawUtilityHlsHelper(h,m1,m2)) / 100;
67: *b = (255 * PetscDrawUtilityHlsHelper(h-120,m1,m2)) / 100;
68: }
69: return(0);
70: }
74: PetscErrorCode PetscDrawUtilitySetCmapHue(unsigned char *red,unsigned char *green,unsigned char * blue,int mapsize)
75: {
77: int i,hue,lightness,saturation;
78: PetscReal igamma = 1.0 / Gamma;
81: red[0] = 0;
82: green[0] = 0;
83: blue[0] = 0;
84: hue = 0; /* in 0:359 */
85: lightness = 50; /* in 0:100 */
86: saturation = 100; /* in 0:100 */
87: for (i = 0; i < mapsize; i++) {
88: PetscDrawUtilityHlsToRgb(hue,lightness,saturation,red + i,green + i,blue + i);
89: red[i] = (int)floor(255.999 * pow(((PetscReal) red[i])/(PetscReal)255.0,igamma));
90: blue[i] = (int)floor(255.999 * pow(((PetscReal)blue[i])/(PetscReal)255.0,igamma));
91: green[i] = (int)floor(255.999 * pow(((PetscReal)green[i])/(PetscReal)255.0,igamma));
92: hue += (359/(mapsize-2));
93: }
94: return(0);
95: }