Actual source code: hue.c

  1: /*$Id: hue.c,v 1.10 2001/03/23 23:20:24 balay Exp $*/

 3:  #include petsc.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;

 17: int PetscDrawUtilitySetGamma(PetscReal g)
 18: {
 20:   Gamma = g;
 21:   return(0);
 22: }


 25: /*
 26:  * This algorithm is from Foley and van Dam, page 616
 27:  * given
 28:  *   (0:359, 0:100, 0:100).
 29:  *      h       l      s
 30:  * set
 31:  *   (0:255, 0:255, 0:255)
 32:  *      r       g      b
 33:  */
 34: static int PetscDrawUtilityHlsHelper(int h,int n1,int n2)
 35: {
 37:   while (h > 360) h = h - 360;
 38:   while (h < 0)   h = h + 360;
 39:   if (h < 60)  PetscFunctionReturn(n1 + (n2-n1)*h/60);
 40:   if (h < 180) PetscFunctionReturn(n2);
 41:   if (h < 240) PetscFunctionReturn(n1 + (n2-n1)*(240-h)/60);
 42:   PetscFunctionReturn(n1);
 43: }

 45: static int PetscDrawUtilityHlsToRgb(int h,int l,int s,unsigned char *r,unsigned char *g,unsigned char *b)
 46: {
 47:   int m1,m2;         /* in 0 to 100 */

 50:   if (l <= 50) m2 = l * (100 + s) / 100 ;           /* not sure of "/100" */
 51:   else         m2 = l + s - l*s/100;

 53:   m1  = 2*l - m2;
 54:   if (!s) {
 55:     /* ignore h */
 56:     *r  = 255 * l / 100;
 57:     *g  = 255 * l / 100;
 58:     *b  = 255 * l / 100;
 59:   } else {
 60:     *r  = (255 * PetscDrawUtilityHlsHelper(h+120,m1,m2)) / 100;
 61:     *g  = (255 * PetscDrawUtilityHlsHelper(h,m1,m2))     / 100;
 62:     *b  = (255 * PetscDrawUtilityHlsHelper(h-120,m1,m2)) / 100;
 63:   }
 64:   return(0);
 65: }

 67: int PetscDrawUtilitySetCmapHue(unsigned char *red,unsigned char *green,unsigned char * blue,int mapsize)
 68: {
 69:   int        ierr,i,hue,lightness,saturation;
 70:   PetscReal  igamma = 1.0 / Gamma;

 73:   red[0]      = 0;
 74:   green[0]    = 0;
 75:   blue[0]     = 0;
 76:   hue         = 0;        /* in 0:359 */
 77:   lightness   = 50;       /* in 0:100 */
 78:   saturation  = 100;      /* in 0:100 */
 79:   for (i = 0; i < mapsize; i++) {
 80:     ierr     = PetscDrawUtilityHlsToRgb(hue,lightness,saturation,red + i,green + i,blue + i);
 81:     red[i]   = (int)floor(255.999 * pow(((double) red[i])/255.0,igamma));
 82:     blue[i]  = (int)floor(255.999 * pow(((double)blue[i])/255.0,igamma));
 83:     green[i] = (int)floor(255.999 * pow(((double)green[i])/255.0,igamma));
 84:     hue     += (359/(mapsize-2));
 85:   }
 86:   return(0);
 87: }