ColorRgbScalar.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #ifndef COLORRGBSCALAR_H
  2. #define COLORRGBSCALAR_H
  3. // STL includes
  4. #include <cstdint>
  5. #include <iostream>
  6. #include <QString>
  7. #include <QTextStream>
  8. #include <QRgb>
  9. #include <utils/ColorRgb.h>
  10. ///
  11. /// Plain-Old-Data structure containing the red-green-blue color specification. Size of the
  12. /// structure is exactly 3 times int for easy writing to led-device
  13. ///
  14. struct ColorRgbScalar
  15. {
  16. /// The red color channel
  17. int red;
  18. /// The green color channel
  19. int green;
  20. /// The blue color channel
  21. int blue;
  22. /// 'Black' RgbColor (0, 0, 0)
  23. static const ColorRgbScalar BLACK;
  24. /// 'Red' RgbColor (255, 0, 0)
  25. static const ColorRgbScalar RED;
  26. /// 'Green' RgbColor (0, 255, 0)
  27. static const ColorRgbScalar GREEN;
  28. /// 'Blue' RgbColor (0, 0, 255)
  29. static const ColorRgbScalar BLUE;
  30. /// 'Yellow' RgbColor (255, 255, 0)
  31. static const ColorRgbScalar YELLOW;
  32. /// 'White' RgbColor (255, 255, 255)
  33. static const ColorRgbScalar WHITE;
  34. ColorRgbScalar() = default;
  35. ColorRgbScalar(int _red, int _green,int _blue):
  36. red(_red),
  37. green(_green),
  38. blue(_blue)
  39. {
  40. }
  41. ColorRgbScalar(ColorRgb rgb):
  42. red(rgb.red),
  43. green(rgb.green),
  44. blue(rgb.blue)
  45. {
  46. }
  47. ColorRgbScalar operator-(const ColorRgbScalar& b) const
  48. {
  49. ColorRgbScalar a(*this);
  50. a.red -= b.red;
  51. a.green -= b.green;
  52. a.blue -= b.blue;
  53. return a;
  54. }
  55. void setRgb(QRgb rgb)
  56. {
  57. red = qRed(rgb);
  58. green = qGreen(rgb);
  59. blue = qBlue(rgb);
  60. }
  61. void setRgb(ColorRgb rgb)
  62. {
  63. red = rgb.red;
  64. green = rgb.green;
  65. blue = rgb.blue;
  66. }
  67. QString toQString() const
  68. {
  69. return QString("(%1,%2,%3)").arg(red).arg(green).arg(blue);
  70. }
  71. };
  72. /// Assert to ensure that the size of the structure is 'only' 3 times int
  73. static_assert(sizeof(ColorRgbScalar) == 3 * sizeof(int), "Incorrect size of ColorRgbInt");
  74. ///
  75. /// Stream operator to write ColorRgbInt to an outputstream (format "'{'[red]','[green]','[blue]'}'")
  76. ///
  77. /// @param os The output stream
  78. /// @param color The color to write
  79. /// @return The output stream (with the color written to it)
  80. ///
  81. inline std::ostream& operator<<(std::ostream& os, const ColorRgbScalar& color)
  82. {
  83. os << "{"
  84. << static_cast<unsigned>(color.red) << ","
  85. << static_cast<unsigned>(color.green) << ","
  86. << static_cast<unsigned>(color.blue)
  87. << "}";
  88. return os;
  89. }
  90. ///
  91. /// Stream operator to write ColorRgbInt to a QTextStream (format "'{'[red]','[green]','[blue]'}'")
  92. ///
  93. /// @param os The output stream
  94. /// @param color The color to write
  95. /// @return The output stream (with the color written to it)
  96. ///
  97. inline QTextStream& operator<<(QTextStream &os, const ColorRgbScalar& color)
  98. {
  99. os << "{"
  100. << static_cast<unsigned>(color.red) << ","
  101. << static_cast<unsigned>(color.green) << ","
  102. << static_cast<unsigned>(color.blue)
  103. << "}";
  104. return os;
  105. }
  106. /// Compare operator to check if a color is 'equal' to another color
  107. inline bool operator==(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
  108. {
  109. return lhs.red == rhs.red &&
  110. lhs.green == rhs.green &&
  111. lhs.blue == rhs.blue;
  112. }
  113. /// Compare operator to check if a color is 'smaller' than another color
  114. inline bool operator<(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
  115. {
  116. return lhs.red < rhs.red &&
  117. lhs.green < rhs.green &&
  118. lhs.blue < rhs.blue;
  119. }
  120. /// Compare operator to check if a color is 'not equal' to another color
  121. inline bool operator!=(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
  122. {
  123. return !(lhs == rhs);
  124. }
  125. /// Compare operator to check if a color is 'smaller' than or 'equal' to another color
  126. inline bool operator<=(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
  127. {
  128. return lhs.red <= rhs.red &&
  129. lhs.green <= rhs.green &&
  130. lhs.blue <= rhs.blue;
  131. }
  132. /// Compare operator to check if a color is 'greater' to another color
  133. inline bool operator>(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
  134. {
  135. return lhs.red > rhs.red &&
  136. lhs.green > rhs.green &&
  137. lhs.blue > rhs.blue;
  138. }
  139. /// Compare operator to check if a color is 'greater' than or 'equal' to another color
  140. inline bool operator>=(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
  141. {
  142. return lhs.red >= rhs.red &&
  143. lhs.green >= rhs.green &&
  144. lhs.blue >= rhs.blue;
  145. }
  146. inline ColorRgbScalar& operator+=(ColorRgbScalar& lhs, const ColorRgbScalar& rhs)
  147. {
  148. lhs.red += rhs.red;
  149. lhs.green += rhs.green;
  150. lhs.blue += rhs.blue;
  151. return lhs;
  152. }
  153. inline ColorRgbScalar operator+(ColorRgbScalar lhs, const ColorRgbScalar rhs)
  154. {
  155. lhs += rhs;
  156. return lhs;
  157. }
  158. inline ColorRgbScalar& operator/=(ColorRgbScalar& lhs, int count)
  159. {
  160. if (count > 0)
  161. {
  162. lhs.red /= count;
  163. lhs.green /= count;
  164. lhs.blue /= count;
  165. }
  166. return lhs;
  167. }
  168. inline ColorRgbScalar operator/(ColorRgbScalar lhs, int count)
  169. {
  170. lhs /= count;
  171. return lhs;
  172. }
  173. #endif // COLORRGBSCALAR_H