FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
dom/color.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni. 保留所有权利。
2// 本源代码的使用受 MIT 许可协议的约束,
3// 该协议可在 LICENSE 文件中找到。
4#include <memory> // for make_shared
5#include <utility> // for move
6
7#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
8#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
9#include "ftxui/screen/box.hpp" // for Box
10#include "ftxui/screen/color.hpp" // for Color
11#include "ftxui/screen/screen.hpp" // for Pixel, Screen
12
13namespace ftxui {
14
15namespace {
16class BgColor : public NodeDecorator {
17 public:
18 BgColor(Element child, Color color)
19 : NodeDecorator(std::move(child)), color_(color) {}
20
21 void Render(Screen& screen) override {
22 if (color_.IsOpaque()) {
23 for (int y = box_.y_min; y <= box_.y_max; ++y) {
24 for (int x = box_.x_min; x <= box_.x_max; ++x) {
25 screen.PixelAt(x, y).background_color = color_;
26 }
27 }
28 } else {
29 for (int y = box_.y_min; y <= box_.y_max; ++y) {
30 for (int x = box_.x_min; x <= box_.x_max; ++x) {
31 Color& color = screen.PixelAt(x, y).background_color;
32 color = Color::Blend(color, color_);
33 }
34 }
35 }
37 }
38
39 Color color_;
40};
41
42class FgColor : public NodeDecorator {
43 public:
44 FgColor(Element child, Color color)
45 : NodeDecorator(std::move(child)), color_(color) {}
46
47 void Render(Screen& screen) override {
48 if (color_.IsOpaque()) {
49 for (int y = box_.y_min; y <= box_.y_max; ++y) {
50 for (int x = box_.x_min; x <= box_.x_max; ++x) {
51 screen.PixelAt(x, y).foreground_color = color_;
52 }
53 }
54 } else {
55 for (int y = box_.y_min; y <= box_.y_max; ++y) {
56 for (int x = box_.x_min; x <= box_.x_max; ++x) {
57 Color& color = screen.PixelAt(x, y).foreground_color;
58 color = Color::Blend(color, color_);
59 }
60 }
61 }
63 }
64
66};
67
68} // namespace
69
70/// @brief 设置元素的前景颜色。
71/// @param color 输出元素的颜色。
72/// @param child 输入元素。
73/// @return 着色的输出元素。
74/// @ingroup dom
75///
76/// ### 示例
77///
78/// ```cpp
79/// Element document = color(Color::Green, text("Success")),
80/// ```
81Element color(Color color, Element child) {
82 return std::make_shared<FgColor>(std::move(child), color);
83}
84
85/// @brief 设置元素的背景颜色。
86/// @param color 输出元素的颜色。
87/// @param child 输入元素。
88/// @return 着色的输出元素。
89/// @ingroup dom
90///
91/// ### 示例
92///
93/// ```cpp
94/// Element document = bgcolor(Color::Green, text("Success")),
95/// ```
97 return std::make_shared<BgColor>(std::move(child), color);
98}
99
100/// @brief 使用前景色进行装饰。
101/// @param c 要应用的前景色。
102/// @return 应用颜色的装饰器。
103/// @ingroup dom
104///
105/// ### 示例
106///
107/// ```cpp
108/// Element document = text("red") | color(Color::Red);
109/// ```
111 return [c](Element child) { return color(c, std::move(child)); };
112}
113
114/// @brief 使用背景色进行装饰。
115/// @param color 要应用的背景颜色。
116/// @return 应用颜色的装饰器。
117/// @ingroup dom
118///
119/// ### 示例
120///
121/// ```cpp
122/// Element document = text("red") | bgcolor(Color::Red);
123/// ```
125 return [color](Element child) { return bgcolor(color, std::move(child)); };
126}
127
128} // namespace ftxui
friend void Render(Screen &screen, Node *node, Selection &selection)
Decorator bgcolor(Color)
使用背景色进行装饰。
void Render(Screen &screen, const Element &element)
在 ftxui::Screen 上显示元素。
Decorator color(Color)
使用前景色进行装饰。
static Color Blend(const Color &lhs, const Color &rhs)
使用 alpha 通道混合两种颜色。
Color 是一个表示终端用户界面中颜色的类。
Color
Color 是一个表示终端颜色支持的枚举。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::function< Element(Element)> Decorator
std::shared_ptr< Node > Element