BsColor.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. namespace bs
  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, float blue = 1.0f, float alpha = 1.0f )
  28. :r(red), g(green), b(blue), a(alpha)
  29. { }
  30. bool operator==(const Color& rhs) const;
  31. bool operator!=(const Color& rhs) const;
  32. /** Returns the color as a 32-bit value in RGBA order. */
  33. RGBA getAsRGBA() const;
  34. /** Returns the color as a 32-bit value in ARGB order. */
  35. ARGB getAsARGB() const;
  36. /** Returns the color as a 32-bit value in BGRA order. */
  37. BGRA getAsBGRA() const;
  38. /** Returns the color as a 32-bit value in ABGR order. */
  39. ABGR getAsABGR() const;
  40. /** Assigns the color from a 32-bit value that encodes a RGBA color. */
  41. void setAsRGBA(RGBA val);
  42. /** Assigns the color from a 32-bit value that encodes a ARGB color. */
  43. void setAsARGB(ARGB val);
  44. /** Assigns the color from a 32-bit value that encodes a BGRA color. */
  45. void setAsBGRA(BGRA val);
  46. /** Assigns the color from a 32-bit value that encodes a ABGR color. */
  47. void setAsABGR(ABGR val);
  48. /** Clamps color value to the range [0, 1]. */
  49. void saturate()
  50. {
  51. if (r < 0)
  52. r = 0;
  53. else if (r > 1)
  54. r = 1;
  55. if (g < 0)
  56. g = 0;
  57. else if (g > 1)
  58. g = 1;
  59. if (b < 0)
  60. b = 0;
  61. else if (b > 1)
  62. b = 1;
  63. if (a < 0)
  64. a = 0;
  65. else if (a > 1)
  66. a = 1;
  67. }
  68. /** Clamps colour value to the range [0, 1]. Returned saturated color as a copy. */
  69. Color saturateCopy() const
  70. {
  71. Color ret = *this;
  72. ret.saturate();
  73. return ret;
  74. }
  75. float operator[] (const UINT32 i) const
  76. {
  77. assert(i < 4);
  78. return *(&r+i);
  79. }
  80. float& operator[] (const UINT32 i)
  81. {
  82. assert(i < 4);
  83. return *(&r+i);
  84. }
  85. /** Pointer accessor for direct copying. */
  86. float* ptr()
  87. {
  88. return &r;
  89. }
  90. /** Pointer accessor for direct copying. */
  91. const float* ptr() const
  92. {
  93. return &r;
  94. }
  95. Color operator+ (const Color& rhs) const
  96. {
  97. Color sum;
  98. sum.r = r + rhs.r;
  99. sum.g = g + rhs.g;
  100. sum.b = b + rhs.b;
  101. sum.a = a + rhs.a;
  102. return sum;
  103. }
  104. Color operator- (const Color& rhs) const
  105. {
  106. Color diff;
  107. diff.r = r - rhs.r;
  108. diff.g = g - rhs.g;
  109. diff.b = b - rhs.b;
  110. diff.a = a - rhs.a;
  111. return diff;
  112. }
  113. Color operator* (float rhs) const
  114. {
  115. Color prod;
  116. prod.r = rhs*r;
  117. prod.g = rhs*g;
  118. prod.b = rhs*b;
  119. prod.a = rhs*a;
  120. return prod;
  121. }
  122. Color operator* (const Color& rhs) const
  123. {
  124. Color prod;
  125. prod.r = rhs.r * r;
  126. prod.g = rhs.g * g;
  127. prod.b = rhs.b * b;
  128. prod.a = rhs.a * a;
  129. return prod;
  130. }
  131. Color operator/ (const Color& rhs) const
  132. {
  133. Color prod;
  134. prod.r = rhs.r / r;
  135. prod.g = rhs.g / g;
  136. prod.b = rhs.b / b;
  137. prod.a = rhs.a / a;
  138. return prod;
  139. }
  140. Color operator/ (float rhs) const
  141. {
  142. assert(rhs != 0.0f);
  143. Color div;
  144. float invRhs = 1.0f / rhs;
  145. div.r = r * invRhs;
  146. div.g = g * invRhs;
  147. div.b = b * invRhs;
  148. div.a = a * invRhs;
  149. return div;
  150. }
  151. friend Color operator* (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. 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. 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. Color& operator*= (float rhs)
  177. {
  178. r *= rhs;
  179. g *= rhs;
  180. b *= rhs;
  181. a *= rhs;
  182. return *this;
  183. }
  184. Color& operator/= (float rhs)
  185. {
  186. assert(rhs != 0.0f);
  187. float invRhs = 1.0f / rhs;
  188. r *= invRhs;
  189. g *= invRhs;
  190. b *= invRhs;
  191. a *= invRhs;
  192. return *this;
  193. }
  194. /**
  195. * Set a color value from hue, saturation and brightness.
  196. *
  197. * @param[in] hue Hue value, scaled to the [0,1] range.
  198. * @param[in] saturation Saturation level, [0,1].
  199. * @param[in] brightness Brightness level, [0,1].
  200. */
  201. void setHSB(float hue, float saturation, float brightness);
  202. /**
  203. * Convert the current color to hue, saturation and brightness values.
  204. *
  205. * @param[in] hue Output hue value, scaled to the [0,1] range.
  206. * @param[in] saturation Output saturation level, [0,1].
  207. * @param[in] brightness Output brightness level, [0,1].
  208. */
  209. void getHSB(float* hue, float* saturation, float* brightness) const;
  210. /**
  211. * Linearly interpolates between the two colors using @p t. t should be in [0, 1] range, where t = 0 corresponds
  212. * to the left color, while t = 1 corresponds to the right color.
  213. */
  214. static Color lerp(float t, const Color& a, const Color& b);
  215. float r, g, b, a;
  216. };
  217. /** @cond SPECIALIZATIONS */
  218. BS_ALLOW_MEMCPY_SERIALIZATION(Color);
  219. /** @endcond */
  220. /** @} */
  221. }
  222. /** @cond SPECIALIZATIONS */
  223. namespace std
  224. {
  225. /** Hash value generator for Color. */
  226. template<>
  227. struct hash<bs::Color>
  228. {
  229. size_t operator()(const bs::Color& color) const
  230. {
  231. size_t hash = 0;
  232. bs::hash_combine(hash, color.r);
  233. bs::hash_combine(hash, color.g);
  234. bs::hash_combine(hash, color.b);
  235. bs::hash_combine(hash, color.a);
  236. return hash;
  237. }
  238. };
  239. }
  240. /** @endcond */