FTXUI  5.0.0
C++ functional terminal UI.
image.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 <sstream> // IWYU pragma: keep
5 
6 #include "ftxui/screen/image.hpp"
7 
8 namespace ftxui {
9 
10 namespace {
11 Pixel& dev_null_pixel() {
12  static Pixel pixel;
13  return pixel;
14 }
15 } // namespace
16 
17 Image::Image(int dimx, int dimy)
18  : stencil{0, dimx - 1, 0, dimy - 1},
19  dimx_(dimx),
20  dimy_(dimy),
21  pixels_(dimy, std::vector<Pixel>(dimx)) {}
22 
23 /// @brief Access a character in a cell at a given position.
24 /// @param x The cell position along the x-axis.
25 /// @param y The cell position along the y-axis.
26 std::string& Image::at(int x, int y) {
27  return PixelAt(x, y).character;
28 }
29 
30 /// @brief Access a character in a cell at a given position.
31 /// @param x The cell position along the x-axis.
32 /// @param y The cell position along the y-axis.
33 const std::string& Image::at(int x, int y) const {
34  return PixelAt(x, y).character;
35 }
36 
37 /// @brief Access a cell (Pixel) at a given position.
38 /// @param x The cell position along the x-axis.
39 /// @param y The cell position along the y-axis.
40 Pixel& Image::PixelAt(int x, int y) {
41  return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
42 }
43 
44 /// @brief Access a cell (Pixel) at a given position.
45 /// @param x The cell position along the x-axis.
46 /// @param y The cell position along the y-axis.
47 const Pixel& Image::PixelAt(int x, int y) const {
48  return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
49 }
50 
51 /// @brief Clear all the pixel from the screen.
52 void Image::Clear() {
53  for (auto& line : pixels_) {
54  for (auto& cell : line) {
55  cell = Pixel();
56  }
57  }
58 }
59 
60 } // namespace ftxui
Pixel & PixelAt(int x, int y)
Access a cell (Pixel) at a given position.
Definition: image.cpp:40
std::string & at(int x, int y)
Access a character in a cell at a given position.
Definition: image.cpp:26
Image()=delete
Box stencil
Definition: image.hpp:38
void Clear()
Clear all the pixel from the screen.
Definition: image.cpp:52
std::vector< std::vector< Pixel > > pixels_
Definition: image.hpp:43
bool Contain(int x, int y) const
Definition: box.cpp:35
A Unicode character and its associated style.
Definition: pixel.hpp:13
std::string character
Definition: pixel.hpp:41