-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHSV.hpp
124 lines (110 loc) · 2.84 KB
/
HSV.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
#pragma once
#ifndef AE_UTILS_HSV_HPP
#define AE_UTILS_HSV_HPP
#include <AE_Effect.h>
#include <initializer_list>
class HSV {
public:
// 0 ~ 1
float h, s, v;
HSV(const PF_Pixel32 &in) noexcept {
float min, max, delta;
min = in.red < in.green ? in.red : in.green;
min = min < in.blue ? min : in.blue;
max = in.red > in.green ? in.red : in.green;
max = max > in.blue ? max : in.blue;
delta = max - min;
if (delta == 0) {
h = 0;
s = 0;
v = max;
return;
}
if (in.red == max) {
h = (in.green - in.blue) / delta;
if (h < 0) h += 6;
} else if (in.green == max) {
h = 2 + (in.blue - in.red) / delta;
} else {
h = 4 + (in.red - in.green) / delta;
}
h /= 6;
if (h > 1) {
h -= 1;
}
s = delta;
if (max != 0) {
s /= max;
}
v = max;
}
HSV(const std::initializer_list<int> &src) noexcept {
h = *src.begin();
s = *(src.begin() + 1);
v = *(src.begin() + 2);
}
HSV(const HSV &) = default;
HSV(HSV &&) = default;
HSV() noexcept {
h = 0.0;
s = 0.0;
v = 0.0;
}
PF_Pixel32 ToRGB() const noexcept {
float hh, p, q, t, ff;
long i;
PF_Pixel32 out;
out.alpha = 1.0;
if (this->s <= 0.0) { // < is bogus, just shuts up warnings
out.red = this->v;
out.green = this->v;
out.blue = this->v;
return out;
}
hh = this->h;
if (hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = this->v * (1.0 - this->s);
q = this->v * (1.0 - (this->s * ff));
t = this->v * (1.0 - (this->s * (1.0 - ff)));
switch (i) {
case 0:
out.red = this->v;
out.green = t;
out.blue = p;
break;
case 1:
out.red = q;
out.green = this->v;
out.blue = p;
break;
case 2:
out.red = p;
out.green = this->v;
out.blue = t;
break;
case 3:
out.red = p;
out.green = q;
out.blue = this->v;
break;
case 4:
out.red = t;
out.green = p;
out.blue = this->v;
break;
case 5:
default:
out.red = this->v;
out.green = p;
out.blue = q;
break;
}
return out;
}
HSV &operator=(const HSV &) = default;
HSV &operator=(HSV &&) = default;
};
#endif