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.
This commit is contained in:
Vlad Zahorodnii 2024-06-13 21:57:00 +03:00
parent c2141dc4ef
commit ec86640f44

View file

@ -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()) {