forked from woodenshark/Lightpack
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathAbstractLedDevice.cpp
More file actions
187 lines (159 loc) · 6.02 KB
/
AbstractLedDevice.cpp
File metadata and controls
187 lines (159 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* AbstractLedDevice.cpp
*
* Created on: 05.02.2013
* Project: Prismatik
*
* Copyright (c) 2013 Timur Sattarov, tim.helloworld [at] gmail.com
*
* Lightpack is an open-source, USB content-driving ambient lighting
* hardware.
*
* Prismatik is a free, open-source software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Prismatik and Lightpack files is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "AbstractLedDevice.hpp"
#include "colorspace_types.h"
#include "PrismatikMath.hpp"
#include "Settings.hpp"
void AbstractLedDevice::setSmoothSlowdown(int value) {
m_softSmoothSteps = value;
setColors(m_colorsSaved);
}
void AbstractLedDevice::setGamma(double value) {
m_gamma = value;
setColors(m_colorsSaved);
}
void AbstractLedDevice::setBrightness(int value) {
m_brightness = value;
setColors(m_colorsSaved);
}
void AbstractLedDevice::setLuminosityThreshold(int value) {
m_luminosityThreshold = value;
setColors(m_colorsSaved);
}
void AbstractLedDevice::setMinimumLuminosityThresholdEnabled(bool value) {
m_isMinimumLuminosityEnabled = value;
setColors(m_colorsSaved);
}
void AbstractLedDevice::updateWBAdjustments(const QList<WBAdjustment> &coefs) {
m_wbAdjustments.clear();
m_wbAdjustments.append(coefs);
setColors(m_colorsSaved);
}
void AbstractLedDevice::setUsbPowerLedDisabled(bool) {
//Default implementation: not supported / unused
emit commandCompleted(true);
}
void AbstractLedDevice::setRefreshDelay(int) {
//Default implementation: not supported / unused
emit commandCompleted(true);
}
void AbstractLedDevice::setColorDepth(int) {
//Default implementation: not supported / unused
emit commandCompleted(true);
}
void AbstractLedDevice::updateDeviceSettings()
{
using namespace SettingsScope;
setGamma(Settings::getDeviceGamma());
setBrightness(Settings::getDeviceBrightness());
setLuminosityThreshold(Settings::getLuminosityThreshold());
setMinimumLuminosityThresholdEnabled(Settings::isMinimumLuminosityEnabled());
updateWBAdjustments(Settings::getLedCoefs());
setSmoothSlowdown(Settings::getDeviceSmooth());
}
/*!
Modifies colors according to gamma, luminosity threshold, white balance and brightness settings
All modifications are made over extended 12bit RGB, so \code outColors \endcode will contain 12bit
RGB instead of 8bit.
*/
void AbstractLedDevice::applyColorModifications(const QList<QRgb> &inColors, QList<StructRgb> &outColors) {
bool isApplyWBAdjustments = m_wbAdjustments.count() == inColors.count();
for(int i = 0; i < inColors.count(); i++) {
//renormalize to 12bit
double k = 4095/255.0;
outColors[i].r = qRed(inColors[i]) * k;
outColors[i].g = qGreen(inColors[i]) * k;
outColors[i].b = qBlue(inColors[i]) * k;
PrismatikMath::gammaCorrection(m_gamma, outColors[i]);
}
StructLab avgColor = PrismatikMath::toLab(PrismatikMath::avgColor(outColors));
for (int i = 0; i < outColors.count(); ++i) {
StructLab lab = PrismatikMath::toLab(outColors[i]);
int dl = m_luminosityThreshold - lab.l;
if (dl > 0) {
if (m_isMinimumLuminosityEnabled) { // apply minimum luminosity or dead-zone
// Cross-fade a and b channels to avarage value within kFadingRange, fadingFactor = (dL - fadingRange)^2 / (fadingRange^2)
const int kFadingRange = 5;
double fadingCoeff = dl < kFadingRange ? (dl - kFadingRange)*(dl - kFadingRange)/(kFadingRange*kFadingRange): 1;
char da = avgColor.a - lab.a;
char db = avgColor.b - lab.b;
lab.l = m_luminosityThreshold;
lab.a += PrismatikMath::round(da * fadingCoeff);
lab.b += PrismatikMath::round(db * fadingCoeff);
StructRgb rgb = PrismatikMath::toRgb(lab);
outColors[i] = rgb;
} else {
outColors[i].r = 0;
outColors[i].g = 0;
outColors[i].b = 0;
}
}
PrismatikMath::brightnessCorrection(m_brightness, outColors[i]);
if (isApplyWBAdjustments) {
outColors[i].r *= m_wbAdjustments[i].red;
outColors[i].g *= m_wbAdjustments[i].green;
outColors[i].b *= m_wbAdjustments[i].blue;
}
}
}
void AbstractLedDevice::setColors(const QList<QRgb> & colors) {
if (m_softSmoothSteps == 0) {
setColorsUnsmoothed(colors);
} else {
if (m_colorsSaved.size() != colors.size()) {
for (int i = 0; i < colors.size(); i++) {
m_colorsSaved.append(0);
}
}
m_softSmoothIndex = 0;
m_colorsSmoothStart = m_colorsSaved;
m_colorsSmoothEnd = colors;
setColorsUnsmoothed(m_colorsSaved);
if (!m_softSmoothTimer) {
m_softSmoothTimer = new QTimer(this);
connect(m_softSmoothTimer, SIGNAL(timeout()), this, SLOT(softSmoothTimerTick()));
m_softSmoothTimer->setInterval(5);
}
if (!m_softSmoothTimer->isActive()) {
m_softSmoothTimer->start();
}
}
}
void AbstractLedDevice::softSmoothTimerTick() {
for (int i = 0; i < m_colorsSmoothStart.size(); i++) {
QColor start = m_colorsSmoothStart[i];
QColor end = m_colorsSmoothEnd[i];
m_colorsSaved[i] = qRgb(
start.red() + (end.red() - start.red()) * ((float)m_softSmoothIndex) / m_softSmoothSteps,
start.green() + (end.green() - start.green()) * ((float)m_softSmoothIndex) / m_softSmoothSteps,
start.blue() + (end.blue() - start.blue()) * ((float)m_softSmoothIndex) / m_softSmoothSteps);
}
setColorsUnsmoothed(m_colorsSaved);
if (m_softSmoothIndex >= m_softSmoothSteps) {
m_softSmoothTimer->stop();
}
m_softSmoothIndex++;
}