Porytiles
Loading...
Searching...
No Matches
porytiles::ColorIndexMap< PixelType > Class Template Reference

A bidirectional mapping between pixel color values and sequential integer indices. More...

#include <color_index_map.hpp>

Public Member Functions

 ColorIndexMap ()=default
 
std::size_t size () const
 Returns the number of unique colors in the mapping.
 
bool empty () const
 Determines if the mapping is empty.
 
void add_tile (const PixelTile< PixelType > &tile)
 Adds colors from a single tile to the mapping using intrinsic 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::max_size > &pal)
 Adds colors from a fixed-size palette 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.
 
void add_pal (const Palette< PixelType > &pal)
 Adds colors from a dynamic palette to the mapping using intrinsic transparency.
 
void add_pal (const Palette< PixelType > &pal, const PixelType &extrinsic)
 Adds colors from a dynamic palette to the mapping using extrinsic transparency.
 
void add_anim (const Animation< PixelType > &anim)
 Adds colors from an animation to the mapping using intrinsic transparency.
 
void add_anim (const Animation< PixelType > &anim, const PixelType &extrinsic)
 Adds colors from an animation to the mapping using extrinsic transparency.
 
std::optional< PixelType > color_at_index (ColorIndex index) const
 Retrieves the color associated with a given index.
 
std::optional< ColorIndexindex_at_color (const PixelType &color) const
 Retrieves the index associated with a given color.
 

Detailed Description

template<SupportsTransparency PixelType>
class porytiles::ColorIndexMap< PixelType >

A bidirectional mapping between pixel color values and sequential integer indices.

ColorIndexMap provides a true bidirectional association between pixel color values and sequential integer indices, starting from 0. The class maintains two internal maps:

  • Color-to-index mapping: look up an index given a color
  • Index-to-color mapping: look up a color given an index

This class is used for palette operations where each unique non-transparent color in a collection of tiles needs to be assigned a unique integer identifier, with the ability to perform lookups in both directions.

The mapping is constructed by examining all unique non-transparent colors across a collection of tiles, filtering out both intrinsically transparent pixels (alpha=0) and extrinsically transparent pixels (e.g., magenta for Rgba32). Each unique color that passes the transparency filter is assigned the next available sequential index.

Template Parameters
PixelTypeThe pixel color type, must satisfy SupportsTransparency concept

Example usage:

std::vector<PixelTile<Rgba32>> tiles = {...};
for (const auto &tile : tiles) {
color_map.add_tile(tile, rgba_magenta);
}
// Forward lookup: color -> index
auto index_opt = color_map.index_at_color(Rgba32{255, 0, 0});
if (index_opt) {
// index_opt contains the ColorIndex for red
}
// Reverse lookup: index -> color
auto color_opt = color_map.color_at_index(ColorIndex{0});
if (color_opt) {
// color_opt contains the color at ColorIndex 0
}
A bidirectional mapping between pixel color values and sequential integer indices.
void add_tile(const PixelTile< PixelType > &tile)
Adds colors from a single tile to the mapping using intrinsic transparency.
constexpr Rgba32 rgba_magenta
Definition rgba32.hpp:138

Definition at line 56 of file color_index_map.hpp.

Constructor & Destructor Documentation

◆ ColorIndexMap()

template<SupportsTransparency PixelType>
porytiles::ColorIndexMap< PixelType >::ColorIndexMap ( )
default

Member Function Documentation

◆ add_anim() [1/2]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_anim ( const Animation< PixelType > &  anim)
inline

Adds colors from an animation to the mapping using intrinsic transparency.

Iterates through all tiles in the animation's key frame (if present) and all regular frames, adding any new colors to the mapping. Colors that already exist in the mapping are skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support intrinsic transparency (e.g., IndexPixel).

Parameters
animThe animation to extract colors from

Definition at line 207 of file color_index_map.hpp.

◆ add_anim() [2/2]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_anim ( const Animation< PixelType > &  anim,
const PixelType &  extrinsic 
)
inline

Adds colors from an animation to the mapping using extrinsic transparency.

Iterates through all tiles in the animation's key frame (if present) and all regular frames, adding any new colors to the mapping. Colors that already exist in the mapping are skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support extrinsic transparency (e.g., Rgba32).

Parameters
animThe animation to extract colors from
extrinsicThe extrinsic transparency value used for transparency filtering

Definition at line 235 of file color_index_map.hpp.

◆ add_pal() [1/4]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_pal ( const Palette< PixelType > &  pal)
inline

Adds colors from a dynamic palette to the mapping using intrinsic transparency.

Extracts non-transparent colors from the provided palette and adds any new colors to the mapping. Wildcards and transparent colors are skipped. Colors that already exist in the mapping are also skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support intrinsic transparency (e.g., IndexPixel).

Parameters
palThe palette to extract colors from

Definition at line 170 of file color_index_map.hpp.

