BsColor.h 6.5 KB

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