gradient.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**************************************************************************/
  2. /* gradient.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 GRADIENT_H
  31. #define GRADIENT_H
  32. #include "core/io/resource.h"
  33. #include "thirdparty/misc/ok_color.h"
  34. class Gradient : public Resource {
  35. GDCLASS(Gradient, Resource);
  36. OBJ_SAVE_TYPE(Gradient);
  37. public:
  38. enum InterpolationMode {
  39. GRADIENT_INTERPOLATE_LINEAR,
  40. GRADIENT_INTERPOLATE_CONSTANT,
  41. GRADIENT_INTERPOLATE_CUBIC,
  42. };
  43. enum ColorSpace {
  44. GRADIENT_COLOR_SPACE_SRGB,
  45. GRADIENT_COLOR_SPACE_LINEAR_SRGB,
  46. GRADIENT_COLOR_SPACE_OKLAB,
  47. };
  48. struct Point {
  49. float offset = 0.0;
  50. Color color;
  51. bool operator<(const Point &p_point) const {
  52. return offset < p_point.offset;
  53. }
  54. };
  55. private:
  56. Vector<Point> points;
  57. bool is_sorted = true;
  58. InterpolationMode interpolation_mode = GRADIENT_INTERPOLATE_LINEAR;
  59. ColorSpace interpolation_color_space = GRADIENT_COLOR_SPACE_SRGB;
  60. _FORCE_INLINE_ void _update_sorting() {
  61. if (!is_sorted) {
  62. points.sort();
  63. is_sorted = true;
  64. }
  65. }
  66. _FORCE_INLINE_ Color transform_color_space(const Color p_color) const {
  67. switch (interpolation_color_space) {
  68. case GRADIENT_COLOR_SPACE_SRGB:
  69. default:
  70. return p_color;
  71. case GRADIENT_COLOR_SPACE_LINEAR_SRGB:
  72. return p_color.srgb_to_linear();
  73. case GRADIENT_COLOR_SPACE_OKLAB:
  74. Color linear_color = p_color.srgb_to_linear();
  75. ok_color::RGB rgb{};
  76. rgb.r = linear_color.r;
  77. rgb.g = linear_color.g;
  78. rgb.b = linear_color.b;
  79. ok_color ok_color;
  80. ok_color::Lab lab_color = ok_color.linear_srgb_to_oklab(rgb);
  81. // Constructs an RGB color using the Lab values directly. This allows reusing the interpolation code.
  82. return { lab_color.L, lab_color.a, lab_color.b, linear_color.a };
  83. }
  84. }
  85. _FORCE_INLINE_ Color inv_transform_color_space(const Color p_color) const {
  86. switch (interpolation_color_space) {
  87. case GRADIENT_COLOR_SPACE_SRGB:
  88. default:
  89. return p_color;
  90. case GRADIENT_COLOR_SPACE_LINEAR_SRGB:
  91. return p_color.linear_to_srgb();
  92. case GRADIENT_COLOR_SPACE_OKLAB:
  93. ok_color::Lab lab{};
  94. lab.L = p_color.r;
  95. lab.a = p_color.g;
  96. lab.b = p_color.b;
  97. ok_color new_ok_color;
  98. ok_color::RGB ok_rgb = new_ok_color.oklab_to_linear_srgb(lab);
  99. Color linear{ ok_rgb.r, ok_rgb.g, ok_rgb.b, p_color.a };
  100. return linear.linear_to_srgb();
  101. }
  102. }
  103. protected:
  104. static void _bind_methods();
  105. void _validate_property(PropertyInfo &p_property) const;
  106. public:
  107. Gradient();
  108. virtual ~Gradient();
  109. void add_point(float p_offset, const Color &p_color);
  110. void remove_point(int p_index);
  111. void set_points(const Vector<Point> &p_points);
  112. Vector<Point> &get_points();
  113. void reverse();
  114. void set_offset(int pos, const float offset);
  115. float get_offset(int pos);
  116. void set_color(int pos, const Color &color);
  117. Color get_color(int pos);
  118. void set_offsets(const Vector<float> &p_offsets);
  119. Vector<float> get_offsets() const;
  120. void set_colors(const Vector<Color> &p_colors);
  121. Vector<Color> get_colors() const;
  122. void set_interpolation_mode(InterpolationMode p_interp_mode);
  123. InterpolationMode get_interpolation_mode();
  124. void set_interpolation_color_space(Gradient::ColorSpace p_color_space);
  125. ColorSpace get_interpolation_color_space();
  126. _FORCE_INLINE_ Color get_color_at_offset(float p_offset) {
  127. if (points.is_empty()) {
  128. return Color(0, 0, 0, 1);
  129. }
  130. _update_sorting();
  131. // Binary search.
  132. int low = 0;
  133. int high = points.size() - 1;
  134. int middle = 0;
  135. #ifdef DEBUG_ENABLED
  136. if (low > high) {
  137. ERR_PRINT("low > high, this may be a bug");
  138. }
  139. #endif
  140. while (low <= high) {
  141. middle = (low + high) / 2;
  142. const Point &point = points[middle];
  143. if (point.offset > p_offset) {
  144. high = middle - 1; //search low end of array
  145. } else if (point.offset < p_offset) {
  146. low = middle + 1; //search high end of array
  147. } else {
  148. return point.color;
  149. }
  150. }
  151. // Return sampled value.
  152. if (points[middle].offset > p_offset) {
  153. middle--;
  154. }
  155. int first = middle;
  156. int second = middle + 1;
  157. if (second >= points.size()) {
  158. return points[points.size() - 1].color;
  159. }
  160. if (first < 0) {
  161. return points[0].color;
  162. }
  163. const Point &point1 = points[first];
  164. const Point &point2 = points[second];
  165. float weight = (p_offset - point1.offset) / (point2.offset - point1.offset);
  166. switch (interpolation_mode) {
  167. case GRADIENT_INTERPOLATE_CONSTANT: {
  168. return point1.color;
  169. }
  170. case GRADIENT_INTERPOLATE_LINEAR:
  171. default: { // Fallback to linear interpolation.
  172. Color color1 = transform_color_space(point1.color);
  173. Color color2 = transform_color_space(point2.color);
  174. Color interpolated = color1.lerp(color2, weight);
  175. return inv_transform_color_space(interpolated);
  176. }
  177. case GRADIENT_INTERPOLATE_CUBIC: {
  178. int p0 = first - 1;
  179. int p3 = second + 1;
  180. if (p3 >= points.size()) {
  181. p3 = second;
  182. }
  183. if (p0 < 0) {
  184. p0 = first;
  185. }
  186. const Point &point0 = points[p0];
  187. const Point &point3 = points[p3];
  188. Color color0 = transform_color_space(point0.color);
  189. Color color1 = transform_color_space(point1.color);
  190. Color color2 = transform_color_space(point2.color);
  191. Color color3 = transform_color_space(point3.color);
  192. Color interpolated;
  193. interpolated[0] = Math::cubic_interpolate(color1[0], color2[0], color0[0], color3[0], weight);
  194. interpolated[1] = Math::cubic_interpolate(color1[1], color2[1], color0[1], color3[1], weight);
  195. interpolated[2] = Math::cubic_interpolate(color1[2], color2[2], color0[2], color3[2], weight);
  196. interpolated[3] = Math::cubic_interpolate(color1[3], color2[3], color0[3], color3[3], weight);
  197. return inv_transform_color_space(interpolated);
  198. }
  199. }
  200. }
  201. int get_point_count() const;
  202. };
  203. VARIANT_ENUM_CAST(Gradient::InterpolationMode);
  204. VARIANT_ENUM_CAST(Gradient::ColorSpace);
  205. #endif // GRADIENT_H