home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / graphics / 13678 < prev    next >
Encoding:
Text File  |  1993-01-12  |  2.9 KB  |  146 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!spool.mu.edu!agate!netsys!news.cerf.net!network.ucsd.edu!ivem.ucsd.edu!spl
  2. From: spl@ivem.ucsd.edu (Steve Lamont)
  3. Newsgroups: comp.graphics
  4. Subject: Re: RGB<->HSV conversion
  5. Date: 12 Jan 1993 15:17:51 GMT
  6. Organization: University of Calif., San Diego/Microscopy and Imaging Resource
  7. Lines: 134
  8. Message-ID: <1iunevINNi3f@network.ucsd.edu>
  9. References: <C0ox1H.2K9@lysator.liu.se> <C0qx5u.G8M@lysator.liu.se>
  10. NNTP-Posting-Host: ivem.ucsd.edu
  11.  
  12. In article <C0qx5u.G8M@lysator.liu.se> marvil@lysator.liu.se (Martin Vilcans) writes:
  13. >I'd like to know how to convert from/to RGB (Red Green Blue) colour
  14. >coordinates to/from HSV (Hue Saturation Value) coordinates, as I'm
  15. >working on a picture processing program. Any pointers appreciated,
  16. >C code will do, but I prefer the algorithm. Thanks.
  17.  
  18. See Foley, van Dam, Feiner, and Hughes, _Computer Graphics: Principles
  19. and Practice, Second Edition_, pp 590-593.
  20.  
  21. Here's my code to convert from HSV to RGB.  I've never needed to go
  22. the other way, so I've never implemented the inverse operation.
  23.  
  24.             - - - chop here - - -
  25. #include <stdio.h>
  26.  
  27. /*
  28.  * Lifted straight from Foley, van Dam, Feiner, and Hughes, _Computer
  29.  * Graphics: Principles and Practice, Second Edition_.
  30.  * 
  31.  * h varies between 0 and 360, s and v vary between 0 and 1.
  32.  * returns r, g, and b between 0 and 1.
  33.  */
  34.  
  35. void hsv2rgb( double h, double s, double v, 
  36.               double *r, double *g, double *b )
  37.     
  38. {
  39.     
  40.     if ( s == 0.0 ) {
  41.  
  42.     if ( h < 0.0 ) {
  43.         
  44.         *r = v;
  45.         *g = v;
  46.         *b = v;
  47.         
  48.     } else {
  49.  
  50.         fprintf( stderr, "hsv2rgb error -- invalid combination.\n" );
  51.         exit( 1 );
  52.  
  53.     }
  54.  
  55.     } else {
  56.     
  57.     int i;
  58.     double f;
  59.     double p;
  60.     double q;
  61.     double t;
  62.     
  63.     h = fmod( h, 360.0 );
  64.     if ( h < 0.0 )
  65.         h += 360.0;
  66.     
  67.     h /= 60.0;
  68.     
  69.     i = floor( h );
  70.     f = h - i;
  71.     p = v * ( 1.0 - s );
  72.     q = v * ( 1.0 - ( s * f ) );
  73.     t = v * ( 1.0 - ( s * ( 1.0 - f ) ) );
  74.     
  75.     switch ( i ) {
  76.         
  77.       case 0: {
  78.           
  79.           *r = v;
  80.           *g = t;
  81.           *b = p;
  82.           break;
  83.           
  84.       }
  85.       case 1: {
  86.           
  87.           *r = q;
  88.           *g = v;
  89.           *b = p;
  90.           break;
  91.           
  92.       }
  93.       case 2: {
  94.           
  95.           *r = p;
  96.           *g = v;
  97.           *b = t;
  98.           break;
  99.           
  100.       }
  101.       case 3: {
  102.           
  103.           *r = p;
  104.           *g = q;
  105.           *b = v;
  106.           break;
  107.           
  108.       }
  109.       case 4: {
  110.           
  111.           *r = t;
  112.           *g = p;
  113.           *b = v;
  114.           break;
  115.           
  116.       }
  117.       case 5: {
  118.           
  119.           *r = v;
  120.           *g = p;
  121.           *b = q;
  122.           break;
  123.           
  124.       }
  125.         
  126.       default: {
  127.           
  128.           fprintf( stderr, "hsv2rgb error: Invalid case!  case = %d\n",
  129.                i );
  130.           abort();
  131.           
  132.       }
  133.         
  134.     }
  135.     
  136.     }
  137.     
  138.     return;
  139.     
  140. }
  141. -- 
  142. Steve Lamont, SciViGuy -- (619) 534-7968 -- spl@szechuan.ucsd.edu
  143. UCSD Microscopy and Imaging Resource/UCSD Med School/La Jolla, CA 92093-0608
  144. "Personally, I don't care whether someone is cool enough to quote Doug
  145. Gwyn--I only care whether Doug Gwyn is cool enough to quote." - Larry Wall
  146.