#include #include #include void rgb2hsi(double *hsi, double *rgb); int main(int argc, char** argv) { if (argc != 4) { cout << "Usage: " << argv[0] << " R G B" << endl; exit(1); } double R = atof(argv[1]); double G = atof(argv[2]); double B = atof(argv[3]); if (R > 1.0) { R = R/256.0; } if (G > 1.0) { G = G/256.0; } if (B > 1.0) { B = B/256.0; } double rgb[3]; rgb[0] = R; rgb[1] = G; rgb[2] = B; double hsi[3] = {0}; rgb2hsi(hsi, rgb); cout << "H = " << hsi[0] << endl; cout << "S = " << hsi[1] << endl; cout << "I = " << hsi[2] << endl; return 0; } ////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // rgb2hsi -- convert from RGB color space to HSI color space. // #ifndef PI #define PI M_PI #endif void rgb2hsi(double *hsi, double *rgb) { double& R = rgb[0]; double& G = rgb[1]; double& B = rgb[2]; double& H = hsi[0]; double& S = hsi[1]; double& I = hsi[2]; double min = R; if (G < min) min = G; if (B < min) min = B; I = (R+G+B)/3.0; S = 1 - min/I; if (S == 0.0) { H = 0.0; } else { H = ((R-G)+(R-B))/2.0; H = H/sqrt((R-G)*(R-G) + (R-B)*(G-B)); H = acos(H); if (B > G) { H = 2*PI - H; } H = H/(2*PI); } }