olor.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // olor.cpp: implementation of the Color class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "olor.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. Color::Color(int red,int green,int blue)
  10. :m_red(red)
  11. ,m_green(green)
  12. ,m_blue(blue)
  13. {
  14. RGBtoHSB(red,green,blue,&m_H,&m_S,&m_B);
  15. }
  16. Color::~Color()
  17. {
  18. }
  19. void Color::RGBtoHSB(int red, int green, int blue, float *H, float *S, float *B)
  20. {
  21. int largestColor; /* holds the largest color (rgb) at the start */
  22. int lowestColor; /* opposite of above */
  23. float hue; /* it puts the `h' in "hsb" */
  24. float saturation; /* `s' */
  25. float brightness; /* and the `b' */
  26. /* Make sure that the array that is returned actually has space */
  27.     
  28. /* assign largestColor to the greater of the red or green */
  29. if (red <= green)
  30. largestColor = green;
  31. else
  32. largestColor = red;
  33.     
  34. /* now see if blue is bigger */
  35. if (blue > largestColor)
  36. largestColor = blue;
  37. /* set lowestColor = to the smallest value */
  38. if (green < red)
  39. lowestColor = green;
  40. else
  41. lowestColor = red;
  42. if (blue < lowestColor)
  43. lowestColor = blue;
  44. /* brightness is calculated like so. */  
  45. brightness = (float) largestColor / 255;
  46. /* if the largestColor isn't zero (so we don't divide by zero) set the variable *
  47. * to the difference of the two as a percentage of the largest                  */
  48. if (largestColor != 0)
  49. saturation = (float) (largestColor - lowestColor) / (float) largestColor;
  50. else
  51. saturation = 0.0F;
  52. if (saturation == 0.0F) {
  53. hue = 0.0F;
  54. } else {
  55. /* some temporary variables to figure out the hue */
  56. float redRatio = (float) (largestColor - red) / (float) (largestColor - lowestColor);
  57. float greenRatio = (float) (largestColor - green) / (float) (largestColor - lowestColor);
  58. float blueRatio = (float) (largestColor - blue) / (float) (largestColor - lowestColor);
  59. /* depending on which of the 3 was the highest, we calculate our hue. */
  60. if (red == largestColor)
  61. hue = blueRatio - greenRatio;
  62. else if (green == largestColor)
  63. hue = (2.0F + redRatio) - blueRatio;
  64. else /* blue is largest */
  65. hue = (4.0F + greenRatio) - redRatio;
  66. /* divide it by 6 */
  67. hue /= 6.0F;
  68. /* I don't know if this prevents us from being negative (as in the worst outcome from the above *
  69. * operations is -1, or if this does something different).                                      */
  70. if (hue < 0.0F)
  71. hue++;
  72. }
  73. * H = hue;
  74. * S = saturation;
  75. * B = brightness;
  76. }
  77. float Color::GetH()
  78. {
  79. return m_H;
  80. }
  81. float Color::GetS()
  82. {
  83. return m_S;
  84. }
  85. float Color::GetB()
  86. {
  87. return m_B;
  88. }
  89. int Color::GetRed()
  90. {
  91. return m_red;
  92. }
  93. int Color::GetGreen()
  94. {
  95. return m_green;
  96. }
  97. int Color::GetBlue()
  98. {
  99. return m_blue;
  100. }