Color.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // Copyright (c) 2008-2017 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. // Defined by Windows headers
  25. #undef TRANSPARENT
  26. namespace Atomic
  27. {
  28. class String;
  29. /// RGBA color.
  30. class ATOMIC_API Color
  31. {
  32. public:
  33. /// Construct with default values (opaque white.)
  34. Color() :
  35. r_(1.0f),
  36. g_(1.0f),
  37. b_(1.0f),
  38. a_(1.0f)
  39. {
  40. }
  41. /// Copy-construct from another color.
  42. Color(const Color& color) :
  43. r_(color.r_),
  44. g_(color.g_),
  45. b_(color.b_),
  46. a_(color.a_)
  47. {
  48. }
  49. /// Construct from another color and modify the alpha.
  50. Color(const Color& color, float a) :
  51. r_(color.r_),
  52. g_(color.g_),
  53. b_(color.b_),
  54. a_(a)
  55. {
  56. }
  57. /// Construct from RGB values and set alpha fully opaque.
  58. Color(float r, float g, float b) :
  59. r_(r),
  60. g_(g),
  61. b_(b),
  62. a_(1.0f)
  63. {
  64. }
  65. /// Construct from RGBA values.
  66. Color(float r, float g, float b, float a) :
  67. r_(r),
  68. g_(g),
  69. b_(b),
  70. a_(a)
  71. {
  72. }
  73. /// Construct from a float array.
  74. explicit Color(const float* data) :
  75. r_(data[0]),
  76. g_(data[1]),
  77. b_(data[2]),
  78. a_(data[3])
  79. {
  80. }
  81. /// Assign from another color.
  82. Color& operator =(const Color& rhs)
  83. {
  84. r_ = rhs.r_;
  85. g_ = rhs.g_;
  86. b_ = rhs.b_;
  87. a_ = rhs.a_;
  88. return *this;
  89. }
  90. /// Test for equality with another color without epsilon.
  91. bool operator ==(const Color& rhs) const { return r_ == rhs.r_ && g_ == rhs.g_ && b_ == rhs.b_ && a_ == rhs.a_; }
  92. /// Test for inequality with another color without epsilon.
  93. bool operator !=(const Color& rhs) const { return r_ != rhs.r_ || g_ != rhs.g_ || b_ != rhs.b_ || a_ != rhs.a_; }
  94. /// Multiply with a scalar.
  95. Color operator *(float rhs) const { return Color(r_ * rhs, g_ * rhs, b_ * rhs, a_ * rhs); }
  96. /// Add a color.
  97. Color operator +(const Color& rhs) const { return Color(r_ + rhs.r_, g_ + rhs.g_, b_ + rhs.b_, a_ + rhs.a_); }
  98. /// Return negation.
  99. Color operator -() const { return Color(-r_, -g_, -b_, -a_); }
  100. /// Subtract a color.
  101. Color operator -(const Color& rhs) const { return Color(r_ - rhs.r_, g_ - rhs.g_, b_ - rhs.b_, a_ - rhs.a_); }
  102. /// Add-assign a color.
  103. Color& operator +=(const Color& rhs)
  104. {
  105. r_ += rhs.r_;
  106. g_ += rhs.g_;
  107. b_ += rhs.b_;
  108. a_ += rhs.a_;
  109. return *this;
  110. }
  111. /// Return float data.
  112. const float* Data() const { return &r_; }
  113. /// Return color packed to a 32-bit integer, with R component in the lowest 8 bits. Components are clamped to [0, 1] range.
  114. unsigned ToUInt() const;
  115. /// Return HSL color-space representation as a Vector3; the RGB values are clipped before conversion but not changed in the process.
  116. Vector3 ToHSL() const;
  117. /// Return HSV color-space representation as a Vector3; the RGB values are clipped before conversion but not changed in the process.
  118. Vector3 ToHSV() const;
  119. /// Set RGBA values from specified HSL values and alpha.
  120. void FromHSL(float h, float s, float l, float a = 1.0f);
  121. /// Set RGBA values from specified HSV values and alpha.
  122. void FromHSV(float h, float s, float v, float a = 1.0f);
  123. /// Return RGB as a three-dimensional vector.
  124. Vector3 ToVector3() const { return Vector3(r_, g_, b_); }
  125. /// Return RGBA as a four-dimensional vector.
  126. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  127. /// Return sum of RGB components.
  128. float SumRGB() const { return r_ + g_ + b_; }
  129. /// Return average value of the RGB channels.
  130. float Average() const { return (r_ + g_ + b_) / 3.0f; }
  131. /// Return the 'grayscale' representation of RGB values, as used by JPEG and PAL/NTSC among others.
  132. float Luma() const { return r_ * 0.299f + g_ * 0.587f + b_ * 0.114f; }
  133. /// Return the colorfulness relative to the brightness of a similarly illuminated white.
  134. float Chroma() const;
  135. /// Return hue mapped to range [0, 1.0).
  136. float Hue() const;
  137. /// Return saturation as defined for HSL.
  138. float SaturationHSL() const;
  139. /// Return saturation as defined for HSV.
  140. float SaturationHSV() const;
  141. /// Return value as defined for HSV: largest value of the RGB components. Equivalent to calling MinRGB().
  142. float Value() const { return MaxRGB(); }
  143. /// Return lightness as defined for HSL: average of the largest and smallest values of the RGB components.
  144. float Lightness() const;
  145. /// Stores the values of least and greatest RGB component at specified pointer addresses, optionally clipping those values to [0, 1] range.
  146. void Bounds(float* min, float* max, bool clipped = false) const;
  147. /// Return the largest value of the RGB components.
  148. float MaxRGB() const;
  149. /// Return the smallest value of the RGB components.
  150. float MinRGB() const;
  151. /// Return range, defined as the difference between the greatest and least RGB component.
  152. float Range() const;
  153. /// Clip to [0, 1.0] range.
  154. void Clip(bool clipAlpha = false);
  155. /// Inverts the RGB channels and optionally the alpha channel as well.
  156. void Invert(bool invertAlpha = false);
  157. /// Return linear interpolation of this color with another color.
  158. Color Lerp(const Color& rhs, float t) const;
  159. /// Return color with absolute components.
  160. Color Abs() const { return Color(Atomic::Abs(r_), Atomic::Abs(g_), Atomic::Abs(b_), Atomic::Abs(a_)); }
  161. /// Test for equality with another color with epsilon.
  162. bool Equals(const Color& rhs) const
  163. {
  164. return Atomic::Equals(r_, rhs.r_) && Atomic::Equals(g_, rhs.g_) && Atomic::Equals(b_, rhs.b_) && Atomic::Equals(a_, rhs.a_);
  165. }
  166. /// Return as string.
  167. String ToString() const;
  168. // ATOMIC BEGIN
  169. /// Return color packed to a 32-bit integer, with B component in the lowest 8 bits. Components are clamped to [0, 1] range.
  170. unsigned ToUIntArgb() const;
  171. // ATOMIC END
  172. /// Red value.
  173. float r_;
  174. /// Green value.
  175. float g_;
  176. /// Blue value.
  177. float b_;
  178. /// Alpha value.
  179. float a_;
  180. /// Opaque white color.
  181. static const Color WHITE;
  182. /// Opaque gray color.
  183. static const Color GRAY;
  184. /// Opaque black color.
  185. static const Color BLACK;
  186. /// Opaque red color.
  187. static const Color RED;
  188. /// Opaque green color.
  189. static const Color GREEN;
  190. /// Opaque blue color.
  191. static const Color BLUE;
  192. /// Opaque cyan color.
  193. static const Color CYAN;
  194. /// Opaque magenta color.
  195. static const Color MAGENTA;
  196. /// Opaque yellow color.
  197. static const Color YELLOW;
  198. /// Transparent color (black with no alpha).
  199. static const Color TRANSPARENT;
  200. protected:
  201. /// Return hue value given greatest and least RGB component, value-wise.
  202. float Hue(float min, float max) const;
  203. /// Return saturation (HSV) given greatest and least RGB component, value-wise.
  204. float SaturationHSV(float min, float max) const;
  205. /// Return saturation (HSL) given greatest and least RGB component, value-wise.
  206. float SaturationHSL(float min, float max) const;
  207. /// Calculate and set RGB values. Convenience function used by FromHSV and FromHSL to avoid code duplication.
  208. void FromHCM(float h, float c, float m);
  209. };
  210. /// Multiply Color with a scalar.
  211. inline Color operator *(float lhs, const Color& rhs) { return rhs * lhs; }
  212. }