BsColor.h 6.0 KB

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