color.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**************************************************************************/
  2. /* color.hpp */
  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. #pragma once
  31. #include <godot_cpp/core/math.hpp>
  32. namespace godot {
  33. class String;
  34. struct [[nodiscard]] Color {
  35. union {
  36. struct {
  37. float r;
  38. float g;
  39. float b;
  40. float a;
  41. };
  42. float components[4] = { 0, 0, 0, 1.0 };
  43. };
  44. uint32_t to_rgba32() const;
  45. uint32_t to_argb32() const;
  46. uint32_t to_abgr32() const;
  47. uint64_t to_rgba64() const;
  48. uint64_t to_argb64() const;
  49. uint64_t to_abgr64() const;
  50. String to_html(bool p_alpha = true) const;
  51. float get_h() const;
  52. float get_s() const;
  53. float get_v() const;
  54. void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
  55. _FORCE_INLINE_ float &operator[](int p_idx) {
  56. return components[p_idx];
  57. }
  58. _FORCE_INLINE_ const float &operator[](int p_idx) const {
  59. return components[p_idx];
  60. }
  61. bool operator==(const Color &p_color) const {
  62. return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a);
  63. }
  64. bool operator!=(const Color &p_color) const {
  65. return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a);
  66. }
  67. Color operator+(const Color &p_color) const;
  68. void operator+=(const Color &p_color);
  69. Color operator-() const;
  70. Color operator-(const Color &p_color) const;
  71. void operator-=(const Color &p_color);
  72. Color operator*(const Color &p_color) const;
  73. Color operator*(float p_scalar) const;
  74. void operator*=(const Color &p_color);
  75. void operator*=(float p_scalar);
  76. Color operator/(const Color &p_color) const;
  77. Color operator/(float p_scalar) const;
  78. void operator/=(const Color &p_color);
  79. void operator/=(float p_scalar);
  80. bool is_equal_approx(const Color &p_color) const;
  81. Color clamp(const Color &p_min = Color(0, 0, 0, 0), const Color &p_max = Color(1, 1, 1, 1)) const;
  82. void invert();
  83. Color inverted() const;
  84. _FORCE_INLINE_ float get_luminance() const {
  85. return 0.2126f * r + 0.7152f * g + 0.0722f * b;
  86. }
  87. _FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
  88. Color res = *this;
  89. res.r = Math::lerp(res.r, p_to.r, p_weight);
  90. res.g = Math::lerp(res.g, p_to.g, p_weight);
  91. res.b = Math::lerp(res.b, p_to.b, p_weight);
  92. res.a = Math::lerp(res.a, p_to.a, p_weight);
  93. return res;
  94. }
  95. _FORCE_INLINE_ Color darkened(float p_amount) const {
  96. Color res = *this;
  97. res.r = res.r * (1.0f - p_amount);
  98. res.g = res.g * (1.0f - p_amount);
  99. res.b = res.b * (1.0f - p_amount);
  100. return res;
  101. }
  102. _FORCE_INLINE_ Color lightened(float p_amount) const {
  103. Color res = *this;
  104. res.r = res.r + (1.0f - res.r) * p_amount;
  105. res.g = res.g + (1.0f - res.g) * p_amount;
  106. res.b = res.b + (1.0f - res.b) * p_amount;
  107. return res;
  108. }
  109. _FORCE_INLINE_ uint32_t to_rgbe9995() const {
  110. // https://github.com/microsoft/DirectX-Graphics-Samples/blob/v10.0.19041.0/MiniEngine/Core/Color.cpp
  111. static const float kMaxVal = float(0x1FF << 7);
  112. static const float kMinVal = float(1.f / (1 << 16));
  113. // Clamp RGB to [0, 1.FF*2^16]
  114. const float _r = CLAMP(r, 0.0f, kMaxVal);
  115. const float _g = CLAMP(g, 0.0f, kMaxVal);
  116. const float _b = CLAMP(b, 0.0f, kMaxVal);
  117. // Compute the maximum channel, no less than 1.0*2^-15
  118. const float MaxChannel = MAX(MAX(_r, _g), MAX(_b, kMinVal));
  119. // Take the exponent of the maximum channel (rounding up the 9th bit) and
  120. // add 15 to it. When added to the channels, it causes the implicit '1.0'
  121. // bit and the first 8 mantissa bits to be shifted down to the low 9 bits
  122. // of the mantissa, rounding the truncated bits.
  123. union {
  124. float f;
  125. uint32_t i;
  126. } R, G, B, E;
  127. E.f = MaxChannel;
  128. E.i += 0x07804000; // Add 15 to the exponent and 0x4000 to the mantissa
  129. E.i &= 0x7F800000; // Zero the mantissa
  130. // This shifts the 9-bit values we need into the lowest bits, rounding as
  131. // needed. Note that if the channel has a smaller exponent than the max
  132. // channel, it will shift even more. This is intentional.
  133. R.f = _r + E.f;
  134. G.f = _g + E.f;
  135. B.f = _b + E.f;
  136. // Convert the Bias to the correct exponent in the upper 5 bits.
  137. E.i <<= 4;
  138. E.i += 0x10000000;
  139. // Combine the fields. RGB floats have unwanted data in the upper 9
  140. // bits. Only red needs to mask them off because green and blue shift
  141. // it out to the left.
  142. return E.i | (B.i << 18U) | (G.i << 9U) | (R.i & 511U);
  143. }
  144. _FORCE_INLINE_ Color blend(const Color &p_over) const {
  145. Color res;
  146. float sa = 1.0f - p_over.a;
  147. res.a = a * sa + p_over.a;
  148. if (res.a == 0) {
  149. return Color(0, 0, 0, 0);
  150. } else {
  151. res.r = (r * a * sa + p_over.r * p_over.a) / res.a;
  152. res.g = (g * a * sa + p_over.g * p_over.a) / res.a;
  153. res.b = (b * a * sa + p_over.b * p_over.a) / res.a;
  154. }
  155. return res;
  156. }
  157. _FORCE_INLINE_ Color srgb_to_linear() const {
  158. return Color(
  159. r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow(float((r + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
  160. g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow(float((g + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
  161. b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow(float((b + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
  162. a);
  163. }
  164. _FORCE_INLINE_ Color linear_to_srgb() const {
  165. return Color(
  166. r < 0.0031308f ? 12.92f * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
  167. g < 0.0031308f ? 12.92f * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
  168. b < 0.0031308f ? 12.92f * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
  169. }
  170. static Color hex(uint32_t p_hex);
  171. static Color hex64(uint64_t p_hex);
  172. static Color html(const String &p_rgba);
  173. static bool html_is_valid(const String &p_color);
  174. static Color named(const String &p_name);
  175. static Color named(const String &p_name, const Color &p_default);
  176. static int find_named_color(const String &p_name);
  177. static int get_named_color_count();
  178. static String get_named_color_name(int p_idx);
  179. static Color get_named_color(int p_idx);
  180. static Color from_string(const String &p_string, const Color &p_default);
  181. static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
  182. static Color from_rgbe9995(uint32_t p_rgbe);
  183. static Color from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8 = 255);
  184. _FORCE_INLINE_ bool operator<(const Color &p_color) const; // Used in set keys.
  185. operator String() const;
  186. // For the binder.
  187. _FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0f); }
  188. _FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(Math::round(r * 255.0f), 0.0f, 255.0f)); }
  189. _FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0f); }
  190. _FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(Math::round(g * 255.0f), 0.0f, 255.0f)); }
  191. _FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0f); }
  192. _FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(Math::round(b * 255.0f), 0.0f, 255.0f)); }
  193. _FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0f); }
  194. _FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(Math::round(a * 255.0f), 0.0f, 255.0f)); }
  195. _FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v(), a); }
  196. _FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v(), a); }
  197. _FORCE_INLINE_ void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v, a); }
  198. _FORCE_INLINE_ Color() {}
  199. /**
  200. * RGBA construct parameters.
  201. * Alpha is not optional as otherwise we can't bind the RGB version for scripting.
  202. */
  203. _FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a) {
  204. r = p_r;
  205. g = p_g;
  206. b = p_b;
  207. a = p_a;
  208. }
  209. /**
  210. * RGB construct parameters.
  211. */
  212. _FORCE_INLINE_ Color(float p_r, float p_g, float p_b) {
  213. r = p_r;
  214. g = p_g;
  215. b = p_b;
  216. a = 1.0f;
  217. }
  218. /**
  219. * Construct a Color from another Color, but with the specified alpha value.
  220. */
  221. _FORCE_INLINE_ Color(const Color &p_c, float p_a) {
  222. r = p_c.r;
  223. g = p_c.g;
  224. b = p_c.b;
  225. a = p_a;
  226. }
  227. Color(const String &p_code) {
  228. if (html_is_valid(p_code)) {
  229. *this = html(p_code);
  230. } else {
  231. *this = named(p_code);
  232. }
  233. }
  234. Color(const String &p_code, float p_a) {
  235. *this = Color(p_code);
  236. a = p_a;
  237. }
  238. };
  239. bool Color::operator<(const Color &p_color) const {
  240. if (r == p_color.r) {
  241. if (g == p_color.g) {
  242. if (b == p_color.b) {
  243. return (a < p_color.a);
  244. } else {
  245. return (b < p_color.b);
  246. }
  247. } else {
  248. return g < p_color.g;
  249. }
  250. } else {
  251. return r < p_color.r;
  252. }
  253. }
  254. _FORCE_INLINE_ Color operator*(float p_scalar, const Color &p_color) {
  255. return p_color * p_scalar;
  256. }
  257. } // namespace godot