◆ add_pal() [2/4]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_pal ( const Palette< PixelType > &  pal,
const PixelType &  extrinsic 
)
inline

Adds colors from a dynamic palette to the mapping using extrinsic transparency.

Extracts non-transparent colors from the provided palette and adds any new colors to the mapping. Wildcards and transparent colors are skipped. Colors that already exist in the mapping are also skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support extrinsic transparency (e.g., Rgba32).

Parameters
palThe palette to extract colors from
extrinsicThe extrinsic transparency value used for transparency filtering

Definition at line 189 of file color_index_map.hpp.

◆ add_pal() [3/4]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_pal ( const Palette< PixelType, pal::max_size > &  pal)
inline

Adds colors from a fixed-size palette to the mapping using intrinsic transparency.

Extracts non-transparent colors from the provided palette and adds any new colors to the mapping. Wildcards and transparent colors are skipped. Colors that already exist in the mapping are also skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support intrinsic transparency (e.g., IndexPixel).

Parameters
palThe palette to extract colors from

Definition at line 133 of file color_index_map.hpp.

◆ add_pal() [4/4]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_pal ( const Palette< PixelType, pal::max_size > &  pal,
const PixelType &  extrinsic 
)
inline

Adds colors from a fixed-size palette to the mapping using extrinsic transparency.

Extracts non-transparent colors from the provided palette and adds any new colors to the mapping. Wildcards and transparent colors are skipped. Colors that already exist in the mapping are also skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support extrinsic transparency (e.g., Rgba32).

Parameters
palThe palette to extract colors from
extrinsicThe extrinsic transparency value used for transparency filtering

Definition at line 152 of file color_index_map.hpp.

◆ add_tile() [1/2]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_tile ( const PixelTile< PixelType > &  tile)
inline

Adds colors from a single tile to the mapping using intrinsic transparency.

Extracts unique non-transparent colors from the provided tile and adds any new colors to the mapping. Colors that already exist in the mapping are skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support intrinsic transparency (e.g., IndexPixel).

Parameters
tileThe tile to extract colors from

Definition at line 96 of file color_index_map.hpp.

◆ add_tile() [2/2]

template<SupportsTransparency PixelType>
void porytiles::ColorIndexMap< PixelType >::add_tile ( const PixelTile< PixelType > &  tile,
const PixelType &  extrinsic 
)
inline

Adds colors from a single tile to the mapping using extrinsic transparency.

Extracts unique non-transparent colors from the provided tile and adds any new colors to the mapping. Colors that already exist in the mapping are skipped. New colors are assigned sequential indices starting from the current size of the mapping.

This overload is only available for pixel types that support extrinsic transparency (e.g., Rgba32).

Parameters
tileThe tile to extract colors from
extrinsicThe extrinsic transparency value used for transparency filtering

Definition at line 115 of file color_index_map.hpp.

◆ color_at_index()

template<SupportsTransparency PixelType>
std::optional< PixelType > porytiles::ColorIndexMap< PixelType >::color_at_index ( ColorIndex  index) const
inline

Retrieves the color associated with a given index.

Performs a lookup in the index-to-color mapping to find the pixel color assigned to the specified index. If the index exists in the map, returns the associated color. If the index does not exist (was never assigned during construction), returns std::nullopt.

This method enables efficient reverse lookup: given an index, retrieve its color.

Parameters
indexThe color index to lookup
Returns
std::optional<PixelType> containing the color if found, std::nullopt otherwise

Definition at line 263 of file color_index_map.hpp.

◆ empty()

template<SupportsTransparency PixelType>
bool porytiles::ColorIndexMap< PixelType >::empty ( ) const
inline

Determines if the mapping is empty.

Returns
Whether the mapping is empty

Definition at line 79 of file color_index_map.hpp.

◆ index_at_color()

template<SupportsTransparency PixelType>
std::optional< ColorIndex > porytiles::ColorIndexMap< PixelType >::index_at_color ( const PixelType &  color) const
inline

Retrieves the index associated with a given color.

Performs a lookup in the color-to-index mapping to find the color index assigned to the specified pixel color. If the color exists in the map, returns the associated index. If the color does not exist (was never assigned during construction or was filtered out as transparent), returns std::nullopt.

This method enables efficient forward lookup: given a color, retrieve its index.

Parameters
colorThe pixel color to lookup
Returns
std::optional<ColorIndex> containing the index if found, std::nullopt otherwise

Definition at line 285 of file color_index_map.hpp.

◆ size()

template<SupportsTransparency PixelType>
std::size_t porytiles::ColorIndexMap< PixelType >::size ( ) const
inline

Returns the number of unique colors in the mapping.

Returns the count of unique non-transparent colors that were identified during construction. This is equivalent to the size of both the color-to-index and index-to-color maps.

Returns
The number of unique colors mapped

Definition at line 69 of file color_index_map.hpp.


The documentation for this class was generated from the following file: