Debian Codegraph @

core/math/color.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* color.cpp */
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 #include "color.h"
32
33 #include "color_names.inc"
34 #include "core/math/math_funcs.h"
35 #include "core/string/ustring.h"
36 #include "core/templates/hash_map.h"
37
38 #include "thirdparty/misc/ok_color.h"
39
40 uint32_t Color::to_argb32() const {
41 uint32_t c = (uint8_t)Math::round(a * 255.0f);
42 c <<= 8;
43 c |= (uint8_t)Math::round(r * 255.0f);
44 c <<= 8;
45 c |= (uint8_t)Math::round(g * 255.0f);
46 c <<= 8;
47 c |= (uint8_t)Math::round(b * 255.0f);
48
49 return c;
50 }
51
52 uint32_t Color::to_abgr32() const {
53 uint32_t c = (uint8_t)Math::round(a * 255.0f);
54 c <<= 8;
55 c |= (uint8_t)Math::round(b * 255.0f);
56 c <<= 8;
57 c |= (uint8_t)Math::round(g * 255.0f);
58 c <<= 8;
59 c |= (uint8_t)Math::round(r * 255.0f);
60
61 return c;
62 }
63
64 uint32_t Color::to_rgba32() const {
65 uint32_t c = (uint8_t)Math::round(r * 255.0f);
66 c <<= 8;
67 c |= (uint8_t)Math::round(g * 255.0f);
68 c <<= 8;
69 c |= (uint8_t)Math::round(b * 255.0f);
70 c <<= 8;
71 c |= (uint8_t)Math::round(a * 255.0f);
72
73 return c;
74 }
75
76 uint64_t Color::to_abgr64() const {
77 uint64_t c = (uint16_t)Math::round(a * 65535.0f);
78 c <<= 16;
79 c |= (uint16_t)Math::round(b * 65535.0f);
80 c <<= 16;
81 c |= (uint16_t)Math::round(g * 65535.0f);
82 c <<= 16;
83 c |= (uint16_t)Math::round(r * 65535.0f);
84
85 return c;
86 }
87
88 uint64_t Color::to_argb64() const {
89 uint64_t c = (uint16_t)Math::round(a * 65535.0f);
90 c <<= 16;
91 c |= (uint16_t)Math::round(r * 65535.0f);
92 c <<= 16;
93 c |= (uint16_t)Math::round(g * 65535.0f);
94 c <<= 16;
95 c |= (uint16_t)Math::round(b * 65535.0f);
96
97 return c;
98 }
99
100 uint64_t Color::to_rgba64() const {
101 uint64_t c = (uint16_t)Math::round(r * 65535.0f);
102 c <<= 16;
103 c |= (uint16_t)Math::round(g * 65535.0f);
104 c <<= 16;
105 c |= (uint16_t)Math::round(b * 65535.0f);
106 c <<= 16;
107 c |= (uint16_t)Math::round(a * 65535.0f);
108
109 return c;
110 }
111
112 void _append_hex(float p_val, char32_t *string) {
113 int v = Math::round(p_val * 255.0f);
114 v = CLAMP(v, 0, 255);
115
116 string[0] = hex_char_table_lower[(v >> 4) & 0xF];
117 string[1] = hex_char_table_lower[v & 0xF];
118 }
119
120 String Color::to_html(bool p_alpha) const {
121 String txt;
122 txt.resize_uninitialized(p_alpha ? 9 : 7);
123 char32_t *ptr = txt.ptrw();
124
125 _append_hex(r, ptr + 0);
126 _append_hex(g, ptr + 2);
127 _append_hex(b, ptr + 4);
128 if (p_alpha) {
129 _append_hex(a, ptr + 6);
130 }
131 ptr[txt.size() - 1] = '\0';
132
133 return txt;
134 }
135
136 float Color::get_h() const {
137 float min = MIN(r, g);
138 min = MIN(min, b);
139 float max = MAX(r, g);
140 max = MAX(max, b);
141
142 float delta = max - min;
143
144 if (delta == 0.0f) {
145 return 0.0f;
146 }
147
148 float h;
149 if (r == max) {
150 h = (g - b) / delta; // between yellow & magenta
151 } else if (g == max) {
152 h = 2 + (b - r) / delta; // between cyan & yellow
153 } else {
154 h = 4 + (r - g) / delta; // between magenta & cyan
155 }
156
157 h /= 6.0f;
158 if (h < 0.0f) {
159 h += 1.0f;
160 }
161
162 return h;
163 }
164
165 float Color::get_s() const {
166 float min = MIN(r, g);
167 min = MIN(min, b);
168 float max = MAX(r, g);
169 max = MAX(max, b);
170
171 float delta = max - min;
172
173 return (max != 0.0f) ? (delta / max) : 0.0f;
174 }
175
176 float Color::get_v() const {
177 float max = MAX(r, g);
178 max = MAX(max, b);
179 return max;
180 }
181
182 void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
183 int i;
184 float f, p, q, t;
185 a = p_alpha;
186
187 if (p_s == 0.0f) {
188 // Achromatic (gray)
189 r = g = b = p_v;
190 return;
191 }
192
193 p_h *= 6.0f;
194 p_h = Math::fmod(p_h, 6);
195 i = Math::floor(p_h);
196
197 f = p_h - i;
198 p = p_v * (1.0f - p_s);
199 q = p_v * (1.0f - p_s * f);
200 t = p_v * (1.0f - p_s * (1.0f - f));
201
202 switch (i) {
203 case 0: // Red is the dominant color
204 r = p_v;
205 g = t;
206 b = p;
207 break;
208 case 1: // Green is the dominant color
209 r = q;
210 g = p_v;
211 b = p;
212 break;
213 case 2:
214 r = p;
215 g = p_v;
216 b = t;
217 break;
218 case 3: // Blue is the dominant color
219 r = p;
220 g = q;
221 b = p_v;
222 break;
223 case 4:
224 r = t;
225 g = p;
226 b = p_v;
227 break;
228 default: // (5) Red is the dominant color
229 r = p_v;
230 g = p;
231 b = q;
232 break;
233 }
234 }
235
236 void Color::set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha) {
237 ok_color::HSL hsl;
238 hsl.h = p_h;
239 hsl.s = p_s;
240 hsl.l = p_l;
241 ok_color::RGB rgb = ok_color::okhsl_to_srgb(hsl);
242 Color c = Color(rgb.r, rgb.g, rgb.b, p_alpha).clamp();
243 r = c.r;
244 g = c.g;
245 b = c.b;
246 a = c.a;
247 }
248
249 void Color::set_ok_hsv(float p_h, float p_s, float p_v, float p_alpha) {
250 ok_color::HSV hsv;
251 hsv.h = p_h;
252 hsv.s = p_s;
253 hsv.v = p_v;
254 ok_color::RGB rgb = ok_color::okhsv_to_srgb(hsv);
255 Color c = Color(rgb.r, rgb.g, rgb.b, p_alpha).clamp();
256 r = c.r;
257 g = c.g;
258 b = c.b;
259 a = c.a;
260 }
261
262 bool Color::is_equal_approx(const Color &p_color) const {
263 return Math::is_equal_approx(r, p_color.r) && Math::is_equal_approx(g, p_color.g) && Math::is_equal_approx(b, p_color.b) && Math::is_equal_approx(a, p_color.a);
264 }
265
266 bool Color::is_same(const Color &p_color) const {
267 return Math::is_same(r, p_color.r) && Math::is_same(g, p_color.g) && Math::is_same(b, p_color.b) && Math::is_same(a, p_color.a);
268 }
269
270 Color Color::clamp(const Color &p_min, const Color &p_max) const {
271 return Color(
272 CLAMP(r, p_min.r, p_max.r),
273 CLAMP(g, p_min.g, p_max.g),
274 CLAMP(b, p_min.b, p_max.b),
275 CLAMP(a, p_min.a, p_max.a));
276 }
277
278 void Color::invert() {
279 r = 1.0f - r;
280 g = 1.0f - g;
281 b = 1.0f - b;
282 }
283
284 Color Color::hex(uint32_t p_hex) {
285 float a = (p_hex & 0xFF) / 255.0f;
286 p_hex >>= 8;
287 float b = (p_hex & 0xFF) / 255.0f;
288 p_hex >>= 8;
289 float g = (p_hex & 0xFF) / 255.0f;
290 p_hex >>= 8;
291 float r = (p_hex & 0xFF) / 255.0f;
292
293 return Color(r, g, b, a);
294 }
295
296 Color Color::hex64(uint64_t p_hex) {
297 float a = (p_hex & 0xFFFF) / 65535.0f;
298 p_hex >>= 16;
299 float b = (p_hex & 0xFFFF) / 65535.0f;
300 p_hex >>= 16;
301 float g = (p_hex & 0xFFFF) / 65535.0f;
302 p_hex >>= 16;
303 float r = (p_hex & 0xFFFF) / 65535.0f;
304
305 return Color(r, g, b, a);
306 }
307
308 static int _parse_col4(const String &p_str, int p_ofs) {
309 char character = p_str[p_ofs];
310
311 if (character >= '0' && character <= '9') {
312 return character - '0';
313 } else if (character >= 'a' && character <= 'f') {
314 return character + (10 - 'a');
315 } else if (character >= 'A' && character <= 'F') {
316 return character + (10 - 'A');
317 }
318 return -1;
319 }
320
321 static int _parse_col8(const String &p_str, int p_ofs) {
322 return _parse_col4(p_str, p_ofs) * 16 + _parse_col4(p_str, p_ofs + 1);
323 }
324
325 Color Color::inverted() const {
326 Color c = *this;
327 c.invert();
328 return c;
329 }
330
331 Color Color::html(const String &p_rgba) {
332 if (p_rgba.is_empty()) {
333 return Color();
334 }
335
336 const int current_pos = (p_rgba[0] == '#') ? 1 : 0;
337 const int num_of_digits = p_rgba.length() - current_pos;
338
339 float r, g, b, a = 1.0f;
340
341 if (num_of_digits == 3) {
342 // #rgb
343 r = _parse_col4(p_rgba, current_pos) / 15.0f;
344 g = _parse_col4(p_rgba, current_pos + 1) / 15.0f;
345 b = _parse_col4(p_rgba, current_pos + 2) / 15.0f;
346 } else if (num_of_digits == 4) {
347 r = _parse_col4(p_rgba, current_pos) / 15.0f;
348 g = _parse_col4(p_rgba, current_pos + 1) / 15.0f;
349 b = _parse_col4(p_rgba, current_pos + 2) / 15.0f;
350 a = _parse_col4(p_rgba, current_pos + 3) / 15.0f;
351 } else if (num_of_digits == 6) {
352 r = _parse_col8(p_rgba, current_pos) / 255.0f;
353 g = _parse_col8(p_rgba, current_pos + 2) / 255.0f;
354 b = _parse_col8(p_rgba, current_pos + 4) / 255.0f;
355 } else if (num_of_digits == 8) {
356 r = _parse_col8(p_rgba, current_pos) / 255.0f;
357 g = _parse_col8(p_rgba, current_pos + 2) / 255.0f;
358 b = _parse_col8(p_rgba, current_pos + 4) / 255.0f;
359 a = _parse_col8(p_rgba, current_pos + 6) / 255.0f;
360 } else {
361 ERR_FAIL_V_MSG(Color(), "Invalid color code: " + p_rgba + ".");
362 }
363
364 ERR_FAIL_COND_V_MSG(r < 0.0f, Color(), "Invalid color code: " + p_rgba + ".");
365 ERR_FAIL_COND_V_MSG(g < 0.0f, Color(), "Invalid color code: " + p_rgba + ".");
366 ERR_FAIL_COND_V_MSG(b < 0.0f, Color(), "Invalid color code: " + p_rgba + ".");
367 ERR_FAIL_COND_V_MSG(a < 0.0f, Color(), "Invalid color code: " + p_rgba + ".");
368
369 return Color(r, g, b, a);
370 }
371
372 bool Color::html_is_valid(const String &p_color) {
373 String color = p_color;
374
375 if (color.is_empty()) {
376 return false;
377 }
378
379 const int current_pos = (color[0] == '#') ? 1 : 0;
380 const int len = color.length();
381 const int num_of_digits = len - current_pos;
382 if (!(num_of_digits == 3 || num_of_digits == 4 || num_of_digits == 6 || num_of_digits == 8)) {
383 return false;
384 }
385
386 // Check if each hex digit is valid.
387 for (int i = current_pos; i < len; i++) {
388 if (!is_hex_digit(p_color[i])) {
389 return false;
390 }
391 }
392
393 return true;
394 }
395
396 Color Color::named(const String &p_name) {
397 int idx = find_named_color(p_name);
398 if (idx == -1) {
399 ERR_FAIL_V_MSG(Color(), "Invalid color name: " + p_name + ".");
400 }
401 return named_colors[idx].color;
402 }
403
404 Color Color::named(const String &p_name, const Color &p_default) {
405 int idx = find_named_color(p_name);
406 if (idx == -1) {
407 return p_default;
408 }
409 return named_colors[idx].color;
410 }
411
412 int Color::find_named_color(const String &p_name) {
413 String name = p_name;
414 // Normalize name.
415 name = name.remove_chars(" -_'.");
416 name = name.to_upper();
417
418 static HashMap<String, int> named_colors_hashmap;
419 if (unlikely(named_colors_hashmap.is_empty())) {
420 const int named_color_count = get_named_color_count();
421 for (int i = 0; i < named_color_count; i++) {
422 named_colors_hashmap[String(named_colors[i].name).remove_char('_')] = i;
423 }
424 }
425
426 const HashMap<String, int>::ConstIterator E = named_colors_hashmap.find(name);
427 if (E) {
428 return E->value;
429 }
430
431 return -1;
432 }
433
434 int Color::get_named_color_count() {
435 return std_size(named_colors);
436 }
437
438 String Color::get_named_color_name(int p_idx) {
439 ERR_FAIL_INDEX_V(p_idx, get_named_color_count(), "");
440 return named_colors[p_idx].name;
441 }
442
443 Color Color::get_named_color(int p_idx) {
444 ERR_FAIL_INDEX_V(p_idx, get_named_color_count(), Color());
445 return named_colors[p_idx].color;
446 }
447
448 // For a version that errors on invalid values instead of returning
449 // a default color, use the Color(String) constructor instead.
450 Color Color::from_string(const String &p_string, const Color &p_default) {
451 if (html_is_valid(p_string)) {
452 return html(p_string);
453 } else {
454 return named(p_string, p_default);
455 }
456 }
457
458 Color Color::from_hsv(float p_h, float p_s, float p_v, float p_alpha) {
459 Color c;
460 c.set_hsv(p_h, p_s, p_v, p_alpha);
461 return c;
462 }
463
464 Color Color::from_rgbe9995(uint32_t p_rgbe) {
465 float r = p_rgbe & 0x1ff;
466 float g = (p_rgbe >> 9) & 0x1ff;
467 float b = (p_rgbe >> 18) & 0x1ff;
468 float e = (p_rgbe >> 27);
469 float m = Math::pow(2.0f, e - 15.0f - 9.0f);
470
471 float rd = r * m;
472 float gd = g * m;
473 float bd = b * m;
474
475 return Color(rd, gd, bd, 1.0f);
476 }
477
478 Color Color::from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8) {
479 return Color(p_r8 / 255.0f, p_g8 / 255.0f, p_b8 / 255.0f, p_a8 / 255.0f);
480 }
481
482 Color::operator String() const {
483 return "(" + String::num(r, 4) + ", " + String::num(g, 4) + ", " + String::num(b, 4) + ", " + String::num(a, 4) + ")";
484 }
485
486 Color Color::from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha) {
487 Color c;
488 c.set_ok_hsl(p_h, p_s, p_l, p_alpha);
489 return c;
490 }
491
492 Color Color::from_ok_hsv(float p_h, float p_s, float p_l, float p_alpha) {
493 Color c;
494 c.set_ok_hsv(p_h, p_s, p_l, p_alpha);
495 return c;
496 }
497
498 float Color::get_ok_hsl_h() const {
499 ok_color::RGB rgb;
500 rgb.r = r;
501 rgb.g = g;
502 rgb.b = b;
503 ok_color::HSL ok_hsl = ok_color::srgb_to_okhsl(rgb);
504 if (Math::is_nan(ok_hsl.h)) {
505 return 0.0f;
506 }
507 return CLAMP(ok_hsl.h, 0.0f, 1.0f);
508 }
509
510 float Color::get_ok_hsl_s() const {
511 ok_color::RGB rgb;
512 rgb.r = r;
513 rgb.g = g;
514 rgb.b = b;
515 ok_color::HSL ok_hsl = ok_color::srgb_to_okhsl(rgb);
516 if (Math::is_nan(ok_hsl.s)) {
517 return 0.0f;
518 }
519 return CLAMP(ok_hsl.s, 0.0f, 1.0f);
520 }
521
522 float Color::get_ok_hsl_l() const {
523 ok_color::RGB rgb;
524 rgb.r = r;
525 rgb.g = g;
526 rgb.b = b;
527 ok_color::HSL ok_hsl = ok_color::srgb_to_okhsl(rgb);
528 if (Math::is_nan(ok_hsl.l)) {
529 return 0.0f;
530 }
531 return CLAMP(ok_hsl.l, 0.0f, 1.0f);
532 }