FTXUI  5.0.0
C++ functional terminal UI.
color.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 <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 
13 namespace ftxui {
14 
15 namespace {
16 class 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  for (int y = box_.y_min; y <= box_.y_max; ++y) {
23  for (int x = box_.x_min; x <= box_.x_max; ++x) {
24  screen.PixelAt(x, y).background_color = color_;
25  }
26  }
27  NodeDecorator::Render(screen);
28  }
29 
30  Color color_;
31 };
32 
33 class FgColor : public NodeDecorator {
34  public:
35  FgColor(Element child, Color color)
36  : NodeDecorator(std::move(child)), color_(color) {}
37 
38  void Render(Screen& screen) override {
39  for (int y = box_.y_min; y <= box_.y_max; ++y) {
40  for (int x = box_.x_min; x <= box_.x_max; ++x) {
41  screen.PixelAt(x, y).foreground_color = color_;
42  }
43  }
44  NodeDecorator::Render(screen);
45  }
46 
47  Color color_;
48 };
49 } // namespace
50 
51 /// @brief Set the foreground color of an element.
52 /// @param color The color of the output element.
53 /// @param child The input element.
54 /// @return The output element colored.
55 /// @ingroup dom
56 ///
57 /// ### Example
58 ///
59 /// ```cpp
60 /// Element document = color(Color::Green, text("Success")),
61 /// ```
63  return std::make_shared<FgColor>(std::move(child), color);
64 }
65 
66 /// @brief Set the background color of an element.
67 /// @param color The color of the output element.
68 /// @param child The input element.
69 /// @return The output element colored.
70 /// @ingroup dom
71 ///
72 /// ### Example
73 ///
74 /// ```cpp
75 /// Element document = bgcolor(Color::Green, text("Success")),
76 /// ```
78  return std::make_shared<BgColor>(std::move(child), color);
79 }
80 
81 /// @brief Decorate using a foreground color.
82 /// @param c The foreground color to be applied.
83 /// @return The Decorator applying the color.
84 /// @ingroup dom
85 ///
86 /// ### Example
87 ///
88 /// ```cpp
89 /// Element document = text("red") | color(Color::Red);
90 /// ```
92  return [c](Element child) { return color(c, std::move(child)); };
93 }
94 
95 /// @brief Decorate using a background color.
96 /// @param color The background color to be applied.
97 /// @return The Decorator applying the color.
98 /// @ingroup dom
99 ///
100 /// ### Example
101 ///
102 /// ```cpp
103 /// Element document = text("red") | bgcolor(Color::Red);
104 /// ```
106  return [color](Element child) { return bgcolor(color, std::move(child)); };
107 }
108 
109 } // namespace ftxui
A class representing terminal colors.
Definition: color.hpp:21
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:32
Decorator bgcolor(Color)
Decorate using a background color.
Definition: color.cpp:105
std::function< Element(Element)> Decorator
Definition: elements.hpp:25
std::shared_ptr< Node > Element
Definition: elements.hpp:23
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47
Decorator color(Color)
Decorate using a foreground color.
Definition: color.cpp:91