gradient.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. LocalVector<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 reverse();
  112. void set_offset(int pos, const float offset);
  113. float get_offset(int pos);
  114. void set_color(int pos, const Color &color);
  115. Color get_color(int pos);
  116. void set_offsets(const Vector<float> &p_offsets);
  117. Vector<float> get_offsets() const;
  118. void set_colors(const Vector<Color> &p_colors);
  119. Vector<Color> get_colors() const;
  120. void set_interpolation_mode(InterpolationMode p_interp_mode);
  121. InterpolationMode get_interpolation_mode();
  122. void set_interpolation_color_space(Gradient::ColorSpace p_color_space);
  123. ColorSpace get_interpolation_color_space();
  124. _FORCE_INLINE_ Color get_color_at_offset(float p_offset) {
  125. if (points.is_empty()) {
  126. return Color(0, 0, 0, 1);
  127. }
  128. _update_sorting();
  129. // Binary search.
  130. int low = 0;
  131. int high = points.size() - 1;
  132. int middle = 0;
  133. #ifdef DEBUG_ENABLED
  134. if (low > high) {
  135. ERR_PRINT("low > high, this may be a bug");
  136. }
  137. #endif
  138. while (low <= high) {
  139. middle = (low + high) / 2;
  140. const Point &point = points[middle];
  141. if (point.offset > p_offset) {
  142. high = middle - 1; //search low end of array
  143. } else if (point.offset < p_offset) {
  144. low = middle + 1; //search high end of array
  145. } else {
  146. return point.color;
  147. }
  148. }
  149. // Return sampled value.
  150. if (points[middle].offset > p_offset) {
  151. middle--;
  152. }
  153. int first = middle;
  154. int second = middle + 1;
  155. if (second >= (int)points.size()) {
  156. return points[points.size() - 1].color;
  157. }
  158. if (first < 0) {
  159. return points[0].color;
  160. }
  161. const Point &point1 = points[first];
  162. const Point &point2 = points[second];
  163. float weight = (p_offset - point1.offset) / (point2.offset - point1.offset);
  164. switch (interpolation_mode) {
  165. case GRADIENT_INTERPOLATE_CONSTANT: {
  166. return point1.color;
  167. }
  168. case GRADIENT_INTERPOLATE_LINEAR:
  169. default: { // Fallback to linear interpolation.
  170. Color color1 = transform_color_space(point1.color);
  171. Color color2 = transform_color_space(point2.color);
  172. Color interpolated = color1.lerp(color2, weight);
  173. return inv_transform_color_space(interpolated);
  174. }
  175. case GRADIENT_INTERPOLATE_CUBIC: {
  176. int p0 = first - 1;
  177. int p3 = second + 1;
  178. if (p3 >= (int)points.size()) {
  179. p3 = second;
  180. }
  181. if (p0 < 0) {
  182. p0 = first;
  183. }
  184. const Point &point0 = points[p0];
  185. const Point &point3 = points[p3];
  186. Color color0 = transform_color_space(point0.color);
  187. Color color1 = transform_color_space(point1.color);
  188. Color color2 = transform_color_space(point2.color);
  189. Color color3 = transform_color_space(point3.color);
  190. Color interpolated;
  191. interpolated[0] = Math::cubic_interpolate(color1[0], color2[0], color0[0], color3[0], weight);
  192. interpolated[1] = Math::cubic_interpolate(color1[1], color2[1], color0[1], color3[1], weight);
  193. interpolated[2] = Math::cubic_interpolate(color1[2], color2[2], color0[2], color3[2], weight);
  194. interpolated[3] = Math::cubic_interpolate(color1[3], color2[3], color0[3], color3[3], weight);
  195. return inv_transform_color_space(interpolated);
  196. }
  197. }
  198. }
  199. int get_point_count() const;
  200. };
  201. VARIANT_ENUM_CAST(Gradient::InterpolationMode);
  202. VARIANT_ENUM_CAST(Gradient::ColorSpace);
  203. #endif // GRADIENT_H