Color.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // Copyright (c) 2008-2013 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 "Vector4.h"
  24. // Defined by Windows headers
  25. #undef TRANSPARENT
  26. namespace Urho3D
  27. {
  28. class String;
  29. /// RGBA color.
  30. class 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. Color(const float* data) :
  75. r_(data[0]),
  76. g_(data[1]),
  77. b_(data[2]),
  78. a_(data[3])
  79. {
  80. }
  81. /// Test for equality with another color.
  82. bool operator == (const Color& rhs) const { return Equals(r_, rhs.r_) && Equals(g_, rhs.g_) && Equals(b_, rhs.b_) && Equals(a_, rhs.a_); }
  83. /// Test for inequality with another color.
  84. bool operator != (const Color& rhs) const { return !Equals(r_, rhs.r_) || !Equals(g_, rhs.g_) || !Equals(b_, rhs.b_) || !Equals(a_, rhs.a_); }
  85. /// Multiply with a scalar.
  86. Color operator * (float rhs) const { return Color(r_ * rhs, g_ * rhs, b_ * rhs, a_ * rhs); }
  87. /// Add a color.
  88. Color operator + (const Color& rhs) const { return Color(r_ + rhs.r_, g_ + rhs.g_, b_ + rhs.b_, a_ + rhs.a_); }
  89. /// Add-assign a color.
  90. Color& operator += (const Color& rhs)
  91. {
  92. r_ += rhs.r_;
  93. g_ += rhs.g_;
  94. b_ += rhs.b_;
  95. a_ += rhs.a_;
  96. return *this;
  97. }
  98. /// Return float data.
  99. const float* Data() const { return &r_; }
  100. /// Return RGB values as a Vector3.
  101. Vector3 RGBValues() const { return Vector3(r_, g_, b_); }
  102. /// Return approximate intensity.
  103. float Intensity() const { return RGBValues().DotProduct(Vector3(0.333f, 0.333f, 0.333f)); }
  104. /// Linear interpolation with another color.
  105. Color Lerp(const Color& rhs, float t) const
  106. {
  107. float invT = 1.0f - t;
  108. return Color(
  109. r_ * invT + rhs.r_ * t,
  110. g_ * invT + rhs.g_ * t,
  111. b_ * invT + rhs.b_ * t,
  112. a_ * invT + rhs.a_ * t
  113. );
  114. }
  115. /// Return color packed to a 32-bit integer. Components are clamped to [0, 1] range.
  116. unsigned ToUInt() const
  117. {
  118. unsigned r = Clamp(((int)(r_ * 255.0f)), 0, 255);
  119. unsigned g = Clamp(((int)(g_ * 255.0f)), 0, 255);
  120. unsigned b = Clamp(((int)(b_ * 255.0f)), 0, 255);
  121. unsigned a = Clamp(((int)(a_ * 255.0f)), 0, 255);
  122. return (a << 24) | (b << 16) | (g << 8) | r;
  123. }
  124. /// Return as a four-dimensional vector.
  125. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  126. /// Return as string.
  127. String ToString() const;
  128. /// Red value.
  129. float r_;
  130. /// Green value.
  131. float g_;
  132. /// Blue value.
  133. float b_;
  134. /// Alpha value.
  135. float a_;
  136. /// Opaque white color.
  137. static const Color WHITE;
  138. /// Opaque yellow color.
  139. static const Color YELLOW;
  140. /// Opaque red color.
  141. static const Color RED;
  142. /// Opaque green color.
  143. static const Color GREEN;
  144. /// Opaque green color.
  145. static const Color BLUE;
  146. /// Opaque black color.
  147. static const Color BLACK;
  148. /// Transparent color (black with no alpha).
  149. static const Color TRANSPARENT;
  150. };
  151. /// Multiply Color with a scalar.
  152. inline Color operator * (float lhs, const Color& rhs) { return rhs * lhs; }
  153. /// Color-time pair for color interpolation.
  154. class ColorFade
  155. {
  156. public:
  157. /// Construct with default color and zero time.
  158. ColorFade() :
  159. time_(0.0f)
  160. {
  161. }
  162. /// Construct with a color and zero time.
  163. ColorFade(const Color& color) :
  164. color_(color),
  165. time_(0.0f)
  166. {
  167. }
  168. /// Construct from a color and time.
  169. ColorFade(const Color& color, float time) :
  170. color_(color),
  171. time_(time)
  172. {
  173. }
  174. /// Return interpolated value with another color-time pair at the time specified.
  175. Color Interpolate(const ColorFade& next, float time)
  176. {
  177. float timeInterval = next.time_ - time_;
  178. if (timeInterval > 0.0f)
  179. {
  180. float t = (time - time_) / timeInterval;
  181. return color_.Lerp(next.color_, t);
  182. }
  183. else
  184. return next.color_;
  185. }
  186. /// Color.
  187. Color color_;
  188. /// Time.
  189. float time_;
  190. };
  191. }