2
0

Color.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. namespace Urho3D
  25. {
  26. class String;
  27. /// RGBA color.
  28. class Color
  29. {
  30. public:
  31. /// Construct with default values (opaque white.)
  32. Color() :
  33. r_(1.0f),
  34. g_(1.0f),
  35. b_(1.0f),
  36. a_(1.0f)
  37. {
  38. }
  39. /// Copy-construct from another color.
  40. Color(const Color& color) :
  41. r_(color.r_),
  42. g_(color.g_),
  43. b_(color.b_),
  44. a_(color.a_)
  45. {
  46. }
  47. /// Construct from another color and modify the alpha.
  48. Color(const Color& color, float a) :
  49. r_(color.r_),
  50. g_(color.g_),
  51. b_(color.b_),
  52. a_(a)
  53. {
  54. }
  55. /// Construct from RGB values and set alpha fully opaque.
  56. Color(float r, float g, float b) :
  57. r_(r),
  58. g_(g),
  59. b_(b),
  60. a_(1.0f)
  61. {
  62. }
  63. /// Construct from RGBA values.
  64. Color(float r, float g, float b, float a) :
  65. r_(r),
  66. g_(g),
  67. b_(b),
  68. a_(a)
  69. {
  70. }
  71. /// Test for equality with another color.
  72. bool operator == (const Color& rhs) const { return Equals(r_, rhs.r_) && Equals(g_, rhs.g_) && Equals(b_, rhs.b_) && Equals(a_, rhs.a_); }
  73. /// Test for inequality with another color.
  74. bool operator != (const Color& rhs) const { return !Equals(r_, rhs.r_) || !Equals(g_, rhs.g_) || !Equals(b_, rhs.b_) || !Equals(a_, rhs.a_); }
  75. /// Multiply with a scalar.
  76. Color operator * (float rhs) const { return Color(r_ * rhs, g_ * rhs, b_ * rhs, a_ * rhs); }
  77. /// Add a color.
  78. Color operator + (const Color& rhs) const { return Color(r_ + rhs.r_, g_ + rhs.g_, b_ + rhs.b_, a_ + rhs.a_); }
  79. /// Add-assign a color.
  80. Color& operator += (const Color& rhs)
  81. {
  82. r_ += rhs.r_;
  83. g_ += rhs.g_;
  84. b_ += rhs.b_;
  85. a_ += rhs.a_;
  86. return *this;
  87. }
  88. /// Return float data.
  89. const float* Data() const { return &r_; }
  90. /// Return RGB values as a Vector3.
  91. Vector3 RGBValues() const { return Vector3(r_, g_, b_); }
  92. /// Return approximate intensity.
  93. float Intensity() const { return RGBValues().DotProduct(Vector3(0.333f, 0.333f, 0.333f)); }
  94. /// Linear interpolation with another color.
  95. Color Lerp(const Color& rhs, float t) const
  96. {
  97. float invT = 1.0f - t;
  98. return Color(
  99. r_ * invT + rhs.r_ * t,
  100. g_ * invT + rhs.g_ * t,
  101. b_ * invT + rhs.b_ * t,
  102. a_ * invT + rhs.a_ * t
  103. );
  104. }
  105. /// Return color packed to a 32-bit integer. Components are clamped to [0, 1] range.
  106. unsigned ToUInt() const
  107. {
  108. unsigned r = Clamp(((int)(r_ * 255.0f)), 0, 255);
  109. unsigned g = Clamp(((int)(g_ * 255.0f)), 0, 255);
  110. unsigned b = Clamp(((int)(b_ * 255.0f)), 0, 255);
  111. unsigned a = Clamp(((int)(a_ * 255.0f)), 0, 255);
  112. return (a << 24) | (b << 16) | (g << 8) | r;
  113. }
  114. /// Return as a four-dimensional vector.
  115. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  116. /// Return as string.
  117. String ToString() const;
  118. /// Red value.
  119. float r_;
  120. /// Green value.
  121. float g_;
  122. /// Blue value.
  123. float b_;
  124. /// Alpha value.
  125. float a_;
  126. /// Opaque white color.
  127. static const Color WHITE;
  128. /// Opaque yellow color.
  129. static const Color YELLOW;
  130. /// Opaque red color.
  131. static const Color RED;
  132. /// Opaque green color.
  133. static const Color GREEN;
  134. /// Opaque green color.
  135. static const Color BLUE;
  136. /// Opaque black color.
  137. static const Color BLACK;
  138. };
  139. /// Multiply Color with a scalar.
  140. inline Color operator * (float lhs, const Color& rhs) { return rhs * lhs; }
  141. /// Color-time pair for color interpolation.
  142. class ColorFade
  143. {
  144. public:
  145. /// Construct with default color and zero time.
  146. ColorFade() :
  147. time_(0.0f)
  148. {
  149. }
  150. /// Construct with a color and zero time.
  151. ColorFade(const Color& color) :
  152. color_(color),
  153. time_(0.0f)
  154. {
  155. }
  156. /// Construct from a color and time.
  157. ColorFade(const Color& color, float time) :
  158. color_(color),
  159. time_(time)
  160. {
  161. }
  162. /// Return interpolated value with another color-time pair, at the time specified.
  163. Color interpolate(const ColorFade& next, float time)
  164. {
  165. float timeInterval = next.time_ - time_;
  166. if (timeInterval > 0.0f)
  167. {
  168. float t = (time - time_) / timeInterval;
  169. return color_.Lerp(next.color_, t);
  170. }
  171. else
  172. return next.color_;
  173. }
  174. /// Color.
  175. Color color_;
  176. /// Time.
  177. float time_;
  178. };
  179. }