1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*************************************************************************/
/* test_gradient.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef TEST_GRADIENT_H
#define TEST_GRADIENT_H
#include "scene/resources/gradient.h"
#include "thirdparty/doctest/doctest.h"
namespace TestGradient {
TEST_CASE("[Gradient] Default gradient") {
// Black-white gradient.
Ref<Gradient> gradient = memnew(Gradient);
CHECK_MESSAGE(
gradient->get_points_count() == 2,
"Default gradient should contain the expected number of points.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.0).is_equal_approx(Color(0, 0, 0)),
"Default gradient should return the expected interpolated value at offset 0.0.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.4).is_equal_approx(Color(0.4, 0.4, 0.4)),
"Default gradient should return the expected interpolated value at offset 0.4.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.8).is_equal_approx(Color(0.8, 0.8, 0.8)),
"Default gradient should return the expected interpolated value at offset 0.8.");
CHECK_MESSAGE(
gradient->get_color_at_offset(1.0).is_equal_approx(Color(1, 1, 1)),
"Default gradient should return the expected interpolated value at offset 1.0.");
// Out of bounds checks.
CHECK_MESSAGE(
gradient->get_color_at_offset(-1.0).is_equal_approx(Color(0, 0, 0)),
"Default gradient should return the expected interpolated value at offset -1.0.");
CHECK_MESSAGE(
gradient->get_color_at_offset(1234.0).is_equal_approx(Color(1, 1, 1)),
"Default gradient should return the expected interpolated value at offset 1234.0.");
}
TEST_CASE("[Gradient] Custom gradient (points specified in order)") {
// Red-yellow-green gradient (with overbright green).
Ref<Gradient> gradient = memnew(Gradient);
Vector<Gradient::Point> points;
points.push_back({ 0.0, Color(1, 0, 0) });
points.push_back({ 0.5, Color(1, 1, 0) });
points.push_back({ 1.0, Color(0, 2, 0) });
gradient->set_points(points);
CHECK_MESSAGE(
gradient->get_points_count() == 3,
"Custom gradient should contain the expected number of points.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 0, 0)),
"Custom gradient should return the expected interpolated value at offset 0.0.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.25).is_equal_approx(Color(1, 0.5, 0)),
"Custom gradient should return the expected interpolated value at offset 0.25.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.5).is_equal_approx(Color(1, 1, 0)),
"Custom gradient should return the expected interpolated value at offset 0.5.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.75).is_equal_approx(Color(0.5, 1.5, 0)),
"Custom gradient should return the expected interpolated value at offset 0.75.");
CHECK_MESSAGE(
gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 2, 0)),
"Custom gradient should return the expected interpolated value at offset 1.0.");
gradient->remove_point(1);
CHECK_MESSAGE(
gradient->get_points_count() == 2,
"Custom gradient should contain the expected number of points after removing one point.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.5).is_equal_approx(Color(0.5, 1, 0)),
"Custom gradient should return the expected interpolated value at offset 0.5 after removing point at index 1.");
}
TEST_CASE("[Gradient] Custom gradient (points specified out-of-order)") {
// HSL rainbow with points specified out of order.
// These should be sorted automatically when adding points.
Ref<Gradient> gradient = memnew(Gradient);
Vector<Gradient::Point> points;
points.push_back({ 0.2, Color(1, 0, 0) });
points.push_back({ 0.0, Color(1, 1, 0) });
points.push_back({ 0.8, Color(0, 1, 0) });
points.push_back({ 0.4, Color(0, 1, 1) });
points.push_back({ 1.0, Color(0, 0, 1) });
points.push_back({ 0.6, Color(1, 0, 1) });
gradient->set_points(points);
CHECK_MESSAGE(
gradient->get_points_count() == 6,
"Custom out-of-order gradient should contain the expected number of points.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 1, 0)),
"Custom out-of-order gradient should return the expected interpolated value at offset 0.0.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.3).is_equal_approx(Color(0.5, 0.5, 0.5)),
"Custom out-of-order gradient should return the expected interpolated value at offset 0.3.");
CHECK_MESSAGE(
gradient->get_color_at_offset(0.6).is_equal_approx(Color(1, 0, 1)),
"Custom out-of-order gradient should return the expected interpolated value at offset 0.6.");
CHECK_MESSAGE(
gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 0, 1)),
"Custom out-of-order gradient should return the expected interpolated value at offset 1.0.");
gradient->remove_point(0);
CHECK_MESSAGE(
gradient->get_points_count() == 5,
"Custom out-of-order gradient should contain the expected number of points after removing one point.");
// The color will be clamped to the nearest point (which is at offset 0.2).
CHECK_MESSAGE(
gradient->get_color_at_offset(0.1).is_equal_approx(Color(1, 0, 0)),
"Custom out-of-order gradient should return the expected interpolated value at offset 0.1 after removing point at index 0.");
}
} // namespace TestGradient
#endif // TEST_GRADIENT_H
|