Color.pkg 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. $#include "Color.h"
  2. /// RGBA color.
  3. class Color
  4. {
  5. public:
  6. /// Construct with default values (opaque white.)
  7. Color() :
  8. r_(1.0f),
  9. g_(1.0f),
  10. b_(1.0f),
  11. a_(1.0f)
  12. {
  13. }
  14. /// Copy-construct from another color.
  15. Color(const Color& color) :
  16. r_(color.r_),
  17. g_(color.g_),
  18. b_(color.b_),
  19. a_(color.a_)
  20. {
  21. }
  22. /// Construct from another color and modify the alpha.
  23. Color(const Color& color, float a) :
  24. r_(color.r_),
  25. g_(color.g_),
  26. b_(color.b_),
  27. a_(a)
  28. {
  29. }
  30. /// Construct from RGB values and set alpha fully opaque.
  31. Color(float r, float g, float b) :
  32. r_(r),
  33. g_(g),
  34. b_(b),
  35. a_(1.0f)
  36. {
  37. }
  38. /// Construct from RGBA values.
  39. Color(float r, float g, float b, float a) :
  40. r_(r),
  41. g_(g),
  42. b_(b),
  43. a_(a)
  44. {
  45. }
  46. /// Test for equality with another color.
  47. bool operator == (const Color& rhs) const { return Equals(r_, rhs.r_) && Equals(g_, rhs.g_) && Equals(b_, rhs.b_) && Equals(a_, rhs.a_); }
  48. /// Multiply with a scalar.
  49. Color operator * (float rhs) const { return Color(r_ * rhs, g_ * rhs, b_ * rhs, a_ * rhs); }
  50. /// Add a color.
  51. Color operator + (const Color& rhs) const { return Color(r_ + rhs.r_, g_ + rhs.g_, b_ + rhs.b_, a_ + rhs.a_); }
  52. /// Return RGB values as a Vector3.
  53. Vector3 RGBValues() const { return Vector3(r_, g_, b_); }
  54. /// Return approximate intensity.
  55. float Intensity() const { return RGBValues().DotProduct(Vector3(0.333f, 0.333f, 0.333f)); }
  56. /// Linear interpolation with another color.
  57. Color Lerp(const Color& rhs, float t) const
  58. {
  59. float invT = 1.0f - t;
  60. return Color(
  61. r_ * invT + rhs.r_ * t,
  62. g_ * invT + rhs.g_ * t,
  63. b_ * invT + rhs.b_ * t,
  64. a_ * invT + rhs.a_ * t
  65. );
  66. }
  67. /// Return color packed to a 32-bit integer. Components are clamped to [0, 1] range.
  68. unsigned ToUInt() const
  69. {
  70. unsigned r = Clamp(((int)(r_ * 255.0f)), 0, 255);
  71. unsigned g = Clamp(((int)(g_ * 255.0f)), 0, 255);
  72. unsigned b = Clamp(((int)(b_ * 255.0f)), 0, 255);
  73. unsigned a = Clamp(((int)(a_ * 255.0f)), 0, 255);
  74. return (a << 24) | (b << 16) | (g << 8) | r;
  75. }
  76. /// Return as a four-dimensional vector.
  77. Vector4 ToVector4() const { return Vector4(r_, g_, b_, a_); }
  78. /// Return as string.
  79. String ToString() const;
  80. /// Red value.
  81. float r_ @ r;
  82. /// Green value.
  83. float g_ @ g;
  84. /// Blue value.
  85. float b_ @ b;
  86. /// Alpha value.
  87. float a_ @ a;
  88. /// Opaque white color.
  89. static const Color WHITE;
  90. /// Opaque yellow color.
  91. static const Color YELLOW;
  92. /// Opaque red color.
  93. static const Color RED;
  94. /// Opaque green color.
  95. static const Color GREEN;
  96. /// Opaque green color.
  97. static const Color BLUE;
  98. /// Opaque black color.
  99. static const Color BLACK;
  100. /// Transparent color (black with no alpha).
  101. static const Color TRANSPARENT;
  102. };