Color.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Vector4.h"
  25. namespace Urho3D
  26. {
  27. class String;
  28. /// RGBA color.
  29. class Color
  30. {
  31. public:
  32. /// Construct with default values (opaque white.)
  33. Color() :
  34. r_(1.0f),
  35. g_(1.0f),
  36. b_(1.0f),
  37. a_(1.0f)
  38. {
  39. }
  40. /// Copy-construct from another color.
  41. Color(const Color& color) :
  42. r_(color.r_),
  43. g_(color.g_),
  44. b_(color.b_),
  45. a_(color.a_)
  46. {
  47. }
  48. /// Construct from another color and modify the alpha.
  49. Color(const Color& color, float a) :
  50. r_(color.r_),
  51. g_(color.g_),
  52. b_(color.b_),
  53. a_(a)
  54. {
  55. }
  56. /// Construct from RGB values and set alpha fully opaque.
  57. Color(float r, float g, float b) :
  58. r_(r),
  59. g_(g),
  60. b_(b),
  61. a_(1.0f)
  62. {
  63. }
  64. /// Construct from RGBA values.
  65. Color(float r, float g, float b, float a) :
  66. r_(r),
  67. g_(g),
  68. b_(b),
  69. a_(a)
  70. {
  71. }
  72. /// Test for equality with another color.
  73. bool operator == (const Color& rhs) const { return Equals(r_, rhs.r_) && Equals(g_, rhs.g_) && Equals(b_, rhs.b_) && Equals(a_, rhs.a_); }
  74. /// Test for inequality with another color.
  75. bool operator != (const Color& rhs) const { return !Equals(r_, rhs.r_) || !Equals(g_, rhs.g_) || !Equals(b_, rhs.b_) || !Equals(a_, rhs.a_); }
  76. /// Multiply with a scalar.
  77. Color operator * (float rhs) const { return Color(r_ * rhs, g_ * rhs, b_ * rhs, a_ * rhs); }
  78. /// Add a color.
  79. Color operator + (const Color& rhs) const { return Color(r_ + rhs.r_, g_ + rhs.g_, b_ + rhs.b_, a_ + rhs.a_); }
  80. /// Add-assign a color.
  81. Color& operator += (const Color& rhs)
  82. {
  83. r_ += rhs.r_;
  84. g_ += rhs.g_;
  85. b_ += rhs.b_;
  86. a_ += rhs.a_;
  87. return *this;
  88. }
  89. /// Return float data.
  90. const float* Data() const { return &r_; }
  91. /// Return RGB values as a Vector3.
  92. Vector3 RGBValues() const { return Vector3(r_, g_, b_); }
  93. /// Return approximate intensity.
  94. float Intensity() const { return RGBValues().DotProduct(Vector3(0.333f, 0.333f, 0.333f)); }
  95. /// Linear interpolation with another color.
  96. Color Lerp(const Color& rhs, float t) const
  97. {
  98. float invT = 1.0f - t;
  99. return Color(
  100. r_ * invT + rhs.r_ * t,
  101. g_ * invT + rhs.g_ * t,
  102. b_ * invT + rhs.b_ * t,
  103. a_ * invT + rhs.a_ * t
  104. );
  105. }
  106. /// Return color packed to a 32-bit integer. Components are clamped to [0, 1] range.
  107. unsigned ToUInt() const
  108. {
  109. unsigned r = Clamp(((int)(r_ * 255.0f)), 0, 255);
  110. unsigned g = Clamp(((int)(g_ * 255.0f)), 0, 255);
  111. unsigned b = Clamp(((int)(b_ * 255.0f)), 0, 255);
  112. unsigned a = Clamp(((int)(a_ * 255.0f)), 0, 255);
  113. return (a << 24) | (b << 16) | (g << 8) | r;
  114. }
  115. /// Return as a four-dimensional vector.
  116. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  117. /// Return as string.
  118. String ToString() const;
  119. /// Red value.
  120. float r_;
  121. /// Green value.
  122. float g_;
  123. /// Blue value.
  124. float b_;
  125. /// Alpha value.
  126. float a_;
  127. /// Opaque white color.
  128. static const Color WHITE;
  129. /// Opaque yellow color.
  130. static const Color YELLOW;
  131. /// Opaque red color.
  132. static const Color RED;
  133. /// Opaque green color.
  134. static const Color GREEN;
  135. /// Opaque green color.
  136. static const Color BLUE;
  137. /// Opaque black color.
  138. static const Color BLACK;
  139. };
  140. /// Multiply Color with a scalar.
  141. inline Color operator * (float lhs, const Color& rhs) { return rhs * lhs; }
  142. /// Color-time pair for color interpolation.
  143. class ColorFade
  144. {
  145. public:
  146. /// Construct with default color and zero time.
  147. ColorFade() :
  148. time_(0.0f)
  149. {
  150. }
  151. /// Construct with a color and zero time.
  152. ColorFade(const Color& color) :
  153. color_(color),
  154. time_(0.0f)
  155. {
  156. }
  157. /// Construct from a color and time.
  158. ColorFade(const Color& color, float time) :
  159. color_(color),
  160. time_(time)
  161. {
  162. }
  163. /// Return interpolated value with another color-time pair, at the time specified.
  164. Color interpolate(const ColorFade& next, float time)
  165. {
  166. float timeInterval = next.time_ - time_;
  167. if (timeInterval > 0.0f)
  168. {
  169. float t = (time - time_) / timeInterval;
  170. return color_.Lerp(next.color_, t);
  171. }
  172. else
  173. return next.color_;
  174. }
  175. /// Color.
  176. Color color_;
  177. /// Time.
  178. float time_;
  179. };
  180. }