-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.h
99 lines (82 loc) · 2.32 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
#pragma once
class Color {
private:
// easy reinterpret.
union {
struct {
uint8_t m_r;
uint8_t m_g;
uint8_t m_b;
uint8_t m_a;
};
uint32_t m_rgba;
};
public:
// ctors.
__forceinline Color() : m_r{ 0 }, m_g{ 0 }, m_b{ 0 }, m_a{ 0 }, m_rgba{} {}
__forceinline Color(int r, int g, int b, int a = 255) : m_r{ (uint8_t)r }, m_g{ (uint8_t)g }, m_b{ (uint8_t)b }, m_a{ (uint8_t)a } {}
__forceinline Color(uint32_t rgba) : m_rgba{ rgba } {}
static Color hsl_to_rgb(float h, float s, float l) {
float q;
if (l < 0.5f)
q = l * (s + 1.f);
else
q = l + s - (l * s);
float p = 2 * l - q;
float rgb[3];
rgb[0] = h + (1.f / 3.f);
rgb[1] = h;
rgb[2] = h - (1.f / 3.f);
for (int i = 0; i < 3; ++i) {
if (rgb[i] < 0)
rgb[i] += 1.f;
if (rgb[i] > 1)
rgb[i] -= 1.f;
if (rgb[i] < (1.f / 6.f))
rgb[i] = p + ((q - p) * 6 * rgb[i]);
else if (rgb[i] < 0.5f)
rgb[i] = q;
else if (rgb[i] < (2.f / 3.f))
rgb[i] = p + ((q - p) * 6 * ((2.f / 3.f) - rgb[i]));
else
rgb[i] = p;
}
return {
int(rgb[0] * 255.f),
int(rgb[1] * 255.f),
int(rgb[2] * 255.f)
};
}
Color malpha(float _a)
{
if (_a > 1.0f)
_a = 1.0f;
else if (_a < 0.0f)
_a = 0.0f;
return Color(this->r(), this->g(), this->b(), this->a() * _a);
}
// member accessors.
__forceinline uint8_t& r() { return m_r; }
__forceinline uint8_t& g() { return m_g; }
__forceinline uint8_t& b() { return m_b; }
__forceinline uint8_t& a() { return m_a; }
__forceinline uint32_t& rgba() { return m_rgba; }
// operators.
__forceinline operator uint32_t() { return m_rgba; }
};
namespace colors {
static Color light_purple{ 157, 100, 217, 255 };
static Color white{ 255, 255, 255, 255 };
static Color black{ 0, 0, 0, 255 };
static Color yellowgreen{ 182,231,23,255 };
static Color red{ 255, 0, 0, 255 };
static Color menucolor{ 157, 100, 217, 255 };
static Color burgundy{ 0xff2d00b3 };
static Color light_blue{ 95, 174, 227, 255 };
static Color orange{ 243, 156, 18, 255 };
static Color green{ 149, 184, 6, 255 };
static Color purple{ 180, 60, 120, 255 };
static Color transparent_green{ 0, 255, 0, 200 };
static Color transparent_yellow{ 255, 255, 0, 200 };
static Color transparent_red{ 255, 0, 0, 200 };
}