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