FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
color_gallery.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <ftxui/screen/color_info.hpp> // for ColorInfo
5#include <ftxui/screen/screen.hpp> // for Full, Screen
6#include <ftxui/screen/terminal.hpp> // for ColorSupport, Color, Palette16, Palette256, TrueColor
7#include <memory> // for allocator, shared_ptr
8#include <utility> // for move
9#include <vector> // for vector
10
11#include "ftxui/dom/elements.hpp" // for text, bgcolor, color, vbox, hbox, separator, operator|, Elements, Element, Fit, border
12#include "ftxui/dom/node.hpp" // for Render
13#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
14
15using namespace ftxui;
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 // True color display.
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}
Element text(std::string_view text)
Display a piece of UTF8 encoded unicode text.
Element bgcolor(Color color, Element child)
Set the background color of an element.
Definition dom/color.cpp:96
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Element color(Color color, Element child)
Set the foreground color of an element.
Definition dom/color.cpp:81
Element vbox(Elements children)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
Element hbox(Elements children)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
std::vector< Element > Elements
Definition elements.hpp:25
std::vector< std::vector< ColorInfo > > ColorInfoSorted2D()
void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:105
Color
Build a transparent color.
Definition elements.hpp:91
int value
Definition elements.hpp:178