color.hpp 9.3 KB

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