/************************************************************************** * 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 * **************************************************************************/ 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; }