ColorBgr.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 ColorBgr
  10. {
  11. /// The blue color channel
  12. uint8_t blue;
  13. /// The green color channel
  14. uint8_t green;
  15. /// The red color channel
  16. uint8_t red;
  17. /// 'Black' RgbColor (0, 0, 0)
  18. static const ColorBgr BLACK;
  19. /// 'Red' RgbColor (255, 0, 0)
  20. static const ColorBgr RED;
  21. /// 'Green' RgbColor (0, 255, 0)
  22. static const ColorBgr GREEN;
  23. /// 'Blue' RgbColor (0, 0, 255)
  24. static const ColorBgr BLUE;
  25. /// 'Yellow' RgbColor (255, 255, 0)
  26. static const ColorBgr YELLOW;
  27. /// 'White' RgbColor (255, 255, 255)
  28. static const ColorBgr WHITE;
  29. };
  30. /// Assert to ensure that the size of the structure is 'only' 3 bytes
  31. static_assert(sizeof(ColorBgr) == 3, "Incorrect size of ColorBgr");
  32. ///
  33. /// Stream operator to write ColorRgb to an outputstream (format "'{'[red]','[green]','[blue]'}'")
  34. ///
  35. /// @param os The output stream
  36. /// @param color The color to write
  37. /// @return The output stream (with the color written to it)
  38. ///
  39. inline std::ostream& operator<<(std::ostream& os, const ColorBgr& color)
  40. {
  41. os << "{"
  42. << color.red << ","
  43. << color.green << ","
  44. << color.blue
  45. << "}";
  46. return os;
  47. }
  48. /// Compare operator to check if a color is 'equal' to another color
  49. inline bool operator==(const ColorBgr & lhs, const ColorBgr & rhs)
  50. {
  51. return (lhs.red == rhs.red) &&
  52. (lhs.green == rhs.green) &&
  53. (lhs.blue == rhs.blue);
  54. }
  55. /// Compare operator to check if a color is 'smaller' than another color
  56. inline bool operator<(const ColorBgr & lhs, const ColorBgr & rhs)
  57. {
  58. return (lhs.red < rhs.red) &&
  59. (lhs.green < rhs.green) &&
  60. (lhs.blue < rhs.blue);
  61. }
  62. /// Compare operator to check if a color is 'smaller' than or 'equal' to another color
  63. inline bool operator<=(const ColorBgr & lhs, const ColorBgr & rhs)
  64. {
  65. return lhs < rhs || lhs == rhs;
  66. }