Color.h 5.9 KB

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