Color.h 5.3 KB

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