FTXUI  0.8.1
C++ functional terminal UI.
color.cpp
Go to the documentation of this file.
1 #include <memory> // for make_shared
2 #include <utility> // for move
3 
4 #include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
5 #include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
6 #include "ftxui/screen/box.hpp" // for Box
7 #include "ftxui/screen/color.hpp" // for Color
8 #include "ftxui/screen/screen.hpp" // for Pixel, Screen
9 
10 namespace ftxui {
11 
12 class BgColor : public NodeDecorator {
13  public:
14  BgColor(Element child, Color color)
15  : NodeDecorator(std::move(child)), color_(color) {}
16 
17  void Render(Screen& screen) override {
18  for (int y = box_.y_min; y <= box_.y_max; ++y) {
19  for (int x = box_.x_min; x <= box_.x_max; ++x) {
20  screen.PixelAt(x, y).background_color = color_;
21  }
22  }
23  NodeDecorator::Render(screen);
24  }
25 
26  Color color_;
27 };
28 
29 class FgColor : public NodeDecorator {
30  public:
31  FgColor(Element child, Color color)
32  : NodeDecorator(std::move(child)), color_(color) {}
33 
34  void Render(Screen& screen) override {
35  for (int y = box_.y_min; y <= box_.y_max; ++y) {
36  for (int x = box_.x_min; x <= box_.x_max; ++x) {
37  screen.PixelAt(x, y).foreground_color = color_;
38  }
39  }
40  NodeDecorator::Render(screen);
41  }
42 
43  Color color_;
44 };
45 
46 /// @brief Set the foreground color of an element.
47 /// @param color The color of the output element.
48 /// @param child The input element.
49 /// @return The output element colored.
50 /// @ingroup dom
51 ///
52 /// ### Example
53 ///
54 /// ```cpp
55 /// Element document = color(Color::Green, text("Success")),
56 /// ```
58  return std::make_shared<FgColor>(std::move(child), color);
59 }
60 
61 /// @brief Set the background color of an element.
62 /// @param color The color of the output element.
63 /// @param child The input element.
64 /// @return The output element colored.
65 /// @ingroup dom
66 ///
67 /// ### Example
68 ///
69 /// ```cpp
70 /// Element document = bgcolor(Color::Green, text("Success")),
71 /// ```
73  return std::make_shared<BgColor>(std::move(child), color);
74 }
75 
76 /// @brief Decorate using a foreground color.
77 /// @param c The foreground color to be applied.
78 /// @return The Decorator applying the color.
79 /// @ingroup dom
80 ///
81 /// ### Example
82 ///
83 /// ```cpp
84 /// Element document = text("red") | color(Color::Red);
85 /// ```
87  return [c](Element child) { return color(c, std::move(child)); };
88 }
89 
90 /// @brief Decorate using a background color.
91 /// @param color The background color to be applied.
92 /// @return The Decorator applying the color.
93 /// @ingroup dom
94 ///
95 /// ### Example
96 ///
97 /// ```cpp
98 /// Element document = text("red") | bgcolor(Color::Red);
99 /// ```
101  return [color](Element child) { return bgcolor(color, std::move(child)); };
102 }
103 
104 } // namespace ftxui
105 
106 // Copyright 2020 Arthur Sonzogni. All rights reserved.
107 // Use of this source code is governed by the MIT license that can be found in
108 // the LICENSE file.
ftxui::Box::x_min
int x_min
Definition: box.hpp:7
ftxui::Node::box_
Box box_
Definition: node.hpp:41
ftxui::Box::y_max
int y_max
Definition: box.hpp:10
ftxui
Definition: captured_mouse.hpp:6
ftxui::Node::Render
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:27
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition: color.cpp:86
box.hpp
ftxui::Box::x_max
int x_max
Definition: box.hpp:8
elements.hpp
node_decorator.hpp
ftxui::Element
std::shared_ptr< Node > Element
Definition: elements.hpp:15
ftxui::bgcolor
Decorator bgcolor(Color)
Decorate using a background color.
Definition: color.cpp:100
ftxui::Terminal::Color
Color
Definition: terminal.hpp:13
ftxui::Box::y_min
int y_min
Definition: box.hpp:9
color.hpp
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition: elements.hpp:17
ftxui::Render
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:34
ftxui::NodeDecorator::NodeDecorator
NodeDecorator(Element child)
Definition: node_decorator.hpp:15
screen.hpp
ftxui::Color
A class representing terminal colors.
Definition: color.hpp:17