Color.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Color.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. /// An object that represents RGBA color
  13. class ColorRGBA
  14. {
  15. public:
  16. enum : uint8_t
  17. {
  18. ALPHA_OPAQUE = 255,
  19. ALPHA_TRANSPARENT = 0,
  20. };
  21. uint8_t r;
  22. uint8_t g;
  23. uint8_t b;
  24. uint8_t a;
  25. //constructors
  26. constexpr ColorRGBA()
  27. :r(0)
  28. ,g(0)
  29. ,b(0)
  30. ,a(0)
  31. {
  32. }
  33. constexpr ColorRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
  34. : r(r)
  35. , g(g)
  36. , b(b)
  37. , a(a)
  38. {}
  39. constexpr ColorRGBA(uint8_t r, uint8_t g, uint8_t b)
  40. : r(r)
  41. , g(g)
  42. , b(b)
  43. , a(ALPHA_OPAQUE)
  44. {}
  45. template <typename Handler>
  46. void serialize(Handler &h)
  47. {
  48. h & r;
  49. h & g;
  50. h & b;
  51. h & a;
  52. }
  53. bool operator==(ColorRGBA const& rhs) const
  54. {
  55. return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a;
  56. }
  57. };
  58. namespace vstd
  59. {
  60. template<typename Floating>
  61. ColorRGBA lerp(const ColorRGBA & left, const ColorRGBA & right, const Floating & factor)
  62. {
  63. return ColorRGBA(
  64. vstd::lerp(left.r, right.r, factor),
  65. vstd::lerp(left.g, right.g, factor),
  66. vstd::lerp(left.b, right.b, factor),
  67. vstd::lerp(left.a, right.a, factor)
  68. );
  69. }
  70. }
  71. VCMI_LIB_NAMESPACE_END