44template <SupportsTransparency ColorType, std::
size_t N = 0>
53 std::conditional_t<N == 0, std::vector<std::optional<ColorType>>, std::array<std::optional<ColorType>, N>>;
68 explicit Palette(std::vector<ColorType> colors)
71 for (
auto &color : colors) {
72 colors_.push_back(std::move(color));
82 explicit Palette(std::array<ColorType, N> colors)
85 for (std::size_t i = 0; i < N; i++) {
86 colors_[i] = std::move(colors[i]);
99 if constexpr (N == 0) {
101 colors_.push_back(color);
105 for (std::size_t i = 0; i < N; i++) {
120 static_assert(N == 0,
"add() is not available for fixed-size Palette");
121 colors_.push_back(color);
131 static_assert(N == 0,
"add_wildcard() is not available for fixed-size Palette");
132 colors_.push_back(std::nullopt);
144 void set(std::size_t index, ColorType color)
146 if (index >=
size()) {
147 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
149 colors_.at(index) = color;
161 if (index >=
size()) {
162 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
164 colors_.at(index) = std::nullopt;
178 if (index >=
size()) {
179 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
181 return !colors_.at(index).has_value();
189 for (std::size_t i = 0; i <
size(); i++) {
190 if (!colors_.at(i).has_value()) {
203 [[nodiscard]] std::size_t
size()
const
205 if constexpr (N == 0) {
206 return colors_.size();
229 panic(
"palette had zero size");
231 if (!colors_.at(0).has_value()) {
232 panic(
"slot 0 is a wildcard");
234 return colors_.at(0).value();
246 [[nodiscard]] ColorType
at(std::size_t index)
const
248 if (index >=
size()) {
249 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
251 if (!colors_.at(index).has_value()) {
252 panic(
"slot " + std::to_string(index) +
" is a wildcard");
254 return colors_.at(index).value();
266 [[nodiscard]] std::optional<ColorType>
at_optional(std::size_t index)
const
268 if (index >=
size()) {
269 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(
size()));
271 return colors_.at(index);
293 std::map<ColorType, PaletteIndex> result{};
294 for (std::size_t i = 1; i <
size(); i++) {
295 if (colors_.at(i).has_value()) {
312 std::map<PaletteIndex, ColorType> result{};
313 for (std::size_t i = 1; i <
size(); i++) {
314 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 max_size
constexpr std::size_t num_palettes
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.