-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_impl.hpp
230 lines (212 loc) · 6.42 KB
/
color_impl.hpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-License-Identifier: GPL-2.0
#include <numbers>
inline unsigned int HSVLookup::lookup(double h, unsigned int v)
{
// Check if it faster with or without fmod (more cache needed without?)
//int x = static_cast<unsigned int>(fmod(h, 1.0)*(6.0*256.0-1.0));
int x = static_cast<unsigned int>(h*(6.0*256.0-1.0));
return (data[x] * v) >> 8;
}
inline QRgb HSVLookup::convert(double h, double v)
{
unsigned int v_int = static_cast<unsigned int>(v * 256.0);
return qRgb(lookup(h, v_int), lookup(h + 1.0/3.0, v_int), lookup(h + 2.0/3.0, v_int));
}
inline QRgb HSVLookup::convert_white(double h, double v)
{
if (v > 0.5) {
v = 1.0 - v;
h += 0.5;
unsigned int v_int = static_cast<unsigned int>(v * 2.0 * 256.0);
return qRgb(255 - lookup(h, v_int), 255 - lookup(h + 1.0/3.0, v_int), 255 - lookup(h + 2.0/3.0, v_int));
} else {
unsigned int v_int = static_cast<unsigned int>(v * 2.0 * 256.0);
return qRgb(lookup(h, v_int), lookup(h + 1.0/3.0, v_int), lookup(h + 2.0/3.0, v_int));
}
}
inline QRgb RWLookup::convert(double h, double v)
{
int x = static_cast<unsigned int>(fmod(h, 1.0)*(8.0*256.0-1.0));
unsigned int v_int = static_cast<unsigned int>(v * 256.0);
return qRgb(
(data[x*3+0] * v_int) >> 8,
(data[x*3+1] * v_int) >> 8,
(data[x*3+2] * v_int) >> 8);
}
// Map interval [0..1] to [0..1] applying the given mode (linear, sqrt or log)
// For values > 1.0, return 1.0. Caller must not pass negative values!
template <ColorMode MODE>
inline double apply_color_mode(double v, double factor1, double factor2);
template <>
inline double apply_color_mode<ColorMode::LINEAR>(double v, double factor1, double)
{
return std::min(1.0, v * factor1);
}
template <>
inline double apply_color_mode<ColorMode::ROOT>(double v, double factor1, double factor2)
{
return pow(v * factor1, factor2);
}
template <>
inline double apply_color_mode<ColorMode::LOG>(double v, double factor1, double factor2)
{
return v <= std::numeric_limits<double>::epsilon() ? 0.0 : factor2 / (factor2 - log(v * factor1));
}
template<ColorMode MODE>
inline uint32_t complex_to_hsv(std::complex<double> c, double factor1, double factor2)
{
double h = (std::arg(c) + std::numbers::pi) / 2.0 / std::numbers::pi;
double v = apply_color_mode<MODE>(std::abs(c), factor1, factor2);
return hsv_lookup.convert(h, v);
}
template<ColorMode MODE>
inline uint32_t real_to_hsv(double v, double factor1, double factor2)
{
if (v < 0.0) {
v = apply_color_mode<MODE>(-v, factor1, factor2);
unsigned char vi = static_cast<unsigned char>(v * 255.0);
return qRgb(vi, 0, 0);
} else {
v = apply_color_mode<MODE>(v, factor1, factor2);
unsigned char vi = static_cast<unsigned char>(v * 255.0);
return qRgb(0, vi, vi);
}
}
template<ColorMode MODE>
inline uint32_t complex_to_hsv_white(std::complex<double> c, double factor1, double factor2)
{
double h = (std::arg(c) + std::numbers::pi) / 2.0 / std::numbers::pi;
double v = apply_color_mode<MODE>(std::abs(c), factor1, factor2);
return hsv_lookup.convert_white(h, v);
}
template<ColorMode MODE>
inline uint32_t real_to_hsv_white(double v, double factor1, double factor2)
{
if (v < 0.0) {
v = apply_color_mode<MODE>(-v, factor1, factor2);
if (v > 1.0)
v = 1.0;
if (v > 0.5) {
v -= 0.5;
unsigned char vi = static_cast<unsigned char>(v * 2.0 * 255.0);
return qRgb(255, vi, vi);
} else {
unsigned char vi = static_cast<unsigned char>(v * 2.0 * 255.0);
return qRgb(vi, 0, 0);
}
} else {
v = apply_color_mode<MODE>(v, factor1, factor2);
if (v > 1.0)
v = 1.0;
if (v > 0.5) {
v -= 0.5;
unsigned char vi = static_cast<unsigned char>(v * 2.0 * 255.0);
return qRgb(vi, 255, 255);
} else {
unsigned char vi = static_cast<unsigned char>(v * 2.0 * 255.0);
return qRgb(0, vi, vi);
}
}
}
template<ColorMode MODE>
inline uint32_t complex_to_rw(std::complex<double> c, double factor1, double factor2)
{
double h = (std::arg(c) + std::numbers::pi) / 2.0 / std::numbers::pi;
double v = apply_color_mode<MODE>(std::abs(c), factor1, factor2);
return rw_lookup.convert(h, v);
}
template<ColorMode MODE>
inline uint32_t real_to_rw(double v, double factor1, double factor2)
{
if (v < 0.0) {
v = apply_color_mode<MODE>(-v, factor1, factor2);
unsigned char vi = static_cast<unsigned char>(v * 255.0);
return qRgb(vi, 0, 0);
} else {
v = apply_color_mode<MODE>(v, factor1, factor2);
unsigned char vi = static_cast<unsigned char>(v * 255.0);
return qRgb(vi, vi, vi);
}
}
inline unsigned char real_to_grayscale_unchecked(double v)
{
return static_cast<unsigned char>(v * 255.0);
}
template<>
inline uint32_t
(*get_color_lookup_function<std::complex<double>>(ColorType type, ColorMode mode))
(std::complex<double> c, double factor1, double factor2)
{
switch (type) {
case ColorType::RW:
default:
switch (mode) {
case ColorMode::LINEAR:
default:
return &complex_to_rw<ColorMode::LINEAR>;
case ColorMode::ROOT:
return &complex_to_rw<ColorMode::ROOT>;
case ColorMode::LOG:
return &complex_to_rw<ColorMode::LOG>;
}
case ColorType::HSV:
switch (mode) {
case ColorMode::LINEAR:
default:
return &complex_to_hsv<ColorMode::LINEAR>;
case ColorMode::ROOT:
return &complex_to_hsv<ColorMode::ROOT>;
case ColorMode::LOG:
return &complex_to_hsv<ColorMode::LOG>;
}
case ColorType::HSV_WHITE:
switch (mode) {
case ColorMode::LINEAR:
default:
return &complex_to_hsv_white<ColorMode::LINEAR>;
case ColorMode::ROOT:
return &complex_to_hsv_white<ColorMode::ROOT>;
case ColorMode::LOG:
return &complex_to_hsv_white<ColorMode::LOG>;
}
}
}
template<>
inline uint32_t
(*get_color_lookup_function<double>(ColorType type, ColorMode mode))
(double c, double factor1, double factor2)
{
switch (type) {
case ColorType::RW:
default:
switch (mode) {
case ColorMode::LINEAR:
default:
return &real_to_rw<ColorMode::LINEAR>;
case ColorMode::ROOT:
return &real_to_rw<ColorMode::ROOT>;
case ColorMode::LOG:
return &real_to_rw<ColorMode::LOG>;
}
case ColorType::HSV:
switch (mode) {
case ColorMode::LINEAR:
default:
return &real_to_hsv<ColorMode::LINEAR>;
case ColorMode::ROOT:
return &real_to_hsv<ColorMode::ROOT>;
case ColorMode::LOG:
return &real_to_hsv<ColorMode::LOG>;
}
case ColorType::HSV_WHITE:
switch (mode) {
case ColorMode::LINEAR:
default:
return &real_to_hsv_white<ColorMode::LINEAR>;
case ColorMode::ROOT:
return &real_to_hsv_white<ColorMode::ROOT>;
case ColorMode::LOG:
return &real_to_hsv_white<ColorMode::LOG>;
}
}
}