22struct LinearGradientNormalized {
29LinearGradientNormalized Normalize(LinearGradient gradient) {
31 if (gradient.stops.empty()) {
32 return LinearGradientNormalized{
40 if (!gradient.stops.front().position) {
41 gradient.stops.front().position = 0.F;
43 if (!gradient.stops.back().position) {
44 gradient.stops.back().position = 1.F;
48 size_t last_checkpoint = 0;
49 for (
size_t i = 1; i < gradient.stops.size(); ++i) {
50 if (!gradient.stops[i].position) {
54 if (i - last_checkpoint >= 2) {
55 const float min = gradient.stops[i].position.value();
57 gradient.stops[last_checkpoint].position.value();
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);
70 gradient.stops.begin(), gradient.stops.end(),
71 [](
const auto& a,
const auto& b) { return a.position < b.position; });
74 if (gradient.stops.front().position != 0) {
75 gradient.stops.insert(gradient.stops.begin(),
76 {gradient.stops.front().color, 0.F});
79 if (gradient.stops.back().position != 1) {
80 gradient.stops.push_back({gradient.stops.back().color, 1.F});
84 LinearGradientNormalized normalized;
85 const float modulo = 360.F;
87 std::fmod(std::fmod(gradient.angle, modulo) + modulo, modulo);
88 for (
auto& stop : gradient.stops) {
89 normalized.colors.push_back(stop.color);
91 normalized.positions.push_back(stop.position.value());
96Color Interpolate(
const LinearGradientNormalized& gradient,
float t) {
103 const float half = 0.5F;
105 gradient.colors.back());
107 if (t <= gradient.positions[i]) {
113 const float t0 = gradient.positions[i - 1];
114 const float t1 = gradient.positions[i - 0];
115 const float tt = (t - t0) / (t1 - t0);
117 const Color& c0 = gradient.colors[i - 1];
118 const Color& c1 = gradient.colors[i - 0];
124class LinearGradientColor :
public NodeDecorator {
126 explicit LinearGradientColor(
Element child,
127 const LinearGradient& gradient,
128 bool background_color)
130 gradient_(Normalize(gradient)),
131 background_color_{background_color} {}
135 const float degtorad = 0.01745329251F;
136 const float dx = std::cos(gradient_.angle * degtorad);
137 const float dy = std::sin(gradient_.angle * degtorad);
144 const float min = std::min({p1, p2, p3, p4});
145 const float max = std::max({p1, p2, p3, p4});
148 const float dY = dy / (max - min);
149 const float dZ = -min / (max - min);
152 if (background_color_) {
155 const float t = float(x) * dX + float(y) * dY + dZ;
156 screen.PixelAt(x, y).background_color = Interpolate(gradient_, t);
162 const float t = float(x) * dX + float(y) * dY + dZ;
163 screen.PixelAt(x, y).foreground_color = Interpolate(gradient_, t);
171 LinearGradientNormalized gradient_;
172 bool background_color_;
188LinearGradient::LinearGradient() =
default;
201 stops.push_back({begin, {}});
202 stops.push_back({end, {}});
217 stops.push_back({c, p});
226 stops.push_back({c, {}});
242 return std::make_shared<LinearGradientColor>(std::move(child), gradient,
258 return std::make_shared<LinearGradientColor>(std::move(child), gradient,
274 [gradient](
Element child) {
return color(gradient, std::move(child)); };
289 [gradient](
Element child) {
return bgcolor(gradient, std::move(child)); };
NodeDecorator(Element child)
LinearGradient & Stop(Color color, float position)
LinearGradient & Angle(float angle)
Définit l'angle du dégradé.
LinearGradient()
Construit le dégradé "vide". Ceci est souvent suivi d'appels à LinearGradient::Angle() et LinearGradi...
std::vector< Stop > stops
friend void Render(Screen &screen, Node *node, Selection &selection)
Decorator bgcolor(Color)
Décore en utilisant une couleur d'arrière-plan.
Decorator color(Color)
Décore en utilisant une couleur de premier plan.
Une classe représentant les paramètres de l'effet de couleur en dégradé linéaire.
static Color Interpolate(float t, const Color &a, const Color &b)
Color est une classe qui représente une couleur dans l'interface utilisateur du terminal.
Color
Color est une énumération qui représente le support des couleurs du terminal.
L'espace de noms FTXUI ftxui::
std::function< Element(Element)> Decorator
std::shared_ptr< Node > Element
std::vector< Color > colors
std::vector< float > positions