FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
color_gallery.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni。保留所有权利。
2// 本源代码受 MIT 许可协议的约束,详情请参阅 LICENSE 文件。
3#include <ftxui/screen/color_info.hpp> // for ColorInfo
4#include <ftxui/screen/screen.hpp> // for Full, Screen
5#include <ftxui/screen/terminal.hpp> // for ColorSupport, Color, Palette16, Palette256, TrueColor
6#include <memory> // for allocator, shared_ptr
7#include <utility> // for move
8#include <vector> // for vector
9
10#include "ftxui/dom/elements.hpp" // for text, bgcolor, color, vbox, hbox, separator, operator|, Elements, Element, Fit, border
11#include "ftxui/dom/node.hpp" // for Render
12#include "ftxui/screen/color.hpp" // for Color, Color::Black, Color::Blue, Color::BlueLight, Color::Cyan, Color::CyanLight, Color::Default, Color::GrayDark, Color::GrayLight, Color::Green, Color::GreenLight, Color::Magenta, Color::MagentaLight, Color::Red, Color::RedLight, Color::White, Color::Yellow, Color::YellowLight, Color::Palette256, ftxui
13
14using namespace ftxui;
15#include "./color_info_sorted_2d.ipp" // for ColorInfoSorted2D
16
17int main() {
18 // clang-format off
19 auto basic_color_display =
20 vbox(
21 text("16 color palette:"),
22 separator(),
23 hbox(
24 vbox(
25 color(Color::Default, text("Default")),
26 color(Color::Black, text("Black")),
27 color(Color::GrayDark, text("GrayDark")),
28 color(Color::GrayLight, text("GrayLight")),
29 color(Color::White, text("White")),
30 color(Color::Blue, text("Blue")),
31 color(Color::BlueLight, text("BlueLight")),
32 color(Color::Cyan, text("Cyan")),
33 color(Color::CyanLight, text("CyanLight")),
34 color(Color::Green, text("Green")),
35 color(Color::GreenLight, text("GreenLight")),
36 color(Color::Magenta, text("Magenta")),
37 color(Color::MagentaLight, text("MagentaLight")),
38 color(Color::Red, text("Red")),
39 color(Color::RedLight, text("RedLight")),
40 color(Color::Yellow, text("Yellow")),
41 color(Color::YellowLight, text("YellowLight"))
42 ),
43 vbox(
44 bgcolor(Color::Default, text("Default")),
45 bgcolor(Color::Black, text("Black")),
46 bgcolor(Color::GrayDark, text("GrayDark")),
47 bgcolor(Color::GrayLight, text("GrayLight")),
48 bgcolor(Color::White, text("White")),
49 bgcolor(Color::Blue, text("Blue")),
50 bgcolor(Color::BlueLight, text("BlueLight")),
51 bgcolor(Color::Cyan, text("Cyan")),
52 bgcolor(Color::CyanLight, text("CyanLight")),
53 bgcolor(Color::Green, text("Green")),
54 bgcolor(Color::GreenLight, text("GreenLight")),
55 bgcolor(Color::Magenta, text("Magenta")),
56 bgcolor(Color::MagentaLight, text("MagentaLight")),
57 bgcolor(Color::Red, text("Red")),
58 bgcolor(Color::RedLight, text("RedLight")),
59 bgcolor(Color::Yellow, text("Yellow")),
60 bgcolor(Color::YellowLight, text("YellowLight"))
61 )
62 )
63 );
64
65 // clang-format on
66 auto palette_256_color_display = text("256 colors palette:");
67 {
68 std::vector<std::vector<ColorInfo>> info_columns = ColorInfoSorted2D();
69 Elements columns;
70 for (auto& column : info_columns) {
71 Elements column_elements;
72 for (auto& it : column) {
73 column_elements.push_back(
74 text(" ") | bgcolor(Color(Color::Palette256(it.index_256))));
75 }
76 columns.push_back(hbox(std::move(column_elements)));
77 }
78 palette_256_color_display = vbox({
79 palette_256_color_display,
80 separator(),
81 vbox(columns),
82 });
83 }
84
85 // 真彩色显示。
86 auto true_color_display = text("TrueColors: 24bits:");
87 {
88 const int max_value = 255;
89 const int value_increment = 8;
90 const int hue_increment = 6;
91 int saturation = max_value;
92 Elements array;
93 for (int value = 0; value < max_value; value += 2 * value_increment) {
94 Elements line;
95 for (int hue = 0; hue < max_value; hue += hue_increment) {
96 line.push_back(
97 text("▀") //
98 | color(Color::HSV(hue, saturation, value)) //
99 | bgcolor(Color::HSV(hue, saturation, value + value_increment)));
100 }
101 array.push_back(hbox(std::move(line)));
102 }
103 true_color_display = vbox({
104 true_color_display,
105 separator(),
106 vbox(std::move(array)),
107 });
108 }
109
110 auto terminal_info =
111 vbox({
112 Terminal::ColorSupport() >= Terminal::Color::Palette16
113 ? text(" 16 color palette support : Yes")
114 : text(" 16 color palette support : No"),
115 Terminal::ColorSupport() >= Terminal::Color::Palette256
116 ? text("256 color palette support : Yes")
117 : text("256 color palette support : No"),
118 Terminal::ColorSupport() >= Terminal::Color::TrueColor
119 ? text(" True color support : Yes")
120 : text(" True color support : No"),
121 }) |
122 border;
123
124 auto document = vbox({hbox({
125 basic_color_display,
126 text(" "),
127 palette_256_color_display,
128 text(" "),
129 true_color_display,
130 }),
131 terminal_info});
132 // clang-format on
133
134 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
135 Render(screen, document);
136
137 screen.Print();
138
139 return 0;
140}
std::vector< std::vector< ftxui::ColorInfo > > ColorInfoSorted2D()
Decorator bgcolor(Color)
使用背景色进行装饰。
Element text(std::wstring text)
显示一段Unicode文本。
Element separator()
在两个其他元素之间绘制垂直或水平分隔线。
void Render(Screen &screen, const Element &element)
在 ftxui::Screen 上显示元素。
Decorator color(Color)
使用前景色进行装饰。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value)
从其 HSV 表示构建一个颜色。 https://en.wikipedia.org/wiki/HSL_and_HSV
static Screen Create(Dimensions dimension)
创建一个具有给定维度的屏幕。
Color 是一个表示终端用户界面中颜色的类。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
Element hbox(Elements)
一个按水平顺序逐一显示元素的容器。
std::vector< Element > Elements