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 <vector>
6
10
11namespace porytiles2 {
12
57template <SupportsTransparency PixelType>
59 public:
60 ColorIndexMap() = default;
61
81 ColorIndexMap(const std::vector<PixelTile<PixelType>> &tiles)
82 requires requires(const PixelType &p) { p.is_transparent(); }
83 {
84 constructor_impl(tiles, [](const PixelTile<PixelType> &tile) { return tile.unique_nontransparent_colors(); });
85 }
86
108 ColorIndexMap(const std::vector<PixelTile<PixelType>> &tiles, const PixelType &extrinsic)
109 requires requires(const PixelType &p) { p.is_transparent(p); }
110 {
111 constructor_impl(tiles, [&extrinsic](const PixelTile<PixelType> &tile) {
112 return tile.unique_nontransparent_colors(extrinsic);
113 });
114 }
115
125 [[nodiscard]] std::size_t size() const
126 {
127 return index_map_.size();
128 }
129
135 [[nodiscard]] bool empty() const
136 {
137 return size() == 0;
138 }
139
153 [[nodiscard]] std::optional<PixelType> color_at_index(ColorIndex index) const
154 {
155 auto it = color_map_.find(index);
156 if (it != color_map_.end()) {
157 return it->second;
158 }
159 return std::nullopt;
160 }
161
175 [[nodiscard]] std::optional<ColorIndex> index_at_color(const PixelType &color) const
176 {
177 auto it = index_map_.find(color);
178 if (it != index_map_.end()) {
179 return it->second;
180 }
181 return std::nullopt;
182 }
183
184 private:
197 template <typename ColorExtractor>
198 void constructor_impl(const std::vector<PixelTile<PixelType>> &tiles, ColorExtractor extract_colors)
199 {
200 std::map<PixelType, ColorIndex> pixel_indexes{};
201 std::map<ColorIndex, PixelType> index_to_color{};
202
203 unsigned int color_index = 0;
204 for (const auto &tile : tiles) {
205 for (const auto &pixel : extract_colors(tile)) {
206 if (pixel_indexes.insert({pixel, ColorIndex{color_index}}).second) {
207 index_to_color.insert({ColorIndex{color_index}, pixel});
208 color_index++;
209 }
210 }
211 }
212
213 index_map_ = pixel_indexes;
214 color_map_ = index_to_color;
215 }
216
217 std::map<PixelType, ColorIndex> index_map_;
218 std::map<ColorIndex, PixelType> color_map_;
219};
220
221} // namespace porytiles2
A bidirectional mapping between pixel color values and sequential integer indices.
std::optional< PixelType > color_at_index(ColorIndex index) const
Retrieves the color associated with a given index.
std::optional< ColorIndex > index_at_color(const PixelType &color) const
Retrieves the index associated with a given color.
std::size_t size() const
Returns the number of unique colors in the mapping.
ColorIndexMap(const std::vector< PixelTile< PixelType > > &tiles, const PixelType &extrinsic)
Constructs a ColorIndexMap from a collection of tiles and an extrinsic transparency value.
ColorIndexMap(const std::vector< PixelTile< PixelType > > &tiles)
Constructs a ColorIndexMap from a collection of tiles using intrinsic transparency.
bool empty() const
Determines if the mapping is empty.
Represents a color index value for palette operations.
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
std::set< PixelType > unique_nontransparent_colors() const
Returns the set of unique non-transparent colors present in this PixelTile (intrinsic transparency on...