color.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*************************************************************************/
  2. /* color.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 COLOR_H
  31. #define COLOR_H
  32. #include "math_funcs.h"
  33. #include "ustring.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. struct Color {
  38. union {
  39. struct {
  40. float r;
  41. float g;
  42. float b;
  43. float a;
  44. };
  45. float components[4];
  46. };
  47. bool operator==(const Color &p_color) const { return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a); }
  48. bool operator!=(const Color &p_color) const { return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a); }
  49. uint32_t to_32() const;
  50. uint32_t to_ARGB32() const;
  51. float gray() 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.0);
  56. _FORCE_INLINE_ float &operator[](int idx) {
  57. return components[idx];
  58. }
  59. _FORCE_INLINE_ const float &operator[](int idx) const {
  60. return components[idx];
  61. }
  62. void invert();
  63. void contrast();
  64. Color inverted() const;
  65. Color contrasted() const;
  66. _FORCE_INLINE_ Color linear_interpolate(const Color &p_b, float p_t) const {
  67. Color res = *this;
  68. res.r += (p_t * (p_b.r - r));
  69. res.g += (p_t * (p_b.g - g));
  70. res.b += (p_t * (p_b.b - b));
  71. res.a += (p_t * (p_b.a - a));
  72. return res;
  73. }
  74. _FORCE_INLINE_ Color blend(const Color &p_over) const {
  75. Color res;
  76. float sa = 1.0 - p_over.a;
  77. res.a = a * sa + p_over.a;
  78. if (res.a == 0) {
  79. return Color(0, 0, 0, 0);
  80. } else {
  81. res.r = (r * a * sa + p_over.r * p_over.a) / res.a;
  82. res.g = (g * a * sa + p_over.g * p_over.a) / res.a;
  83. res.b = (b * a * sa + p_over.b * p_over.a) / res.a;
  84. }
  85. return res;
  86. }
  87. _FORCE_INLINE_ Color to_linear() const {
  88. return Color(
  89. r < 0.04045 ? r * (1.0 / 12.92) : Math::pow((r + 0.055) * (1.0 / (1 + 0.055)), 2.4),
  90. g < 0.04045 ? g * (1.0 / 12.92) : Math::pow((g + 0.055) * (1.0 / (1 + 0.055)), 2.4),
  91. b < 0.04045 ? b * (1.0 / 12.92) : Math::pow((b + 0.055) * (1.0 / (1 + 0.055)), 2.4),
  92. a);
  93. }
  94. static Color hex(uint32_t p_hex);
  95. static Color html(const String &p_color);
  96. static bool html_is_valid(const String &p_color);
  97. static Color named(const String &p_name);
  98. String to_html(bool p_alpha = true) const;
  99. _FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys
  100. operator String() const;
  101. /**
  102. * No construct parameters, r=0, g=0, b=0. a=255
  103. */
  104. _FORCE_INLINE_ Color() {
  105. r = 0;
  106. g = 0;
  107. b = 0;
  108. a = 1.0;
  109. }
  110. /**
  111. * RGB / RGBA construct parameters. Alpha is optional, but defaults to 1.0
  112. */
  113. _FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a = 1.0) {
  114. r = p_r;
  115. g = p_g;
  116. b = p_b;
  117. a = p_a;
  118. }
  119. private:
  120. friend void unregister_core_types();
  121. static void cleanup();
  122. };
  123. bool Color::operator<(const Color &p_color) const {
  124. if (r == p_color.r) {
  125. if (g == p_color.g) {
  126. if (b == p_color.b) {
  127. return (a < p_color.a);
  128. } else
  129. return (b < p_color.b);
  130. } else
  131. return g < p_color.g;
  132. } else
  133. return r < p_color.r;
  134. }
  135. #endif