/**************************************************************************
* Func: rgb2hsi *
* *
* Desc: converts a value from RGB to HSI color space *
* *
* Params: r - normalized red value *
* g - normalized green value *
* b - normalized blue value *
* h - hue in degrees *
* s - normalized saturation *
* i - normalized intensity *
**************************************************************************/// http://wangzaiqiqi.taobao.com
void rgb2hsi(float r,float g,float b,float *h,float *s,float *i)
{
float min, max; /* minimum and maximum RGB values */
float angle; /* temp variable used to compute Hue */
if((r<=g) && (r<=b))
min = r;
else if((g<=r) && (g<=b))
min = g;
else
min =b;
/* compute intensity */
*i=(r + g + b) / 3.0;
/* compute hue and saturation */
if((r==g) && (g==b)) /* gray-scale */
{
*s = 0.0;
*h = 0.0;
return;
}
else
{
*s= 1.0 - (3.0 / (r + g + b)) * min;
angle = (r - 0.5 * g - 0.5 * b) /
sqrt((r - g) * (r - g)+(r - b) * (g - b));
*h = acos(angle);
*h *= 57.29577951; /* convert to degrees */
}
if(b>g)
*h = 360.0 - *h;
}
/***************************************************************************
* Func: hsi2rgb *
* *
* Desc: converts a value from HSI to RGB color space *
* *
* Params: r - normalized red *
* g - normalized green *
* b - normalized blue *
* h - hue in degrees *
* s - normalized saturation *
* i - normalized intensity *
***************************************************************************/
// http://wangzaiqiqi.taobao.com
void hsi2rgb(float *r,float *g,float *b,float h,float s,float i)
{
float angle1, angle2, scale, temp, denom; /* temp variables */
if(i==0.0) /* BLACK */
{
*r = 0.0;
*g = 0.0;
*b = 0.0;
return;
}
if(s==0.0) /* gray-scale H is undefined*/
{
*r = i;
*g = i;
*b = i;
return;
}
if(h<0.0)
h+=360.0;
scale = 3.0 * i;
if(h<=120.0)
{
angle1=h*0.017453293; /* convert to radians - mul by pi/180 */
angle2=(60.0-h)*0.017453293;
*b = (1.0-s)/3.0;
*r = (1.0 + (s*cos(angle1)/cos(angle2)))/3.0;
*g = 1.0-*r-*b;
*b *= scale;
*r *= scale;
*g *= scale;
}
else if((h>120.0) && (h<=240.0))
{
h -= 120.0;
angle1=h*0.017453293; /* convert to radians - mul by pi/180 */
angle2=(60.0-h)*0.017453293;
*r = (1.0-s)/3.0;
*g = (1.0 + (s*cos(angle1)/cos(angle2)))/3.0;
*b = 1.0 - *r - *g;
*r *= scale;
*g *= scale;
*b *= scale;
}
else
{
h -= 240.0;
angle1=h*0.017453293; /* convert to radians - mul by pi/180 */
angle2=(60.0-h)*0.017453293;
*g = (1.0-s)/3.0;
*b = (1.0 + (s*cos(angle1)/cos(angle2)))/3.0;
*r = 1.0 - *g - *b;
*r *= scale;
*g *= scale;
*b *= scale;
}
}