1- #include < QtCore/qmath.h>
21#include < utils/RgbTransform.h>
2+
3+ #include < QtMath>
4+ #include < QtGlobal>
5+
36#include < utils/KelvinToRgb.h>
47
58RgbTransform::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
7982int 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
9093bool 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
167170void 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