2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* gradient.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2019-02-12 17:18:13 +01:00
|
|
|
#ifndef GRADIENT_H
|
|
|
|
#define GRADIENT_H
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/io/resource.h"
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2023-04-24 03:22:45 +02:00
|
|
|
#include "thirdparty/misc/ok_color.h"
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
class Gradient : public Resource {
|
|
|
|
GDCLASS(Gradient, Resource);
|
|
|
|
OBJ_SAVE_TYPE(Gradient);
|
2015-05-24 20:18:52 +02:00
|
|
|
|
|
|
|
public:
|
2021-11-08 19:11:36 +01:00
|
|
|
enum InterpolationMode {
|
|
|
|
GRADIENT_INTERPOLATE_LINEAR,
|
|
|
|
GRADIENT_INTERPOLATE_CONSTANT,
|
|
|
|
GRADIENT_INTERPOLATE_CUBIC,
|
|
|
|
};
|
|
|
|
|
2023-04-24 03:22:45 +02:00
|
|
|
enum ColorSpace {
|
|
|
|
GRADIENT_COLOR_SPACE_SRGB,
|
|
|
|
GRADIENT_COLOR_SPACE_LINEAR_SRGB,
|
|
|
|
GRADIENT_COLOR_SPACE_OKLAB,
|
|
|
|
};
|
|
|
|
|
2015-05-24 20:18:52 +02:00
|
|
|
struct Point {
|
2021-02-09 18:24:36 +01:00
|
|
|
float offset = 0.0;
|
2015-05-24 20:18:52 +02:00
|
|
|
Color color;
|
2023-01-20 17:01:37 +01:00
|
|
|
bool operator<(const Point &p_point) const {
|
|
|
|
return offset < p_point.offset;
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<Point> points;
|
2021-02-09 18:24:36 +01:00
|
|
|
bool is_sorted = true;
|
2021-11-08 19:11:36 +01:00
|
|
|
InterpolationMode interpolation_mode = GRADIENT_INTERPOLATE_LINEAR;
|
2023-04-24 03:22:45 +02:00
|
|
|
ColorSpace interpolation_color_space = GRADIENT_COLOR_SPACE_SRGB;
|
2021-11-08 19:11:36 +01:00
|
|
|
|
2020-08-10 10:23:12 +02:00
|
|
|
_FORCE_INLINE_ void _update_sorting() {
|
|
|
|
if (!is_sorted) {
|
|
|
|
points.sort();
|
|
|
|
is_sorted = true;
|
|
|
|
}
|
|
|
|
}
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2023-04-24 03:22:45 +02:00
|
|
|
_FORCE_INLINE_ Color transform_color_space(const Color p_color) const {
|
|
|
|
switch (interpolation_color_space) {
|
|
|
|
case GRADIENT_COLOR_SPACE_SRGB:
|
|
|
|
default:
|
|
|
|
return p_color;
|
|
|
|
|
|
|
|
case GRADIENT_COLOR_SPACE_LINEAR_SRGB:
|
|
|
|
return p_color.srgb_to_linear();
|
|
|
|
|
|
|
|
case GRADIENT_COLOR_SPACE_OKLAB:
|
|
|
|
Color linear_color = p_color.srgb_to_linear();
|
|
|
|
ok_color::RGB rgb{};
|
|
|
|
rgb.r = linear_color.r;
|
|
|
|
rgb.g = linear_color.g;
|
|
|
|
rgb.b = linear_color.b;
|
|
|
|
|
|
|
|
ok_color ok_color;
|
|
|
|
ok_color::Lab lab_color = ok_color.linear_srgb_to_oklab(rgb);
|
|
|
|
|
|
|
|
// Constructs an RGB color using the Lab values directly. This allows reusing the interpolation code.
|
|
|
|
return { lab_color.L, lab_color.a, lab_color.b, linear_color.a };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Color inv_transform_color_space(const Color p_color) const {
|
|
|
|
switch (interpolation_color_space) {
|
|
|
|
case GRADIENT_COLOR_SPACE_SRGB:
|
|
|
|
default:
|
|
|
|
return p_color;
|
|
|
|
|
|
|
|
case GRADIENT_COLOR_SPACE_LINEAR_SRGB:
|
|
|
|
return p_color.linear_to_srgb();
|
|
|
|
|
|
|
|
case GRADIENT_COLOR_SPACE_OKLAB:
|
|
|
|
ok_color::Lab lab{};
|
|
|
|
lab.L = p_color.r;
|
|
|
|
lab.a = p_color.g;
|
|
|
|
lab.b = p_color.b;
|
|
|
|
|
|
|
|
ok_color new_ok_color;
|
|
|
|
ok_color::RGB ok_rgb = new_ok_color.oklab_to_linear_srgb(lab);
|
|
|
|
Color linear{ ok_rgb.r, ok_rgb.g, ok_rgb.b, p_color.a };
|
|
|
|
return linear.linear_to_srgb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 20:18:52 +02:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
2023-04-24 03:22:45 +02:00
|
|
|
void _validate_property(PropertyInfo &p_property) const;
|
2015-05-24 20:18:52 +02:00
|
|
|
|
|
|
|
public:
|
2017-05-29 02:46:48 +02:00
|
|
|
Gradient();
|
|
|
|
virtual ~Gradient();
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void add_point(float p_offset, const Color &p_color);
|
2015-12-12 13:45:17 +01:00
|
|
|
void remove_point(int p_index);
|
2022-04-28 11:35:39 +02:00
|
|
|
void set_points(const Vector<Point> &p_points);
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<Point> &get_points();
|
2021-11-08 19:11:36 +01:00
|
|
|
void reverse();
|
2015-05-24 20:18:52 +02:00
|
|
|
|
|
|
|
void set_offset(int pos, const float offset);
|
2020-08-10 10:23:12 +02:00
|
|
|
float get_offset(int pos);
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_color(int pos, const Color &color);
|
2020-08-10 10:23:12 +02:00
|
|
|
Color get_color(int pos);
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void set_offsets(const Vector<float> &p_offsets);
|
2015-05-24 20:18:52 +02:00
|
|
|
Vector<float> get_offsets() const;
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void set_colors(const Vector<Color> &p_colors);
|
2015-05-24 20:18:52 +02:00
|
|
|
Vector<Color> get_colors() const;
|
|
|
|
|
2021-11-08 19:11:36 +01:00
|
|
|
void set_interpolation_mode(InterpolationMode p_interp_mode);
|
|
|
|
InterpolationMode get_interpolation_mode();
|
|
|
|
|
2023-04-24 03:22:45 +02:00
|
|
|
void set_interpolation_color_space(Gradient::ColorSpace p_color_space);
|
|
|
|
ColorSpace get_interpolation_color_space();
|
|
|
|
|
2015-05-27 23:11:54 +02:00
|
|
|
_FORCE_INLINE_ Color get_color_at_offset(float p_offset) {
|
2020-12-15 13:04:21 +01:00
|
|
|
if (points.is_empty()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return Color(0, 0, 0, 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-05-27 23:11:54 +02:00
|
|
|
|
2020-08-10 10:23:12 +02:00
|
|
|
_update_sorting();
|
2015-05-27 23:11:54 +02:00
|
|
|
|
2021-11-08 19:11:36 +01:00
|
|
|
// Binary search.
|
2015-05-27 23:11:54 +02:00
|
|
|
int low = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
int high = points.size() - 1;
|
2017-09-01 22:33:39 +02:00
|
|
|
int middle = 0;
|
|
|
|
|
2019-04-05 23:20:20 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-05-14 16:41:43 +02:00
|
|
|
if (low > high) {
|
2017-09-01 22:33:39 +02:00
|
|
|
ERR_PRINT("low > high, this may be a bug");
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-01 22:33:39 +02:00
|
|
|
#endif
|
2015-05-27 23:11:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (low <= high) {
|
|
|
|
middle = (low + high) / 2;
|
2018-07-25 03:11:03 +02:00
|
|
|
const Point &point = points[middle];
|
2017-03-05 16:44:50 +01:00
|
|
|
if (point.offset > p_offset) {
|
2015-05-27 23:11:54 +02:00
|
|
|
high = middle - 1; //search low end of array
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (point.offset < p_offset) {
|
2015-05-27 23:11:54 +02:00
|
|
|
low = middle + 1; //search high end of array
|
|
|
|
} else {
|
|
|
|
return point.color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
// Return sampled value.
|
2017-03-05 16:44:50 +01:00
|
|
|
if (points[middle].offset > p_offset) {
|
2015-05-27 23:11:54 +02:00
|
|
|
middle--;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
int first = middle;
|
|
|
|
int second = middle + 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (second >= points.size()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return points[points.size() - 1].color;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (first < 0) {
|
2015-05-27 23:11:54 +02:00
|
|
|
return points[0].color;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-04-24 03:22:45 +02:00
|
|
|
const Point &point1 = points[first];
|
|
|
|
const Point &point2 = points[second];
|
|
|
|
float weight = (p_offset - point1.offset) / (point2.offset - point1.offset);
|
2021-11-08 19:11:36 +01:00
|
|
|
|
|
|
|
switch (interpolation_mode) {
|
|
|
|
case GRADIENT_INTERPOLATE_CONSTANT: {
|
2023-04-24 03:22:45 +02:00
|
|
|
return point1.color;
|
|
|
|
}
|
|
|
|
case GRADIENT_INTERPOLATE_LINEAR:
|
|
|
|
default: { // Fallback to linear interpolation.
|
|
|
|
Color color1 = transform_color_space(point1.color);
|
|
|
|
Color color2 = transform_color_space(point2.color);
|
|
|
|
|
|
|
|
Color interpolated = color1.lerp(color2, weight);
|
|
|
|
return inv_transform_color_space(interpolated);
|
|
|
|
}
|
2021-11-08 19:11:36 +01:00
|
|
|
case GRADIENT_INTERPOLATE_CUBIC: {
|
|
|
|
int p0 = first - 1;
|
|
|
|
int p3 = second + 1;
|
|
|
|
if (p3 >= points.size()) {
|
|
|
|
p3 = second;
|
|
|
|
}
|
|
|
|
if (p0 < 0) {
|
|
|
|
p0 = first;
|
|
|
|
}
|
2023-04-24 03:22:45 +02:00
|
|
|
const Point &point0 = points[p0];
|
|
|
|
const Point &point3 = points[p3];
|
|
|
|
|
|
|
|
Color color0 = transform_color_space(point0.color);
|
|
|
|
Color color1 = transform_color_space(point1.color);
|
|
|
|
Color color2 = transform_color_space(point2.color);
|
|
|
|
Color color3 = transform_color_space(point3.color);
|
|
|
|
|
|
|
|
Color interpolated;
|
|
|
|
interpolated[0] = Math::cubic_interpolate(color1[0], color2[0], color0[0], color3[0], weight);
|
|
|
|
interpolated[1] = Math::cubic_interpolate(color1[1], color2[1], color0[1], color3[1], weight);
|
|
|
|
interpolated[2] = Math::cubic_interpolate(color1[2], color2[2], color0[2], color3[2], weight);
|
|
|
|
interpolated[3] = Math::cubic_interpolate(color1[3], color2[3], color0[3], color3[3], weight);
|
|
|
|
|
|
|
|
return inv_transform_color_space(interpolated);
|
2021-11-08 19:11:36 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-27 23:11:54 +02:00
|
|
|
}
|
2015-05-26 21:17:54 +02:00
|
|
|
|
2023-01-02 15:52:05 +01:00
|
|
|
int get_point_count() const;
|
2015-05-24 20:18:52 +02:00
|
|
|
};
|
|
|
|
|
2021-11-08 19:11:36 +01:00
|
|
|
VARIANT_ENUM_CAST(Gradient::InterpolationMode);
|
2023-04-24 03:22:45 +02:00
|
|
|
VARIANT_ENUM_CAST(Gradient::ColorSpace);
|
2021-11-08 19:11:36 +01:00
|
|
|
|
2019-02-12 17:18:13 +01:00
|
|
|
#endif // GRADIENT_H
|