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#include "./color_info_sorted_2d.ipp" // for ColorInfoSorted2D
17
18int main() {
19 // clang-format off
20 auto basic_color_display =
21 vbox(
22 text("16 color palette:"),
23 separator(),
24 hbox(
25 vbox(
26 color(Color::Default, text("Default")),
27 color(Color::Black, text("Black")),
28 color(Color::GrayDark, text("GrayDark")),
29 color(Color::GrayLight, text("GrayLight")),
30 color(Color::White, text("White")),
31 color(Color::Blue, text("Blue")),
32 color(Color::BlueLight, text("BlueLight")),
33 color(Color::Cyan, text("Cyan")),
34 color(Color::CyanLight, text("CyanLight")),
35 color(Color::Green, text("Green")),
36 color(Color::GreenLight, text("GreenLight")),
37 color(Color::Magenta, text("Magenta")),
38 color(Color::MagentaLight, text("MagentaLight")),
39 color(Color::Red, text("Red")),
40 color(Color::RedLight, text("RedLight")),
41 color(Color::Yellow, text("Yellow")),
42 color(Color::YellowLight, text("YellowLight"))
43 ),
44 vbox(
45 bgcolor(Color::Default, text("Default")),
46 bgcolor(Color::Black, text("Black")),
47 bgcolor(Color::GrayDark, text("GrayDark")),
48 bgcolor(Color::GrayLight, text("GrayLight")),
49 bgcolor(Color::White, text("White")),
50 bgcolor(Color::Blue, text("Blue")),
51 bgcolor(Color::BlueLight, text("BlueLight")),
52 bgcolor(Color::Cyan, text("Cyan")),
53 bgcolor(Color::CyanLight, text("CyanLight")),
54 bgcolor(Color::Green, text("Green")),
55 bgcolor(Color::GreenLight, text("GreenLight")),
56 bgcolor(Color::Magenta, text("Magenta")),
57 bgcolor(Color::MagentaLight, text("MagentaLight")),
58 bgcolor(Color::Red, text("Red")),
59 bgcolor(Color::RedLight, text("RedLight")),
60 bgcolor(Color::Yellow, text("Yellow")),
61 bgcolor(Color::YellowLight, text("YellowLight"))
62 )
63 )
64 );
65
66 // clang-format on
67 auto palette_256_color_display = text("256 colors palette:");
68 {
69 std::vector<std::vector<ColorInfo>> info_columns = ColorInfoSorted2D();
70 Elements columns;
71 for (auto& column : info_columns) {
72 Elements column_elements;
73 for (auto& it : column) {
74 column_elements.push_back(
75 text(" ") | bgcolor(Color(Color::Palette256(it.index_256))));
76 }
77 columns.push_back(hbox(std::move(column_elements)));
78 }
79 palette_256_color_display = vbox({
80 palette_256_color_display,
81 separator(),
82 vbox(columns),
83 });
84 }
85
86 // True color display.
87 auto true_color_display = text("TrueColors: 24bits:");
88 {
89 const int max_value = 255;
90 const int value_increment = 8;
91 const int hue_increment = 6;
92 int saturation = max_value;
93 Elements array;
94 for (int value = 0; value < max_value; value += 2 * value_increment) {
95 Elements line;
96 for (int hue = 0; hue < max_value; hue += hue_increment) {
97 line.push_back(
98 text("▀") //
99 | color(Color::HSV(hue, saturation, value)) //
100 | bgcolor(Color::HSV(hue, saturation, value + value_increment)));
101 }
102 array.push_back(hbox(std::move(line)));
103 }
104 true_color_display = vbox({
105 true_color_display,
106 separator(),
107 vbox(std::move(array)),
108 });
109 }
110
111 auto terminal_info =
112 vbox({
113 Terminal::ColorSupport() >= Terminal::Color::Palette16
114 ? text(" 16 color palette support : Yes")
115 : text(" 16 color palette support : No"),
116 Terminal::ColorSupport() >= Terminal::Color::Palette256
117 ? text("256 color palette support : Yes")
118 : text("256 color palette support : No"),
119 Terminal::ColorSupport() >= Terminal::Color::TrueColor
120 ? text(" True color support : Yes")
121 : text(" True color support : No"),
122 }) |
123 border;
124
125 auto document = vbox({hbox({
126 basic_color_display,
127 text(" "),
128 palette_256_color_display,
129 text(" "),
130 true_color_display,
131 }),
132 terminal_info});
133 // clang-format on
134
135 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
136 Render(screen, document);
137
138 screen.Print();
139
140 return 0;
141}
std::vector< std::vector< ftxui::ColorInfo > > ColorInfoSorted2D()
Decorator bgcolor(Color)
Decorate using a background color.
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
Element separator()
Draw a vertical or horizontal separation in between two other elements.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
Decorator color(Color)
Decorate using a foreground color.
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value)
Build a Color from its HSV representation. https://en.wikipedia.org/wiki/HSL_and_HSV.
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:394
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
Color ColorSupport()
Get the color support of the terminal.
Definition terminal.cpp:130
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
std::vector< Element > Elements
Definition elements.hpp:23