Porytiles
Loading...
Searching...
No Matches
palette.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <map>
6#include <optional>
7#include <string>
8#include <type_traits>
9#include <vector>
10
14
15namespace porytiles {
16
17namespace palette {
18
19inline constexpr std::size_t max_size = 16;
20
21inline constexpr std::size_t num_palettes = 16;
22
23} // namespace palette
24
44template <SupportsTransparency ColorType, std::size_t N = 0>
45class Palette {
46 public:
53 std::conditional_t<N == 0, std::vector<std::optional<ColorType>>, std::array<std::optional<ColorType>, N>>;
54
60 Palette() = default;
61
68 explicit Palette(std::vector<ColorType> colors)
69 requires(N == 0)
70 {
71 for (auto &color : colors) {
72 colors_.push_back(std::move(color));
73 }
74 }
75
82 explicit Palette(std::array<ColorType, N> colors)
83 requires(N > 0)
84 {
85 for (std::size_t i = 0; i < N; i++) {
86 colors_[i] = std::move(colors[i]);
87 }
88 }
89
97 explicit Palette(ColorType color)
98 {
99 if constexpr (N == 0) {
100 for (std::size_t i = 0; i < palette::max_size; i++) {
101 colors_.push_back(color);
102 }
103 }
104 else {
105 for (std::size_t i = 0; i < N; i++) {
106 colors_[i] = color;
107 }
108 }
109 }
110
118 void add(ColorType color)
119 {
120 static_assert(N == 0, "add() is not available for fixed-size Palette");
121 colors_.push_back(color);
122 }
123
130 {
131 static_assert(N == 0, "add_wildcard() is not available for fixed-size Palette");
132 colors_.push_back(std::nullopt);
133 }
134
144 void set(std::size_t index, ColorType color)
145 {
146 if (index >= size()) {
147 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
148 }
149 colors_.at(index) = color;
150 }
151
159 void set_wildcard(std::size_t index)
160 {
161 if (index >= size()) {
162 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
163 }
164 colors_.at(index) = std::nullopt;
165 }
166
176 [[nodiscard]] bool is_wildcard(std::size_t index) const
177 {
178 if (index >= size()) {
179 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
180 }
181 return !colors_.at(index).has_value();
182 }
183
187 [[nodiscard]] bool has_any_wildcards() const
188 {
189 for (std::size_t i = 0; i < size(); i++) {
190 if (!colors_.at(i).has_value()) {
191 return true;
192 }
193 }
194 return false;
195 }
196
203 [[nodiscard]] std::size_t size() const
204 {
205 if constexpr (N == 0) {
206 return colors_.size();
207 }
208 else {
209 return N;
210 }
211 }
212
226 [[nodiscard]] ColorType slot_zero_color() const
227 {
228 if (size() == 0) {
229 panic("palette had zero size");
230 }
231 if (!colors_.at(0).has_value()) {
232 panic("slot 0 is a wildcard");
233 }
234 return colors_.at(0).value();
235 }
236
246 [[nodiscard]] ColorType at(std::size_t index) const
247 {
248 if (index >= size()) {
249 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
250 }
251 if (!colors_.at(index).has_value()) {
252 panic("slot " + std::to_string(index) + " is a wildcard");
253 }
254 return colors_.at(index).value();
255 }
256
266 [[nodiscard]] std::optional<ColorType> at_optional(std::size_t index) const
267 {
268 if (index >= size()) {
269 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
270 }
271 return colors_.at(index);
272 }
273
291 [[nodiscard]] std::map<ColorType, PaletteIndex> color_to_index_map() const
292 {
293 std::map<ColorType, PaletteIndex> result{};
294 for (std::size_t i = 1; i < size(); i++) {
295 if (colors_.at(i).has_value()) {
296 result.emplace(colors_.at(i).value(), PaletteIndex{i});
297 }
298 }
299 return result;
300 }
301
310 [[nodiscard]] std::map<PaletteIndex, ColorType> index_to_color_map() const
311 {
312 std::map<PaletteIndex, ColorType> result{};
313 for (std::size_t i = 1; i < size(); i++) {
314 if (colors_.at(i).has_value()) {
315 result.emplace(PaletteIndex{i}, colors_.at(i).value());
316 }
317 }
318 return result;
319 }
320
321 private:
322 StorageType colors_{};
323};
324
325} // namespace porytiles
Represents a palette index value within a particular palette.
A generic palette container for colors that support transparency checking.
Definition palette.hpp:45
ColorType slot_zero_color() const
Get the slot 0 palette color.
Definition palette.hpp:226
void add_wildcard()
Adds a wildcard slot to the end of the palette.
Definition palette.hpp:129
std::optional< ColorType > at_optional(std::size_t index) const
Gets the optional color at a specific index.
Definition palette.hpp:266
std::map< PaletteIndex, ColorType > index_to_color_map() const
Creates a map from palette indices to their colors.
Definition palette.hpp:310
std::size_t size() const
Returns the number of slots in the palette.
Definition palette.hpp:203
Palette(ColorType color)
Constructs a Palette filled with a single color.
Definition palette.hpp:97
Palette(std::vector< ColorType > colors)
Construct this palette with a given color vector.
Definition palette.hpp:68
void add(ColorType color)
Adds a color to the end of the palette.
Definition palette.hpp:118
std::map< ColorType, PaletteIndex > color_to_index_map() const
Creates a map from colors to their palette indices.
Definition palette.hpp:291
Palette(std::array< ColorType, N > colors)
Construct this palette from an array of colors.
Definition palette.hpp:82
void set_wildcard(std::size_t index)
Sets a slot as a wildcard at a specific index.
Definition palette.hpp:159
std::conditional_t< N==0, std::vector< std::optional< ColorType > >, std::array< std::optional< ColorType >, N > > StorageType
The storage type used internally, selected based on N.
Definition palette.hpp:53
ColorType at(std::size_t index) const
Gets the color at a specific index.
Definition palette.hpp:246
void set(std::size_t index, ColorType color)
Sets the color at a specific index in the palette.
Definition palette.hpp:144
Palette()=default
Default constructs a Palette.
bool is_wildcard(std::size_t index) const
Checks if a slot is a wildcard.
Definition palette.hpp:176
bool has_any_wildcards() const
Checks if the palette contains any wildcard slots.
Definition palette.hpp:187
constexpr std::size_t max_size
Definition palette.hpp:19
constexpr std::size_t num_palettes
Definition palette.hpp:21
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43