color.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #ifndef COLOR_H
  31. #define COLOR_H
  32. #include "core/math/math_funcs.h"
  33. #include "core/ustring.h"
  34. struct _NO_DISCARD_CLASS_ Color {
  35. union {
  36. struct {
  37. float r;
  38. float g;
  39. float b;
  40. float a;
  41. };
  42. float components[4];
  43. };
  44. bool operator==(const Color &p_color) const { return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a); }
  45. bool operator!=(const Color &p_color) const { return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a); }
  46. uint32_t to_rgba32() const;
  47. uint32_t to_argb32() const;
  48. uint32_t to_abgr32() const;
  49. uint64_t to_rgba64() const;
  50. uint64_t to_argb64() const;
  51. uint64_t to_abgr64() const;
  52. float gray() 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. _FORCE_INLINE_ float &operator[](int p_idx) {
  58. return components[p_idx];
  59. }
  60. _FORCE_INLINE_ const float &operator[](int p_idx) const {
  61. return components[p_idx];
  62. }
  63. Color operator+(const Color &p_color) const;
  64. void operator+=(const Color &p_color);
  65. Color operator-() const;
  66. Color operator-(const Color &p_color) const;
  67. void operator-=(const Color &p_color);
  68. Color operator*(const Color &p_color) const;
  69. Color operator*(real_t p_scalar) const;
  70. void operator*=(const Color &p_color);
  71. void operator*=(real_t p_scalar);
  72. Color operator/(const Color &p_color) const;
  73. Color operator/(real_t p_scalar) const;
  74. void operator/=(const Color &p_color);
  75. void operator/=(real_t p_scalar);
  76. bool is_equal_approx(const Color &p_color) const;
  77. void invert();
  78. void contrast();
  79. Color inverted() const;
  80. Color contrasted() const;
  81. _FORCE_INLINE_ float get_luminance() const {
  82. return 0.2126 * r + 0.7152 * g + 0.0722 * b;
  83. }
  84. _FORCE_INLINE_ Color linear_interpolate(const Color &p_to, float p_weight) const {
  85. Color res = *this;
  86. res.r += (p_weight * (p_to.r - r));
  87. res.g += (p_weight * (p_to.g - g));
  88. res.b += (p_weight * (p_to.b - b));
  89. res.a += (p_weight * (p_to.a - a));
  90. return res;
  91. }
  92. _FORCE_INLINE_ Color darkened(float p_amount) const {
  93. Color res = *this;
  94. res.r = res.r * (1.0f - p_amount);
  95. res.g = res.g * (1.0f - p_amount);
  96. res.b = res.b * (1.0f - p_amount);
  97. return res;
  98. }
  99. _FORCE_INLINE_ Color lightened(float p_amount) const {
  100. Color res = *this;
  101. res.r = res.r + (1.0f - res.r) * p_amount;
  102. res.g = res.g + (1.0f - res.g) * p_amount;
  103. res.b = res.b + (1.0f - res.b) * p_amount;
  104. return res;
  105. }
  106. _FORCE_INLINE_ uint32_t to_rgbe9995() const {
  107. const float pow2to9 = 512.0f;
  108. const float B = 15.0f;
  109. //const float Emax = 31.0f;
  110. const float N = 9.0f;
  111. float sharedexp = 65408.000f; //(( pow2to9 - 1.0f)/ pow2to9)*powf( 2.0f, 31.0f - 15.0f);
  112. float cRed = MAX(0.0f, MIN(sharedexp, r));
  113. float cGreen = MAX(0.0f, MIN(sharedexp, g));
  114. float cBlue = MAX(0.0f, MIN(sharedexp, b));
  115. float cMax = MAX(cRed, MAX(cGreen, cBlue));
  116. // expp = MAX(-B - 1, log2(maxc)) + 1 + B
  117. float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / Math_LN2)) + 1.0f + B;
  118. float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
  119. float exps = expp + 1.0f;
  120. if (0.0 <= sMax && sMax < pow2to9) {
  121. exps = expp;
  122. }
  123. float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
  124. float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
  125. float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
  126. 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);
  127. }
  128. _FORCE_INLINE_ Color blend(const Color &p_over) const {
  129. Color res;
  130. float sa = 1.0 - p_over.a;
  131. res.a = a * sa + p_over.a;
  132. if (res.a == 0) {
  133. return Color(0, 0, 0, 0);
  134. } else {
  135. res.r = (r * a * sa + p_over.r * p_over.a) / res.a;
  136. res.g = (g * a * sa + p_over.g * p_over.a) / res.a;
  137. res.b = (b * a * sa + p_over.b * p_over.a) / res.a;
  138. }
  139. return res;
  140. }
  141. _FORCE_INLINE_ Color to_linear() const {
  142. return Color(
  143. r < 0.04045 ? r * (1.0 / 12.92) : Math::pow((r + 0.055) * (1.0 / (1 + 0.055)), 2.4),
  144. g < 0.04045 ? g * (1.0 / 12.92) : Math::pow((g + 0.055) * (1.0 / (1 + 0.055)), 2.4),
  145. b < 0.04045 ? b * (1.0 / 12.92) : Math::pow((b + 0.055) * (1.0 / (1 + 0.055)), 2.4),
  146. a);
  147. }
  148. _FORCE_INLINE_ Color to_srgb() const {
  149. return Color(
  150. r < 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
  151. g < 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
  152. b < 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
  153. }
  154. static Color hex(uint32_t p_hex);
  155. static Color hex64(uint64_t p_hex);
  156. static Color html(const String &p_color);
  157. static bool html_is_valid(const String &p_color);
  158. static Color named(const String &p_name);
  159. String to_html(bool p_alpha = true) const;
  160. Color from_hsv(float p_h, float p_s, float p_v, float p_a) const;
  161. static Color from_rgbe9995(uint32_t p_rgbe);
  162. _FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys
  163. operator String() const;
  164. /**
  165. * No construct parameters, r=0, g=0, b=0. a=255
  166. */
  167. _FORCE_INLINE_ Color() {
  168. r = 0;
  169. g = 0;
  170. b = 0;
  171. a = 1.0;
  172. }
  173. /**
  174. * RGB / RGBA construct parameters. Alpha is optional, but defaults to 1.0
  175. */
  176. _FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a = 1.0) {
  177. r = p_r;
  178. g = p_g;
  179. b = p_b;
  180. a = p_a;
  181. }
  182. };
  183. bool Color::operator<(const Color &p_color) const {
  184. if (r == p_color.r) {
  185. if (g == p_color.g) {
  186. if (b == p_color.b) {
  187. return (a < p_color.a);
  188. } else {
  189. return (b < p_color.b);
  190. }
  191. } else {
  192. return g < p_color.g;
  193. }
  194. } else {
  195. return r < p_color.r;
  196. }
  197. }
  198. #endif // COLOR_H