-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiColor.h
More file actions
200 lines (182 loc) · 6.26 KB
/
Copy pathwiColor.h
File metadata and controls
200 lines (182 loc) · 6.26 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
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
#pragma once
#include "CommonInclude.h"
#include "wiMath.h"
namespace wi
{
struct Color
{
uint32_t rgba = 0;
constexpr Color(uint32_t rgba) :rgba(rgba) {}
constexpr Color(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0, uint8_t a = 255) : rgba(uint32_t(r) | (uint32_t(g) << 8) | (uint32_t(b) << 16) | (uint32_t(a) << 24)) {}
constexpr Color(const char* hex)
{
int len = 0;
while (hex[len++] != 0) {}
rgba = 0;
uint32_t shift = 0u;
for (int i = len - 1; i >= 0; i--)
{
const char c = hex[i];
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
rgba |= (c - '0') << shift;
shift += 4u;
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
rgba |= (c - 'A' + 10) << shift;
shift += 4u;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
rgba |= (c - 'a' + 10) << shift;
shift += 4u;
break;
case '#':
break;
default:
case 0:
break;
}
if (len < 8)
setA(255);
}
}
constexpr uint8_t getR() const { return (rgba >> 0) & 0xFF; }
constexpr uint8_t getG() const { return (rgba >> 8) & 0xFF; }
constexpr uint8_t getB() const { return (rgba >> 16) & 0xFF; }
constexpr uint8_t getA() const { return (rgba >> 24) & 0xFF; }
constexpr void setR(uint8_t value) { *this = Color(value, getG(), getB(), getA()); }
constexpr void setG(uint8_t value) { *this = Color(getR(), value, getB(), getA()); }
constexpr void setB(uint8_t value) { *this = Color(getR(), getG(), value, getA()); }
constexpr void setA(uint8_t value) { *this = Color(getR(), getG(), getB(), value); }
constexpr XMFLOAT3 toFloat3() const
{
return XMFLOAT3(
((rgba >> 0) & 0xFF) / 255.0f,
((rgba >> 8) & 0xFF) / 255.0f,
((rgba >> 16) & 0xFF) / 255.0f
);
}
constexpr XMFLOAT4 toFloat4() const
{
return XMFLOAT4(
((rgba >> 0) & 0xFF) / 255.0f,
((rgba >> 8) & 0xFF) / 255.0f,
((rgba >> 16) & 0xFF) / 255.0f,
((rgba >> 24) & 0xFF) / 255.0f
);
}
constexpr operator XMFLOAT3() const { return toFloat3(); }
constexpr operator XMFLOAT4() const { return toFloat4(); }
constexpr operator uint32_t() const { return rgba; }
template<size_t capacity>
struct char_return
{
char text[capacity] = {};
constexpr operator const char* () const { return text; }
};
constexpr const char_return<9> to_hex() const
{
char_return<9> ret;
for (int i = 7; i >= 0; i--)
{
const uint8_t v = (rgba >> (i * 4)) & 0xF;
ret.text[7 - i] = v < 10 ? ('0' + v) : ('A' + v - 10);
}
return ret;
}
static constexpr Color fromFloat4(const XMFLOAT4& value)
{
return Color((uint8_t)(saturate(value.x) * 255), (uint8_t)(saturate(value.y) * 255), (uint8_t)(saturate(value.z) * 255), (uint8_t)(saturate(value.w) * 255));
}
static constexpr Color fromFloat3(const XMFLOAT3& value)
{
return Color((uint8_t)(saturate(value.x) * 255), (uint8_t)(saturate(value.y) * 255), (uint8_t)(saturate(value.z) * 255));
}
static constexpr Color lerp(Color a, Color b, float i)
{
return fromFloat4(wi::math::Lerp(a.toFloat4(), b.toFloat4(), i));
}
static constexpr Color Red() { return Color(255, 0, 0, 255); }
static constexpr Color Green() { return Color(0, 255, 0, 255); }
static constexpr Color Blue() { return Color(0, 0, 255, 255); }
static constexpr Color Black() { return Color(0, 0, 0, 255); }
static constexpr Color White() { return Color(255, 255, 255, 255); }
static constexpr Color Yellow() { return Color(255, 255, 0, 255); }
static constexpr Color Purple() { return Color(255, 0, 255, 255); }
static constexpr Color Cyan() { return Color(0, 255, 255, 255); }
static constexpr Color Transparent() { return Color(0, 0, 0, 0); }
static constexpr Color Gray() { return Color(127, 127, 127, 255); }
static constexpr Color Ghost() { return Color(127, 127, 127, 127); }
static constexpr Color Booger() { return Color(127, 127, 127, 200); }
static constexpr Color Shadow() { return Color(0, 0, 0, 100); }
static constexpr Color Warning() { return 0xFF66FFFF; } // light yellow
static constexpr Color Error() { return 0xFF6666FF; } // light red
};
struct Color16
{
uint64_t rgba = 0;
constexpr Color16(uint64_t rgba) :rgba(rgba) {}
constexpr Color16(uint16_t r = 0, uint16_t g = 0, uint16_t b = 0, uint16_t a = 65535) : rgba(uint64_t(r) | (uint64_t(g) << 16) | (uint64_t(b) << 32) | (uint64_t(a) << 48)) {}
constexpr Color16(Color color) { *this = fromFloat4(color.toFloat4()); }
constexpr uint16_t getR() const { return (rgba >> 0) & 0xFFFF; }
constexpr uint16_t getG() const { return (rgba >> 16) & 0xFFFF; }
constexpr uint16_t getB() const { return (rgba >> 32) & 0xFFFF; }
constexpr uint16_t getA() const { return (rgba >> 48) & 0xFFFF; }
constexpr void setR(uint16_t value) { *this = Color16(value, getG(), getB(), getA()); }
constexpr void setG(uint16_t value) { *this = Color16(getR(), value, getB(), getA()); }
constexpr void setB(uint16_t value) { *this = Color16(getR(), getG(), value, getA()); }
constexpr void setA(uint16_t value) { *this = Color16(getR(), getG(), getB(), value); }
constexpr XMFLOAT3 toFloat3() const
{
return XMFLOAT3(
((rgba >> 0) & 0xFFFF) / 65535.0f,
((rgba >> 16) & 0xFFFF) / 65535.0f,
((rgba >> 32) & 0xFFFF) / 65535.0f
);
}
constexpr XMFLOAT4 toFloat4() const
{
return XMFLOAT4(
((rgba >> 0) & 0xFFFF) / 65535.0f,
((rgba >> 16) & 0xFFFF) / 65535.0f,
((rgba >> 32) & 0xFFFF) / 65535.0f,
((rgba >> 48) & 0xFFFF) / 65535.0f
);
}
constexpr operator XMFLOAT3() const { return toFloat3(); }
constexpr operator XMFLOAT4() const { return toFloat4(); }
constexpr operator uint64_t() const { return rgba; }
static constexpr Color16 fromFloat4(const XMFLOAT4& value)
{
return Color16(uint16_t(value.x * 65535), uint16_t(value.y * 65535), uint16_t(value.z * 65535), uint16_t(value.w * 65535));
}
static constexpr Color16 fromFloat3(const XMFLOAT3& value)
{
return Color16(uint16_t(value.x * 65535), uint16_t(value.y * 65535), uint16_t(value.z * 65535));
}
static constexpr Color16 lerp(Color16 a, Color16 b, float i)
{
return fromFloat4(wi::math::Lerp(a.toFloat4(), b.toFloat4(), i));
}
};
}