46template <SupportsTransparency ColorType, std::
size_t N = 0>
57 std::conditional_t<N == 0, std::vector<std::optional<ColorType>>, std::array<std::optional<ColorType>, N>>;
76 explicit Palette(std::vector<ColorType> colors)
79 for (
auto &color : colors) {
80 colors_.push_back(std::move(color));
92 explicit Palette(std::array<ColorType, N> colors)
95 for (std::size_t i = 0; i < N; i++) {
96 colors_[i] = std::move(colors[i]);
111 if constexpr (N == 0) {
113 colors_.push_back(color);
117 for (std::size_t i = 0; i < N; i++) {
134 static_assert(N == 0,
"add() is not available for fixed-size Palette");
135 colors_.push_back(color);
147 static_assert(N == 0,
"add_wildcard() is not available for fixed-size Palette");
148 colors_.push_back(std::nullopt);
162 void set(std::size_t index, ColorType color)
164 if (index >=
size()) {
165 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
167 colors_.at(index) = color;
181 if (index >=
size()) {
182 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
184 colors_.at(index) = std::nullopt;
200 if (index >=
size()) {
201 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
203 return !colors_.at(index).has_value();
213 for (std::size_t i = 0; i <
size(); i++) {
214 if (!colors_.at(i).has_value()) {
229 [[nodiscard]] std::size_t
size()
const
231 if constexpr (N == 0) {
232 return colors_.size();
257 panic(
"palette had zero size");
259 if (!colors_.at(0).has_value()) {
260 panic(
"slot 0 is a wildcard");
262 return colors_.at(0).value();
276 [[nodiscard]] ColorType
at(std::size_t index)
const
278 if (index >=
size()) {
279 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
281 if (!colors_.at(index).has_value()) {
282 panic(
"slot " + std::to_string(index) +
" is a wildcard");
284 return colors_.at(index).value();
298 [[nodiscard]] std::optional<ColorType>
at_optional(std::size_t index)
const
300 if (index >=
size()) {
301 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
303 return colors_.at(index);
327 std::map<ColorType, PaletteIndex> result{};
328 for (std::size_t i = 1; i <
size(); i++) {
329 if (colors_.at(i).has_value()) {
348 std::map<PaletteIndex, ColorType> result{};
349 for (std::size_t i = 1; i <
size(); i++) {
350 if (colors_.at(i).has_value()) {
Represents a palette index value within a particular palette.
A generic palette container for colors that support transparency checking.
ColorType slot_zero_color() const
Get the slot 0 palette color.
void add_wildcard()
Adds a wildcard slot to the end of the palette.
std::optional< ColorType > at_optional(std::size_t index) const
Gets the optional color at a specific index.
std::map< PaletteIndex, ColorType > index_to_color_map() const
Creates a map from palette indices to their colors.
std::size_t size() const
Returns the number of slots in the palette.
Palette(ColorType color)
Constructs a Palette filled with a single color.
Palette(std::vector< ColorType > colors)
Construct this palette with a given color vector.
void add(ColorType color)
Adds a color to the end of the palette.
std::map< ColorType, PaletteIndex > color_to_index_map() const
Creates a map from colors to their palette indices.
Palette(std::array< ColorType, N > colors)
Construct this palette from an array of colors.
void set_wildcard(std::size_t index)
Sets a slot as a wildcard at a specific index.
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.
ColorType at(std::size_t index) const
Gets the color at a specific index.
void set(std::size_t index, ColorType color)
Sets the color at a specific index in the palette.
Palette()=default
Default constructs a Palette.
bool is_wildcard(std::size_t index) const
Checks if a slot is a wildcard.
bool has_any_wildcards() const
Checks if the palette contains any wildcard slots.
constexpr std::size_t num_pals
constexpr std::size_t max_size
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.