plugins/nightlight: Ensure the target temperature remains within reasonable bounds

The interpolated temperature can get out of the bounds of [target1, target2]
if the current time is slightly earlier than m_prev.first, which can be
unexpected. This change addresses that by adding a relevant guard.
This commit is contained in:
Vlad Zahorodnii 2024-06-13 15:51:45 +03:00
parent 21a45c2700
commit f46174453d

View file

@ -609,16 +609,18 @@ int NightLightManager::currentTargetTemp() const
const QDateTime todayNow = QDateTime::currentDateTime();
auto f = [this, todayNow](int target1, int target2) {
if (todayNow < m_prev.second) {
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;
} else {
if (todayNow <= m_prev.first) {
return target1;
}
if (todayNow >= m_prev.second) {
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;
};
if (daylight()) {