BsColor.h 6.2 KB

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