FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/dom/linear_gradient.cpp
Go to the documentation of this file.
1// Copyright 2023 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for max, min, sort, copy
5#include <cmath> // for fmod, cos, sin
6#include <cstddef> // for size_t
7#include <ftxui/dom/linear_gradient.hpp> // for LinearGradient::Stop, LinearGradient
8#include <memory> // for allocator_traits<>::value_type, make_shared
9#include <optional> // for optional, operator!=, operator<
10#include <utility> // for move
11#include <vector> // for vector
12
13#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
14#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
15#include "ftxui/screen/box.hpp" // for Box
16#include "ftxui/screen/color.hpp" // for Color, Color::Default, Color::Blue
17#include "ftxui/screen/screen.hpp" // for Cell, Screen
18
19namespace ftxui {
20namespace {
21
22struct LinearGradientNormalized {
23 float angle = 0.F;
24 std::vector<Color> colors;
25 std::vector<float> positions; // 已排序。
26};
27
28// 將 LinearGradient 轉換為標準化版本。
29LinearGradientNormalized Normalize(LinearGradient gradient) {
30 // 處理大小為 0 的漸層。
31 if (gradient.stops.empty()) {
32 return LinearGradientNormalized{
33 0.F,
34 {Color::Default, Color::Default},
35 {0.F, 1.F},
36 };
37 }
38
39 // 如果未提供,填入兩端的範圍。
40 if (!gradient.stops.front().position) {
41 gradient.stops.front().position = 0.F;
42 }
43 if (!gradient.stops.back().position) {
44 gradient.stops.back().position = 1.F;
45 }
46
47 // 透過插值位置來填補空白。
48 size_t last_checkpoint = 0;
49 for (size_t i = 1; i < gradient.stops.size(); ++i) {
50 if (!gradient.stops[i].position) {
51 continue;
52 }
53
54 if (i - last_checkpoint >= 2) {
55 const float min = gradient.stops[i].position.value(); // NOLINT
56 const float max =
57 gradient.stops[last_checkpoint].position.value(); // NOLINT
58 for (size_t j = last_checkpoint + 1; j < i; ++j) {
59 gradient.stops[j].position = min + (max - min) *
60 float(j - last_checkpoint) /
61 float(i - last_checkpoint);
62 }
63 }
64
65 last_checkpoint = i;
66 }
67
68 // 依位置對停駐點排序。
69 std::sort(
70 gradient.stops.begin(), gradient.stops.end(),
71 [](const auto& a, const auto& b) { return a.position < b.position; });
72
73 // 如果我們不是從零開始,就在零處新增一個停駐點。
74 if (gradient.stops.front().position != 0) {
75 gradient.stops.insert(gradient.stops.begin(),
76 {gradient.stops.front().color, 0.F});
77 }
78 // 如果我們不是以一結束,就在一處新增一個停駐點。
79 if (gradient.stops.back().position != 1) {
80 gradient.stops.push_back({gradient.stops.back().color, 1.F});
81 }
82
83 // 正規化角度。
84 LinearGradientNormalized normalized;
85 const float modulo = 360.F;
86 normalized.angle =
87 std::fmod(std::fmod(gradient.angle, modulo) + modulo, modulo);
88 for (auto& stop : gradient.stops) {
89 normalized.colors.push_back(stop.color);
90 // NOLINTNEXTLINE
91 normalized.positions.push_back(stop.position.value());
92 }
93 return normalized;
94}
95
96Color Interpolate(const LinearGradientNormalized& gradient, float t) {
97 // 在漸層的停駐點中找到正確的顏色。
98 size_t i = 1;
99 while (true) {
100 // 請注意,由於浮點精度,`t` 可能會略大於 1.0。
101 // 這就是為什麼我們需要處理 `t` 大於最後一個停止點位置的情況。
102 // 請參閱 https://github.com/ArthurSonzogni/FTXUI/issues/998
103 if (i >= gradient.positions.size()) {
104 const float half = 0.5F;
105 return Color::Interpolate(half, gradient.colors.back(),
106 gradient.colors.back());
107 }
108 if (t <= gradient.positions[i]) {
109 break;
110 }
111 ++i;
112 }
113
114 const float t0 = gradient.positions[i - 1];
115 const float t1 = gradient.positions[i - 0];
116 const float tt = (t - t0) / (t1 - t0);
117
118 const Color& c0 = gradient.colors[i - 1];
119 const Color& c1 = gradient.colors[i - 0];
120 const Color& cc = Color::Interpolate(tt, c0, c1);
121
122 return cc;
123}
124
125class LinearGradientColor : public NodeDecorator {
126 public:
127 explicit LinearGradientColor(Element child,
128 const LinearGradient& gradient,
129 bool background_color)
130 : NodeDecorator(std::move(child)),
131 gradient_(Normalize(gradient)),
132 background_color_{background_color} {}
133
134 private:
135 void Render(Screen& screen) override {
136 const float degtorad = 0.01745329251F;
137 const float dx = std::cos(gradient_.angle * degtorad);
138 const float dy = std::sin(gradient_.angle * degtorad);
139
140 // 投影每個角落以取得漸層的範圍。
141 const float p1 = float(box_.x_min) * dx + float(box_.y_min) * dy;
142 const float p2 = float(box_.x_min) * dx + float(box_.y_max) * dy;
143 const float p3 = float(box_.x_max) * dx + float(box_.y_min) * dy;
144 const float p4 = float(box_.x_max) * dx + float(box_.y_max) * dy;
145 const float min = std::min({p1, p2, p3, p4});
146 const float max = std::max({p1, p2, p3, p4});
147
148 // 使用範圍與投影幾何,將投影重新正規化到
149 // [0, 1]。
150 const float dX = dx / (max - min);
151 const float dY = dy / (max - min);
152 const float dZ = -min / (max - min);
153
154 // 投影每個像素以取得顏色。
155 if (background_color_) {
156 for (int y = box_.y_min; y <= box_.y_max; ++y) {
157 for (int x = box_.x_min; x <= box_.x_max; ++x) {
158 const float t = float(x) * dX + float(y) * dY + dZ;
159 screen.CellAt(x, y).background_color = Interpolate(gradient_, t);
160 }
161 }
162 } else {
163 for (int y = box_.y_min; y <= box_.y_max; ++y) {
164 for (int x = box_.x_min; x <= box_.x_max; ++x) {
165 const float t = float(x) * dX + float(y) * dY + dZ;
166 screen.CellAt(x, y).foreground_color = Interpolate(gradient_, t);
167 }
168 }
169 }
170
171 NodeDecorator::Render(screen);
172 }
173
174 LinearGradientNormalized gradient_;
175 bool background_color_;
176};
177
178} // namespace
179
180/// @brief 建立「空」漸變。這通常會接著呼叫 LinearGradient::Angle() 和 LinearGradient::Stop()。
181/// 範例:
182/// ```cpp
183/// auto gradient =
184/// LinearGradient()
185/// .Angle(45)
186/// .Stop(Color::Red, 0.0)
187/// .Stop(Color::Green, 0.5)
188/// .Stop(Color::Blue, 1.0);;
189/// ```
190LinearGradient::LinearGradient() = default;
191
192/// @brief 建立一個包含兩種顏色的漸變。
193/// @param begin 漸變開始處的顏色。
194/// @param end 漸變結束處的顏色。
195LinearGradient::LinearGradient(Color begin, Color end)
196 : LinearGradient(0, begin, end) {}
197
198/// @brief 建立一個包含兩種顏色和角度的漸變。
199/// @param a 漸變的角度。
200/// @param begin 漸變開始處的顏色。
201/// @param end 漸變結束處的顏色。
202LinearGradient::LinearGradient(float a, Color begin, Color end) : angle(a) {
203 stops.push_back({begin, {}});
204 stops.push_back({end, {}});
205}
206
207/// @brief 設定漸變的角度。
208/// @param a 漸變的角度。
209/// @return 漸變。
210LinearGradient& LinearGradient::Angle(float a) {
211 angle = a;
212 return *this;
213}
214
215/// @brief 為漸變添加一個顏色停止點。
216/// @param c 停止點的顏色。
217/// @param p 停止點的位置。
218LinearGradient& LinearGradient::Stop(Color c, float p) {
219 stops.push_back({c, p});
220 return *this;
221}
222
223/// @brief 為漸變添加一個顏色停止點。
224/// @param c 停止點的顏色。
225/// @return 漸變。
226/// @note 停止點的位置是從附近的停止點內插而來的。
227LinearGradient& LinearGradient::Stop(Color c) {
228 stops.push_back({c, {}});
229 return *this;
230}
231
232/// @brief 使用線性漸變效果設定元素的前景色。
233/// @param gradient 要應用於輸出元素的漸變效果。
234/// @param child 輸入元素。
235/// @return 已著色的輸出元素。
236/// @ingroup dom
237///
238/// ### 範例
239///
240/// ```cpp
241/// color(LinearGradient{0, {Color::Red, Color::Blue}}, text("Hello"))
242/// ```
243Element color(const LinearGradient& gradient, Element child) {
244 return std::make_shared<LinearGradientColor>(std::move(child), gradient,
245 /*background_color*/ false);
246}
247
248/// @brief 使用線性漸變效果設定元素的背景顏色。
249/// @param gradient 要應用於輸出元素的漸變效果。
250/// @param child 輸入元素。
251/// @return 已著色的輸出元素。
252/// @ingroup dom
253///
254/// ### 範例
255///
256/// ```cpp
257/// bgcolor(LinearGradient{0, {Color::Red, Color::Blue}}, text("Hello"))
258/// ```
259Element bgcolor(const LinearGradient& gradient, Element child) {
260 return std::make_shared<LinearGradientColor>(std::move(child), gradient,
261 /*background_color*/ true);
262}
263
264/// @brief 使用線性漸變效果裝飾前景色。
265/// @param gradient 要應用於輸出元素的漸變效果。
266/// @return 應用顏色的 Decorator。
267/// @ingroup dom
268///
269/// ### 範例
270///
271/// ```cpp
272/// text("Hello") | color(LinearGradient{0, {Color::Red, Color::Blue}})
273/// ```
274Decorator color(const LinearGradient& gradient) {
275 return
276 [gradient](Element child) { return color(gradient, std::move(child)); };
277}
278
279/// @brief 使用線性漸變效果裝飾背景顏色。
280/// @param gradient 要應用於輸出元素的漸變效果。
281/// @return 應用顏色的 Decorator。
282/// @ingroup dom
283///
284/// ### 範例
285///
286/// ```cpp
287/// text("Hello") | color(LinearGradient{0, {Color::Red, Color::Blue}})
288/// ```
289Decorator bgcolor(const LinearGradient& gradient) {
290 return
291 [gradient](Element child) { return bgcolor(gradient, std::move(child)); };
292}
293
294} // namespace ftxui
Element bgcolor(Color color, Element child)
設定元素的背景顏色。
Definition dom/color.cpp:96
Element color(Color color, Element child)
設定元素的前景顏色。
Definition dom/color.cpp:81
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:25
std::function< Element(Element)> Decorator
Definition elements.hpp:27
int y
Definition elements.hpp:131
void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:105
std::vector< Color > colors
std::vector< float > positions