TermOx
brush.hpp
1 #ifndef TERMOX_PAINTER_BRUSH_HPP
2 #define TERMOX_PAINTER_BRUSH_HPP
3 #include <tuple>
4 #include <utility>
5 
6 #include <termox/painter/color.hpp>
7 #include <termox/painter/trait.hpp>
8 
9 namespace ox {
10 
12 
13 class Brush {
14  public:
15  Color background = Color::Background;
16  Color foreground = Color::Foreground;
17  Traits traits = Trait::None;
18 
19  public:
21  template <typename... Items>
22  explicit constexpr Brush(Items... items)
23  {
24  this->add_items(items...);
25  }
26 
27  private:
29  template <typename Head, typename... Tail>
30  void constexpr add_items(Head t, Tail... ts)
31  {
32  this->set_item(t);
33  this->add_items(ts...);
34  }
35  void constexpr add_items() {}
36 
38  void constexpr set_item(Background_color bg)
39  {
40  background = Color{bg.value};
41  }
42 
44  void constexpr set_item(Foreground_color fg)
45  {
46  foreground = Color{fg.value};
47  }
48 
50  void constexpr set_item(Trait t) { traits |= t; }
51 
53  void constexpr set_item(Traits t) { traits |= t; }
54 };
55 
56 // Pipes -----------------------------------------------------------------------
57 
58 auto constexpr operator|(Brush& b, Traits ts) -> Brush&
59 {
60  b.traits |= ts;
61  return b;
62 }
63 
64 [[nodiscard]] auto constexpr operator|(Brush const& b, Traits ts) -> Brush
65 {
66  auto copy = b;
67  copy.traits |= ts;
68  return copy;
69 }
70 
71 [[nodiscard]] auto constexpr operator|(Brush&& b, Traits ts) -> Brush
72 {
73  b.traits |= ts;
74  return std::move(b);
75 }
76 
77 auto constexpr operator|(Brush& b, Background_color c) -> Brush&
78 {
79  b.background = Color{c.value};
80  return b;
81 }
82 
83 [[nodiscard]] auto constexpr operator|(Brush const& b, Background_color c)
84  -> Brush
85 {
86  auto copy = b;
87  copy.background = Color{c.value};
88  return copy;
89 }
90 
91 [[nodiscard]] auto constexpr operator|(Brush&& b, Background_color c) -> Brush
92 {
93  b.background = Color{c.value};
94  return std::move(b);
95 }
96 
97 auto constexpr operator|(Brush& b, Foreground_color c) -> Brush&
98 {
99  b.foreground = Color{c.value};
100  return b;
101 }
102 
103 [[nodiscard]] auto constexpr operator|(Brush const& b, Foreground_color c)
104  -> Brush
105 {
106  auto copy = b;
107  copy.foreground = Color{c.value};
108  return copy;
109 }
110 
111 [[nodiscard]] auto constexpr operator|(Brush&& b, Foreground_color c) -> Brush
112 {
113  b.foreground = Color{c.value};
114  return std::move(b);
115 }
116 
117 // -----------------------------------------------------------------------------
118 
120 [[nodiscard]] auto constexpr operator==(Brush a, Brush b) -> bool
121 {
122  return std::tie(a.traits, a.background, a.foreground) ==
123  std::tie(b.traits, b.background, b.foreground);
124 }
125 
127 [[nodiscard]] auto constexpr merge(Brush primary, Brush secondary) -> Brush
128 {
129  if (primary.background == Color::Background)
130  primary.background = secondary.background;
131  if (primary.foreground == Color::Foreground)
132  primary.foreground = secondary.foreground;
133  primary.traits |= secondary.traits;
134  return primary;
135 }
136 
137 } // namespace ox
138 #endif // TERMOX_PAINTER_BRUSH_HPP
Holds the look of any paintable object with Traits and Colors.
Definition: brush.hpp:13
constexpr Brush(Items... items)
Construct a Brush with given Traits and Colors.
Definition: brush.hpp:22
Color numbers [0 - 180] are valid.
Definition: color.hpp:16