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 | #ifndef COLOR_H |
32 | #define COLOR_H |
33 | |
34 | #include "core/math/math_funcs.h" |
35 | |
36 | class String; |
37 | |
38 | struct _NO_DISCARD_ Color { |
39 | union { |
40 | struct { |
41 | float r; |
42 | float g; |
43 | float b; |
44 | float a; |
45 | }; |
46 | float components[4] = { 0, 0, 0, 1.0 }; |
47 | }; |
48 | |
49 | uint32_t to_rgba32() const; |
50 | uint32_t to_argb32() const; |
51 | uint32_t to_abgr32() const; |
52 | uint64_t to_rgba64() const; |
53 | uint64_t to_argb64() const; |
54 | uint64_t to_abgr64() const; |
55 | String to_html(bool p_alpha = true) const; |
56 | float get_h() const; |
57 | float get_s() const; |
58 | float get_v() const; |
59 | void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f); |
60 | float get_ok_hsl_h() const; |
61 | float get_ok_hsl_s() const; |
62 | float get_ok_hsl_l() const; |
63 | void set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f); |
64 | |
65 | _FORCE_INLINE_ float &operator[](int p_idx) { |
66 | return components[p_idx]; |
67 | } |
68 | _FORCE_INLINE_ const float &operator[](int p_idx) const { |
69 | return components[p_idx]; |
70 | } |
71 | |
72 | bool operator==(const Color &p_color) const { |
73 | return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a); |
74 | } |
75 | 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 | |
79 | Color operator+(const Color &p_color) const; |
80 | void operator+=(const Color &p_color); |
81 | |
82 | Color operator-() const; |
83 | Color operator-(const Color &p_color) const; |
84 | void operator-=(const Color &p_color); |
85 | |
86 | Color operator*(const Color &p_color) const; |
87 | Color operator*(float p_scalar) const; |
88 | void operator*=(const Color &p_color); |
89 | void operator*=(float p_scalar); |
90 | |
91 | Color operator/(const Color &p_color) const; |
92 | Color operator/(float p_scalar) const; |
93 | void operator/=(const Color &p_color); |
94 | void operator/=(float p_scalar); |
95 | |
96 | bool is_equal_approx(const Color &p_color) const; |
97 | |
98 | Color clamp(const Color &p_min = Color(0, 0, 0, 0), const Color &p_max = Color(1, 1, 1, 1)) const; |
99 | void invert(); |
100 | Color inverted() const; |
101 | |
102 | _FORCE_INLINE_ float get_luminance() const { |
103 | return 0.2126f * r + 0.7152f * g + 0.0722f * b; |
104 | } |
105 | |
106 | _FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const { |
107 | Color res = *this; |
108 | res.r = Math::lerp(res.r, p_to.r, p_weight); |
109 | res.g = Math::lerp(res.g, p_to.g, p_weight); |
110 | res.b = Math::lerp(res.b, p_to.b, p_weight); |
111 | res.a = Math::lerp(res.a, p_to.a, p_weight); |
112 | return res; |
113 | } |
114 | |
115 | _FORCE_INLINE_ Color darkened(float p_amount) const { |
116 | Color res = *this; |
117 | res.r = res.r * (1.0f - p_amount); |
118 | res.g = res.g * (1.0f - p_amount); |
119 | res.b = res.b * (1.0f - p_amount); |
120 | return res; |
121 | } |
122 | |
123 | _FORCE_INLINE_ Color lightened(float p_amount) const { |
124 | Color res = *this; |
125 | res.r = res.r + (1.0f - res.r) * p_amount; |
126 | res.g = res.g + (1.0f - res.g) * p_amount; |
127 | res.b = res.b + (1.0f - res.b) * p_amount; |
128 | return res; |
129 | } |
130 | |
131 | _FORCE_INLINE_ uint32_t to_rgbe9995() const { |
132 | const float pow2to9 = 512.0f; |
133 | const float B = 15.0f; |
134 | const float N = 9.0f; |
135 | |
136 | float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f) |
137 | |
138 | float cRed = MAX(0.0f, MIN(sharedexp, r)); |
139 | float cGreen = MAX(0.0f, MIN(sharedexp, g)); |
140 | float cBlue = MAX(0.0f, MIN(sharedexp, b)); |
141 | |
142 | float cMax = MAX(cRed, MAX(cGreen, cBlue)); |
143 | |
144 | float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B; |
145 | |
146 | float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f); |
147 | |
148 | float exps = expp + 1.0f; |
149 | |
150 | if (0.0f <= sMax && sMax < pow2to9) { |
151 | exps = expp; |
152 | } |
153 | |
154 | float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f); |
155 | float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f); |
156 | float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f); |
157 | |
158 | return (uint32_t(Math::fast_ftoi(sRed)) & 0x1FF) | ((uint32_t(Math::fast_ftoi(sGreen)) & 0x1FF) << 9) | ((uint32_t(Math::fast_ftoi(sBlue)) & 0x1FF) << 18) | ((uint32_t(Math::fast_ftoi(exps)) & 0x1F) << 27); |
159 | } |
160 | |
161 | _FORCE_INLINE_ Color blend(const Color &p_over) const { |
162 | Color res; |
163 | float sa = 1.0f - p_over.a; |
164 | res.a = a * sa + p_over.a; |
165 | if (res.a == 0) { |
166 | return Color(0, 0, 0, 0); |
167 | } else { |
168 | res.r = (r * a * sa + p_over.r * p_over.a) / res.a; |
169 | res.g = (g * a * sa + p_over.g * p_over.a) / res.a; |
170 | res.b = (b * a * sa + p_over.b * p_over.a) / res.a; |
171 | } |
172 | return res; |
173 | } |
174 | |
175 | _FORCE_INLINE_ Color srgb_to_linear() const { |
176 | return Color( |
177 | r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow((r + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f), |
178 | g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow((g + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f), |
179 | b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow((b + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f), |
180 | a); |
181 | } |
182 | _FORCE_INLINE_ Color linear_to_srgb() const { |
183 | return Color( |
184 | r < 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * Math::pow(r, 1.0f / 2.4f) - 0.055f, |
185 | g < 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * Math::pow(g, 1.0f / 2.4f) - 0.055f, |
186 | b < 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * Math::pow(b, 1.0f / 2.4f) - 0.055f, a); |
187 | } |
188 | |
189 | static Color hex(uint32_t p_hex); |
190 | static Color hex64(uint64_t p_hex); |
191 | static Color html(const String &p_rgba); |
192 | static bool html_is_valid(const String &p_color); |
193 | static Color named(const String &p_name); |
194 | static Color named(const String &p_name, const Color &p_default); |
195 | static int find_named_color(const String &p_name); |
196 | static int get_named_color_count(); |
197 | static String get_named_color_name(int p_idx); |
198 | static Color get_named_color(int p_idx); |
199 | static Color from_string(const String &p_string, const Color &p_default); |
200 | static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f); |
201 | static Color from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f); |
202 | static Color from_rgbe9995(uint32_t p_rgbe); |
203 | |
204 | _FORCE_INLINE_ bool operator<(const Color &p_color) const; // Used in set keys. |
205 | operator String() const; |
206 | |
207 | // For the binder. |
208 | _FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0f); } |
209 | _FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(Math::round(r * 255.0f), 0.0f, 255.0f)); } |
210 | _FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0f); } |
211 | _FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(Math::round(g * 255.0f), 0.0f, 255.0f)); } |
212 | _FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0f); } |
213 | _FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(Math::round(b * 255.0f), 0.0f, 255.0f)); } |
214 | _FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0f); } |
215 | _FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(Math::round(a * 255.0f), 0.0f, 255.0f)); } |
216 | |
217 | _FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v(), a); } |
218 | _FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v(), a); } |
219 | _FORCE_INLINE_ void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v, a); } |
220 | _FORCE_INLINE_ void set_ok_hsl_h(float p_h) { set_ok_hsl(p_h, get_ok_hsl_s(), get_ok_hsl_l(), a); } |
221 | _FORCE_INLINE_ void set_ok_hsl_s(float p_s) { set_ok_hsl(get_ok_hsl_h(), p_s, get_ok_hsl_l(), a); } |
222 | _FORCE_INLINE_ void set_ok_hsl_l(float p_l) { set_ok_hsl(get_ok_hsl_h(), get_ok_hsl_s(), p_l, a); } |
223 | |
224 | _FORCE_INLINE_ Color() {} |
225 | |
226 | /** |
227 | * RGBA construct parameters. |
228 | * Alpha is not optional as otherwise we can't bind the RGB version for scripting. |
229 | */ |
230 | _FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a) { |
231 | r = p_r; |
232 | g = p_g; |
233 | b = p_b; |
234 | a = p_a; |
235 | } |
236 | |
237 | /** |
238 | * RGB construct parameters. |
239 | */ |
240 | _FORCE_INLINE_ Color(float p_r, float p_g, float p_b) { |
241 | r = p_r; |
242 | g = p_g; |
243 | b = p_b; |
244 | a = 1.0f; |
245 | } |
246 | |
247 | /** |
248 | * Construct a Color from another Color, but with the specified alpha value. |
249 | */ |
250 | _FORCE_INLINE_ Color(const Color &p_c, float p_a) { |
251 | r = p_c.r; |
252 | g = p_c.g; |
253 | b = p_c.b; |
254 | a = p_a; |
255 | } |
256 | |
257 | Color(const String &p_code) { |
258 | if (html_is_valid(p_code)) { |
259 | *this = html(p_code); |
260 | } else { |
261 | *this = named(p_code); |
262 | } |
263 | } |
264 | |
265 | Color(const String &p_code, float p_a) { |
266 | *this = Color(p_code); |
267 | a = p_a; |
268 | } |
269 | }; |
270 | |
271 | bool Color::operator<(const Color &p_color) const { |
272 | if (r == p_color.r) { |
273 | if (g == p_color.g) { |
274 | if (b == p_color.b) { |
275 | return (a < p_color.a); |
276 | } else { |
277 | return (b < p_color.b); |
278 | } |
279 | } else { |
280 | return g < p_color.g; |
281 | } |
282 | } else { |
283 | return r < p_color.r; |
284 | } |
285 | } |
286 | |
287 | _FORCE_INLINE_ Color operator*(float p_scalar, const Color &p_color) { |
288 | return p_color * p_scalar; |
289 | } |
290 | |
291 | #endif // COLOR_H |
292 | |