Move struct Palette into its own file (#1850)

This commit is contained in:
Rangi
2025-10-20 16:59:24 -04:00
committed by GitHub
parent 2873e0b8c8
commit 7462bccb72
11 changed files with 114 additions and 79 deletions

View File

@@ -92,6 +92,7 @@ set(rgbgfx_src
"gfx/pal_packing.cpp"
"gfx/pal_sorting.cpp"
"gfx/pal_spec.cpp"
"gfx/palette.cpp"
"gfx/png.cpp"
"gfx/process.cpp"
"gfx/reverse.cpp"

View File

@@ -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();
}

View File

@@ -11,7 +11,7 @@
#include "helpers.hpp"
#include "verbosity.hpp"
#include "gfx/main.hpp"
#include "gfx/palette.hpp"
#include "gfx/rgba.hpp"
void sortIndexed(std::vector<Palette> &palettes, std::vector<Rgba> const &embPal) {

55
src/gfx/palette.cpp Normal file
View File

@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
#include "gfx/palette.hpp"
#include <algorithm>
#include <stdint.h>
#include "helpers.hpp"
#include "gfx/main.hpp"
#include "gfx/rgba.hpp"
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();
}

View File

@@ -26,9 +26,11 @@
#include "verbosity.hpp"
#include "gfx/color_set.hpp"
#include "gfx/flip.hpp"
#include "gfx/main.hpp"
#include "gfx/pal_packing.hpp"
#include "gfx/pal_sorting.hpp"
#include "gfx/palette.hpp"
#include "gfx/png.hpp"
#include "gfx/rgba.hpp"
#include "gfx/warning.hpp"

View File

@@ -23,6 +23,7 @@
#include "helpers.hpp" // assume
#include "verbosity.hpp"
#include "gfx/flip.hpp"
#include "gfx/main.hpp"
#include "gfx/rgba.hpp"
#include "gfx/warning.hpp"