color.hpp 7.2 KB

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