gradient_helper.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 GRADIENT_HELPER_HPP
  23. #define GRADIENT_HELPER_HPP
  24. #include "colorwidgets_global.hpp"
  25. #include <QGradient>
  26. namespace color_widgets {
  27. inline QColor blendColors(const QColor& a, const QColor& b, qreal ratio)
  28. {
  29. return QColor::fromRgbF(
  30. a.redF() * (1-ratio) + b.redF() * ratio,
  31. a.greenF() * (1-ratio) + b.greenF() * ratio,
  32. a.blueF() * (1-ratio) + b.blueF() * ratio,
  33. a.alphaF() * (1-ratio) + b.alphaF() * ratio
  34. );
  35. }
  36. /**
  37. * \brief Get an insertion point in the gradient
  38. * \param gradient Gradient stops to look into (must be properly set up)
  39. * \param factor Value in [0, 1] to get the color for
  40. * \return A pair whose first element is the index to insert the new value at, and a GradientStop
  41. */
  42. inline QPair<int, QGradientStop> Q_DECL_EXPORT gradientBlendedColorInsert(const QGradientStops& gradient, qreal factor)
  43. {
  44. if ( gradient.empty() )
  45. return {0, {0, QColor()}};
  46. if ( gradient.size() == 1 || factor <= 0 )
  47. return {0, gradient.front()};
  48. int i = 0;
  49. QGradientStop s1;
  50. for ( auto s2 : gradient )
  51. {
  52. if ( factor < s2.first )
  53. {
  54. qreal ratio = (factor - s1.first) / (s2.first - s1.first);
  55. return {i, {factor, blendColors(s1.second, s2.second, ratio)}};
  56. }
  57. s1 = s2;
  58. ++i;
  59. }
  60. return {gradient.size(), gradient.back()};
  61. }
  62. /**
  63. * \brief Returns a color in the gradient
  64. * \param gradient Gradient stops to look into (must be properly set up)
  65. * \param factor Value in [0, 1] to get the color for
  66. */
  67. inline QColor Q_DECL_EXPORT gradientBlendedColor(const QGradientStops& gradient, qreal factor)
  68. {
  69. return gradientBlendedColorInsert(gradient, factor).second.second;
  70. }
  71. /**
  72. * \brief Returns a color in the gradient
  73. * \param gradient Gradient to look into
  74. * \param factor Value in [0, 1] to get the color for
  75. */
  76. inline QColor Q_DECL_EXPORT gradientBlendedColor(const QGradient& gradient, qreal factor)
  77. {
  78. return gradientBlendedColor(gradient.stops(), factor);
  79. }
  80. } // namespace color_widgets
  81. #endif // GRADIENT_HELPER_HPP