From 17a4890fdcd9adac77e17de14d98e59020aac67e Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 17 Jul 2020 14:54:31 +0200 Subject: [PATCH] Refactor clamped and rounded linear interpolation --- AdaptiveBrightness/main_window.cpp | 13 +++++-------- AdaptiveBrightness/monitor_win.cpp | 5 ++--- AdaptiveBrightness/sensor.cpp | 3 +-- AdaptiveBrightness/utils.hpp | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/AdaptiveBrightness/main_window.cpp b/AdaptiveBrightness/main_window.cpp index 53fbfb6..bcb0c13 100644 --- a/AdaptiveBrightness/main_window.cpp +++ b/AdaptiveBrightness/main_window.cpp @@ -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(utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100), 0, 100); - const auto minimum = std::clamp(utils::map(m_monitors[index].minBrightness, 0.f, 1.f, 0, 100), 0, 100); - const auto maximum = std::clamp(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(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(); diff --git a/AdaptiveBrightness/monitor_win.cpp b/AdaptiveBrightness/monitor_win.cpp index fd1e6c8..b2490de 100644 --- a/AdaptiveBrightness/monitor_win.cpp +++ b/AdaptiveBrightness/monitor_win.cpp @@ -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(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 diff --git a/AdaptiveBrightness/sensor.cpp b/AdaptiveBrightness/sensor.cpp index 904c40f..906bd43 100644 --- a/AdaptiveBrightness/sensor.cpp +++ b/AdaptiveBrightness/sensor.cpp @@ -47,8 +47,7 @@ std::vector Sensor::readValues() std::vector 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; diff --git a/AdaptiveBrightness/utils.hpp b/AdaptiveBrightness/utils.hpp index 918b2d0..3563e81 100644 --- a/AdaptiveBrightness/utils.hpp +++ b/AdaptiveBrightness/utils.hpp @@ -1,17 +1,30 @@ #pragma once +#include #include +#include +#include #include namespace utils { template -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 +auto map(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax) +{ + if constexpr(std::is_integral_v) { + return std::clamp(static_cast(std::round(interpolate(value, inMin, inMax, outMin, outMax))), outMin, outMax); + } + + return std::clamp(static_cast(interpolate(value, inMin, inMax, outMin, outMax)), outMin, outMax); +} + template struct array_size;