Color.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Math/Vector4.h"
  24. namespace Urho3D
  25. {
  26. class String;
  27. /// RGBA color.
  28. class URHO3D_API Color
  29. {
  30. public:
  31. /// Mask describing color channels.
  32. struct ChannelMask
  33. {
  34. /// Red channel mask. If zero, red channel is set to 0.
  35. unsigned r_;
  36. /// Green channel mask. If zero, green channel is set to 0.
  37. unsigned g_;
  38. /// Blue channel mask. If zero, blue channel is set to 0.
  39. unsigned b_;
  40. /// Alpha channel mask. If zero, alpha channel is set to 1.
  41. unsigned a_;
  42. };
  43. /// Mask for 0xAABBGGRR layout.
  44. static const ChannelMask ABGR;
  45. /// Mask for 0xAARRGGBB layout.
  46. static const ChannelMask ARGB;
  47. /// Construct with default values (opaque white).
  48. Color() noexcept :
  49. r_(1.0f),
  50. g_(1.0f),
  51. b_(1.0f),
  52. a_(1.0f)
  53. {
  54. }
  55. /// Copy-construct from another color.
  56. Color(const Color& color) noexcept = default;
  57. /// Construct from another color and modify the alpha.
  58. Color(const Color& color, float a) noexcept :
  59. r_(color.r_),
  60. g_(color.g_),
  61. b_(color.b_),
  62. a_(a)
  63. {
  64. }
  65. /// Construct from RGB values and set alpha fully opaque.
  66. Color(float r, float g, float b) noexcept :
  67. r_(r),
  68. g_(g),
  69. b_(b),
  70. a_(1.0f)
  71. {
  72. }
  73. /// Construct from RGBA values.
  74. Color(float r, float g, float b, float a) noexcept :
  75. r_(r),
  76. g_(g),
  77. b_(b),
  78. a_(a)
  79. {
  80. }
  81. /// Construct from a float array.
  82. explicit Color(const float* data) noexcept :
  83. r_(data[0]),
  84. g_(data[1]),
  85. b_(data[2]),
  86. a_(data[3])
  87. {
  88. }
  89. /// Construct from 32-bit integer. Default format is 0xAABBGGRR.
  90. explicit Color(unsigned color, const ChannelMask& mask = ABGR) { FromUIntMask(color, mask); }
  91. /// Construct from 3-vector.
  92. explicit Color(const Vector3& color) : Color(color.x_, color.y_, color.z_) {}
  93. /// Construct from 4-vector.
  94. explicit Color(const Vector4& color) : Color(color.x_, color.y_, color.z_, color.w_) {}
  95. /// Assign from another color.
  96. Color& operator =(const Color& rhs) noexcept = default;
  97. /// Test for equality with another color without epsilon.
  98. bool operator ==(const Color& rhs) const { return r_ == rhs.r_ && g_ == rhs.g_ && b_ == rhs.b_ && a_ == rhs.a_; }
  99. /// Test for inequality with another color without epsilon.
  100. bool operator !=(const Color& rhs) const { return r_ != rhs.r_ || g_ != rhs.g_ || b_ != rhs.b_ || a_ != rhs.a_; }
  101. /// Multiply with a scalar.
  102. Color operator *(float rhs) const { return Color(r_ * rhs, g_ * rhs, b_ * rhs, a_ * rhs); }
  103. /// Add a color.
  104. Color operator +(const Color& rhs) const { return Color(r_ + rhs.r_, g_ + rhs.g_, b_ + rhs.b_, a_ + rhs.a_); }
  105. /// Return negation.
  106. Color operator -() const { return Color(-r_, -g_, -b_, -a_); }
  107. /// Subtract a color.
  108. Color operator -(const Color& rhs) const { return Color(r_ - rhs.r_, g_ - rhs.g_, b_ - rhs.b_, a_ - rhs.a_); }
  109. /// Add-assign a color.
  110. Color& operator +=(const Color& rhs)
  111. {
  112. r_ += rhs.r_;
  113. g_ += rhs.g_;
  114. b_ += rhs.b_;
  115. a_ += rhs.a_;
  116. return *this;
  117. }
  118. /// Return float data.
  119. const float* Data() const { return &r_; }
  120. /// Return color packed to a 32-bit integer, with R component in the lowest 8 bits. Components are clamped to [0, 1] range.
  121. unsigned ToUInt() const;
  122. /// Return color packed to a 32-bit integer with arbitrary channel mask. Components are clamped to [0, 1] range.
  123. unsigned ToUIntMask(const ChannelMask& mask) const;
  124. /// Return HSL color-space representation as a Vector3; the RGB values are clipped before conversion but not changed in the process.
  125. Vector3 ToHSL() const;
  126. /// Return HSV color-space representation as a Vector3; the RGB values are clipped before conversion but not changed in the process.
  127. Vector3 ToHSV() const;
  128. /// Set RGBA values from packed 32-bit integer, with R component in the lowest 8 bits (format 0xAABBGGRR).
  129. void FromUInt(unsigned color);
  130. /// Set RGBA values from packed 32-bit integer with arbitrary channel mask.
  131. void FromUIntMask(unsigned color, const ChannelMask& mask);
  132. /// Set RGBA values from specified HSL values and alpha.
  133. void FromHSL(float h, float s, float l, float a = 1.0f);
  134. /// Set RGBA values from specified HSV values and alpha.
  135. void FromHSV(float h, float s, float v, float a = 1.0f);
  136. /// Return RGB as a three-dimensional vector.
  137. Vector3 ToVector3() const { return Vector3(r_, g_, b_); }
  138. /// Return RGBA as a four-dimensional vector.
  139. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  140. /// Return sum of RGB components.
  141. float SumRGB() const { return r_ + g_ + b_; }
  142. /// Return average value of the RGB channels.
  143. float Average() const { return (r_ + g_ + b_) / 3.0f; }
  144. /// Return the 'grayscale' representation of RGB values, as used by JPEG and PAL/NTSC among others.
  145. float Luma() const { return r_ * 0.299f + g_ * 0.587f + b_ * 0.114f; }
  146. /// Return the colorfulness relative to the brightness of a similarly illuminated white.
  147. float Chroma() const;
  148. /// Return hue mapped to range [0, 1.0).
  149. float Hue() const;
  150. /// Return saturation as defined for HSL.
  151. float SaturationHSL() const;
  152. /// Return saturation as defined for HSV.
  153. float SaturationHSV() const;
  154. /// Return value as defined for HSV: largest value of the RGB components. Equivalent to calling MinRGB().
  155. float Value() const { return MaxRGB(); }
  156. /// Convert single component of the color from gamma to linear space.
  157. static float ConvertGammaToLinear(float value)
  158. {
  159. if (value <= 0.04045f)
  160. return value / 12.92f;
  161. else if (value < 1.0f)
  162. return Pow((value + 0.055f) / 1.055f, 2.4f);
  163. else
  164. return Pow(value, 2.2f);
  165. }
  166. /// Convert single component of the color from linear to gamma space.
  167. static float ConvertLinearToGamma(float value)
  168. {
  169. if (value <= 0.0f)
  170. return 0.0f;
  171. else if (value <= 0.0031308f)
  172. return 12.92f * value;
  173. else if (value < 1.0f)
  174. return 1.055f * Pow(value, 0.4166667f) - 0.055f;
  175. else
  176. return Pow(value, 0.45454545f);
  177. }
  178. /// Convert color from gamma to linear space.
  179. Color GammaToLinear() const { return { ConvertGammaToLinear(r_), ConvertGammaToLinear(g_), ConvertGammaToLinear(b_), a_ }; }
  180. /// Convert color from linear to gamma space.
  181. Color LinearToGamma() const { return { ConvertLinearToGamma(r_), ConvertLinearToGamma(g_), ConvertLinearToGamma(b_), a_ }; }
  182. /// Return lightness as defined for HSL: average of the largest and smallest values of the RGB components.
  183. float Lightness() const;
  184. /// Stores the values of least and greatest RGB component at specified pointer addresses, optionally clipping those values to [0, 1] range.
  185. void Bounds(float* min, float* max, bool clipped = false) const;
  186. /// Return the largest value of the RGB components.
  187. float MaxRGB() const;
  188. /// Return the smallest value of the RGB components.
  189. float MinRGB() const;
  190. /// Return range, defined as the difference between the greatest and least RGB component.
  191. float Range() const;
  192. /// Clip to [0, 1.0] range.
  193. void Clip(bool clipAlpha = false);
  194. /// Inverts the RGB channels and optionally the alpha channel as well.
  195. void Invert(bool invertAlpha = false);
  196. /// Return linear interpolation of this color with another color.
  197. Color Lerp(const Color& rhs, float t) const;
  198. /// Return color with absolute components.
  199. Color Abs() const { return Color(Urho3D::Abs(r_), Urho3D::Abs(g_), Urho3D::Abs(b_), Urho3D::Abs(a_)); }
  200. /// Test for equality with another color with epsilon.
  201. bool Equals(const Color& rhs) const
  202. {
  203. return Urho3D::Equals(r_, rhs.r_) && Urho3D::Equals(g_, rhs.g_) && Urho3D::Equals(b_, rhs.b_) && Urho3D::Equals(a_, rhs.a_);
  204. }
  205. /// Return as string.
  206. String ToString() const;
  207. /// Return color packed to a 32-bit integer, with B component in the lowest 8 bits. Components are clamped to [0, 1] range.
  208. unsigned ToUIntArgb() const { return ToUIntMask(ARGB); }
  209. /// Return hash value for HashSet & HashMap.
  210. unsigned ToHash() const { return ToUInt(); }
  211. /// Red value.
  212. float r_;
  213. /// Green value.
  214. float g_;
  215. /// Blue value.
  216. float b_;
  217. /// Alpha value.
  218. float a_;
  219. /// Opaque white color.
  220. static const Color WHITE;
  221. /// Opaque gray color.
  222. static const Color GRAY;
  223. /// Opaque black color.
  224. static const Color BLACK;
  225. /// Opaque red color.
  226. static const Color RED;
  227. /// Opaque green color.
  228. static const Color GREEN;
  229. /// Opaque blue color.
  230. static const Color BLUE;
  231. /// Opaque cyan color.
  232. static const Color CYAN;
  233. /// Opaque magenta color.
  234. static const Color MAGENTA;
  235. /// Opaque yellow color.
  236. static const Color YELLOW;
  237. /// Transparent black color (black with no alpha).
  238. static const Color TRANSPARENT_BLACK;
  239. protected:
  240. /// Return hue value given greatest and least RGB component, value-wise.
  241. float Hue(float min, float max) const;
  242. /// Return saturation (HSV) given greatest and least RGB component, value-wise.
  243. float SaturationHSV(float min, float max) const;
  244. /// Return saturation (HSL) given greatest and least RGB component, value-wise.
  245. float SaturationHSL(float min, float max) const;
  246. /// Calculate and set RGB values. Convenience function used by FromHSV and FromHSL to avoid code duplication.
  247. void FromHCM(float h, float c, float m);
  248. };
  249. /// Multiply Color with a scalar.
  250. inline Color operator *(float lhs, const Color& rhs) { return rhs * lhs; }
  251. }