color_utils.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * \file
  3. *
  4. * \author Mattia Basaglia
  5. *
  6. * \copyright Copyright (C) 2013-2020 Mattia Basaglia
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef COLOR_UTILS_HPP
  23. #define COLOR_UTILS_HPP
  24. #include <QColor>
  25. #include <QPoint>
  26. #include <qmath.h>
  27. #include "QtColorWidgets/colorwidgets_global.hpp"
  28. namespace color_widgets {
  29. namespace utils {
  30. QCP_EXPORT inline qreal color_chromaF(const QColor& c)
  31. {
  32. qreal max = qMax(c.redF(), qMax(c.greenF(), c.blueF()));
  33. qreal min = qMin(c.redF(), qMin(c.greenF(), c.blueF()));
  34. return max - min;
  35. }
  36. QCP_EXPORT inline qreal color_lumaF(const QColor& c)
  37. {
  38. return 0.30 * c.redF() + 0.59 * c.greenF() + 0.11 * c.blueF();
  39. }
  40. QCP_EXPORT QColor color_from_lch(qreal hue, qreal chroma, qreal luma, qreal alpha = 1 );
  41. QCP_EXPORT inline QColor rainbow_lch(qreal hue)
  42. {
  43. return color_from_lch(hue,1,1);
  44. }
  45. QCP_EXPORT inline QColor rainbow_hsv(qreal hue)
  46. {
  47. return QColor::fromHsvF(hue,1,1);
  48. }
  49. QCP_EXPORT inline qreal color_lightnessF(const QColor& c)
  50. {
  51. return ( qMax(c.redF(),qMax(c.greenF(),c.blueF())) +
  52. qMin(c.redF(),qMin(c.greenF(),c.blueF())) ) / 2;
  53. }
  54. QCP_EXPORT inline qreal color_HSL_saturationF(const QColor& col)
  55. {
  56. qreal c = color_chromaF(col);
  57. qreal l = color_lightnessF(col);
  58. if ( qFuzzyCompare(l+1,1) || qFuzzyCompare(l+1,2) )
  59. return 0;
  60. return c / (1-qAbs(2*l-1));
  61. }
  62. QCP_EXPORT QColor color_from_hsl(qreal hue, qreal sat, qreal lig, qreal alpha = 1 );
  63. QCP_EXPORT QColor get_screen_color(const QPoint &global_pos);
  64. } // namespace utils
  65. } // namespace color_widgets
  66. #endif // COLOR_UTILS_HPP