color.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Color
  4. //
  5. //////////////////////////////////////////////////////////////////////////////
  6. #ifndef _color_h_
  7. #define _color_h_
  8. class Color;
  9. class Color {
  10. private:
  11. float m_r, m_g, m_b, m_a;
  12. static const Color s_colorWhite;
  13. static const Color s_colorRed;
  14. static const Color s_colorGreen;
  15. static const Color s_colorBlue;
  16. static const Color s_colorYellow;
  17. static const Color s_colorMagenta;
  18. static const Color s_colorCyan;
  19. static const Color s_colorBlack;
  20. static const Color s_colorGray;
  21. public:
  22. Color() {}
  23. Color(float r, float g, float b, float a = 1) :
  24. m_r(r),
  25. m_g(g),
  26. m_b(b),
  27. m_a(a)
  28. {
  29. }
  30. Color(float gray, float a = 1) :
  31. m_r(gray),
  32. m_g(gray),
  33. m_b(gray),
  34. m_a(a)
  35. {
  36. }
  37. static const Color& White() { return s_colorWhite; }
  38. static const Color& Red() { return s_colorRed; }
  39. static const Color& Green() { return s_colorGreen; }
  40. static const Color& Blue() { return s_colorBlue; }
  41. static const Color& Yellow() { return s_colorYellow; }
  42. static const Color& Magenta() { return s_colorMagenta; }
  43. static const Color& Cyan() { return s_colorCyan; }
  44. static const Color& Black() { return s_colorBlack; }
  45. static const Color& Gray() { return s_colorGray; }
  46. void SetHSBA(float hue, float saturation, float brightness, float alpha = 1);
  47. void SetRGBA(float r, float g, float b, float a = 1)
  48. {
  49. m_r = r;
  50. m_g = g;
  51. m_b = b;
  52. m_a = a;
  53. }
  54. float GetRed() const { return m_r; }
  55. float GetGreen() const { return m_g; }
  56. float GetBlue() const { return m_b; }
  57. float GetAlpha() const { return m_a; }
  58. void GetHSB(float& h, float& s, float& b);
  59. float R() const { return m_r; }
  60. float G() const { return m_g; }
  61. float B() const { return m_b; }
  62. float A() const { return m_a; }
  63. void SetRed(float value) { m_r = value; }
  64. void SetGreen(float value) { m_g = value; }
  65. void SetBlue(float value) { m_b = value; }
  66. void SetAlpha(float value) { m_a = value; }
  67. COLORREF MakeCOLORREF() const
  68. {
  69. return RGB(min(m_r * 255, 255), min(m_g * 255, 255), min(m_b * 255, 255));
  70. }
  71. Color AdjustAlpha(float scale)
  72. {
  73. return
  74. Color(
  75. scale * m_r,
  76. scale * m_g,
  77. scale * m_b,
  78. scale * m_a
  79. );
  80. }
  81. friend Color operator*(const Color& color, float scale)
  82. {
  83. return
  84. Color(
  85. scale * color.m_r,
  86. scale * color.m_g,
  87. scale * color.m_b,
  88. color.m_a
  89. );
  90. }
  91. friend Color operator*(float scale, const Color& color)
  92. {
  93. return color * scale;
  94. }
  95. friend Color operator+(const Color& color1, const Color& color2)
  96. {
  97. return
  98. Color(
  99. color1.GetRed() + color2.GetRed(),
  100. color1.GetGreen() + color2.GetGreen(),
  101. color1.GetBlue() + color2.GetBlue()
  102. );
  103. }
  104. friend bool operator==(const Color& color1, const Color& color2)
  105. {
  106. return
  107. (color1.GetRed() == color2.GetRed()) &&
  108. (color1.GetGreen() == color2.GetGreen()) &&
  109. (color1.GetBlue() == color2.GetBlue());
  110. }
  111. friend bool operator!=(const Color& color1, const Color& color2)
  112. {
  113. return
  114. (color1.GetRed() != color2.GetRed()) ||
  115. (color1.GetGreen() != color2.GetGreen()) ||
  116. (color1.GetBlue() != color2.GetBlue());
  117. }
  118. };
  119. class HSBColor : public Color {
  120. public:
  121. HSBColor(float hue, float saturation, float brightness, float alpha = 1)
  122. {
  123. SetHSBA(hue, saturation, brightness, alpha);
  124. }
  125. };
  126. inline Color MakeColorFromCOLORREF(COLORREF color)
  127. {
  128. return
  129. Color(
  130. (float)((color >> 16) & 0xff) / 255.0f,
  131. (float)((color >> 8) & 0xff) / 255.0f,
  132. (float)((color >> 0) & 0xff) / 255.0f,
  133. (float)((color >> 24) & 0xff) / 255.0f
  134. );
  135. }
  136. #endif