CmColor.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef _COLOURVALUE_H__
  25. #define _COLOURVALUE_H__
  26. #include "CmPrerequisitesUtil.h"
  27. namespace CamelotFramework {
  28. /** \addtogroup Core
  29. * @{
  30. */
  31. /** \addtogroup General
  32. * @{
  33. */
  34. typedef UINT32 RGBA;
  35. typedef UINT32 ARGB;
  36. typedef UINT32 ABGR;
  37. typedef UINT32 BGRA;
  38. /** Class representing colour.
  39. @remarks
  40. Colour is represented as 4 components, each of which is a
  41. floating-point value from 0.0 to 1.0.
  42. @par
  43. The 3 'normal' colour components are red, green and blue, a higher
  44. number indicating greater amounts of that component in the colour.
  45. The forth component is the 'alpha' value, which represents
  46. transparency. In this case, 0.0 is completely transparent and 1.0 is
  47. fully opaque.
  48. */
  49. class CM_UTILITY_EXPORT Color
  50. {
  51. public:
  52. static const Color ZERO;
  53. static const Color Black;
  54. static const Color White;
  55. static const Color Red;
  56. static const Color Green;
  57. static const Color Blue;
  58. explicit Color( float red = 1.0f,
  59. float green = 1.0f,
  60. float blue = 1.0f,
  61. float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha)
  62. { }
  63. bool operator==(const Color& rhs) const;
  64. bool operator!=(const Color& rhs) const;
  65. float r,g,b,a;
  66. /** Retrieves colour as RGBA.
  67. */
  68. RGBA getAsRGBA(void) const;
  69. /** Retrieves colour as ARGB.
  70. */
  71. ARGB getAsARGB(void) const;
  72. /** Retrieves colour as BGRA.
  73. */
  74. BGRA getAsBGRA(void) const;
  75. /** Retrieves colours as ABGR */
  76. ABGR getAsABGR(void) const;
  77. /** Sets colour as RGBA.
  78. */
  79. void setAsRGBA(const RGBA val);
  80. /** Sets colour as ARGB.
  81. */
  82. void setAsARGB(const ARGB val);
  83. /** Sets colour as BGRA.
  84. */
  85. void setAsBGRA(const BGRA val);
  86. /** Sets colour as ABGR.
  87. */
  88. void setAsABGR(const ABGR val);
  89. /** Clamps colour value to the range [0, 1].
  90. */
  91. void saturate(void)
  92. {
  93. if (r < 0)
  94. r = 0;
  95. else if (r > 1)
  96. r = 1;
  97. if (g < 0)
  98. g = 0;
  99. else if (g > 1)
  100. g = 1;
  101. if (b < 0)
  102. b = 0;
  103. else if (b > 1)
  104. b = 1;
  105. if (a < 0)
  106. a = 0;
  107. else if (a > 1)
  108. a = 1;
  109. }
  110. /** As saturate, except that this colour value is unaffected and
  111. the saturated colour value is returned as a copy. */
  112. Color saturateCopy(void) const
  113. {
  114. Color ret = *this;
  115. ret.saturate();
  116. return ret;
  117. }
  118. /// Array accessor operator
  119. inline float operator [] ( const size_t i ) const
  120. {
  121. assert( i < 4 );
  122. return *(&r+i);
  123. }
  124. /// Array accessor operator
  125. inline float& operator [] ( const size_t i )
  126. {
  127. assert( i < 4 );
  128. return *(&r+i);
  129. }
  130. /// Pointer accessor for direct copying
  131. inline float* ptr()
  132. {
  133. return &r;
  134. }
  135. /// Pointer accessor for direct copying
  136. inline const float* ptr() const
  137. {
  138. return &r;
  139. }
  140. // arithmetic operations
  141. inline Color operator + ( const Color& rkVector ) const
  142. {
  143. Color kSum;
  144. kSum.r = r + rkVector.r;
  145. kSum.g = g + rkVector.g;
  146. kSum.b = b + rkVector.b;
  147. kSum.a = a + rkVector.a;
  148. return kSum;
  149. }
  150. inline Color operator - ( const Color& rkVector ) const
  151. {
  152. Color kDiff;
  153. kDiff.r = r - rkVector.r;
  154. kDiff.g = g - rkVector.g;
  155. kDiff.b = b - rkVector.b;
  156. kDiff.a = a - rkVector.a;
  157. return kDiff;
  158. }
  159. inline Color operator * (const float fScalar ) const
  160. {
  161. Color kProd;
  162. kProd.r = fScalar*r;
  163. kProd.g = fScalar*g;
  164. kProd.b = fScalar*b;
  165. kProd.a = fScalar*a;
  166. return kProd;
  167. }
  168. inline Color operator * ( const Color& rhs) const
  169. {
  170. Color kProd;
  171. kProd.r = rhs.r * r;
  172. kProd.g = rhs.g * g;
  173. kProd.b = rhs.b * b;
  174. kProd.a = rhs.a * a;
  175. return kProd;
  176. }
  177. inline Color operator / ( const Color& rhs) const
  178. {
  179. Color kProd;
  180. kProd.r = rhs.r / r;
  181. kProd.g = rhs.g / g;
  182. kProd.b = rhs.b / b;
  183. kProd.a = rhs.a / a;
  184. return kProd;
  185. }
  186. inline Color operator / (const float fScalar ) const
  187. {
  188. assert( fScalar != 0.0 );
  189. Color kDiv;
  190. float fInv = 1.0f / fScalar;
  191. kDiv.r = r * fInv;
  192. kDiv.g = g * fInv;
  193. kDiv.b = b * fInv;
  194. kDiv.a = a * fInv;
  195. return kDiv;
  196. }
  197. inline friend Color operator * (const float fScalar, const Color& rkVector )
  198. {
  199. Color kProd;
  200. kProd.r = fScalar * rkVector.r;
  201. kProd.g = fScalar * rkVector.g;
  202. kProd.b = fScalar * rkVector.b;
  203. kProd.a = fScalar * rkVector.a;
  204. return kProd;
  205. }
  206. // arithmetic updates
  207. inline Color& operator += ( const Color& rkVector )
  208. {
  209. r += rkVector.r;
  210. g += rkVector.g;
  211. b += rkVector.b;
  212. a += rkVector.a;
  213. return *this;
  214. }
  215. inline Color& operator -= ( const Color& rkVector )
  216. {
  217. r -= rkVector.r;
  218. g -= rkVector.g;
  219. b -= rkVector.b;
  220. a -= rkVector.a;
  221. return *this;
  222. }
  223. inline Color& operator *= (const float fScalar )
  224. {
  225. r *= fScalar;
  226. g *= fScalar;
  227. b *= fScalar;
  228. a *= fScalar;
  229. return *this;
  230. }
  231. inline Color& operator /= (const float fScalar )
  232. {
  233. assert( fScalar != 0.0 );
  234. float fInv = 1.0f / fScalar;
  235. r *= fInv;
  236. g *= fInv;
  237. b *= fInv;
  238. a *= fInv;
  239. return *this;
  240. }
  241. /** Set a colour value from Hue, Saturation and Brightness.
  242. @param hue Hue value, scaled to the [0,1] range as opposed to the 0-360
  243. @param saturation Saturation level, [0,1]
  244. @param brightness Brightness level, [0,1]
  245. */
  246. void setHSB(float hue, float saturation, float brightness);
  247. /** Convert the current colour to Hue, Saturation and Brightness values.
  248. @param hue Output hue value, scaled to the [0,1] range as opposed to the 0-360
  249. @param saturation Output saturation level, [0,1]
  250. @param brightness Output brightness level, [0,1]
  251. */
  252. void getHSB(float* hue, float* saturation, float* brightness) const;
  253. /** Function for writing to a stream.
  254. */
  255. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  256. ( std::ostream& o, const Color& c )
  257. {
  258. o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
  259. return o;
  260. }
  261. };
  262. /** @} */
  263. /** @} */
  264. CM_ALLOW_MEMCPY_SERIALIZATION(Color);
  265. } // namespace
  266. #endif