Color.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2006-2019 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_COLOR_H
  21. #define LOVE_COLOR_H
  22. namespace love
  23. {
  24. template <typename T>
  25. struct ColorT
  26. {
  27. T r;
  28. T g;
  29. T b;
  30. T a;
  31. ColorT() : r(0), g(0), b(0), a(0) {}
  32. ColorT(T r_, T g_, T b_, T a_) : r(r_), g(g_), b(b_), a(a_) {}
  33. void set(T r_, T g_, T b_, T a_)
  34. {
  35. r = r_;
  36. g = g_;
  37. b = b_;
  38. a = a_;
  39. }
  40. bool operator==(const ColorT<T> &other) const;
  41. bool operator!=(const ColorT<T> &other) const;
  42. ColorT<T> operator+=(const ColorT<T> &other);
  43. ColorT<T> operator*=(const ColorT<T> &other);
  44. ColorT<T> operator*=(T s);
  45. ColorT<T> operator/=(T s);
  46. };
  47. template <typename T>
  48. bool ColorT<T>::operator==(const ColorT<T> &other) const
  49. {
  50. return r == other.r && g == other.g && b == other.b && a == other.a;
  51. }
  52. template <typename T>
  53. bool ColorT<T>::operator!=(const ColorT<T> &other) const
  54. {
  55. return !(operator==(other));
  56. }
  57. template <typename T>
  58. ColorT<T> ColorT<T>::operator+=(const ColorT<T> &other)
  59. {
  60. r += other.r;
  61. g += other.g;
  62. b += other.b;
  63. a += other.a;
  64. return *this;
  65. }
  66. template <typename T>
  67. ColorT<T> ColorT<T>::operator*=(const ColorT<T> &other)
  68. {
  69. r *= other.r;
  70. g *= other.g;
  71. b *= other.b;
  72. a *= other.a;
  73. return *this;
  74. }
  75. template <typename T>
  76. ColorT<T> ColorT<T>::operator*=(T s)
  77. {
  78. r *= s;
  79. g *= s;
  80. b *= s;
  81. a *= s;
  82. return *this;
  83. }
  84. template <typename T>
  85. ColorT<T> ColorT<T>::operator/=(T s)
  86. {
  87. r /= s;
  88. g /= s;
  89. b /= s;
  90. a /= s;
  91. return *this;
  92. }
  93. template <typename T>
  94. ColorT<T> operator+(const ColorT<T> &a, const ColorT<T> &b)
  95. {
  96. ColorT<T> tmp(a);
  97. return tmp += b;
  98. }
  99. template <typename T>
  100. ColorT<T> operator*(const ColorT<T> &a, const ColorT<T> &b)
  101. {
  102. ColorT<T> res;
  103. res.r = a.r * b.r;
  104. res.g = a.g * b.g;
  105. res.b = a.b * b.b;
  106. res.a = a.a * b.a;
  107. return res;
  108. }
  109. template <typename T>
  110. ColorT<T> operator*(const ColorT<T> &a, T s)
  111. {
  112. ColorT<T> tmp(a);
  113. return tmp *= s;
  114. }
  115. template <typename T>
  116. ColorT<T> operator/(const ColorT<T> &a, T s)
  117. {
  118. ColorT<T> tmp(a);
  119. return tmp /= s;
  120. }
  121. typedef ColorT<unsigned char> Color32;
  122. typedef ColorT<float> Colorf;
  123. inline Color32 toColor32(Colorf cf)
  124. {
  125. return Color32((unsigned char) (cf.r * 255.0f),
  126. (unsigned char) (cf.g * 255.0f),
  127. (unsigned char) (cf.b * 255.0f),
  128. (unsigned char) (cf.a * 255.0f));
  129. }
  130. inline Colorf toColorf(Color32 c)
  131. {
  132. return Colorf(c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f);
  133. }
  134. } // love
  135. #endif // LOVE_COLOR_H