home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / graphics / 8830 < prev    next >
Encoding:
Text File  |  1992-08-14  |  3.5 KB  |  111 lines

  1. Newsgroups: comp.graphics
  2. Path: sparky!uunet!munnari.oz.au!metro!usage!cad13!steve
  3. From: steve@keystone.arch.unsw.edu.au (Stephen Peter)
  4. Subject: Re: HSV -> RGB , RGB -> HSV
  5. Message-ID: <1992Aug13.221142.11907@usage.csd.unsw.OZ.AU>
  6. Sender: news@usage.csd.unsw.OZ.AU
  7. Nntp-Posting-Host: cad13.arch.unsw.edu.au
  8. Reply-To: steve@keystone.arch.unsw.edu.au
  9. Organization: Faculty of Architecture, University of New South Wales
  10. References: <3ka-5q_@engin.umich.edu>
  11. Date: Thu, 13 Aug 1992 22:11:42 GMT
  12. Lines: 97
  13.  
  14. In article @engin.umich.edu, amadi@engin.umich.edu (Amadi Nwankpa) writes:
  15. >
  16. >   I need to find a formula to convert RGB to a color scheme with 
  17. >a brightness parameter ranging from 0 to 1.  Any formulas converting
  18. >HSV to RGB and vice versa might be helpful.  
  19. >
  20. Ask and you shall receive....
  21.  
  22. This code is probably can from either  'Rogers & Adams' or 'Foley, van Damm (etc)'
  23. (check one of this groups dozens :-) of FAQs if you don't recognise the names...)
  24. I say probably because I've used the code in a number of programs (in different
  25. languages & platforms) over the years and it's origins are lost :-)
  26.  
  27. /**************************************************************************
  28.  * convert a set of HSV colour values into RGB values
  29.  *
  30.  * Hue is in the range 0-360
  31.  * Saturation and Value are in the range 0.0-1.0.
  32.  *
  33.  * RETURNS: red,green,blue  in the range 0.0-1.0
  34.  **************************************************************************/
  35. void
  36. hsv2rgb (hue, saturation, value,   r, g, b)
  37. float    hue, saturation, value;
  38. float    *r, *g, *b;
  39. {
  40.   int    HueQuadrant;
  41.   float    HueLocal, Diff, m, n, k;
  42.  
  43.     HueLocal = hue;
  44.     while (HueLocal >= 360.0)  HueLocal = HueLocal - 360.0;
  45.     HueLocal = HueLocal / 60.0;
  46.     HueQuadrant = (int) HueLocal;
  47.     Diff = HueLocal - HueQuadrant;
  48.     m = value * (1.0 - saturation);
  49.     n = value * (1.0 - saturation * Diff);
  50.     k = value * (1.0 - saturation * (1.0 - Diff));
  51.  
  52.     switch (HueQuadrant) {
  53.         case 0: *r = value;    *g = k;        *b = m;        break;
  54.         case 1: *r = n;        *g = value;    *b = m;        break;
  55.         case 2: *r = m;        *g = value;    *b = k;        break;
  56.         case 3: *r = m;        *g = n;        *b = value;    break;
  57.         case 4: *r = k;        *g = m;        *b = value;    break;
  58.         case 5: *r = value;    *g = m;        *b = n;
  59.         }
  60. }
  61.  
  62. /**************************************************************************
  63.    convert rgb to hsv
  64.  
  65.  **************************************************************************/
  66. void
  67. rgb2hsv (r, g, b,  hue, saturation, value)
  68. float    *hue, *saturation, *value;
  69. float    r, g, b;
  70. {
  71.     float x, Rtemp, Gtemp, Btemp;
  72.  
  73.     *value = max (r, max (g, b));
  74.  
  75.     x =     min (r, min (g, b));
  76.  
  77.     *saturation = (*value - x) / *value;
  78.  
  79.     Rtemp = (*value - r) * 60 / (*value - x);
  80.     Gtemp = (*value - g) * 60 / (*value - x);
  81.     Btemp = (*value - b) * 60 / (*value - x);
  82.  
  83.     if (r == *value)  {
  84.         if (g == x)
  85.         *hue = 300 + Btemp;
  86.         else
  87.         *hue = 60 - Gtemp;  }
  88.     else if (g == *value) {
  89.         if (b == x)
  90.         *hue = 60 + Rtemp;
  91.         else
  92.         *hue = 180 - Btemp;  }
  93.     else {
  94.         if (r == x)
  95.         *hue = 180 + Gtemp;
  96.         else
  97.         *hue = 300 - Rtemp;   }
  98. }
  99.  
  100. /**************************************************************************/
  101.  
  102. hope this helps!
  103. Stephen.
  104. ---
  105.  _--_|\                                                  S.Peter@unsw.EDU.AU
  106. /      \    Stephen Peter                or  steve@keystone.arch.unsw.EDU.AU
  107. \_.--._/<-------------------------------------------------------------------
  108.       v     School of Architecture, University of New South Wales, Australia
  109.             Phone +61 2 6974816   Fax +61 2 6621378   Messages +61 2 6974799
  110.  
  111.