diff --git a/ui/effects/animation_value.h b/ui/effects/animation_value.h index 0a7d226..dc5d530 100644 --- a/ui/effects/animation_value.h +++ b/ui/effects/animation_value.h @@ -11,6 +11,7 @@ #include "ui/style/style_core.h" #include +#include namespace anim { @@ -354,4 +355,53 @@ void DrawStaticLoading( QPen pen, QBrush brush = Qt::NoBrush); +class linear_gradient { +public: + linear_gradient( + std::vector colors_from, + std::vector colors_to, + QPointF point1, + QPointF point2) + : _colors_from(colors_from) + , _colors_to(colors_to) + , _point1(point1) + , _point2(point2) + , _gradient_from(gradient(colors_from)) + , _gradient_to(gradient(colors_to)) { + Expects(colors_from.size() == colors_to.size()); + } + + QLinearGradient gradient(float64 b_ratio) const { + if (b_ratio == 0.) { + return _gradient_from; + } else if (b_ratio == 1.) { + return _gradient_to; + } + auto colors = std::vector(_colors_to.size()); + for (auto i = 0; i < colors.size(); i++) { + colors[i] = color(_colors_from[i], _colors_to[i], b_ratio); + } + return gradient(colors); + } + +private: + QLinearGradient gradient(const std::vector &colors) const { + auto gradient = QLinearGradient(_point1, _point2); + const auto size = colors.size(); + for (auto i = 0; i < size; i++) { + gradient.setColorAt(i / (size - 1), colors[i]); + } + return gradient; + } + + std::vector _colors_from; + std::vector _colors_to; + QPointF _point1; + QPointF _point2; + + QLinearGradient _gradient_from; + QLinearGradient _gradient_to; + }; + +} // namespace anim