ColorRgbw.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. // STL includes
  3. #include <cstdint>
  4. #include <iostream>
  5. ///
  6. /// Plain-Old-Data structure containing the red-green-blue color specification. Size of the
  7. /// structure is exactly 3-bytes for easy writing to led-device
  8. ///
  9. struct ColorRgbw
  10. {
  11. /// The red color channel
  12. uint8_t red;
  13. /// The green color channel
  14. uint8_t green;
  15. /// The blue color channel
  16. uint8_t blue;
  17. /// The white color channel
  18. uint8_t white;
  19. /// 'Black' RgbColor (0, 0, 0, 0)
  20. static const ColorRgbw BLACK;
  21. /// 'Red' RgbColor (255, 0, 0, 0)
  22. static const ColorRgbw RED;
  23. /// 'Green' RgbColor (0, 255, 0, 0)
  24. static const ColorRgbw GREEN;
  25. /// 'Blue' RgbColor (0, 0, 255, 0)
  26. static const ColorRgbw BLUE;
  27. /// 'Yellow' RgbColor (255, 255, 0, 0)
  28. static const ColorRgbw YELLOW;
  29. /// 'White' RgbColor (0, 0, 0, 255)
  30. static const ColorRgbw WHITE;
  31. };
  32. /// Assert to ensure that the size of the structure is 'only' 4 bytes
  33. static_assert(sizeof(ColorRgbw) == 4, "Incorrect size of ColorRgbw");
  34. ///
  35. /// Stream operator to write ColorRgb to an outputstream (format "'{'[red]','[green]','[blue]'}'")
  36. ///
  37. /// @param os The output stream
  38. /// @param color The color to write
  39. /// @return The output stream (with the color written to it)
  40. ///
  41. inline std::ostream& operator<<(std::ostream& os, const ColorRgbw& color)
  42. {
  43. os << "{"
  44. << color.red << ","
  45. << color.green << ","
  46. << color.blue << ","
  47. << color.white <<
  48. "}";
  49. return os;
  50. }
  51. /// Compare operator to check if a color is 'equal' than another color
  52. inline bool operator==(const ColorRgbw & lhs, const ColorRgbw & rhs)
  53. {
  54. return lhs.red == rhs.red &&
  55. lhs.green == rhs.green &&
  56. lhs.blue == rhs.blue &&
  57. lhs.white == rhs.white;
  58. }
  59. /// Compare operator to check if a color is 'smaller' than another color
  60. inline bool operator<(const ColorRgbw & lhs, const ColorRgbw & rhs)
  61. {
  62. return lhs.red < rhs.red &&
  63. lhs.green < rhs.green &&
  64. lhs.blue < rhs.blue &&
  65. lhs.white < rhs.white;
  66. }
  67. /// Compare operator to check if a color is 'smaller' than or 'equal' to another color
  68. inline bool operator<=(const ColorRgbw & lhs, const ColorRgbw & rhs)
  69. {
  70. return lhs < rhs || lhs == rhs;
  71. }