color.hpp 9.8 KB

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