23template <SupportsTransparency ColorType>
80template <SupportsTransparency ColorType,
typename TransparencyPredicate, std::
size_t N = 0>
89 std::set<ColorType> palette_colors_set;
91 palette_colors_set.insert(color);
95 std::set<ColorType> tile_colors;
97 const auto &pixel = tile.
at(i);
98 if (!is_transparent_pred(pixel)) {
99 tile_colors.insert(pixel);
102 if (!palette_colors_set.contains(pixel)) {
109 for (
const auto &color : tile_colors) {
110 if (palette_colors_set.contains(color)) {
149template <SupportsTransparency ColorType, std::
size_t N = 0>
150[[nodiscard]] PaletteMatchResult<ColorType>
152 requires requires(
const ColorType &c) { c.is_transparent(); }
154 if (palette.size() == 0) {
155 panic(
"palette is empty");
179template <SupportsTransparency ColorType, std::
size_t N = 0>
182 requires requires(
const ColorType &c) { c.is_transparent(c); }
184 if (palette.size() == 0) {
185 panic(
"palette is empty");
188 tile, palette, [&extrinsic](
const ColorType &c) {
return c.is_transparent(extrinsic); });
222template <SupportsTransparency ColorType,
typename PaletteContainer>
224 const PixelTile<ColorType> &tile,
const PaletteContainer &palettes,
const ColorType &extrinsic, std::size_t top_n)
225 requires requires(
const ColorType &c) { c.is_transparent(c); }
227 if (palettes.empty()) {
228 panic(
"palettes container is empty");
231 panic(
"top_n must be greater than 0");
235 std::vector<PaletteMatchResult<ColorType>> complete_matches;
236 std::vector<PaletteMatchResult<ColorType>> incomplete_matches;
238 for (std::size_t i = 0; i < palettes.size(); ++i) {
240 result.pal_index = i;
242 if (result.is_covered) {
243 complete_matches.push_back(result);
246 incomplete_matches.push_back(result);
251 if (!complete_matches.empty()) {
252 return complete_matches;
256 std::sort(incomplete_matches.begin(), incomplete_matches.end(), [](
const auto &a,
const auto &b) {
257 return a.missing_colors.size() < b.missing_colors.size();
261 if (incomplete_matches.size() > top_n) {
262 incomplete_matches.resize(top_n);
265 return incomplete_matches;
A generic palette container for colors that support transparency checking.
std::map< ColorType, PaletteIndex > color_to_index_map() const
Creates a map from colors to their palette indices.
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
PixelType at(std::size_t i) const
PaletteMatchResult< ColorType > match_tile_to_palette_impl(const PixelTile< ColorType > &tile, const Palette< ColorType, N > &palette, TransparencyPredicate is_transparent_pred)
Helper function implementing the core palette matching logic.
constexpr std::size_t size_pix
PaletteMatchResult< ColorType > match_tile_to_palette(const PixelTile< ColorType > &tile, const Palette< ColorType, N > &palette)
Matches a PixelTile against a Palette (intrinsic transparency only).
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
std::vector< PaletteMatchResult< ColorType > > match_or_best(const PixelTile< ColorType > &tile, const PaletteContainer &palettes, const ColorType &extrinsic, std::size_t top_n)
Finds the best palette match(es) for a tile (extrinsic transparency).
Result type for palette matching operations.
std::set< ColorType > missing_colors
The set of non-transparent colors from the tile that are NOT present in the palette.
std::vector< std::size_t > uncovered_pixel_indices
The linear indices of tile pixels whose colors are not covered by the palette.
bool is_covered
True if the palette covers all non-transparent colors in the tile, false otherwise.
std::set< ColorType > covered_colors
The set of non-transparent colors from the tile that ARE present in the palette.
std::size_t pal_index
The palette index of the match, useful in batch operations.