-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorModel.cpp
More file actions
96 lines (79 loc) · 2.2 KB
/
ColorModel.cpp
File metadata and controls
96 lines (79 loc) · 2.2 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
#include "ColorModel.h"
#include <QtMath>
static uint8_t clampDoubleToUint8(double x, double min, double max)
{
if (x < min)
return min;
if (x > max)
return max;
return x;
}
void ColorModel::setKelvin(uint16_t kelvin)
{
if (m_kelvin != kelvin) {
m_kelvin = kelvin;
updateRgbColor();
emit kelvinChanged();
}
}
void ColorModel::updateRgbColor()
{
const auto temp = m_kelvin / 100.0;
float red, green, blue;
if (temp <= 66) {
red = 255;
green = temp;
green = 99.4708025861 * qLn(green) - 161.1195681661;
if (temp <= 19) {
blue = 0;
} else {
blue = temp - 10;
blue = 138.5177312231 * qLn(blue) - 305.0447927307;
}
} else {
red = temp - 60;
red = 329.698727446 * qPow(red, -0.1332047592);
green = temp - 60;
green = 288.1221695283 * qPow(green, -0.0755148492);
blue = 255;
}
QColor newColor(clampDoubleToUint8(red, 0, 255), clampDoubleToUint8(green, 0, 255), clampDoubleToUint8(blue, 0, 255));
if (m_rgbColor != newColor) {
m_rgbColor = newColor;
updateHSB();
emit rgbColorChanged();
}
}
void ColorModel::updateHSB()
{
const auto r = m_rgbColor.redF();
const auto g = m_rgbColor.greenF();
const auto b = m_rgbColor.blueF();
const auto max = qMax(qMax(r, g), b);
const auto min = qMin(qMin(r, g), b);
const auto delta = max - min;
hsb newHsb;
// Calculate brightness
newHsb.b = qRound(max * 100);
// Calculate saturation
newHsb.s = max == 0 ? 0 : qRound((delta / max) * 100);
// Calculate hue
newHsb.h = 0;
if (delta > 0) {
if (max == r) {
newHsb.h = qRound(60.0 * (fmod(((g - b) / delta), 6)));
} else if (max == g) {
newHsb.h = qRound(60.0 * (((b - r) / delta) + 2));
} else {
newHsb.h = qRound(60.0 * (((r - g) / delta) + 4));
}
if (newHsb.h < 0) {
newHsb.h += 360;
}
}
// Update properties if changed
if (m_hsb != newHsb) {
m_hsb = newHsb;
emit hsbChanged();
}
}