Refactor clamped and rounded linear interpolation

This commit is contained in:
BlackMark 2020-07-17 14:54:31 +02:00
parent e4f18911f9
commit 17a4890fdc
4 changed files with 22 additions and 14 deletions

View File

@ -123,8 +123,7 @@ void MainWindow::setupMonitorsTab()
[this](int state) { m_ui.monitorOverallBrightnessSlider->setEnabled(state == Qt::CheckState::Checked); });
connect(m_ui.monitorBrightnessSlider, &QSlider::valueChanged, [this](int value) {
const auto mappedBrightness = utils::map(value, 0, 100, 0.f, 1.f);
const auto newBrightness = std::clamp(mappedBrightness, 0.f, 1.f);
const auto newBrightness = utils::map(value, 0, 100, 0.f, 1.f);
qDebug(ltr("Overriding brightness with %1").arg(newBrightness));
m_brightness = newBrightness;
});
@ -183,9 +182,9 @@ void MainWindow::setupCallbackTimer()
void MainWindow::updateCurrentMonitorGUI(int index)
{
const auto brightness = std::clamp<int>(utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100), 0, 100);
const auto minimum = std::clamp<int>(utils::map(m_monitors[index].minBrightness, 0.f, 1.f, 0, 100), 0, 100);
const auto maximum = std::clamp<int>(utils::map(m_monitors[index].maxBrightness, 0.f, 1.f, 0, 100), 0, 100);
const auto brightness = utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100);
const auto minimum = utils::map(m_monitors[index].minBrightness, 0.f, 1.f, 0, 100);
const auto maximum = utils::map(m_monitors[index].maxBrightness, 0.f, 1.f, 0, 100);
m_ui.monitorMinBrightnessSlider->setValue(minimum);
m_ui.monitorMinBrightnessValueLabel->setText(QString("%1%").arg(minimum, 3, 10));
@ -205,7 +204,6 @@ void MainWindow::updateState()
bool sensorError = false;
bool monitorError = false;
size_t iconBrightness = 0;
const auto getAverageSensorValue = [this](float& brightness) {
if(m_sensors.empty()) {
@ -246,8 +244,7 @@ void MainWindow::updateState()
sensorError = true;
}
const auto roundedBrightness = std::round(utils::map(m_brightness, 0.f, 1.f, 0, 8));
iconBrightness = std::clamp<size_t>(roundedBrightness, 0, 8);
const auto iconBrightness = utils::map(m_brightness, 0.f, 1.f, size_t{0}, size_t{8});
if(!setAllMonitorsBrightness(m_brightness)) {
enumMonitors();

View File

@ -64,8 +64,7 @@ bool Monitor::setBrightness(float percentage)
return false;
}
const auto mappedBrightness = utils::map(percentage, 0.f, 1.f, m_brightnessRange->first, m_brightnessRange->second);
const auto newBrightness = std::clamp<DWORD>(std::round(mappedBrightness), m_brightnessRange->first, m_brightnessRange->second);
const auto newBrightness = utils::map(percentage, 0.f, 1.f, m_brightnessRange->first, m_brightnessRange->second);
if(!SetMonitorBrightness(m_physicalMonitor.handle, newBrightness)) {
qCritical(ltr("Failed to set brightness of monitor '%1' with handle '0x%2' and physical handle '0x%3', error: %4")
@ -110,7 +109,7 @@ float Monitor::getBrightness()
m_brightnessRange = std::pair{minBrightness, maxBrightness};
m_errorOccurred = false;
return std::clamp(utils::map(brightness, minBrightness, maxBrightness, 0.f, 1.f), 0.f, 1.f);
return utils::map(brightness, minBrightness, maxBrightness, 0.f, 1.f);
}
QString Monitor::getName() const

View File

@ -47,8 +47,7 @@ std::vector<float> Sensor::readValues()
std::vector<float> normalizedValues;
for(const auto& value: values) {
const auto mappedValue = utils::map(value, m_range->first, m_range->second, 0.f, 1.f);
normalizedValues.push_back(std::clamp(mappedValue, 0.f, 1.f));
normalizedValues.push_back(utils::map(value, m_range->first, m_range->second, 0.f, 1.f));
}
return normalizedValues;

View File

@ -1,17 +1,30 @@
#pragma once
#include <algorithm>
#include <array>
#include <type_traits>
#include <cmath>
#include <cstddef>
namespace utils {
template<typename InType, typename OutType>
auto map(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
auto interpolate(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
{
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
template<typename InType, typename OutType>
auto map(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
{
if constexpr(std::is_integral_v<OutType>) {
return std::clamp(static_cast<OutType>(std::round(interpolate(value, inMin, inMax, outMin, outMax))), outMin, outMax);
}
return std::clamp(static_cast<OutType>(interpolate(value, inMin, inMax, outMin, outMax)), outMin, outMax);
}
template<typename>
struct array_size;