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;

 19: int 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 int 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 int 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: int PetscDrawUtilitySetCmapHue(unsigned char *red,unsigned char *green,unsigned char * blue,int mapsize)
 76: {
 77:   int        ierr,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(((double) red[i])/255.0,igamma));
 90:     blue[i]  = (int)floor(255.999 * pow(((double)blue[i])/255.0,igamma));
 91:     green[i] = (int)floor(255.999 * pow(((double)green[i])/255.0,igamma));
 92:     hue     += (359/(mapsize-2));
 93:   }
 94:   return(0);
 95: }