FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
dom/color.cpp
Go to the documentation of this file.
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)
使用背景顏色進行裝飾。
Decorator color(Color)
使用前景顏色進行裝飾。
static Color Blend(const Color &lhs, const Color &rhs)
使用 alpha 色版將兩種顏色混合在一起。
Color 是一個在終端使用者介面中表示顏色的類別。
Definition color.hpp:20
Color
Color 是一個列舉,表示終端機的色彩支援
Definition terminal.hpp:22
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:24
std::shared_ptr< Node > Element
Definition elements.hpp:22
void Render(Screen &screen, const Element &element)