Porytiles
Loading...
Searching...
No Matches
palette.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <string>
5#include <vector>
6
9
10namespace porytiles2 {
11
12namespace pal {
13
14inline constexpr std::size_t max_size = 16;
15
16inline constexpr std::size_t num_pals = 16;
17
18} // namespace pal
19
33template <SupportsTransparency ColorType>
34class Palette {
35 public:
36 Palette() = default;
37
47 explicit Palette(ColorType color)
48 {
49 for (std::size_t i = 0; i < pal::max_size; i++) {
50 add(color);
51 }
52 }
53
62 void add(ColorType color)
63 {
64 colors_.push_back(color);
65 }
66
76 void set(ColorType color, std::size_t index)
77 {
78 if (index >= size()) {
79 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
80 }
81 colors_.at(index) = color;
82 }
83
89 [[nodiscard]] std::size_t size() const
90 {
91 return colors_.size();
92 }
93
99 [[nodiscard]] const std::vector<ColorType> &colors() const
100 {
101 return colors_;
102 }
103
104 private:
105 std::vector<ColorType> colors_;
106};
107
108} // namespace porytiles2
A palette container for colors that support transparency checking.
Definition palette.hpp:34
const std::vector< ColorType > & colors() const
Returns a const reference to the internal color vector.
Definition palette.hpp:99
std::size_t size() const
Returns the number of colors in the palette.
Definition palette.hpp:89
void set(ColorType color, std::size_t index)
Sets the color at a specific index in the palette.
Definition palette.hpp:76
void add(ColorType color)
Adds a color to the end of the palette.
Definition palette.hpp:62
Palette(ColorType color)
Constructs a palette filled with a single color.
Definition palette.hpp:47
constexpr std::size_t max_size
Definition palette.hpp:14
constexpr std::size_t num_pals
Definition palette.hpp:16
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53