ColorSys.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef COLORSYS_H
  2. #define COLORSYS_H
  3. // STL includes
  4. #include <cstdint>
  5. ///
  6. /// Color transformation to adjust the saturation and luminance of a RGB color value
  7. ///
  8. class ColorSys
  9. {
  10. public:
  11. ///
  12. /// Translates an RGB (red, green, blue) color to an HSL (hue, saturation, luminance) color
  13. ///
  14. /// @param[in] red The red RGB-component
  15. /// @param[in] green The green RGB-component
  16. /// @param[in] blue The blue RGB-component
  17. /// @param[out] hue The hue HSL-component
  18. /// @param[out] saturation The saturation HSL-component
  19. /// @param[out] luminance The luminance HSL-component
  20. ///
  21. static void rgb2hsl(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, float & saturation, float & luminance);
  22. ///
  23. /// Translates an HSL (hue, saturation, luminance) color to an RGB (red, green, blue) color
  24. ///
  25. /// @param[in] hue The hue HSL-component
  26. /// @param[in] saturation The saturation HSL-component
  27. /// @param[in] luminance The luminance HSL-component
  28. /// @param[out] red The red RGB-component
  29. /// @param[out] green The green RGB-component
  30. /// @param[out] blue The blue RGB-component
  31. ///
  32. static void hsl2rgb(uint16_t hue, float saturation, float luminance, uint8_t & red, uint8_t & green, uint8_t & blue);
  33. ///
  34. /// Translates an RGB (red, green, blue) color to an HSV (hue, saturation, value) color
  35. ///
  36. /// @param[in] red The red RGB-component
  37. /// @param[in] green The green RGB-component
  38. /// @param[in] blue The blue RGB-component
  39. /// @param[out] hue The hue HSV-component
  40. /// @param[out] saturation The saturation HSV-component
  41. /// @param[out] value The value HSV-component
  42. ///
  43. /// @note Integer version of the conversion are faster, but a little less accurate all values
  44. /// are unsigned 8 bit values and scaled between 0 and 255 except for the hue which is a 16 bit
  45. /// number and scaled between 0 and 360
  46. ///
  47. static void rgb2hsv(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, uint8_t & saturation, uint8_t & value);
  48. ///
  49. /// Translates an HSV (hue, saturation, value) color to an RGB (red, green, blue) color
  50. ///
  51. /// @param[in] hue The hue HSV-component
  52. /// @param[in] saturation The saturation HSV-component
  53. /// @param[in] value The value HSV-component
  54. /// @param[out] red The red RGB-component
  55. /// @param[out] green The green RGB-component
  56. /// @param[out] blue The blue RGB-component
  57. ///
  58. /// @note Integer version of the conversion are faster, but a little less accurate all values
  59. /// are unsigned 8 bit values and scaled between 0 and 255 except for the hue which is a 16 bit
  60. /// number and scaled between 0 and 360
  61. ///
  62. static void hsv2rgb(uint16_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue);
  63. ///
  64. /// Translates a YUV (luminance, chrominance, chrominance) color to an RGB (red, green, blue) color
  65. ///
  66. /// @param[in] y The luminance YUV-component
  67. /// @param[in] u The chrominance YUV-component
  68. /// @param[in] v The chrominance YUV-component
  69. /// @param[out] red The red RGB-component
  70. /// @param[out] green The green RGB-component
  71. /// @param[out] blue The blue RGB-component
  72. static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, uint8_t & b);
  73. ///
  74. /// Translates an RGB (red, green, blue) color to an Okhsv (hue, saturation, value) color
  75. ///
  76. /// @param[in] red The red RGB-component
  77. /// @param[in] green The green RGB-component
  78. /// @param[in] blue The blue RGB-component
  79. /// @param[out] hue The hue Okhsv-component
  80. /// @param[out] saturation The saturation Okhsv-component
  81. /// @param[out] value The value Okhsv-component
  82. ///
  83. /// @note See https://bottosson.github.io/posts/colorpicker/#okhsv
  84. ///
  85. static void rgb2okhsv(uint8_t red, uint8_t green, uint8_t blue, double & hue, double & saturation, double & value);
  86. ///
  87. /// Translates an Okhsv (hue, saturation, value) color to an RGB (red, green, blue) color
  88. ///
  89. /// @param[in] hue The hue Okhsv-component
  90. /// @param[in] saturation The saturation Okhsv-component
  91. /// @param[in] value The value Okhsv-component
  92. /// @param[out] red The red RGB-component
  93. /// @param[out] green The green RGB-component
  94. /// @param[out] blue The blue RGB-component
  95. ///
  96. /// @note See https://bottosson.github.io/posts/colorpicker/#okhsv
  97. ///
  98. static void okhsv2rgb(double hue, double saturation, double value, uint8_t & red, uint8_t & green, uint8_t & blue);
  99. template <typename Pixel_T>
  100. static double rgb_euclidean(Pixel_T p1, Pixel_T p2)
  101. {
  102. double val = sqrt(
  103. (p1.red - p2.red) * (p1.red - p2.red) +
  104. (p1.green - p2.green) * (p1.green - p2.green) +
  105. (p1.blue - p2.blue) * (p1.blue - p2.blue)
  106. );
  107. return val;
  108. }
  109. };
  110. #endif // COLORSYS_H