FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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
13namespace ftxui {
14
15namespace {
16class 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 if (color_.IsOpaque()) {
23 for (int y = box_.y_min; y <= box_.y_max; ++y) {
24 for (int x = box_.x_min; x <= box_.x_max; ++x) {
25 screen.PixelAt(x, y).background_color = color_;
26 }
27 }
28 } else {
29 for (int y = box_.y_min; y <= box_.y_max; ++y) {
30 for (int x = box_.x_min; x <= box_.x_max; ++x) {
31 Color& color = screen.PixelAt(x, y).background_color;
32 color = Color::Blend(color, color_);
33 }
34 }
35 }
37 }
38
39 Color color_;
40};
41
42class FgColor : public NodeDecorator {
43 public:
44 FgColor(Element child, Color color)
45 : NodeDecorator(std::move(child)), color_(color) {}
46
47 void Render(Screen& screen) override {
48 if (color_.IsOpaque()) {
49 for (int y = box_.y_min; y <= box_.y_max; ++y) {
50 for (int x = box_.x_min; x <= box_.x_max; ++x) {
51 screen.PixelAt(x, y).foreground_color = color_;
52 }
53 }
54 } else {
55 for (int y = box_.y_min; y <= box_.y_max; ++y) {
56 for (int x = box_.x_min; x <= box_.x_max; ++x) {
57 Color& color = screen.PixelAt(x, y).foreground_color;
58 color = Color::Blend(color, color_);
59 }
60 }
61 }
63 }
64
65 Color color_;
66};
67
68} // namespace
69
70/// @brief Set the foreground color of an element.
71/// @param color The color of the output element.
72/// @param child The input element.
73/// @return The output element colored.
74/// @ingroup dom
75///
76/// ### Example
77///
78/// ```cpp
79/// Element document = color(Color::Green, text("Success")),
80/// ```
82 return std::make_shared<FgColor>(std::move(child), color);
83}
84
85/// @brief Set the background color of an element.
86/// @param color The color of the output element.
87/// @param child The input element.
88/// @return The output element colored.
89/// @ingroup dom
90///
91/// ### Example
92///
93/// ```cpp
94/// Element document = bgcolor(Color::Green, text("Success")),
95/// ```
97 return std::make_shared<BgColor>(std::move(child), color);
98}
99
100/// @brief Decorate using a foreground color.
101/// @param c The foreground color to be applied.
102/// @return The Decorator applying the color.
103/// @ingroup dom
104///
105/// ### Example
106///
107/// ```cpp
108/// Element document = text("red") | color(Color::Red);
109/// ```
111 return [c](Element child) { return color(c, std::move(child)); };
112}
113
114/// @brief Decorate using a background color.
115/// @param color The background color to be applied.
116/// @return The Decorator applying the color.
117/// @ingroup dom
118///
119/// ### Example
120///
121/// ```cpp
122/// Element document = text("red") | bgcolor(Color::Red);
123/// ```
125 return [color](Element child) { return bgcolor(color, std::move(child)); };
126}
127
128} // namespace ftxui
A class representing terminal colors.
Definition color.hpp:20
static Color Blend(const Color &lhs, const Color &rhs)
Blend two colors together using the alpha channel.
Definition color.cpp:281
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:124
std::function< Element(Element)> Decorator
Definition elements.hpp:24
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
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:110