Loading...
Searching...
No Matches
color.cpp
Go to the documentation of this file.
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <memory> // for make_shared
#include <utility> // for move
#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/color.hpp" // for Color
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
namespace ftxui {
namespace {
class BgColor : public NodeDecorator {
public:
BgColor(Element child, Color color)
: NodeDecorator(std::move(child)), color_(color) {}
void Render(Screen& screen) override {
if (color_.IsOpaque()) {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
screen.PixelAt(x, y).background_color = color_;
}
}
} else {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
Color& color = screen.PixelAt(x, y).background_color;
color = Color::Blend(color, color_);
}
}
}
NodeDecorator::Render(screen);
}
Color color_;
};
class FgColor : public NodeDecorator {
public:
FgColor(Element child, Color color)
: NodeDecorator(std::move(child)), color_(color) {}
void Render(Screen& screen) override {
if (color_.IsOpaque()) {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
screen.PixelAt(x, y).foreground_color = color_;
}
}
} else {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
Color& color = screen.PixelAt(x, y).foreground_color;
color = Color::Blend(color, color_);
}
}
}
NodeDecorator::Render(screen);
}
Color color_;
};
} // namespace
/// @brief Set the foreground color of an element.
/// @param color The color of the output element.
/// @param child The input element.
/// @return The output element colored.
/// @ingroup dom
///
/// ### Example
///
/// ```cpp
/// Element document = color(Color::Green, text("Success")),
/// ```
Element color(Color color, Element child) {
return std::make_shared<FgColor>(std::move(child), color);
}
/// @brief Set the background color of an element.
/// @param color The color of the output element.
/// @param child The input element.
/// @return The output element colored.
/// @ingroup dom
///
/// ### Example
///
/// ```cpp
/// Element document = bgcolor(Color::Green, text("Success")),
/// ```
Element bgcolor(Color color, Element child) {
return std::make_shared<BgColor>(std::move(child), color);
}
/// @brief Decorate using a foreground color.
/// @param c The foreground color to be applied.
/// @return The Decorator applying the color.
/// @ingroup dom
///
/// ### Example
///
/// ```cpp
/// Element document = text("red") | color(Color::Red);
/// ```
Decorator color(Color c) {
return [c](Element child) { return color(c, std::move(child)); };
}
/// @brief Decorate using a background color.
/// @param color The background color to be applied.
/// @return The Decorator applying the color.
/// @ingroup dom
///
/// ### Example
///
/// ```cpp
/// Element document = text("red") | bgcolor(Color::Red);
/// ```
Decorator bgcolor(Color color) {
return [color](Element child) { return bgcolor(color, std::move(child)); };
}
} // namespace ftxui
box.hpp
ftxui::Color
A class representing terminal colors.
Definition color.hpp:20
ftxui::Color::Blend
static Color Blend(const Color &lhs, const Color &rhs)
Blend two colors together using the alpha channel.
Definition color.cpp:281
ftxui::Node::Render
friend void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:100
color.hpp
elements.hpp
ftxui::Terminal::Color
Color
Definition terminal.hpp:17
ftxui
Definition animation.hpp:10
ftxui::bgcolor
Decorator bgcolor(Color)
Decorate using a background color.
Definition color.cpp:124
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition elements.hpp:24
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Render
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:88
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:110
node_decorator.hpp
screen.hpp