Debian Codegraph @

scene/resources/gradient.h

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