Porytiles
Loading...
Searching...
No Matches
color_index_map.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <optional>
5#include <set>
6#include <vector>
7
13
14namespace porytiles {
15
55template <SupportsTransparency PixelType>
57 public:
58 ColorIndexMap() = default;
59
69 [[nodiscard]] std::size_t size() const
70 {
71 return index_map_.size();
72 }
73
79 [[nodiscard]] bool empty() const
80 {
81 return size() == 0;
82 }
83
97 requires requires(const PixelType &p) { p.is_transparent(); }
98 {
99 add_colors_impl(tile.unique_nontransparent_colors());
100 }
101
115 void add_tile(const PixelTile<PixelType> &tile, const PixelType &extrinsic)
116 requires requires(const PixelType &p) { p.is_transparent(p); }
117 {
118 add_colors_impl(tile.unique_nontransparent_colors(extrinsic));
119 }
120
134 requires requires(const PixelType &p) { p.is_transparent(); }
135 {
136 add_colors_from_pal_impl(pal, [](const PixelType &p) { return p.is_transparent(); });
137 }
138
152 void add_pal(const Palette<PixelType, pal::max_size> &pal, const PixelType &extrinsic)
153 requires requires(const PixelType &p) { p.is_transparent(p); }
154 {
155 add_colors_from_pal_impl(pal, [&extrinsic](const PixelType &p) { return p.is_transparent(extrinsic); });
156 }
157
171 requires requires(const PixelType &p) { p.is_transparent(); }
172 {
173 add_colors_from_pal_impl(pal, [](const PixelType &p) { return p.is_transparent(); });
174 }
175
189 void add_pal(const Palette<PixelType> &pal, const PixelType &extrinsic)
190 requires requires(const PixelType &p) { p.is_transparent(p); }
191 {
192 add_colors_from_pal_impl(pal, [&extrinsic](const PixelType &p) { return p.is_transparent(extrinsic); });
193 }
194
208 requires requires(const PixelType &p) { p.is_transparent(); }
209 {
210 if (anim.has_key_frame()) {
211 for (const auto &tile : anim.key_frame().tiles()) {
212 add_tile(tile);
213 }
214 }
215 for (const auto &[name, frame] : anim.frames()) {
216 for (const auto &tile : frame.tiles()) {
217 add_tile(tile);
218 }
219 }
220 }
221
235 void add_anim(const Animation<PixelType> &anim, const PixelType &extrinsic)
236 requires requires(const PixelType &p) { p.is_transparent(p); }
237 {
238 if (anim.has_key_frame()) {
239 for (const auto &tile : anim.key_frame().tiles()) {
240 add_tile(tile, extrinsic);
241 }
242 }
243 for (const auto &[name, frame] : anim.frames()) {
244 for (const auto &tile : frame.tiles()) {
245 add_tile(tile, extrinsic);
246 }
247 }
248 }
249
263 [[nodiscard]] std::optional<PixelType> color_at_index(ColorIndex index) const
264 {
265 auto it = color_map_.find(index);
266 if (it != color_map_.end()) {
267 return it->second;
268 }
269 return std::nullopt;
270 }
271
285 [[nodiscard]] std::optional<ColorIndex> index_at_color(const PixelType &color) const
286 {
287 auto it = index_map_.find(color);
288 if (it != index_map_.end()) {
289 return it->second;
290 }
291 return std::nullopt;
292 }
293
294 private:
304 void add_colors_impl(const std::set<PixelType> &colors)
305 {
306 auto color_index = size();
307 for (const auto &pixel : colors) {
308 if (index_map_.insert({pixel, ColorIndex{color_index}}).second) {
309 color_map_.insert({ColorIndex{color_index}, pixel});
310 ++color_index;
311 }
312 }
313 }
314
327 template <std::size_t N, typename TransparencyPredicate>
328 void add_colors_from_pal_impl(const Palette<PixelType, N> &pal, TransparencyPredicate is_transparent_pred)
329 {
330 auto color_index = size();
331 for (std::size_t i = 0; i < pal.size(); i++) {
332 auto color_opt = pal.at_optional(i);
333 if (!color_opt.has_value()) {
334 continue; // Skip wildcards
335 }
336 const auto &color = color_opt.value();
337 if (is_transparent_pred(color)) {
338 continue; // Skip transparent colors
339 }
340 if (index_map_.insert({color, ColorIndex{color_index}}).second) {
341 color_map_.insert({ColorIndex{color_index}, color});
342 ++color_index;
343 }
344 }
345 }
346
347 std::map<PixelType, ColorIndex> index_map_;
348 std::map<ColorIndex, PixelType> color_map_;
349};
350
351} // namespace porytiles
A complete tileset animation with name, configuration, and frame data.
Definition animation.hpp:78
A bidirectional mapping between pixel color values and sequential integer indices.
std::size_t size() const
Returns the number of unique colors in the mapping.
void add_pal(const Palette< PixelType > &pal, const PixelType &extrinsic)
Adds colors from a dynamic palette to the mapping using extrinsic transparency.
void add_tile(const PixelTile< PixelType > &tile)
Adds colors from a single tile to the mapping using intrinsic transparency.
void add_pal(const Palette< PixelType, pal::max_size > &pal, const PixelType &extrinsic)
Adds colors from a fixed-size palette to the mapping using extrinsic transparency.
std::optional< ColorIndex > index_at_color(const PixelType &color) const
Retrieves the index associated with a given color.
void add_anim(const Animation< PixelType > &anim, const PixelType &extrinsic)
Adds colors from an animation to the mapping using extrinsic transparency.
void add_tile(const PixelTile< PixelType > &tile, const PixelType &extrinsic)
Adds colors from a single tile to the mapping using extrinsic transparency.
void add_pal(const Palette< PixelType > &pal)
Adds colors from a dynamic palette to the mapping using intrinsic transparency.
bool empty() const
Determines if the mapping is empty.
std::optional< PixelType > color_at_index(ColorIndex index) const
Retrieves the color associated with a given index.
void add_pal(const Palette< PixelType, pal::max_size > &pal)
Adds colors from a fixed-size palette to the mapping using intrinsic transparency.
void add_anim(const Animation< PixelType > &anim)
Adds colors from an animation to the mapping using intrinsic transparency.
Represents a color index value for palette operations.
A generic palette container for colors that support transparency checking.
Definition palette.hpp:47
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.