From cea7f128a55a3f93df956ad21eb51634f851cc70 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 19 May 2022 02:01:34 +0300 Subject: [PATCH] Added util for calculating of intermediate gradient value. --- CMakeLists.txt | 1 + ui/effects/gradient.cpp | 28 ++++++++++++++++++++++++++++ ui/effects/gradient.h | 4 ++++ 3 files changed, 33 insertions(+) create mode 100644 ui/effects/gradient.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4448b46..9df509c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ PRIVATE ui/effects/cross_line.h ui/effects/fade_animation.cpp ui/effects/fade_animation.h + ui/effects/gradient.cpp ui/effects/gradient.h ui/effects/numbers_animation.cpp ui/effects/numbers_animation.h diff --git a/ui/effects/gradient.cpp b/ui/effects/gradient.cpp new file mode 100644 index 0000000..55b5dda --- /dev/null +++ b/ui/effects/gradient.cpp @@ -0,0 +1,28 @@ +// This file is part of Desktop App Toolkit, +// a set of libraries for developing nice desktop applications. +// +// For license and copyright information please follow this link: +// https://github.com/desktop-app/legal/blob/master/LEGAL +// +#include "ui/effects/gradient.h" + +namespace anim { + +QColor gradient_color_at(const QGradient &gradient, float64 ratio) { + const auto &stops = gradient.stops(); + + for (auto i = 1; i < stops.size(); i++) { + const auto currentPoint = stops[i].first; + const auto previousPoint = stops[i - 1].first; + + if ((ratio <= currentPoint) && (ratio >= previousPoint)) { + return anim::color( + stops[i - 1].second, + stops[i].second, + (ratio - previousPoint) / (currentPoint - previousPoint)); + } + } + return QColor(); +} + +} // namespace anim diff --git a/ui/effects/gradient.h b/ui/effects/gradient.h index 1c882f0..ddf6949 100644 --- a/ui/effects/gradient.h +++ b/ui/effects/gradient.h @@ -14,6 +14,10 @@ namespace anim { +[[nodiscard]] QColor gradient_color_at( + const QGradient &gradient, + float64 ratio); + struct gradient_colors { explicit gradient_colors(QColor color) { stops.push_back({ 0., color });