fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. #define MIN3(A, B, C) std::min(A, std::min(B, C))
  5.  
  6. typedef double value_type;
  7.  
  8. using std::cout;
  9. using std::endl;
  10. using std::sqrt;
  11.  
  12. struct RGB
  13. {
  14. value_type R, G, B;
  15. };
  16.  
  17. struct HSI
  18. {
  19. value_type H, S, I;
  20. };
  21.  
  22. void convertRGBtoHSI(RGB input, HSI &output)
  23. {
  24. RGB &color = input;
  25. value_type &hue = output.H;
  26. value_type &saturation = output.S;
  27. value_type &intensity = output.I;
  28.  
  29. intensity = (color.R + color.G + color.B) / 3;
  30.  
  31. if((color.R + color.G + color.B) == 765)
  32. {
  33. saturation = 0;
  34. hue = 0;
  35. }
  36.  
  37. MIN3(color.R, color.G, color.B);
  38. if(intensity > 0)
  39. {
  40. value_type did = MIN3(color.R, color.G, color.B)/ intensity;
  41. saturation = 1 - did;
  42. }
  43. else if (intensity == 0)
  44. {
  45. saturation = 0;
  46. }
  47. value_type color123 = (color.R * color.R) + (color.G * color.G) +
  48. (color.B * color.B) - (color.R * color.G) -
  49. (color.R * color.B) - (color.G * color.B);
  50. double sqrt2 = (double)sqrt((double)color123);
  51. value_type temp = (color.R - (color.G/2) - (color.B/2)) / ( sqrt2 );
  52. if (color.G >= color.B)
  53. {
  54. hue = acos(temp);
  55. }
  56. else if (color.B > color.G)
  57. {
  58. hue = 360 - acos(temp);
  59. }
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65. RGB a = {255, 255, 255};
  66. HSI b;
  67. convertRGBtoHSI(a, b);
  68.  
  69. cout << "Input : " << a.R << ", " << a.G << ", " << a.B << endl;
  70. cout << "Output : " << b.H << ", " << b.S << ", " << b.I << endl;
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Input  : 255, 255, 255
Output : nan, 0, 255