Color.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Vector4.h"
  5. namespace Urho3D
  6. {
  7. class String;
  8. using color32 = u32;
  9. /// RGBA color.
  10. class URHO3D_API Color
  11. {
  12. public:
  13. /// Mask describing color channels.
  14. struct ChannelMask
  15. {
  16. /// Red channel mask. If zero, red channel is set to 0.
  17. unsigned r_;
  18. /// Green channel mask. If zero, green channel is set to 0.
  19. unsigned g_;
  20. /// Blue channel mask. If zero, blue channel is set to 0.
  21. unsigned b_;
  22. /// Alpha channel mask. If zero, alpha channel is set to 1.
  23. unsigned a_;
  24. };
  25. /// Mask for 0xAABBGGRR layout.
  26. static const ChannelMask ABGR;
  27. /// Mask for 0xAARRGGBB layout.
  28. static const ChannelMask ARGB;
  29. /// Construct with default values (opaque white).
  30. Color() noexcept :
  31. r_(1.0f),
  32. g_(1.0f),
  33. b_(1.0f),
  34. a_(1.0f)
  35. {
  36. }
  37. /// Copy-construct from another color.
  38. Color(const Color& color) noexcept = default;
  39. /// Construct from another color and modify the alpha.
  40. Color(const Color& color, float a) noexcept :
  41. r_(color.r_),
  42. g_(color.g_),
  43. b_(color.b_),
  44. a_(a)
  45. {
  46. }
  47. /// Construct from RGB values and set alpha fully opaque.
  48. Color(float r, float g, float b) noexcept :
  49. r_(r),
  50. g_(g),
  51. b_(b),
  52. a_(1.0f)
  53. {
  54. }
  55. /// Construct from RGBA values.
  56. Color(float r, float g, float b, float a) noexcept :
  57. r_(r),
  58. g_(g),
  59. b_(b),
  60. a_(a)
  61. {
  62. }
  63. /// Construct from a float array.
  64. explicit Color(const float* data) noexcept :
  65. r_(data[0]),
  66. g_(data[1]),
  67. b_(data[2]),
  68. a_(data[3])
  69. {
  70. }
  71. /// Construct from 32-bit integer. Default format is 0xAABBGGRR.
  72. explicit Color(unsigned color, const ChannelMask& mask = ABGR) { FromU32(color, mask); }
  73. /// Construct from 3-vector.
  74. explicit Color(const Vector3& color) : Color(color.x_, color.y_, color.z_) {}
  75. /// Construct from 4-vector.
  76. explicit Color(const Vector4& color) : Color(color.x_, color.y_, color.z_, color.w_) {}
  77. /// Assign from another color.
  78. Color& operator =(const Color& rhs) noexcept = default;
  79. /// Test for equality with another color without epsilon.
  80. bool operator ==(const Color& rhs) const { return r_ == rhs.r_ && g_ == rhs.g_ && b_ == rhs.b_ && a_ == rhs.a_; }
  81. /// Test for inequality with another color without epsilon.
  82. bool operator !=(const Color& rhs) const { return r_ != rhs.r_ || g_ != rhs.g_ || b_ != rhs.b_ || a_ != rhs.a_; }
  83. /// Multiply with a scalar.
  84. Color operator *(float rhs) const { return Color(r_ * rhs, g_ * rhs, b_ * rhs, a_ * rhs); }
  85. /// Add a color.
  86. Color operator +(const Color& rhs) const { return Color(r_ + rhs.r_, g_ + rhs.g_, b_ + rhs.b_, a_ + rhs.a_); }
  87. /// Return negation.
  88. Color operator -() const { return Color(-r_, -g_, -b_, -a_); }
  89. /// Subtract a color.
  90. Color operator -(const Color& rhs) const { return Color(r_ - rhs.r_, g_ - rhs.g_, b_ - rhs.b_, a_ - rhs.a_); }
  91. /// Add-assign a color.
  92. Color& operator +=(const Color& rhs)
  93. {
  94. r_ += rhs.r_;
  95. g_ += rhs.g_;
  96. b_ += rhs.b_;
  97. a_ += rhs.a_;
  98. return *this;
  99. }
  100. /// Return float data.
  101. const float* Data() const { return &r_; }
  102. /// Return color packed to a 32-bit integer, with R component in the lowest 8 bits. Components are clamped to [0, 1] range.
  103. color32 ToU32() const;
  104. /// Return color packed to a 32-bit integer with arbitrary channel mask. Components are clamped to [0, 1] range.
  105. color32 ToU32(const ChannelMask& mask) const;
  106. /// Return HSL color-space representation as a Vector3; the RGB values are clipped before conversion but not changed in the process.
  107. Vector3 ToHSL() const;
  108. /// Return HSV color-space representation as a Vector3; the RGB values are clipped before conversion but not changed in the process.
  109. Vector3 ToHSV() const;
  110. /// Set RGBA values from packed 32-bit integer, with R component in the lowest 8 bits (format 0xAABBGGRR).
  111. void FromU32(color32 color);
  112. /// Set RGBA values from packed 32-bit integer with arbitrary channel mask.
  113. void FromU32(color32 color, const ChannelMask& mask);
  114. /// Set RGBA values from specified HSL values and alpha.
  115. void FromHSL(float h, float s, float l, float a = 1.0f);
  116. /// Set RGBA values from specified HSV values and alpha.
  117. void FromHSV(float h, float s, float v, float a = 1.0f);
  118. /// Return RGB as a three-dimensional vector.
  119. /// @property{get_rgb}
  120. Vector3 ToVector3() const { return Vector3(r_, g_, b_); }
  121. /// Return RGBA as a four-dimensional vector.
  122. /// @property{get_rgba}
  123. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  124. /// Return sum of RGB components.
  125. float SumRGB() const { return r_ + g_ + b_; }
  126. /// Return average value of the RGB channels.
  127. float Average() const { return (r_ + g_ + b_) / 3.0f; }
  128. /// Return the 'grayscale' representation of RGB values, as used by JPEG and PAL/NTSC among others.
  129. float Luma() const { return r_ * 0.299f + g_ * 0.587f + b_ * 0.114f; }
  130. /// Return the colorfulness relative to the brightness of a similarly illuminated white.
  131. float Chroma() const;
  132. /// Return hue mapped to range [0, 1.0).
  133. float Hue() const;
  134. /// Return saturation as defined for HSL.
  135. float SaturationHSL() const;
  136. /// Return saturation as defined for HSV.
  137. float SaturationHSV() const;
  138. /// Return value as defined for HSV: largest value of the RGB components. Equivalent to calling MinRGB().
  139. float Value() const { return MaxRGB(); }
  140. /// Convert single component of the color from gamma to linear space.
  141. static float ConvertGammaToLinear(float value)
  142. {
  143. if (value <= 0.04045f)
  144. return value / 12.92f;
  145. else if (value < 1.0f)
  146. return Pow((value + 0.055f) / 1.055f, 2.4f);
  147. else
  148. return Pow(value, 2.2f);
  149. }
  150. /// Convert single component of the color from linear to gamma space.
  151. static float ConvertLinearToGamma(float value)
  152. {
  153. if (value <= 0.0f)
  154. return 0.0f;
  155. else if (value <= 0.0031308f)
  156. return 12.92f * value;
  157. else if (value < 1.0f)
  158. return 1.055f * Pow(value, 0.4166667f) - 0.055f;
  159. else
  160. return Pow(value, 0.45454545f);
  161. }
  162. /// Convert color from gamma to linear space.
  163. Color GammaToLinear() const { return { ConvertGammaToLinear(r_), ConvertGammaToLinear(g_), ConvertGammaToLinear(b_), a_ }; }
  164. /// Convert color from linear to gamma space.
  165. Color LinearToGamma() const { return { ConvertLinearToGamma(r_), ConvertLinearToGamma(g_), ConvertLinearToGamma(b_), a_ }; }
  166. /// Return lightness as defined for HSL: average of the largest and smallest values of the RGB components.
  167. float Lightness() const;
  168. /// Stores the values of least and greatest RGB component at specified pointer addresses, optionally clipping those values to [0, 1] range.
  169. void Bounds(float* min, float* max, bool clipped = false) const;
  170. /// Return the largest value of the RGB components.
  171. float MaxRGB() const;
  172. /// Return the smallest value of the RGB components.
  173. float MinRGB() const;
  174. /// Return range, defined as the difference between the greatest and least RGB component.
  175. float Range() const;
  176. /// Clip to [0, 1.0] range.
  177. void Clip(bool clipAlpha = false);
  178. /// Inverts the RGB channels and optionally the alpha channel as well.
  179. void Invert(bool invertAlpha = false);
  180. /// Return linear interpolation of this color with another color.
  181. Color Lerp(const Color& rhs, float t) const;
  182. /// Return color with absolute components.
  183. Color Abs() const { return Color(Urho3D::Abs(r_), Urho3D::Abs(g_), Urho3D::Abs(b_), Urho3D::Abs(a_)); }
  184. /// Test for equality with another color with epsilon.
  185. bool Equals(const Color& rhs) const
  186. {
  187. return Urho3D::Equals(r_, rhs.r_) && Urho3D::Equals(g_, rhs.g_) && Urho3D::Equals(b_, rhs.b_) && Urho3D::Equals(a_, rhs.a_);
  188. }
  189. /// Return as string.
  190. String ToString() const;
  191. /// Return color packed to a 32-bit integer, with B component in the lowest 8 bits. Components are clamped to [0, 1] range.
  192. color32 ToU32Argb() const { return ToU32(ARGB); }
  193. /// Return hash value for HashSet & HashMap.
  194. hash32 ToHash() const { return ToU32(); }
  195. /// Red value.
  196. float r_;
  197. /// Green value.
  198. float g_;
  199. /// Blue value.
  200. float b_;
  201. /// Alpha value.
  202. float a_;
  203. /// Opaque white color.
  204. static const Color WHITE;
  205. /// Opaque gray color.
  206. static const Color GRAY;
  207. /// Opaque black color.
  208. static const Color BLACK;
  209. /// Opaque red color.
  210. static const Color RED;
  211. /// Opaque green color.
  212. static const Color GREEN;
  213. /// Opaque blue color.
  214. static const Color BLUE;
  215. /// Opaque cyan color.
  216. static const Color CYAN;
  217. /// Opaque magenta color.
  218. static const Color MAGENTA;
  219. /// Opaque yellow color.
  220. static const Color YELLOW;
  221. /// Transparent black color (black with no alpha).
  222. static const Color TRANSPARENT_BLACK;
  223. protected:
  224. /// Return hue value given greatest and least RGB component, value-wise.
  225. float Hue(float min, float max) const;
  226. /// Return saturation (HSV) given greatest and least RGB component, value-wise.
  227. float SaturationHSV(float min, float max) const;
  228. /// Return saturation (HSL) given greatest and least RGB component, value-wise.
  229. float SaturationHSL(float min, float max) const;
  230. /// Calculate and set RGB values. Convenience function used by FromHSV and FromHSL to avoid code duplication.
  231. void FromHCM(float h, float c, float m);
  232. };
  233. /// Multiply Color with a scalar.
  234. inline Color operator *(float lhs, const Color& rhs) { return rhs * lhs; }
  235. }