FTXUI/src/ftxui/dom/color.cpp

109 lines
2.9 KiB
C++
Raw Normal View History

2021-05-10 02:32:27 +08:00
#include <memory> // for make_shared
#include <utility> // for move
2021-05-02 02:40:35 +08:00
#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
2021-05-10 02:32:27 +08:00
#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
2018-10-12 15:23:37 +08:00
namespace ftxui {
2018-10-12 15:23:37 +08:00
class BgColor : public NodeDecorator {
public:
BgColor(Element child, Color color)
: NodeDecorator(std::move(child)), color_(color) {}
2018-10-12 15:23:37 +08:00
void Render(Screen& screen) override {
2019-01-20 05:06:05 +08:00
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
2018-10-12 15:23:37 +08:00
screen.PixelAt(x, y).background_color = color_;
}
}
NodeDecorator::Render(screen);
2018-10-12 15:23:37 +08:00
}
Color color_;
};
class FgColor : public NodeDecorator {
public:
FgColor(Element child, Color color)
: NodeDecorator(std::move(child)), color_(color) {}
2018-10-12 15:23:37 +08:00
void Render(Screen& screen) override {
2019-01-20 05:06:05 +08:00
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
2018-10-12 15:23:37 +08:00
screen.PixelAt(x, y).foreground_color = color_;
}
}
NodeDecorator::Render(screen);
2018-10-12 15:23:37 +08:00
}
Color color_;
};
2020-05-25 07:34:13 +08:00
/// @brief Set the foreground color of an element.
2020-08-16 08:24:50 +08:00
/// @param color The color of the output element.
/// @param child The input element.
2020-05-25 07:34:13 +08:00
/// @return The output element colored.
/// @ingroup dom
///
/// ### Example
///
/// ```cpp
/// Element document = color(Color::Green, text(L"Success")),
/// ```
Element color(Color color, Element child) {
return std::make_shared<FgColor>(std::move(child), color);
2018-10-12 15:23:37 +08:00
}
2020-05-25 07:34:13 +08:00
/// @brief Set the background color of an element.
2020-08-16 08:24:50 +08:00
/// @param color The color of the output element.
/// @param child The input element.
2020-05-25 07:34:13 +08:00
/// @return The output element colored.
/// @ingroup dom
///
/// ### Example
///
/// ```cpp
/// Element document = bgcolor(Color::Green, text(L"Success")),
/// ```
Element bgcolor(Color color, Element child) {
return std::make_shared<BgColor>(std::move(child), color);
2018-10-12 15:23:37 +08:00
}
2020-05-25 07:34:13 +08:00
/// @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(L"red") | color(Color::Red);
/// ```
2019-01-03 07:35:59 +08:00
Decorator color(Color c) {
2020-03-23 05:32:44 +08:00
return [c](Element child) { return color(c, std::move(child)); };
2019-01-03 07:35:59 +08:00
}
2020-05-25 07:34:13 +08:00
/// @brief Decorate using a background color.
2020-08-16 08:24:50 +08:00
/// @param color The background color to be applied.
2020-05-25 07:34:13 +08:00
/// @return The Decorator applying the color.
/// @ingroup dom
///
/// ### Example
///
/// ```cpp
/// Element document = text(L"red") | bgcolor(Color::Red);
/// ```
2020-08-16 08:24:50 +08:00
Decorator bgcolor(Color color) {
return [color](Element child) { return bgcolor(color, std::move(child)); };
2019-01-03 07:35:59 +08:00
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui
// 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.