-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorUtils.cpp
More file actions
63 lines (54 loc) · 1.69 KB
/
Copy pathColorUtils.cpp
File metadata and controls
63 lines (54 loc) · 1.69 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
#include "include/ColorUtils.hpp"
namespace ColorUtils {
Color hsvToRGB(std::vector<float> hsv) {
float h = hsv[0], s = hsv[1], v = hsv[2];
float r = 0, g = 0, b = 0;
int hi = static_cast<int>(h / 60) % 6;
float f = (h / 60.0f) - hi;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
switch (hi) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return (Color){
static_cast<unsigned char>(r * 255),
static_cast<unsigned char>(g * 255),
static_cast<unsigned char>(b * 255),
static_cast<unsigned char>(255)
};
}
std::vector<float> rgbToHSV(Color color) {
float r = (int)color.r / 255.0f;
float g = (int)color.g / 255.0f;
float b = (int)color.b / 255.0f;
float maxVal = std::max({r, g, b});
float minVal = std::min({r, g, b});
float delta = maxVal - minVal;
float h = 0.0f, s = 0.0f, v = maxVal;
if (delta > 0.0f) {
if (maxVal == r) {
h = 60.0f * fmod(((g - b) / delta), 6);
} else if (maxVal == g) {
h = 60.0f * (((b - r) / delta) + 2);
} else if (maxVal == b) {
h = 60.0f * (((r - g) / delta) + 4);
}
if (maxVal > 0.0f) {
s = delta / maxVal;
}
}
if (h < 0.0f) {
h += 360.0f;
}
return {h, s, v};
}
bool colorEqual(Color a, Color b) {
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
}