home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / graphics / 8234 < prev    next >
Encoding:
Text File  |  1992-07-28  |  2.3 KB  |  139 lines

  1. Path: sparky!uunet!ogicse!network.ucsd.edu!alex!spl
  2. From: spl@alex.uucp (Steve Lamont)
  3. Newsgroups: comp.graphics
  4. Subject: Re: Color conversion algorithm
  5. Message-ID: <154289INNs76@network.ucsd.edu>
  6. Date: 28 Jul 92 18:04:57 GMT
  7. Article-I.D.: network.154289INNs76
  8. References: <l7apjnINN2a6@news.bbn.com>
  9. Organization: University of Calif., San Diego/Microscopy and Imaging Resource
  10. Lines: 126
  11. NNTP-Posting-Host: alex.ucsd.edu
  12.  
  13. In article <l7apjnINN2a6@news.bbn.com> slackey@BBN.COM (Stan Lackey) writes:
  14. >If anyone has access to an algorithm for converting hue/brighntess/saturation
  15. >to r/g/b, would you be so kind as to either post it or email me?  Thanks much!
  16.  
  17. #include <stdio.h>
  18.  
  19. /*
  20.  * Straight out of Foley, van Dam, Feiner, and Hughes.
  21.  */
  22.  
  23. hsv2rgb( h, s, v, r, g, b )
  24.     
  25.     double h;
  26.     double s;
  27.     double v;
  28.     double *r;
  29.     double *g;
  30.     double *b;
  31.     
  32. {
  33.     
  34.     if ( s == 0.0 ) {
  35.  
  36.     if ( h < 0.0 ) {
  37.         
  38.         *r = v;
  39.         *g = v;
  40.         *b = v;
  41.         
  42.     } else
  43.         fprintf( stderr, "hsv2rgb error -- invalid combination.\n" );
  44.  
  45.     } else {
  46.     
  47.     int i;
  48.     double f;
  49.     double p;
  50.     double q;
  51.     double t;
  52.     
  53.     h = fmod( h, 360.0 );
  54.     if ( h < 0.0 )
  55.         h += 360.0;
  56.     
  57.     h /= 60.0;
  58.     
  59.     i = floor( h );
  60.     f = h - i;
  61.     p = v * ( 1.0 - s );
  62.     q = v * ( 1.0 - ( s * f ) );
  63.     t = v * ( 1.0 - ( s * ( 1.0 - f ) ) );
  64.     
  65.     switch ( i ) {
  66.         
  67.       case 0: {
  68.           
  69.           *r = v;
  70.           *g = t;
  71.           *b = p;
  72.           break;
  73.           
  74.       }
  75.       case 1: {
  76.           
  77.           *r = q;
  78.           *g = v;
  79.           *b = p;
  80.           break;
  81.           
  82.       }
  83.       case 2: {
  84.           
  85.           *r = p;
  86.           *g = v;
  87.           *b = t;
  88.           break;
  89.           
  90.       }
  91.       case 3: {
  92.           
  93.           *r = p;
  94.           *g = q;
  95.           *b = v;
  96.           break;
  97.           
  98.       }
  99.       case 4: {
  100.           
  101.           *r = t;
  102.           *g = p;
  103.           *b = v;
  104.           break;
  105.           
  106.       }
  107.       case 5: {
  108.           
  109.           *r = v;
  110.           *g = p;
  111.           *b = q;
  112.           break;
  113.           
  114.       }
  115.         
  116.       default: {
  117.           
  118.           fprintf( stderr, "hsv2rgb error: Invalid case!  case = %d\n",
  119.                i );
  120.           abort();
  121.           
  122.       }
  123.         
  124.     }
  125.     
  126.     }
  127.     
  128.     return;
  129.     
  130. }
  131.  
  132.  
  133.  
  134. -- 
  135. Steve Lamont, SciViGuy -- (619) 534-7968 -- spl@dim.ucsd.edu
  136. UCSD Microscopy and Imaging Resource/UCSD Med School/La Jolla, CA 92093-0608
  137. "Out of the ash/I rise with my red hair/And I eat men like air."
  138.                         - Sylvia Plath, "Lady Lazarus"
  139.