-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathColor.h
478 lines (428 loc) · 15 KB
/
Color.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//
// Copyright (C) Wojciech Jarosz <wjarosz@gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE.txt file.
//
#pragma once
#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include "Fwd.h"
class Color3
{
public:
float r, g, b;
//-----------------------------------------------------------------------
//@{ \name Constructors and assignment
//-----------------------------------------------------------------------
Color3() = default;
Color3(const Color3 & c) : r(c.r), g(c.g), b(c.b) {}
Color3(float x, float y, float z) : r(x), g(y), b(z) {}
explicit Color3(float c) : r(c), g(c), b(c) {}
explicit Color3(const float* c) : r(c[0]), g(c[1]), b(c[2]) {}
Color3 & operator=(float c) {r = g = b = c; return *this;}
//@}
//-----------------------------------------------------------------------
//@{ \name Casting operators.
//-----------------------------------------------------------------------
explicit operator const float*() const {return(const float*)&r;}
explicit operator float*() {return(float*)&r;}
//@}
//-----------------------------------------------------------------------
//@{ \name Element access and manipulation.
//-----------------------------------------------------------------------
float& operator[](int i) {return(&r)[i];}
const float & operator[](int i) const {return(&r)[i];}
void set(float s) {r = g = b = s;}
void set(float x, float y, float z) {r = x; g = y; b = z;}
void set(const Color3& c) {r = c.r; g = c.g; b = c.b;}
//@}
//-----------------------------------------------------------------------
//@{ \name Addition.
//-----------------------------------------------------------------------
Color3 operator+(const Color3& c) const
{
return Color3(r + c.r, g + c.g, b + c.b);
}
const Color3 & operator+=(const Color3& c)
{
r += c.r; g += c.g; b += c.b; return *this;
}
const Color3 & operator+=(float a)
{
r += a; g += a; b += a; return *this;
}
//@}
//-----------------------------------------------------------------------
//@{ \name Subtraction.
//-----------------------------------------------------------------------
Color3 operator-(const Color3& c) const
{
return Color3(r - c.r, g - c.g, b - c.b);
}
const Color3 & operator-=(const Color3& c)
{
r -= c.r; g -= c.g; b -= c.b; return *this;
}
const Color3 & operator-=(float a)
{
r -= a; g -= a; b -= a; return *this;
}
//@}
//-----------------------------------------------------------------------
//@{ \name Multiplication.
//-----------------------------------------------------------------------
Color3 operator*(float a) const
{
return Color3(r * a, g * a, b * a);
}
Color3 operator*(const Color3& c) const
{
return Color3(r * c.r, g * c.g, b * c.b);
}
const Color3 & operator*=(float a)
{
r *= a; g *= a; b *= a; return *this;
}
const Color3 & operator*=(const Color3& c)
{
r *= c.r; g *= c.g; b *= c.b; return *this;
}
Color3 operator-() const {return Color3(-r, -g, -b);}
//@}
//-----------------------------------------------------------------------
//@{ \name Division.
//-----------------------------------------------------------------------
Color3 operator/(float a) const
{
float inv = 1.0f / a;
return Color3(r * inv, g * inv, b * inv);
}
Color3 operator/(const Color3 & c) const
{
return Color3(r / c.r, g / c.g, b / c.b);
}
const Color3 & operator/=(float a)
{
float inv = 1.0f / a;
r *= inv; g *= inv; b *= inv; return *this;
}
const Color3 & operator/=(const Color3 & c)
{
r /= c.r; g /= c.g; b /= c.b; return *this;
}
//@}
float sum() const {return r + g + b;}
float average() const {return sum() / 3.0f;}
float luminance() const
{
return 0.212671f * r + 0.715160f * g + 0.072169f * b;
}
float min() const {return std::min(std::min(r, g), b);}
Color3 min(const Color3 & m) const
{
return Color3(std::min(r,m.r), std::min(g,m.g), std::min(b,m.b));
}
Color3 min(float m) const
{
return Color3(std::min(r,m), std::min(g,m), std::min(b,m));
}
float max() const {return std::max(std::max(r, g), b);}
Color3 max(const Color3 & m) const
{
return Color3(std::max(r,m.r), std::max(g,m.g), std::max(b,m.b));
}
Color3 max(float m) const
{
return Color3(std::max(r,m), std::max(g,m), std::max(b,m));
}
Color3 pow(const Color3& exp) const
{
Color3 res;
for (int i = 0; i < 3; ++i)
res[i] = (*this)[i] > 0.0f ? powf((*this)[i], exp[i]) : 0.0f;
return res;
}
Color3 convert(EColorSpace dst, EColorSpace src) const;
Color3 LinearSRGBToXYZ() const;
Color3 XYZToLinearSRGB() const;
Color3 LinearAdobeRGBToXYZ() const;
Color3 XYZToLinearAdobeRGB() const;
Color3 XYZToLab() const;
Color3 LabToXYZ() const;
Color3 XYZToLuv() const;
Color3 LuvToXYZ() const;
Color3 xyYToXYZ() const;
Color3 XYZToxyY() const;
Color3 RGBToHSV() const;
Color3 HSVToRGB() const;
Color3 RGBToHSL() const;
Color3 HSLToRGB() const;
Color3 HSIAdjust(float h, float s, float i) const;
Color3 HSLAdjust(float h, float s, float l) const;
friend std::ostream& operator<<(std::ostream& out, const Color3& c)
{
return(out << c.r << " " << c.g << " " << c.b);
}
friend std::istream& operator>>(std::istream& in, Color3& c)
{
return(in >> c.r >> c.g >> c.b);
}
friend Color3 operator*(float s, const Color3& c)
{
return Color3(c.r * s, c.g * s, c.b * s);
}
friend Color3 operator+(float s, const Color3& c)
{
return Color3(s + c.r, s + c.g, s + c.b);
}
friend Color3 operator-(float s, const Color3& c)
{
return Color3(s - c.r, s - c.g, s - c.b);
}
};
class Color4 : public Color3
{
public:
float a;
//-----------------------------------------------------------------------
//@{ \name Constructors and assignment
//-----------------------------------------------------------------------
Color4() = default;
Color4(float x, float y, float z, float w) : Color3(x, y, z), a(w) {}
Color4(float g, float a) : Color3(g), a(a) {}
Color4(const Color3 &c, float a) : Color3(c), a(a) {}
explicit Color4(float x) : Color3(x), a(x) {}
explicit Color4(const float* c) : Color3(c), a(c[3]) {}
const Color4 & operator=(float c) {r = g = b = a = c; return *this;}
//@}
//-----------------------------------------------------------------------
//@{ \name Casting operators.
//-----------------------------------------------------------------------
explicit operator const float*() const {return(const float*)&r;}
explicit operator float*() {return(float*)&r;}
//@}
//-----------------------------------------------------------------------
//@{ \name Element access and manipulation.
//-----------------------------------------------------------------------
float & operator[](int i) {return(&r)[i];}
const float & operator[](int i) const {return(&r)[i];}
void set(float x) {r = g = b = a = x;}
void set(float x, float y, float z, float w) {r = x; g = y; b = z; a = w;}
//@}
//-----------------------------------------------------------------------
//@{ \name Addition.
//-----------------------------------------------------------------------
Color4 operator+(const Color4& v) const
{
return {r + v.r, g + v.g, b + v.b, a + v.a};
}
const Color4 & operator+=(const Color4& v)
{
r += v.r; g += v.g; b += v.b; a += v.a; return *this;
}
const Color4 & operator+=(float c)
{
r += c; g += c; b += c; a += c; return *this;
}
//@}
//-----------------------------------------------------------------------
//@{ \name Subtraction.
//-----------------------------------------------------------------------
Color4 operator-(const Color4& v) const
{
return {r - v.r, g - v.g, b - v.b, a - v.a};
}
const Color4 & operator-=(const Color4& v)
{
r -= v.r; g -= v.g; b -= v.b; a -= v.a; return *this;
}
const Color4 & operator-=(float c)
{
r -= c; g -= c; b -= c; a -= c; return *this;
}
//@}
//-----------------------------------------------------------------------
//@{ \name Multiplication.
//-----------------------------------------------------------------------
Color4 operator*(float c) const
{
return {r * c, g * c, b * c, a * c};
}
Color4 operator*(const Color4& v) const
{
return Color4(r * v.r, g * v.g, b * v.b, a * v.a);
}
const Color4 & operator*=(float c)
{
r *= c; g *= c; b *= c; a *= c; return *this;
}
const Color4 & operator*=(const Color4& v)
{
r *= v.r; g *= v.g; b *= v.b; a *= v.a; return *this;
}
Color4 operator-() const {return {-r, -g, -b, -a};}
//@}
//-----------------------------------------------------------------------
//@{ \name Division.
//-----------------------------------------------------------------------
Color4 operator/(float c) const
{
float inv = 1.0f / c;
return {r * inv, g * inv, b * inv, a * inv};
}
Color4 operator/(const Color4 & v) const
{
return {r / v.r, g / v.g, b / v.b, a / v.a};
}
const Color4 & operator/=(float c)
{
float inv = 1.0f / c;
r *= inv; g *= inv; b *= inv; a *= inv;
return *this;
}
const Color4 & operator/=(const Color4 & v)
{
r /= v.r; g /= v.g; b /= v.b; a /= v.a; return *this;
}
//@}
float sum() const {return r + g + b + a;}
float average() const {return sum() / 4.0f;}
float min() const {return std::min(Color3::min(), a);}
Color4 min(const Color4 & m) const {return Color4(Color3::min(m), std::min(a,m.a));}
Color4 min(float m) const {return Color4(Color3::min(m), std::min(a,m));}
float max() const {return std::max(Color3::max(), a);}
Color4 max(const Color4 & m) const {return Color4(Color3::max(m), std::max(a,m.a));}
Color4 max(float m) const {return Color4(Color3::max(m), std::max(a,m));}
Color4 convert(EColorSpace dst, EColorSpace src) const {return Color4(Color3::convert(dst, src), a);}
Color4 LinearSRGBToXYZ() const {return Color4(Color3::LinearSRGBToXYZ(), a);}
Color4 XYZToLinearSRGB() const {return Color4(Color3::XYZToLinearSRGB(), a);}
Color3 LinearAdobeRGBToXYZ() const {return Color4(Color3::LinearAdobeRGBToXYZ(), a);}
Color3 XYZToLinearAdobeRGB() const {return Color4(Color3::XYZToLinearAdobeRGB(), a);}
Color4 XYZToLab() const {return Color4(Color3::XYZToLab(), a);}
Color4 LabToXYZ() const {return Color4(Color3::LabToXYZ(), a);}
Color4 XYZToLuv() const {return Color4(Color3::XYZToLuv(), a);}
Color4 LuvToXYZ() const {return Color4(Color3::LuvToXYZ(), a);}
Color4 xyYToXYZ() const {return Color4(Color3::xyYToXYZ(), a);}
Color4 XYZToxyY() const {return Color4(Color3::XYZToxyY(), a);}
Color4 RGBToHSV() const {return Color4(Color3::RGBToHSV(), a);}
Color4 HSVToRGB() const {return Color4(Color3::HSVToRGB(), a);}
Color4 RGBToHSL() const {return Color4(Color3::RGBToHSL(), a);}
Color4 HSLToRGB() const {return Color4(Color3::HSLToRGB(), a);}
Color4 HSIAdjust(float h, float s, float i) const {return Color4(Color3::HSIAdjust(h,s,i), a);}
Color4 HSLAdjust(float h, float s, float l) const {return Color4(Color3::HSLAdjust(h,s,l), a);}
friend std::ostream& operator<<(std::ostream& out, const Color4& c)
{
return(out << c.r << " " << c.g << " " << c.b << " " << c.a);
}
friend std::istream& operator>>(std::istream& in, Color4& c)
{
return(in >> c.r >> c.g >> c.b >> c.a);
}
friend Color4 operator*(float s, const Color4& c)
{
return {c.r * s, c.g * s, c.b * s, c.a * s};
}
friend Color4 operator+(float s, const Color4& c)
{
return {s + c.r, s + c.g, s + c.b, c.a};
}
friend Color4 operator-(float s, const Color4& c)
{
return {s - c.r, s - c.g, s - c.b, c.a};
}
};
#define COLOR_FUNCTION_WRAPPER(FUNC) \
inline Color3 FUNC(const Color3 & c) \
{ \
return Color3(std:: FUNC(c[0]), \
std:: FUNC(c[1]), \
std:: FUNC(c[2])); \
} \
inline Color4 FUNC(const Color4 & c) \
{ \
return Color4(std:: FUNC(c[0]), std:: FUNC(c[1]), \
std:: FUNC(c[2]), std:: FUNC(c[3])); \
}
#define COLOR_FUNCTION_WRAPPER2(FUNC) \
inline Color3 FUNC(const Color3 & c, const Color3 & e) \
{ \
return Color3(std:: FUNC(c[0], e[0]), \
std:: FUNC(c[1], e[1]), \
std:: FUNC(c[2], e[2])); \
} \
template <typename T> \
inline Color3 FUNC(const Color3 & c, T e) \
{ \
return Color3(std:: FUNC(c[0], e), \
std:: FUNC(c[1], e), \
std:: FUNC(c[2], e)); \
} \
inline Color4 FUNC(const Color4 & c, const Color4 & e) \
{ \
return Color4(std:: FUNC(c[0], e[0]), \
std:: FUNC(c[1], e[1]), \
std:: FUNC(c[2], e[2]), \
std:: FUNC(c[3], e[3])); \
} \
template <typename T> \
inline Color4 FUNC(const Color4 & c, T e) \
{ \
return Color4(std:: FUNC(c[0], e), \
std:: FUNC(c[1], e), \
std:: FUNC(c[2], e), \
std:: FUNC(c[3], e)); \
}
//
// create vectorized versions of the math functions across the elements of
// a Color3 or Color4
//
COLOR_FUNCTION_WRAPPER(exp)
COLOR_FUNCTION_WRAPPER(exp2)
COLOR_FUNCTION_WRAPPER(expm1)
COLOR_FUNCTION_WRAPPER(log)
COLOR_FUNCTION_WRAPPER(log10)
COLOR_FUNCTION_WRAPPER(log2)
COLOR_FUNCTION_WRAPPER(log1p)
COLOR_FUNCTION_WRAPPER(fabs)
COLOR_FUNCTION_WRAPPER(abs)
COLOR_FUNCTION_WRAPPER(sqrt)
COLOR_FUNCTION_WRAPPER(cbrt)
COLOR_FUNCTION_WRAPPER(sin)
COLOR_FUNCTION_WRAPPER(cos)
COLOR_FUNCTION_WRAPPER(tan)
COLOR_FUNCTION_WRAPPER(asin)
COLOR_FUNCTION_WRAPPER(acos)
COLOR_FUNCTION_WRAPPER(atan)
COLOR_FUNCTION_WRAPPER(erf)
COLOR_FUNCTION_WRAPPER(erfc)
COLOR_FUNCTION_WRAPPER(tgamma)
COLOR_FUNCTION_WRAPPER(lgamma)
COLOR_FUNCTION_WRAPPER(ceil)
COLOR_FUNCTION_WRAPPER(floor)
COLOR_FUNCTION_WRAPPER(trunc)
COLOR_FUNCTION_WRAPPER(round)
COLOR_FUNCTION_WRAPPER2(pow)
COLOR_FUNCTION_WRAPPER2(fmin)
COLOR_FUNCTION_WRAPPER2(fmax)
COLOR_FUNCTION_WRAPPER2(min)
COLOR_FUNCTION_WRAPPER2(max)
namespace Eigen
{
template<> struct NumTraits<Color4>
: NumTraits<float> // permits to get the epsilon, dummy_precision, lowest, highest functions
{
typedef Color4 Real;
typedef Color4 NonInteger;
typedef Color4 & Nested;
enum {
IsComplex = 0,
IsInteger = 0,
IsSigned = 1,
RequireInitialization = 1,
ReadCost = 1,
AddCost = 3,
MulCost = 3
};
};
} // namespace Eigen