home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_10 / 2n10082a < prev    next >
Text File  |  1991-07-17  |  2KB  |  95 lines

  1. /*
  2.  *  From netlib archives:  netlib@ornl.gov
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <math.h>
  7.  
  8. /*
  9.  * Hue conversion table
  10.  *
  11.  * Computed from the FMC-1 color difference formula, Barco monitor
  12.  * max(r,g,b)=1, n=61 magenta, 2 Jan 1986
  13.  */
  14. double huettab[61] = {
  15.     0.0000, 0.0062, 0.0130, 0.0202, 0.0280,
  16.     0.0365, 0.0457, 0.0559, 0.0671, 0.0796,
  17.     0.0936, 0.1095, 0.1275, 0.1482, 0.1806,
  18.     0.2113, 0.2393, 0.2652, 0.2892, 0.3119,
  19.     0.3333, 0.3556, 0.3815, 0.4129, 0.4526,
  20.     0.5060, 0.5296, 0.5501, 0.5679, 0.5834,
  21.     0.5970, 0.6088, 0.6191, 0.6281, 0.6361,
  22.     0.6430, 0.6490, 0.6544, 0.6590, 0.6631,
  23.     0.6667, 0.6713, 0.6763, 0.6815, 0.6873,
  24.     0.6937, 0.7009, 0.7092, 0.7190, 0.7308,
  25.     0.7452, 0.7631, 0.7856, 0.8142, 0.8621,
  26.     0.9029, 0.9344, 0.9580, 0.9755, 0.9889,
  27.     1.0000
  28. };
  29.  
  30. /*
  31.  * This routine computes colors suitable for use in color level plots.
  32.  * Typically s=v=1 and h varies from 0 (red) to 1 (blue) in
  33.  * equally spaced steps.  (h=.5 gives green; 1<h<1.5 gives magenta.)
  34.  * To convert for frame buffer, use   R = floor(255.999*pow(*r,1/gamma))  
  35.  * etc.
  36.  */
  37. double
  38. rainbow( double h, double s, double v,
  39.     double *r, double *g, double *b ) {
  40.  
  41.     int i;
  42.     double modf(double, *double);
  43.     double trash;
  44.  
  45.     h = 60*modf( h / 1.5, &trash );
  46.     i = floor( h );
  47.     h = huettab[i] + ( huettab[i+1] - huettab[i] ) * ( h - i );
  48.     hsv2rgb( h, s, v, r, g, b );
  49.   }
  50.  
  51. /*
  52.  *  This routine does the actual conversion.
  53.  *  Here, h=.667 gives blue, h=0 or 1 gives red.
  54.  *  See Alvy Ray Smith, Color Gamut Transform Pairs, SIGGRAPH '78
  55.  */
  56.  
  57. void
  58. hsv2rgb( double h, double s, double v,
  59.     double *r, double *g, double *b ) {
  60.  
  61.     int i;
  62.     double f, m, n, k;
  63.     double modf(double, *double);
  64.     double trash;
  65.  
  66.     h = 6*modf(h,&trash);
  67.     i = floor(h);
  68.     f = h-i;
  69.     m = (1-s);
  70.     n = (1-s*f);
  71.     k = (1-(s*(1-f)));
  72.     switch(i){
  73.         case 0:  *r=1; *g=k; *b=m; break;
  74.         case 1:  *r=n; *g=1; *b=m; break;
  75.         case 2:  *r=m; *g=1; *b=k; break;
  76.         case 3:  *r=m; *g=n; *b=1; break;
  77.         case 4:  *r=k; *g=m; *b=1; break;
  78.         case 5:  *r=1; *g=m; *b=n; break; 
  79.         default:
  80.             fprintf( stderr,"hsv2rgb: bad i: %f %d",
  81.                         h, i );
  82.             exit( 1 );
  83.             break;
  84.       }
  85.     f = *r;
  86.     f = ( f < *g ) ? *g : f;
  87.     f = ( f < *b ) ? *b : f;
  88.     f = v / f;
  89.     *r *= f;
  90.     *g *= f;
  91.     *b *= f;
  92.     }
  93.  
  94.  
  95.