BsColor.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsPrerequisitesUtil.h"
  6. namespace BansheeEngine
  7. {
  8. typedef UINT32 RGBA;
  9. typedef UINT32 ARGB;
  10. typedef UINT32 ABGR;
  11. typedef UINT32 BGRA;
  12. /**
  13. * @brief Color represented as 4 components, each being a floating point value ranging from
  14. * 0 to 1. Color components are Red, Green, Blue and Alpha.
  15. */
  16. class BS_UTILITY_EXPORT Color
  17. {
  18. public:
  19. static const Color ZERO;
  20. static const Color Black;
  21. static const Color White;
  22. static const Color Red;
  23. static const Color Green;
  24. static const Color Blue;
  25. explicit Color(float red = 1.0f, float green = 1.0f,
  26. float blue = 1.0f, float alpha = 1.0f )
  27. :r(red), g(green), b(blue), a(alpha)
  28. { }
  29. bool operator==(const Color& rhs) const;
  30. bool operator!=(const Color& rhs) const;
  31. RGBA getAsRGBA(void) const;
  32. ARGB getAsARGB(void) const;
  33. BGRA getAsBGRA(void) const;
  34. ABGR getAsABGR(void) const;
  35. void setAsRGBA(const RGBA val);
  36. void setAsARGB(const ARGB val);
  37. void setAsBGRA(const BGRA val);
  38. void setAsABGR(const ABGR val);
  39. /**
  40. * @brief Clamps colour value to the range [0, 1].
  41. */
  42. void saturate()
  43. {
  44. if (r < 0)
  45. r = 0;
  46. else if (r > 1)
  47. r = 1;
  48. if (g < 0)
  49. g = 0;
  50. else if (g > 1)
  51. g = 1;
  52. if (b < 0)
  53. b = 0;
  54. else if (b > 1)
  55. b = 1;
  56. if (a < 0)
  57. a = 0;
  58. else if (a > 1)
  59. a = 1;
  60. }
  61. /**
  62. * @brief Clamps colour value to the range [0, 1]. Returned saturated
  63. * color as a copy.
  64. */
  65. Color saturateCopy() const
  66. {
  67. Color ret = *this;
  68. ret.saturate();
  69. return ret;
  70. }
  71. inline float operator[] (const UINT32 i) const
  72. {
  73. assert(i < 4);
  74. return *(&r+i);
  75. }
  76. inline float& operator[] (const UINT32 i)
  77. {
  78. assert(i < 4);
  79. return *(&r+i);
  80. }
  81. /**
  82. * @brief Pointer accessor for direct copying.
  83. */
  84. inline float* ptr()
  85. {
  86. return &r;
  87. }
  88. /**
  89. * @brief Pointer accessor for direct copying.
  90. */
  91. inline const float* ptr() const
  92. {
  93. return &r;
  94. }
  95. inline Color operator+ (const Color& rhs) const
  96. {
  97. Color kSum;
  98. kSum.r = r + rhs.r;
  99. kSum.g = g + rhs.g;
  100. kSum.b = b + rhs.b;
  101. kSum.a = a + rhs.a;
  102. return kSum;
  103. }
  104. inline Color operator- ( const Color& rhs) const
  105. {
  106. Color kDiff;
  107. kDiff.r = r - rhs.r;
  108. kDiff.g = g - rhs.g;
  109. kDiff.b = b - rhs.b;
  110. kDiff.a = a - rhs.a;
  111. return kDiff;
  112. }
  113. inline Color operator* (const float rhs) const
  114. {
  115. Color kProd;
  116. kProd.r = rhs*r;
  117. kProd.g = rhs*g;
  118. kProd.b = rhs*b;
  119. kProd.a = rhs*a;
  120. return kProd;
  121. }
  122. inline Color operator* (const Color& rhs) const
  123. {
  124. Color kProd;
  125. kProd.r = rhs.r * r;
  126. kProd.g = rhs.g * g;
  127. kProd.b = rhs.b * b;
  128. kProd.a = rhs.a * a;
  129. return kProd;
  130. }
  131. inline Color operator/ (const Color& rhs) const
  132. {
  133. Color kProd;
  134. kProd.r = rhs.r / r;
  135. kProd.g = rhs.g / g;
  136. kProd.b = rhs.b / b;
  137. kProd.a = rhs.a / a;
  138. return kProd;
  139. }
  140. inline Color operator/ (const float rhs) const
  141. {
  142. assert(rhs != 0.0f);
  143. Color kDiv;
  144. float fInv = 1.0f / rhs;
  145. kDiv.r = r * fInv;
  146. kDiv.g = g * fInv;
  147. kDiv.b = b * fInv;
  148. kDiv.a = a * fInv;
  149. return kDiv;
  150. }
  151. inline friend Color operator* (const float lhs, const Color& rhs)
  152. {
  153. Color result;
  154. result.r = lhs * rhs.r;
  155. result.g = lhs * rhs.g;
  156. result.b = lhs * rhs.b;
  157. result.a = lhs * rhs.a;
  158. return result;
  159. }
  160. inline Color& operator+= (const Color& rhs)
  161. {
  162. r += rhs.r;
  163. g += rhs.g;
  164. b += rhs.b;
  165. a += rhs.a;
  166. return *this;
  167. }
  168. inline Color& operator-= (const Color& rhs)
  169. {
  170. r -= rhs.r;
  171. g -= rhs.g;
  172. b -= rhs.b;
  173. a -= rhs.a;
  174. return *this;
  175. }
  176. inline Color& operator*= (const float rhs)
  177. {
  178. r *= rhs;
  179. g *= rhs;
  180. b *= rhs;
  181. a *= rhs;
  182. return *this;
  183. }
  184. inline Color& operator/= (const float rhs)
  185. {
  186. assert(rhs != 0.0f);
  187. float fInv = 1.0f / rhs;
  188. r *= rhs;
  189. g *= rhs;
  190. b *= rhs;
  191. a *= rhs;
  192. return *this;
  193. }
  194. /**
  195. * @brief Set a colour value from Hue, Saturation and Brightness.
  196. *
  197. * @param hue Hue value, scaled to the [0,1] range.
  198. * @param saturation Saturation level, [0,1].
  199. * @param brightness Brightness level, [0,1].
  200. */
  201. void setHSB(float hue, float saturation, float brightness);
  202. /**
  203. * @brief Convert the current color to Hue, Saturation and Brightness values.
  204. *
  205. * @param hue Output hue value, scaled to the [0,1] range.
  206. * @param saturation Output saturation level, [0,1].
  207. * @param brightness Output brightness level, [0,1].
  208. */
  209. void getHSB(float* hue, float* saturation, float* brightness) const;
  210. float r, g, b, a;
  211. };
  212. BS_ALLOW_MEMCPY_SERIALIZATION(Color);
  213. }