Refactor clamped and rounded linear interpolation
This commit is contained in:
parent
e4f18911f9
commit
17a4890fdc
@ -123,8 +123,7 @@ void MainWindow::setupMonitorsTab()
|
|||||||
[this](int state) { m_ui.monitorOverallBrightnessSlider->setEnabled(state == Qt::CheckState::Checked); });
|
[this](int state) { m_ui.monitorOverallBrightnessSlider->setEnabled(state == Qt::CheckState::Checked); });
|
||||||
|
|
||||||
connect(m_ui.monitorBrightnessSlider, &QSlider::valueChanged, [this](int value) {
|
connect(m_ui.monitorBrightnessSlider, &QSlider::valueChanged, [this](int value) {
|
||||||
const auto mappedBrightness = utils::map(value, 0, 100, 0.f, 1.f);
|
const auto newBrightness = utils::map(value, 0, 100, 0.f, 1.f);
|
||||||
const auto newBrightness = std::clamp(mappedBrightness, 0.f, 1.f);
|
|
||||||
qDebug(ltr("Overriding brightness with %1").arg(newBrightness));
|
qDebug(ltr("Overriding brightness with %1").arg(newBrightness));
|
||||||
m_brightness = newBrightness;
|
m_brightness = newBrightness;
|
||||||
});
|
});
|
||||||
@ -183,9 +182,9 @@ void MainWindow::setupCallbackTimer()
|
|||||||
|
|
||||||
void MainWindow::updateCurrentMonitorGUI(int index)
|
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 brightness = utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100);
|
||||||
const auto minimum = std::clamp<int>(utils::map(m_monitors[index].minBrightness, 0.f, 1.f, 0, 100), 0, 100);
|
const auto minimum = utils::map(m_monitors[index].minBrightness, 0.f, 1.f, 0, 100);
|
||||||
const auto maximum = std::clamp<int>(utils::map(m_monitors[index].maxBrightness, 0.f, 1.f, 0, 100), 0, 100);
|
const auto maximum = utils::map(m_monitors[index].maxBrightness, 0.f, 1.f, 0, 100);
|
||||||
|
|
||||||
m_ui.monitorMinBrightnessSlider->setValue(minimum);
|
m_ui.monitorMinBrightnessSlider->setValue(minimum);
|
||||||
m_ui.monitorMinBrightnessValueLabel->setText(QString("%1%").arg(minimum, 3, 10));
|
m_ui.monitorMinBrightnessValueLabel->setText(QString("%1%").arg(minimum, 3, 10));
|
||||||
@ -205,7 +204,6 @@ void MainWindow::updateState()
|
|||||||
|
|
||||||
bool sensorError = false;
|
bool sensorError = false;
|
||||||
bool monitorError = false;
|
bool monitorError = false;
|
||||||
size_t iconBrightness = 0;
|
|
||||||
|
|
||||||
const auto getAverageSensorValue = [this](float& brightness) {
|
const auto getAverageSensorValue = [this](float& brightness) {
|
||||||
if(m_sensors.empty()) {
|
if(m_sensors.empty()) {
|
||||||
@ -246,8 +244,7 @@ void MainWindow::updateState()
|
|||||||
sensorError = true;
|
sensorError = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto roundedBrightness = std::round(utils::map(m_brightness, 0.f, 1.f, 0, 8));
|
const auto iconBrightness = utils::map(m_brightness, 0.f, 1.f, size_t{0}, size_t{8});
|
||||||
iconBrightness = std::clamp<size_t>(roundedBrightness, 0, 8);
|
|
||||||
|
|
||||||
if(!setAllMonitorsBrightness(m_brightness)) {
|
if(!setAllMonitorsBrightness(m_brightness)) {
|
||||||
enumMonitors();
|
enumMonitors();
|
||||||
|
@ -64,8 +64,7 @@ bool Monitor::setBrightness(float percentage)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto mappedBrightness = utils::map(percentage, 0.f, 1.f, m_brightnessRange->first, m_brightnessRange->second);
|
const auto newBrightness = 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);
|
|
||||||
|
|
||||||
if(!SetMonitorBrightness(m_physicalMonitor.handle, newBrightness)) {
|
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")
|
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_brightnessRange = std::pair{minBrightness, maxBrightness};
|
||||||
m_errorOccurred = false;
|
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
|
QString Monitor::getName() const
|
||||||
|
@ -47,8 +47,7 @@ std::vector<float> Sensor::readValues()
|
|||||||
std::vector<float> normalizedValues;
|
std::vector<float> normalizedValues;
|
||||||
|
|
||||||
for(const auto& value: values) {
|
for(const auto& value: values) {
|
||||||
const auto mappedValue = utils::map(value, m_range->first, m_range->second, 0.f, 1.f);
|
normalizedValues.push_back(utils::map(value, m_range->first, m_range->second, 0.f, 1.f));
|
||||||
normalizedValues.push_back(std::clamp(mappedValue, 0.f, 1.f));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return normalizedValues;
|
return normalizedValues;
|
||||||
|
@ -1,17 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
|
||||||
template<typename InType, typename OutType>
|
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;
|
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>
|
template<typename>
|
||||||
struct array_size;
|
struct array_size;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user