2023-08-19 19:56:36 +08:00
|
|
|
// 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.
|
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
|
|
|
|
2021-07-20 15:59:47 +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
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-12 15:23:37 +08:00
|
|
|
|
2023-09-27 05:08:42 +08:00
|
|
|
namespace {
|
2018-10-12 15:23:37 +08:00
|
|
|
class BgColor : public NodeDecorator {
|
|
|
|
public:
|
2021-07-20 15:59:47 +08:00
|
|
|
BgColor(Element child, Color color)
|
|
|
|
: NodeDecorator(std::move(child)), color_(color) {}
|
2018-10-12 15:23:37 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
void Render(Screen& screen) override {
|
2024-06-14 00:43:14 +08:00
|
|
|
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_);
|
|
|
|
}
|
2018-10-12 15:23:37 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-05 09:03:49 +08:00
|
|
|
NodeDecorator::Render(screen);
|
2018-10-12 15:23:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Color color_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FgColor : public NodeDecorator {
|
|
|
|
public:
|
2021-07-20 15:59:47 +08:00
|
|
|
FgColor(Element child, Color color)
|
|
|
|
: NodeDecorator(std::move(child)), color_(color) {}
|
2018-10-12 15:23:37 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
void Render(Screen& screen) override {
|
2024-06-14 00:43:14 +08:00
|
|
|
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_);
|
|
|
|
}
|
2018-10-12 15:23:37 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-05 09:03:49 +08:00
|
|
|
NodeDecorator::Render(screen);
|
2018-10-12 15:23:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Color color_;
|
|
|
|
};
|
2024-06-14 00:43:14 +08:00
|
|
|
|
2023-08-19 20:56:28 +08:00
|
|
|
} // namespace
|
2018-10-12 15:23:37 +08:00
|
|
|
|
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
|
2021-08-09 05:25:20 +08:00
|
|
|
/// Element document = color(Color::Green, text("Success")),
|
2020-05-25 07:34:13 +08:00
|
|
|
/// ```
|
|
|
|
Element color(Color color, Element child) {
|
2021-07-20 15:59:47 +08:00
|
|
|
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
|
2021-08-09 05:25:20 +08:00
|
|
|
/// Element document = bgcolor(Color::Green, text("Success")),
|
2020-05-25 07:34:13 +08:00
|
|
|
/// ```
|
|
|
|
Element bgcolor(Color color, Element child) {
|
2021-07-20 15:59:47 +08:00
|
|
|
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
|
2021-08-09 05:25:20 +08:00
|
|
|
/// Element document = text("red") | color(Color::Red);
|
2020-05-25 07:34:13 +08:00
|
|
|
/// ```
|
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
|
2021-08-09 05:25:20 +08:00
|
|
|
/// Element document = text("red") | bgcolor(Color::Red);
|
2020-05-25 07:34:13 +08:00
|
|
|
/// ```
|
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
|