From: steve@keystone.arch.unsw.edu.au (Stephen Peter)
Subject: Re: HSV -> RGB , RGB -> HSV
Reply-To: steve@keystone.arch.unsw.edu.au
Organization: Faculty of Architecture, University of New South Wales
Date: Thu, 13 Aug 1992 22:11:42 GMT

In article @engin.umich.edu, amadi@engin.umich.edu (Amadi Nwankpa) writes:
>
>   I need to find a formula to convert RGB to a color scheme with 
>a brightness parameter ranging from 0 to 1.  Any formulas converting
>HSV to RGB and vice versa might be helpful.  
>
Ask and you shall receive....

This code is probably can from either  'Rogers & Adams' or 'Foley, van Damm (etc)'
(check one of this groups dozens :-) of FAQs if you don't recognise the names...)
I say probably because I've used the code in a number of programs (in different
languages & platforms) over the years and it's origins are lost :-)

/**************************************************************************
 * convert a set of HSV colour values into RGB values
 *
 * Hue is in the range 0-360
 * Saturation and Value are in the range 0.0-1.0.
 *
 * RETURNS: red,green,blue  in the range 0.0-1.0
 **************************************************************************/
void
hsv2rgb (hue, saturation, value,   r, g, b)
float	hue, saturation, value;
float	*r, *g, *b;
{
  int	HueQuadrant;
  float	HueLocal, Diff, m, n, k;

	HueLocal = hue;
	while (HueLocal >= 360.0)  HueLocal = HueLocal - 360.0;
	HueLocal = HueLocal / 60.0;
	HueQuadrant = (int) HueLocal;
	Diff = HueLocal - HueQuadrant;
	m = value * (1.0 - saturation);
	n = value * (1.0 - saturation * Diff);
	k = value * (1.0 - saturation * (1.0 - Diff));

	switch (HueQuadrant) {
		case 0: *r = value;	*g = k;		*b = m;        break;
		case 1: *r = n;		*g = value;	*b = m;        break;
		case 2: *r = m;		*g = value;	*b = k;        break;
		case 3: *r = m;		*g = n;		*b = value;    break;
		case 4: *r = k;		*g = m;		*b = value;    break;
		case 5: *r = value;	*g = m;		*b = n;
		}
}

/**************************************************************************
   convert rgb to hsv

 **************************************************************************/
void
rgb2hsv (r, g, b,  hue, saturation, value)
float	*hue, *saturation, *value;
float	r, g, b;
{
	float x, Rtemp, Gtemp, Btemp;

	*value = max (r, max (g, b));

	x =     min (r, min (g, b));

	*saturation = (*value - x) / *value;

	Rtemp = (*value - r) * 60 / (*value - x);
	Gtemp = (*value - g) * 60 / (*value - x);
	Btemp = (*value - b) * 60 / (*value - x);

	if (r == *value)  {
	    if (g == x)
		*hue = 300 + Btemp;
	    else
		*hue = 60 - Gtemp;  }
	else if (g == *value) {
	    if (b == x)
		*hue = 60 + Rtemp;
	    else
		*hue = 180 - Btemp;  }
	else {
	    if (r == x)
		*hue = 180 + Gtemp;
	    else
		*hue = 300 - Rtemp;   }
}

/**************************************************************************/

hope this helps!
Stephen.
---
 _--_|\                                                  S.Peter@unsw.EDU.AU
/      \    Stephen Peter                or  steve@keystone.arch.unsw.EDU.AU
\_.--._/<-------------------------------------------------------------------
      v     School of Architecture, University of New South Wales, Australia
            Phone +61 2 6974816   Fax +61 2 6621378   Messages +61 2 6974799