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
53template <SupportsTransparency PixelType>
55 public:
56 ColorIndexMap() = default;
57
65 [[nodiscard]] std::size_t size() const
66 {
67 return index_map_.size();
68 }
69
73 [[nodiscard]] bool empty() const
74 {
75 return size() == 0;
76 }
77
89 requires requires(const PixelType &p) { p.is_transparent(); }
90 {
91 add_colors_impl(tile.unique_nontransparent_colors());
92 }
93
105 void add_tile(const PixelTile<PixelType> &tile, const PixelType &extrinsic)
106 requires requires(const PixelType &p) { p.is_transparent(p); }
107 {
108 add_colors_impl(tile.unique_nontransparent_colors(extrinsic));
109 }
110
122 requires requires(const PixelType &p) { p.is_transparent(); }
123 {
124 add_colors_from_palette_impl(palette, [](const PixelType &p) { return p.is_transparent(); });
125 }
126
138 void add_palette(const Palette<PixelType, palette::max_size> &palette, const PixelType &extrinsic)
139 requires requires(const PixelType &p) { p.is_transparent(p); }
140 {
141 add_colors_from_palette_impl(palette, [&extrinsic](const PixelType &p) { return p.is_transparent(extrinsic); });
142 }
143
154 void add_palette(const Palette<PixelType> &palette)
155 requires requires(const PixelType &p) { p.is_transparent(); }
156 {
157 add_colors_from_palette_impl(palette, [](const PixelType &p) { return p.is_transparent(); });
158 }
159
171 void add_palette(const Palette<PixelType> &palette, const PixelType &extrinsic)
172 requires requires(const PixelType &p) { p.is_transparent(p); }
173 {
174 add_colors_from_palette_impl(palette, [&extrinsic](const PixelType &p) { return p.is_transparent(extrinsic); });
175 }
176
188 requires requires(const PixelType &p) { p.is_transparent(); }
189 {
190 if (anim.has_key_frame()) {
191 for (const auto &tile : anim.key_frame().tiles()) {
192 add_tile(tile);
193 }
194 }
195 for (const auto &[name, frame] : anim.frames()) {
196 for (const auto &tile : frame.tiles()) {
197 add_tile(tile);
198 }
199 }
200 }
201
213 void add_anim(const Animation<PixelType> &anim, const PixelType &extrinsic)
214 requires requires(const PixelType &p) { p.is_transparent(p); }
215 {
216 if (anim.has_key_frame()) {
217 for (const auto &tile : anim.key_frame().tiles()) {
218 add_tile(tile, extrinsic);
219 }
220 }
221 for (const auto &[name, frame] : anim.frames()) {
222 for (const auto &tile : frame.tiles()) {
223 add_tile(tile, extrinsic);
224 }
225 }
226 }
227
239 [[nodiscard]] std::optional<PixelType> color_at_index(ColorIndex index) const
240 {
241 auto it = color_map_.find(index);
242 if (it != color_map_.end()) {
243 return it->second;
244 }
245 return std::nullopt;
246 }
247
259 [[nodiscard]] std::optional<ColorIndex> index_at_color(const PixelType &color) const
260 {
261 auto it = index_map_.find(color);
262 if (it != index_map_.end()) {
263 return it->second;
264 }
265 return std::nullopt;
266 }
267
268 private:
276 void add_colors_impl(const std::set<PixelType> &colors)
277 {
278 auto color_index = size();
279 for (const auto &pixel : colors) {
280 if (index_map_.insert({pixel, ColorIndex{color_index}}).second) {
281 color_map_.insert({ColorIndex{color_index}, pixel});
282 ++color_index;
283 }
284 }
285 }
286
297 template <std::size_t N, typename TransparencyPredicate>
298 void add_colors_from_palette_impl(const Palette<PixelType, N> &palette, TransparencyPredicate is_transparent_pred)
299 {
300 auto color_index = size();
301 for (std::size_t i = 0; i < palette.size(); i++) {
302 auto color_opt = palette.at_optional(i);
303 if (!color_opt.has_value()) {
304 continue; // Skip wildcards
305 }
306 const auto &color = color_opt.value();
307 if (is_transparent_pred(color)) {
308 continue; // Skip transparent colors
309 }
310 if (index_map_.insert({color, ColorIndex{color_index}}).second) {
311 color_map_.insert({ColorIndex{color_index}, color});
312 ++color_index;
313 }
314 }
315 }
316
317 std::map<PixelType, ColorIndex> index_map_;
318 std::map<ColorIndex, PixelType> color_map_;
319};
320
321} // namespace porytiles
A complete tileset animation with name, configuration, and frame data.
Definition animation.hpp:89
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_palette(const Palette< PixelType, palette::max_size > &palette, const PixelType &extrinsic)
Adds colors from a fixed-size 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.
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_palette(const Palette< PixelType > &palette, const PixelType &extrinsic)
Adds colors from a dynamic palette to the mapping using extrinsic 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_palette(const Palette< PixelType, palette::max_size > &palette)
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.
void add_palette(const Palette< PixelType > &palette)
Adds colors from a dynamic palette 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:45
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
std::string name