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 pal {
18
19inline constexpr std::size_t max_size = 16;
20
21inline constexpr std::size_t num_pals = 16;
22
23} // namespace pal
24
46template <SupportsTransparency ColorType, std::size_t N = 0>
47class Palette {
48 public:
57 std::conditional_t<N == 0, std::vector<std::optional<ColorType>>, std::array<std::optional<ColorType>, N>>;
58
66 Palette() = default;
67
76 explicit Palette(std::vector<ColorType> colors)
77 requires(N == 0)
78 {
79 for (auto &color : colors) {
80 colors_.push_back(std::move(color));
81 }
82 }
83
92 explicit Palette(std::array<ColorType, N> colors)
93 requires(N > 0)
94 {
95 for (std::size_t i = 0; i < N; i++) {
96 colors_[i] = std::move(colors[i]);
97 }
98 }
99
109 explicit Palette(ColorType color)
110 {
111 if constexpr (N == 0) {
112 for (std::size_t i = 0; i < pal::max_size; i++) {
113 colors_.push_back(color);
114 }
115 }
116 else {
117 for (std::size_t i = 0; i < N; i++) {
118 colors_[i] = color;
119 }
120 }
121 }
122
132 void add(ColorType color)
133 {
134 static_assert(N == 0, "add() is not available for fixed-size Palette");
135 colors_.push_back(color);
136 }
137
146 {
147 static_assert(N == 0, "add_wildcard() is not available for fixed-size Palette");
148 colors_.push_back(std::nullopt);
149 }
150
162 void set(std::size_t index, ColorType color)
163 {
164 if (index >= size()) {
165 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
166 }
167 colors_.at(index) = color;
168 }
169
179 void set_wildcard(std::size_t index)
180 {
181 if (index >= size()) {
182 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
183 }
184 colors_.at(index) = std::nullopt;
185 }
186
198 [[nodiscard]] bool is_wildcard(std::size_t index) const
199 {
200 if (index >= size()) {
201 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
202 }
203 return !colors_.at(index).has_value();
204 }
205
211 [[nodiscard]] bool has_any_wildcards() const
212 {
213 for (std::size_t i = 0; i < size(); i++) {
214 if (!colors_.at(i).has_value()) {
215 return true;
216 }
217 }
218 return false;
219 }
220
229 [[nodiscard]] std::size_t size() const
230 {
231 if constexpr (N == 0) {
232 return colors_.size();
233 }
234 else {
235 return N;
236 }
237 }
238
254 [[nodiscard]] ColorType slot_zero_color() const
255 {
256 if (size() == 0) {
257 panic("palette had zero size");
258 }
259 if (!colors_.at(0).has_value()) {
260 panic("slot 0 is a wildcard");
261 }
262 return colors_.at(0).value();
263 }
264
276 [[nodiscard]] ColorType at(std::size_t index) const
277 {
278 if (index >= size()) {
279 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
280 }
281 if (!colors_.at(index).has_value()) {
282 panic("slot " + std::to_string(index) + " is a wildcard");
283 }
284 return colors_.at(index).value();
285 }
286
298 [[nodiscard]] std::optional<ColorType> at_optional(std::size_t index) const
299 {
300 if (index >= size()) {
301 panic("index " + std::to_string(index) + " >= size " + std::to_string(size()));
302 }
303 return colors_.at(index);
304 }
305
325 [[nodiscard]] std::map<ColorType, PaletteIndex> color_to_index_map() const
326 {
327 std::map<ColorType, PaletteIndex> result{};
328 for (std::size_t i = 1; i < size(); i++) {
329 if (colors_.at(i).has_value()) {
330 result.emplace(colors_.at(i).value(), PaletteIndex{i});
331 }
332 }
333 return result;
334 }
335
346 [[nodiscard]] std::map<PaletteIndex, ColorType> index_to_color_map() const
347 {
348 std::map<PaletteIndex, ColorType> result{};
349 for (std::size_t i = 1; i < size(); i++) {
350 if (colors_.at(i).has_value()) {
351 result.emplace(PaletteIndex{i}, colors_.at(i).value());
352 }
353 }
354 return result;
355 }
356
357 private:
358 StorageType colors_{};
359};
360
361} // namespace porytiles
Represents a palette index value within a particular palette.
A generic palette container for colors that support transparency checking.
Definition palette.hpp:47
ColorType slot_zero_color() const
Get the slot 0 palette color.
Definition palette.hpp:254
void add_wildcard()
Adds a wildcard slot to the end of the palette.
Definition palette.hpp:145
std::optional< ColorType > at_optional(std::size_t index) const
Gets the optional color at a specific index.
Definition palette.hpp:298
std::map< PaletteIndex, ColorType > index_to_color_map() const
Creates a map from palette indices to their colors.
Definition palette.hpp:346
std::size_t size() const
Returns the number of slots in the palette.
Definition palette.hpp:229
Palette(ColorType color)
Constructs a Palette filled with a single color.
Definition palette.hpp:109
Palette(std::vector< ColorType > colors)
Construct this palette with a given color vector.
Definition palette.hpp:76
void add(ColorType color)
Adds a color to the end of the palette.
Definition palette.hpp:132
std::map< ColorType, PaletteIndex > color_to_index_map() const
Creates a map from colors to their palette indices.
Definition palette.hpp:325
Palette(std::array< ColorType, N > colors)
Construct this palette from an array of colors.
Definition palette.hpp:92
void set_wildcard(std::size_t index)
Sets a slot as a wildcard at a specific index.
Definition palette.hpp:179
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:57
ColorType at(std::size_t index) const
Gets the color at a specific index.
Definition palette.hpp:276
void set(std::size_t index, ColorType color)
Sets the color at a specific index in the palette.
Definition palette.hpp:162
Palette()=default
Default constructs a Palette.
bool is_wildcard(std::size_t index) const
Checks if a slot is a wildcard.
Definition palette.hpp:198
bool has_any_wildcards() const
Checks if the palette contains any wildcard slots.
Definition palette.hpp:211
constexpr std::size_t num_pals
Definition palette.hpp:21
constexpr std::size_t max_size
Definition palette.hpp:19
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43