Color.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /// \todo Model data will need to be converted on OpenGL if it has vertex colors
  112. #ifdef USE_OPENGL
  113. return (((a) & 0xff) << 24) | (((b) & 0xff) << 16) | (((g) & 0xff) << 8) | ((r) & 0xff);
  114. #else
  115. return (((a) & 0xff) << 24) | (((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff);
  116. #endif
  117. }
  118. /// Return as a four-dimensional vector
  119. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  120. /// Return as string
  121. String ToString() const;
  122. /// Red value
  123. float r_;
  124. /// Green value
  125. float g_;
  126. /// Blue value
  127. float b_;
  128. /// Alpha value
  129. float a_;
  130. /// Opaque white color
  131. static const Color WHITE;
  132. /// Opaque black color
  133. static const Color BLACK;
  134. };
  135. /// Color-time pair for color interpolation
  136. class ColorFade
  137. {
  138. public:
  139. /// Construct with default color and zero time
  140. ColorFade() :
  141. time_(0.0f)
  142. {
  143. }
  144. /// Construct with a color and zero time
  145. ColorFade(const Color& color) :
  146. color_(color),
  147. time_(0.0f)
  148. {
  149. }
  150. /// Construct from a color and time
  151. ColorFade(const Color& color, float time) :
  152. color_(color),
  153. time_(time)
  154. {
  155. }
  156. /// Return interpolated value with another color-time pair, at the time specified
  157. Color interpolate(const ColorFade& next, float time)
  158. {
  159. float timeInterval = next.time_ - time_;
  160. if (timeInterval > 0.0f)
  161. {
  162. float t = (time - time_) / timeInterval;
  163. return color_.Lerp(next.color_, t);
  164. }
  165. else
  166. return next.color_;
  167. }
  168. /// Color
  169. Color color_;
  170. /// Time
  171. float time_;
  172. };