22struct LinearGradientNormalized {
24 std::vector<Color> colors;
25 std::vector<float> positions;
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;
49 for (
size_t i = 1; i <
gradient.stops.size(); ++i) {
55 const float min =
gradient.stops[i].position.value();
59 gradient.stops[j].position = min + (max - min) *
71 [](
const auto& a,
const auto& b) { return a.position < b.position; });
74 if (
gradient.stops.front().position != 0) {
76 {gradient.stops.front().color, 0.F});
79 if (
gradient.stops.back().position != 1) {
85 const float modulo = 360.F;
96Color Interpolate(
const LinearGradientNormalized&
gradient,
float t) {
100 if (i >
gradient.positions.size()) {
101 const float half = 0.5F;
113 const float tt = (t -
t0) / (
t1 -
t0);
122class LinearGradientColor :
public NodeDecorator {
126 bool background_color)
129 background_color_{background_color} {}
133 const float degtorad = 0.01745329251F;
134 const float dx = std::cos(gradient_.angle *
degtorad);
135 const float dy = std::sin(gradient_.angle *
degtorad);
142 const float min = std::min({
p1,
p2,
p3,
p4});
143 const float max = std::max({
p1,
p2,
p3,
p4});
147 const float dX =
dx / (max - min);
148 const float dY =
dy / (max - min);
149 const float dZ = -min / (max - min);
152 if (background_color_) {
153 for (
int y = box_.y_min; y <= box_.y_max; ++y) {
154 for (
int x = box_.x_min; x <= box_.x_max; ++x) {
156 screen.PixelAt(x, y).background_color = Interpolate(gradient_, t);
160 for (
int y = box_.y_min; y <= box_.y_max; ++y) {
161 for (
int x = box_.x_min; x <= box_.x_max; ++x) {
163 screen.PixelAt(x, y).foreground_color = Interpolate(gradient_, t);
171 LinearGradientNormalized gradient_;
172 bool background_color_;
222 stops.push_back({c, p});
232 stops.push_back({c, {}});
248 return std::make_shared<LinearGradientColor>(std::move(
child),
gradient,
264 return std::make_shared<LinearGradientColor>(std::move(
child),
gradient,
A class representing terminal colors.
static Color Interpolate(float t, const Color &a, const Color &b)
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Decorator bgcolor(Color)
Decorate using a background color.
std::function< Element(Element)> Decorator
std::shared_ptr< Node > Element
std::shared_ptr< T > Make(Args &&... args)
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Decorator color(Color)
Decorate using a foreground color.
A class representing the settings for linear-gradient color effect.
LinearGradient & Angle(float angle)
Set the angle of the gradient.
LinearGradient()
Build the "empty" gradient. This is often followed by calls to LinearGradient::Angle() and LinearGrad...
std::vector< Stop > stops