Debian Codegraph @

core/math/color.h

raw symbols SCIP
1 /**************************************************************************/
2 /* color.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/math/math_funcs.h"
34 #include "core/templates/hashfuncs.h"
35
36 class String;
37
38 struct [[nodiscard]] Color {
39 union {
40 // NOLINTBEGIN(modernize-use-default-member-init)
41 struct {
42 float r;
43 float g;
44 float b;
45 float a;
46 };
47 float components[4] = { 0, 0, 0, 1.0 };
48 // NOLINTEND(modernize-use-default-member-init)
49 };
50
51 uint32_t to_rgba32() const;
52 uint32_t to_argb32() const;
53 uint32_t to_abgr32() const;
54 uint64_t to_rgba64() const;
55 uint64_t to_argb64() const;
56 uint64_t to_abgr64() const;
57 String to_html(bool p_alpha = true) const;
58 float get_h() const;
59 float get_s() const;
60 float get_v() const;
61 void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
62 float get_ok_hsl_h() const;
63 float get_ok_hsl_s() const;
64 float get_ok_hsl_l() const;
65 void set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f);
66 void set_ok_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
67
68 _FORCE_INLINE_ float &operator[](int p_idx) {
69 return components[p_idx];
70 }
71 _FORCE_INLINE_ const float &operator[](int p_idx) const {
72 return components[p_idx];
73 }
74
75 constexpr bool operator==(const Color &p_color) const {
76 return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a);
77 }
78 constexpr bool operator!=(const Color &p_color) const {
79 return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a);
80 }
81
82 constexpr Color operator+(const Color &p_color) const;
83 constexpr void operator+=(const Color &p_color);
84
85 constexpr Color operator-() const;
86 constexpr Color operator-(const Color &p_color) const;
87 constexpr void operator-=(const Color &p_color);
88
89 constexpr Color operator*(const Color &p_color) const;
90 constexpr Color operator*(float p_scalar) const;
91 constexpr void operator*=(const Color &p_color);
92 constexpr void operator*=(float p_scalar);
93
94 constexpr Color operator/(const Color &p_color) const;
95 constexpr Color operator/(float p_scalar) const;
96 constexpr void operator/=(const Color &p_color);
97 constexpr void operator/=(float p_scalar);
98
99 bool is_equal_approx(const Color &p_color) const;
100 bool is_same(const Color &p_color) const;
101
102 Color clamp(const Color &p_min = Color(0, 0, 0, 0), const Color &p_max = Color(1, 1, 1, 1)) const;
103 void invert();
104 Color inverted() const;
105
106 _FORCE_INLINE_ float get_luminance() const {
107 return 0.2126f * r + 0.7152f * g + 0.0722f * b;
108 }
109
110 _FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
111 Color res = *this;
112 res.r = Math::lerp(res.r, p_to.r, p_weight);
113 res.g = Math::lerp(res.g, p_to.g, p_weight);
114 res.b = Math::lerp(res.b, p_to.b, p_weight);
115 res.a = Math::lerp(res.a, p_to.a, p_weight);
116 return res;
117 }
118
119 _FORCE_INLINE_ Color darkened(float p_amount) const {
120 Color res = *this;
121 res.r = res.r * (1.0f - p_amount);
122 res.g = res.g * (1.0f - p_amount);
123 res.b = res.b * (1.0f - p_amount);
124 return res;
125 }
126
127 _FORCE_INLINE_ Color lightened(float p_amount) const {
128 Color res = *this;
129 res.r = res.r + (1.0f - res.r) * p_amount;
130 res.g = res.g + (1.0f - res.g) * p_amount;
131 res.b = res.b + (1.0f - res.b) * p_amount;
132 return res;
133 }
134
135 _FORCE_INLINE_ uint32_t to_rgbe9995() const {
136 // https://github.com/microsoft/DirectX-Graphics-Samples/blob/v10.0.19041.0/MiniEngine/Core/Color.cpp
137 static const float kMaxVal = float(0x1FF << 7);
138 static const float kMinVal = float(1.f / (1 << 16));
139
140 // Clamp RGB to [0, 1.FF*2^16]
141 const float _r = CLAMP(r, 0.0f, kMaxVal);
142 const float _g = CLAMP(g, 0.0f, kMaxVal);
143 const float _b = CLAMP(b, 0.0f, kMaxVal);
144
145 // Compute the maximum channel, no less than 1.0*2^-15
146 const float MaxChannel = MAX(MAX(_r, _g), MAX(_b, kMinVal));
147
148 // Take the exponent of the maximum channel (rounding up the 9th bit) and
149 // add 15 to it. When added to the channels, it causes the implicit '1.0'
150 // bit and the first 8 mantissa bits to be shifted down to the low 9 bits
151 // of the mantissa, rounding the truncated bits.
152 union {
153 float f;
154 uint32_t i;
155 } R, G, B, E;
156
157 E.f = MaxChannel;
158 E.i += 0x07804000; // Add 15 to the exponent and 0x4000 to the mantissa
159 E.i &= 0x7F800000; // Zero the mantissa
160
161 // This shifts the 9-bit values we need into the lowest bits, rounding as
162 // needed. Note that if the channel has a smaller exponent than the max
163 // channel, it will shift even more. This is intentional.
164 R.f = _r + E.f;
165 G.f = _g + E.f;
166 B.f = _b + E.f;
167
168 // Convert the Bias to the correct exponent in the upper 5 bits.
169 E.i <<= 4;
170 E.i += 0x10000000;
171
172 // Combine the fields. RGB floats have unwanted data in the upper 9
173 // bits. Only red needs to mask them off because green and blue shift
174 // it out to the left.
175 return E.i | (B.i << 18U) | (G.i << 9U) | (R.i & 511U);
176 }
177
178 _FORCE_INLINE_ Color blend(const Color &p_over) const {
179 Color res;
180 float sa = 1.0f - p_over.a;
181 res.a = a * sa + p_over.a;
182 if (res.a == 0) {
183 return Color(0, 0, 0, 0);
184 } else {
185 res.r = (r * a * sa + p_over.r * p_over.a) / res.a;
186 res.g = (g * a * sa + p_over.g * p_over.a) / res.a;
187 res.b = (b * a * sa + p_over.b * p_over.a) / res.a;
188 }
189 return res;
190 }
191
192 _FORCE_INLINE_ Color srgb_to_linear() const {
193 return Color(
194 r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow(float((r + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
195 g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow(float((g + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
196 b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow(float((b + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
197 a);
198 }
199 _FORCE_INLINE_ Color linear_to_srgb() const {
200 return Color(
201 r < 0.0031308f ? 12.92f * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
202 g < 0.0031308f ? 12.92f * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
203 b < 0.0031308f ? 12.92f * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
204 }
205
206 static Color hex(uint32_t p_hex);
207 static Color hex64(uint64_t p_hex);
208 static Color html(const String &p_rgba);
209 static bool html_is_valid(const String &p_color);
210 static Color named(const String &p_name);
211 static Color named(const String &p_name, const Color &p_default);
212 static int find_named_color(const String &p_name);
213 static int get_named_color_count();
214 static String get_named_color_name(int p_idx);
215 static Color get_named_color(int p_idx);
216 static Color from_string(const String &p_string, const Color &p_default);
217 static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
218 static Color from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f);
219 static Color from_ok_hsv(float p_h, float p_s, float p_l, float p_alpha = 1.0f);
220 static Color from_rgbe9995(uint32_t p_rgbe);
221 static Color from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8 = 255);
222
223 constexpr bool operator<(const Color &p_color) const; // Used in set keys.
224 explicit operator String() const;
225
226 // For the binder.
227 _FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0f); }
228 _FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(Math::round(r * 255.0f), 0.0f, 255.0f)); }
229 _FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0f); }
230 _FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(Math::round(g * 255.0f), 0.0f, 255.0f)); }
231 _FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0f); }
232 _FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(Math::round(b * 255.0f), 0.0f, 255.0f)); }
233 _FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0f); }
234 _FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(Math::round(a * 255.0f), 0.0f, 255.0f)); }
235
236 _FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v(), a); }
237 _FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v(), a); }
238 _FORCE_INLINE_ void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v, a); }
239 _FORCE_INLINE_ void set_ok_hsl_h(float p_h) { set_ok_hsl(p_h, get_ok_hsl_s(), get_ok_hsl_l(), a); }
240 _FORCE_INLINE_ void set_ok_hsl_s(float p_s) { set_ok_hsl(get_ok_hsl_h(), p_s, get_ok_hsl_l(), a); }
241 _FORCE_INLINE_ void set_ok_hsl_l(float p_l) { set_ok_hsl(get_ok_hsl_h(), get_ok_hsl_s(), p_l, a); }
242
243 uint32_t hash() const {
244 uint32_t h = hash_murmur3_one_float(r);
245 h = hash_murmur3_one_float(r, h);
246 h = hash_murmur3_one_float(b, h);
247 h = hash_murmur3_one_float(a, h);
248 return hash_fmix32(h);
249 }
250
251 constexpr Color() :
252 r(0), g(0), b(0), a(1) {}
253
254 /**
255 * RGBA construct parameters.
256 * Alpha is not optional as otherwise we can't bind the RGB version for scripting.
257 */
258 constexpr Color(float p_r, float p_g, float p_b, float p_a) :
259 r(p_r), g(p_g), b(p_b), a(p_a) {}
260
261 /**
262 * RGB construct parameters.
263 */
264 constexpr Color(float p_r, float p_g, float p_b) :
265 r(p_r), g(p_g), b(p_b), a(1) {}
266
267 /**
268 * Construct a Color from another Color, but with the specified alpha value.
269 */
270 constexpr Color(const Color &p_c, float p_a) :
271 r(p_c.r), g(p_c.g), b(p_c.b), a(p_a) {}
272
273 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
274 Color(const String &p_code) {
275 if (html_is_valid(p_code)) {
276 *this = html(p_code);
277 } else {
278 *this = named(p_code);
279 }
280 }
281
282 Color(const String &p_code, float p_a) {
283 *this = Color(p_code);
284 a = p_a;
285 }
286 // NOLINTEND(cppcoreguidelines-pro-type-member-init)
287 };
288
289 constexpr Color Color::operator+(const Color &p_color) const {
290 return Color(
291 r + p_color.r,
292 g + p_color.g,
293 b + p_color.b,
294 a + p_color.a);
295 }
296
297 constexpr void Color::operator+=(const Color &p_color) {
298 r = r + p_color.r;
299 g = g + p_color.g;
300 b = b + p_color.b;
301 a = a + p_color.a;
302 }
303
304 constexpr Color Color::operator-(const Color &p_color) const {
305 return Color(
306 r - p_color.r,
307 g - p_color.g,
308 b - p_color.b,
309 a - p_color.a);
310 }
311
312 constexpr void Color::operator-=(const Color &p_color) {
313 r = r - p_color.r;
314 g = g - p_color.g;
315 b = b - p_color.b;
316 a = a - p_color.a;
317 }
318
319 constexpr Color Color::operator*(const Color &p_color) const {
320 return Color(
321 r * p_color.r,
322 g * p_color.g,
323 b * p_color.b,
324 a * p_color.a);
325 }
326
327 constexpr Color Color::operator*(float p_scalar) const {
328 return Color(
329 r * p_scalar,
330 g * p_scalar,
331 b * p_scalar,
332 a * p_scalar);
333 }
334
335 constexpr void Color::operator*=(const Color &p_color) {
336 r = r * p_color.r;
337 g = g * p_color.g;
338 b = b * p_color.b;
339 a = a * p_color.a;
340 }
341
342 constexpr void Color::operator*=(float p_scalar) {
343 r = r * p_scalar;
344 g = g * p_scalar;
345 b = b * p_scalar;
346 a = a * p_scalar;
347 }
348
349 constexpr Color Color::operator/(const Color &p_color) const {
350 return Color(
351 r / p_color.r,
352 g / p_color.g,
353 b / p_color.b,
354 a / p_color.a);
355 }
356
357 constexpr Color Color::operator/(float p_scalar) const {
358 return Color(
359 r / p_scalar,
360 g / p_scalar,
361 b / p_scalar,
362 a / p_scalar);
363 }
364
365 constexpr void Color::operator/=(const Color &p_color) {
366 r = r / p_color.r;
367 g = g / p_color.g;
368 b = b / p_color.b;
369 a = a / p_color.a;
370 }
371
372 constexpr void Color::operator/=(float p_scalar) {
373 r = r / p_scalar;
374 g = g / p_scalar;
375 b = b / p_scalar;
376 a = a / p_scalar;
377 }
378
379 constexpr Color Color::operator-() const {
380 return Color(
381 1.0f - r,
382 1.0f - g,
383 1.0f - b,
384 1.0f - a);
385 }
386
387 constexpr bool Color::operator<(const Color &p_color) const {
388 if (r == p_color.r) {
389 if (g == p_color.g) {
390 if (b == p_color.b) {
391 return (a < p_color.a);
392 } else {
393 return (b < p_color.b);
394 }
395 } else {
396 return g < p_color.g;
397 }
398 } else {
399 return r < p_color.r;
400 }
401 }
402
403 constexpr Color operator*(float p_scalar, const Color &p_color) {
404 return p_color * p_scalar;
405 }