Skip to content

Commit 6097eb6

Browse files
authored
Fix #1899, #1922 (#1933)
1 parent a285ac1 commit 6097eb6

File tree

6 files changed

+48
-52
lines changed

6 files changed

+48
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Hue Bridge - Alternate certificate support
2020
- Linux: New DRM/KMS screen grabber with plane-based capture - not feature complete yet
2121
- Logging/Tracing: Introduced qlogging categories to enable dynamic tracing
22+
- Home Assistant: Dynamically set brightness for higher dynamic range (#1922)
2223

2324
---
2425

@@ -41,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4142
- LED-devices are not retrying to establish connectivity, if supported by the device
4243
- LED-devices are resolving IP-addresses for API and UDP two times in sequence
4344
- LED-device updates queue up and let Hyperion crash (#1887)
45+
- The color of the backlight threshold is green, not white/gray (#1899)
4446
- Install - Ubuntu 25.10 unable to install due to libcec package (#1934)
4547

4648
---

include/utils/RgbTransform.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RgbTransform
2727
/// @param brightnessHigh The used higher brightness
2828
/// @param temeprature The given color temperature (in Kelvin)
2929
///
30-
RgbTransform(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature);
30+
RgbTransform(double gammaR, double gammaG, double gammaB, int backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature);
3131

3232
/// @return The current red gamma value
3333
double getGammaR() const;
@@ -47,7 +47,7 @@ class RgbTransform
4747
int getBacklightThreshold() const;
4848

4949
/// @param backlightThreshold New lower brightness
50-
void setBacklightThreshold(double backlightThreshold);
50+
void setBacklightThreshold(int backlightThreshold);
5151

5252
/// @return The current state
5353
bool getBacklightColored() const;
@@ -93,7 +93,7 @@ class RgbTransform
9393
///
9494
/// @note The values are updated in place.
9595
///
96-
void applyGamma(uint8_t & red, uint8_t & green, uint8_t & blue);
96+
void applyGamma(uint8_t & red, uint8_t & green, uint8_t & blue) const;
9797

9898
///
9999
/// Apply Backlight the the given RGB values.
@@ -123,7 +123,7 @@ class RgbTransform
123123
/// @param brightnessCompensation The used brightness compensation
124124
/// @param temeprature apply the given color temperature (in Kelvin)
125125
///
126-
void init(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature);
126+
void init(double gammaR, double gammaG, double gammaB, int backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature);
127127

128128
/// (re)-initilize the color mapping
129129
void initializeMapping(); /// The saturation gain
@@ -133,8 +133,8 @@ class RgbTransform
133133
/// backlight variables
134134
bool _backLightEnabled;
135135
bool _backlightColored;
136-
double _backlightThreshold;
137-
double _sumBrightnessLow;
136+
int _backlightThreshold;
137+
uint8_t _sumBrightnessLow;
138138

139139
/// gamma variables
140140
double _gammaR;

libsrc/hyperion/MultiColorAdjustment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@ void MultiColorAdjustment::applyAdjustment(std::vector<ColorRgb>& ledColors)
159159
color.blue = OB + RB + GB + BB + CB + MB + YB + WB;
160160

161161
adjustment->_rgbTransform.applyTemperature(color);
162-
adjustment->_rgbTransform.applyBacklight(color.red, color.green, color.green);
162+
adjustment->_rgbTransform.applyBacklight(color.red, color.green, color.blue);
163163
}
164164
}

libsrc/leddevice/dev_net/LedDeviceHomeAssistant.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,6 @@ bool LedDeviceHomeAssistant::powerOff()
401401

402402
int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
403403
{
404-
int retVal = 0;
405-
406404
QJsonObject serviceAttributes{ {ENTITY_ID, QJsonArray::fromStringList(_lightEntityIds)} };
407405
ColorRgb ledValue = ledValues.at(0);
408406

@@ -415,20 +413,18 @@ int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
415413
_restApi->setPath(API_LIGHT_TURN_ON);
416414
serviceAttributes.insert(RGB_COLOR, QJsonArray{ ledValue.red, ledValue.green, ledValue.blue });
417415

418-
int brightness = _brightness;
419-
420-
// Some devices cannot deal with a black color and brightness > 0
421-
if (ledValue == ColorRgb::BLACK)
416+
int brightness;
417+
if (!_isBrightnessOverwrite)
422418
{
423-
brightness = 0;
419+
brightness = qBound(0, qRound(0.2126 * ledValue.red + 0.7152 * ledValue.green + 0.0722 * ledValue.blue), 255);
424420
}
425-
426-
// Add brightness attribute if applicable
427-
if (brightness == 0 || _isBrightnessOverwrite)
421+
else
428422
{
429-
serviceAttributes.insert(BRIGHTNESS, brightness);
423+
brightness = _brightness;
430424
}
431425

426+
serviceAttributes.insert(BRIGHTNESS, brightness);
427+
432428
if (_transitionTime > 0)
433429
{
434430
serviceAttributes.insert(TRANSITION, _transitionTime);
@@ -438,8 +434,8 @@ int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
438434
if (response.error())
439435
{
440436
Warning(_log, "Updating lights failed with error: '%s'", QSTRING_CSTR(response.getErrorReason()));
441-
retVal = -1;
437+
return -1;
442438
}
443439

444-
return retVal;
440+
return 0;
445441
}

libsrc/leddevice/schemas/schema-homeassistant.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"type": "boolean",
5454
"format": "checkbox",
5555
"title": "edt_dev_spec_brightnessOverwrite_title",
56-
"default": true,
56+
"default": false,
5757
"required": true,
5858
"access": "advanced",
5959
"propertyOrder": 6

libsrc/utils/RgbTransform.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
#include <QtCore/qmath.h>
21
#include <utils/RgbTransform.h>
2+
3+
#include <QtMath>
4+
#include <QtGlobal>
5+
36
#include <utils/KelvinToRgb.h>
47

58
RgbTransform::RgbTransform()
69
: RgbTransform::RgbTransform(1.0, 1.0, 1.0, 0.0, false, 100, 100, ColorTemperature::DEFAULT)
710
{
811
}
912

10-
RgbTransform::RgbTransform(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature)
13+
RgbTransform::RgbTransform(double gammaR, double gammaG, double gammaB, int backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature)
1114
: _brightness(brightness)
1215
, _brightnessCompensation(brightnessCompensation)
1316
{
1417
init(gammaR, gammaG, gammaB, backlightThreshold, backlightColored, _brightness, _brightnessCompensation, temperature);
1518
}
1619

17-
void RgbTransform::init(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature)
20+
void RgbTransform::init(double gammaR, double gammaG, double gammaB, int backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature)
1821
{
1922
_backLightEnabled = true;
2023
setGamma(gammaR,gammaG,gammaB);
@@ -64,9 +67,9 @@ void RgbTransform::initializeMapping()
6467
double gammaCorrectedValueB = qPow(normalizedValueB, _gammaB) * UINT8_MAX;
6568

6669
// Clamp values to valid range [0, UINT8_MAX]
67-
quint8 clampedValueR = static_cast<quint8>(qBound(0.0, gammaCorrectedValueR, static_cast<double>(UINT8_MAX)));
68-
quint8 clampedValueG = static_cast<quint8>(qBound(0.0, gammaCorrectedValueG, static_cast<double>(UINT8_MAX)));
69-
quint8 clampedValueB = static_cast<quint8>(qBound(0.0, gammaCorrectedValueB, static_cast<double>(UINT8_MAX)));
70+
auto clampedValueR = static_cast<quint8>(qBound(0.0, gammaCorrectedValueR, static_cast<double>(UINT8_MAX)));
71+
auto clampedValueG = static_cast<quint8>(qBound(0.0, gammaCorrectedValueG, static_cast<double>(UINT8_MAX)));
72+
auto clampedValueB = static_cast<quint8>(qBound(0.0, gammaCorrectedValueB, static_cast<double>(UINT8_MAX)));
7073

7174
// Assign clamped values to _mapping arrays
7275
_mappingR[i] = clampedValueR;
@@ -78,13 +81,13 @@ void RgbTransform::initializeMapping()
7881

7982
int RgbTransform::getBacklightThreshold() const
8083
{
81-
return static_cast<int>(_backlightThreshold);
84+
return _backlightThreshold;
8285
}
83-
84-
void RgbTransform::setBacklightThreshold(double backlightThreshold)
86+
87+
void RgbTransform::setBacklightThreshold(int backlightThreshold)
8588
{
8689
_backlightThreshold = backlightThreshold;
87-
_sumBrightnessLow = 765.0 * ((qPow(2.0,(_backlightThreshold/100)*2)-1) / 3.0);
90+
_sumBrightnessLow = static_cast<uint8_t>(qBound(0, _backlightThreshold, static_cast<int>(UINT8_MAX)));
8891
}
8992

9093
bool RgbTransform::getBacklightColored() const
@@ -153,10 +156,10 @@ void RgbTransform::getBrightnessComponents(uint8_t & rgb, uint8_t & cmy, uint8_t
153156
{
154157
rgb = _brightness_rgb;
155158
cmy = _brightness_cmy;
156-
white = _brightness_w;
159+
white = _brightness_w;
157160
}
158161

159-
void RgbTransform::applyGamma(uint8_t & red, uint8_t & green, uint8_t & blue)
162+
void RgbTransform::applyGamma(uint8_t & red, uint8_t & green, uint8_t & blue) const
160163
{
161164
// apply gamma
162165
red = _mappingR[red];
@@ -166,31 +169,26 @@ void RgbTransform::applyGamma(uint8_t & red, uint8_t & green, uint8_t & blue)
166169

167170
void RgbTransform::applyBacklight(uint8_t & red, uint8_t & green, uint8_t & blue) const
168171
{
169-
// apply brightnesss
170-
int rgbSum = red+green+blue;
171-
172-
if ( _backLightEnabled && _sumBrightnessLow > 0 && rgbSum < _sumBrightnessLow)
172+
if (_backLightEnabled && _sumBrightnessLow > 0)
173173
{
174174
if (_backlightColored)
175175
{
176-
if (rgbSum == 0)
177-
{
178-
if (red ==0) { red = 1; }
179-
if (green==0) { green = 1; }
180-
if (blue ==0) { blue = 1; }
181-
rgbSum = red+green+blue;
182-
}
183-
184-
uint8_t cLow = static_cast<uint8_t>(qMin(static_cast<double>(_sumBrightnessLow/rgbSum), static_cast<double>(UINT8_MAX)));
185-
red *= cLow;
186-
green *= cLow;
187-
blue *= cLow;
176+
red = qMax(red, _sumBrightnessLow);
177+
green = qMax(green, _sumBrightnessLow);
178+
blue = qMax(blue, _sumBrightnessLow);
188179
}
189180
else
190181
{
191-
red = static_cast<uint8_t>(qMin(static_cast<double>(_sumBrightnessLow/3.0), static_cast<double>(UINT8_MAX)));
192-
green = red;
193-
blue = red;
182+
// Average of min and max channel values for backlight decision
183+
int minVal = qMin<int>(red, qMin<int>(green, blue));
184+
int maxVal = qMax<int>(red, qMax<int>(green, blue));
185+
int avVal = (minVal + maxVal) / 2;
186+
if (avVal < _sumBrightnessLow)
187+
{
188+
red = _sumBrightnessLow;
189+
green = _sumBrightnessLow;
190+
blue = _sumBrightnessLow;
191+
}
194192
}
195193
}
196194
}

0 commit comments

Comments
 (0)