gradient.h 7.6 KB

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