From ec86640f44fbdacd19f78c0bf44e9f9f82eb00a7 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Thu, 13 Jun 2024 21:57:00 +0300 Subject: [PATCH] plugins/nightlight: Tidy temperature lerp code This change simplifies the color temperature interpolation code by doing the following: - compute a progress value so 0 corresponds to target1, i.e. the beginning of the transition, and 1 corresponds to target2, the end of the transition, instead of vice versa - and use std::lerp() function Rounding to a multiple of ten is not needed, it is for nicer digits in the applet, but if the applet really cares about it, it could perform such rounding on its own. In the night light manager, it's better to avoid rounding the interpolated values because that can result in the final temperature getting outside of the [target1, target2] interval. --- src/plugins/nightlight/nightlightmanager.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/plugins/nightlight/nightlightmanager.cpp b/src/plugins/nightlight/nightlightmanager.cpp index a711dc5809..7aab0746cb 100644 --- a/src/plugins/nightlight/nightlightmanager.cpp +++ b/src/plugins/nightlight/nightlightmanager.cpp @@ -608,7 +608,7 @@ int NightLightManager::currentTargetTemp() const const QDateTime todayNow = QDateTime::currentDateTime(); - auto f = [this, todayNow](int target1, int target2) { + auto f = [this, todayNow](int target1, int target2) -> int { if (todayNow <= m_prev.first) { return target1; } @@ -616,11 +616,8 @@ int NightLightManager::currentTargetTemp() const return target2; } - double residueQuota = todayNow.msecsTo(m_prev.second) / (double)m_prev.first.msecsTo(m_prev.second); - double ret = (int)((1. - residueQuota) * (double)target2 + residueQuota * (double)target1); - // remove single digits - ret = ((int)(0.1 * ret)) * 10; - return (int)ret; + const double progress = double(m_prev.first.msecsTo(todayNow)) / m_prev.first.msecsTo(m_prev.second); + return std::lerp(target1, target2, progress); }; if (daylight()) {