mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Move struct Palette into its own file (#1850)
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "gfx/main.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <ios>
|
||||
@@ -848,47 +847,3 @@ int main(int argc, char *argv[]) {
|
||||
requireZeroErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Palette::addColor(uint16_t color) {
|
||||
for (size_t i = 0; true; ++i) {
|
||||
assume(i < colors.size()); // The packing should guarantee this
|
||||
if (colors[i] == color) { // The color is already present
|
||||
break;
|
||||
} else if (colors[i] == UINT16_MAX) { // Empty slot
|
||||
colors[i] = color;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the ID of the color in the palette, or `size()` if the color is not in
|
||||
uint8_t Palette::indexOf(uint16_t color) const {
|
||||
return color == Rgba::transparent
|
||||
? 0
|
||||
: std::find(begin(), colors.end(), color) - begin() + options.hasTransparentPixels;
|
||||
}
|
||||
|
||||
auto Palette::begin() -> decltype(colors)::iterator {
|
||||
// Skip the first slot if reserved for transparency
|
||||
return colors.begin() + options.hasTransparentPixels;
|
||||
}
|
||||
|
||||
auto Palette::end() -> decltype(colors)::iterator {
|
||||
// Return an iterator pointing past the last non-empty element.
|
||||
// Since the palette may contain gaps, we must scan from the end.
|
||||
return std::find_if(RRANGE(colors), [](uint16_t c) { return c != UINT16_MAX; }).base();
|
||||
}
|
||||
|
||||
auto Palette::begin() const -> decltype(colors)::const_iterator {
|
||||
// Same as the non-const begin().
|
||||
return colors.begin() + options.hasTransparentPixels;
|
||||
}
|
||||
|
||||
auto Palette::end() const -> decltype(colors)::const_iterator {
|
||||
// Same as the non-const end().
|
||||
return std::find_if(RRANGE(colors), [](uint16_t c) { return c != UINT16_MAX; }).base();
|
||||
}
|
||||
|
||||
uint8_t Palette::size() const {
|
||||
return end() - colors.begin();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user