-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.hpp
81 lines (69 loc) · 1.43 KB
/
color.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
/*
* color.hpp
* -------------------------
*
* -------------------------
* Stavros Avramidis 1/17/2019
*/
#pragma once
#include <cstdint>
#include <math.h>
struct HSV_COLOR {
double hue;
double saturation;
double value;
HSV_COLOR() : hue(0), saturation(0), value(0) {}
HSV_COLOR(uint8_t h, uint8_t s, uint8_t v) : hue(h), saturation(s), value(v) {}
};
struct RGB_COLOR {
uint8_t red;
uint8_t green;
uint8_t blue;
RGB_COLOR() : red(0), green(0), blue(0) {}
RGB_COLOR(uint8_t r, uint8_t g, uint8_t b) : red(r), green(g), blue(b) {}
// RGB_COLOR& operator= (HSV_COLOR &hsv) {
// this = hsv2rgb(hsv.hue, hsv.saturation, hsv.value);
// }
};
RGB_COLOR hsv2rgb(double H, double S, double V) {
double P, Q, T, fract;
double _r = 0, _g = 0, _b = 0;
while (H > 359) {
H = H - 360;
}
(H==360.) ? (H = 0.) : (H /= 60.);
fract = H - floor(H);
P = V*(1. - S);
Q = V*(1. - S*fract);
T = V*(1. - S*(1. - fract));
if (0. <= H && H < 1.) {
_r = V;
_g = T;
_b = P;
} else if (1. <= H && H < 2.) {
_r = Q;
_g = V;
_b = P;
} else if (2. <= H && H < 3.) {
_r = P;
_g = V;
_b = T;
} else if (3. <= H && H < 4.) {
_r = P;
_g = Q;
_b = V;
} else if (4. <= H && H < 5.) {
_r = T;
_g = P;
_b = V;
} else if (5. <= H && H < 6.) {
_r = V;
_g = P;
_b = Q;
}
uint8_t
r = 255*_r,
g = 255*_g,
b = 255*_b;
return {r, g, b};
}