Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_gradient.h
10277 views
1
/**************************************************************************/
2
/* test_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 "scene/resources/gradient.h"
34
35
#include "thirdparty/doctest/doctest.h"
36
37
namespace TestGradient {
38
39
TEST_CASE("[Gradient] Default gradient") {
40
// Black-white gradient.
41
Ref<Gradient> gradient = memnew(Gradient);
42
43
CHECK_MESSAGE(
44
gradient->get_point_count() == 2,
45
"Default gradient should contain the expected number of points.");
46
47
CHECK_MESSAGE(
48
gradient->get_color_at_offset(0.0).is_equal_approx(Color(0, 0, 0)),
49
"Default gradient should return the expected interpolated value at offset 0.0.");
50
CHECK_MESSAGE(
51
gradient->get_color_at_offset(0.4).is_equal_approx(Color(0.4, 0.4, 0.4)),
52
"Default gradient should return the expected interpolated value at offset 0.4.");
53
CHECK_MESSAGE(
54
gradient->get_color_at_offset(0.8).is_equal_approx(Color(0.8, 0.8, 0.8)),
55
"Default gradient should return the expected interpolated value at offset 0.8.");
56
CHECK_MESSAGE(
57
gradient->get_color_at_offset(1.0).is_equal_approx(Color(1, 1, 1)),
58
"Default gradient should return the expected interpolated value at offset 1.0.");
59
60
// Out of bounds checks.
61
CHECK_MESSAGE(
62
gradient->get_color_at_offset(-1.0).is_equal_approx(Color(0, 0, 0)),
63
"Default gradient should return the expected interpolated value at offset -1.0.");
64
CHECK_MESSAGE(
65
gradient->get_color_at_offset(1234.0).is_equal_approx(Color(1, 1, 1)),
66
"Default gradient should return the expected interpolated value at offset 1234.0.");
67
}
68
69
TEST_CASE("[Gradient] Custom gradient (points specified in order)") {
70
// Red-yellow-green gradient (with overbright green).
71
Ref<Gradient> gradient = memnew(Gradient);
72
Vector<float> offsets = { 0.0, 0.5, 1.0 };
73
Vector<Color> colors = { Color(1, 0, 0), Color(1, 1, 0), Color(0, 2, 0) };
74
75
gradient->set_offsets(offsets);
76
gradient->set_colors(colors);
77
78
CHECK_MESSAGE(
79
gradient->get_point_count() == 3,
80
"Custom gradient should contain the expected number of points.");
81
82
CHECK_MESSAGE(
83
gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 0, 0)),
84
"Custom gradient should return the expected interpolated value at offset 0.0.");
85
CHECK_MESSAGE(
86
gradient->get_color_at_offset(0.25).is_equal_approx(Color(1, 0.5, 0)),
87
"Custom gradient should return the expected interpolated value at offset 0.25.");
88
CHECK_MESSAGE(
89
gradient->get_color_at_offset(0.5).is_equal_approx(Color(1, 1, 0)),
90
"Custom gradient should return the expected interpolated value at offset 0.5.");
91
CHECK_MESSAGE(
92
gradient->get_color_at_offset(0.75).is_equal_approx(Color(0.5, 1.5, 0)),
93
"Custom gradient should return the expected interpolated value at offset 0.75.");
94
CHECK_MESSAGE(
95
gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 2, 0)),
96
"Custom gradient should return the expected interpolated value at offset 1.0.");
97
98
gradient->remove_point(1);
99
CHECK_MESSAGE(
100
gradient->get_point_count() == 2,
101
"Custom gradient should contain the expected number of points after removing one point.");
102
CHECK_MESSAGE(
103
gradient->get_color_at_offset(0.5).is_equal_approx(Color(0.5, 1, 0)),
104
"Custom gradient should return the expected interpolated value at offset 0.5 after removing point at index 1.");
105
}
106
107
TEST_CASE("[Gradient] Custom gradient (points specified out-of-order)") {
108
// HSL rainbow with points specified out of order.
109
// These should be sorted automatically when adding points.
110
Ref<Gradient> gradient = memnew(Gradient);
111
LocalVector<Gradient::Point> points;
112
Vector<float> offsets = { 0.2, 0.0, 0.8, 0.4, 1.0, 0.6 };
113
Vector<Color> colors = { Color(1, 0, 0), Color(1, 1, 0), Color(0, 1, 0), Color(0, 1, 1), Color(0, 0, 1), Color(1, 0, 1) };
114
115
gradient->set_offsets(offsets);
116
gradient->set_colors(colors);
117
118
CHECK_MESSAGE(
119
gradient->get_point_count() == 6,
120
"Custom out-of-order gradient should contain the expected number of points.");
121
122
CHECK_MESSAGE(
123
gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 1, 0)),
124
"Custom out-of-order gradient should return the expected interpolated value at offset 0.0.");
125
CHECK_MESSAGE(
126
gradient->get_color_at_offset(0.3).is_equal_approx(Color(0.5, 0.5, 0.5)),
127
"Custom out-of-order gradient should return the expected interpolated value at offset 0.3.");
128
CHECK_MESSAGE(
129
gradient->get_color_at_offset(0.6).is_equal_approx(Color(1, 0, 1)),
130
"Custom out-of-order gradient should return the expected interpolated value at offset 0.6.");
131
CHECK_MESSAGE(
132
gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 0, 1)),
133
"Custom out-of-order gradient should return the expected interpolated value at offset 1.0.");
134
135
gradient->remove_point(0);
136
CHECK_MESSAGE(
137
gradient->get_point_count() == 5,
138
"Custom out-of-order gradient should contain the expected number of points after removing one point.");
139
// The color will be clamped to the nearest point (which is at offset 0.2).
140
CHECK_MESSAGE(
141
gradient->get_color_at_offset(0.1).is_equal_approx(Color(1, 0, 0)),
142
"Custom out-of-order gradient should return the expected interpolated value at offset 0.1 after removing point at index 0.");
143
}
144
} // namespace TestGradient
145
146