Format CGB colors in error/verbose messages as "GB:rr,gg,bb"

These should be more debuggable than packed "$xxxx" format.
This commit is contained in:
Rangi
2026-07-14 10:34:27 -04:00
committed by Rangi
parent 7e2a3491a5
commit 9a15b723f2
8 changed files with 79 additions and 72 deletions
+7 -5
View File
@@ -260,12 +260,14 @@ static void verboseOutputAssignments(
) {
verboseDo(VERB_INFO, [&]() {
for (AssignedSets const &assignment : assignments) {
fputs("{ ", stderr);
fputs("- { ", stderr);
for (ColorSetAttrs const &attrs : assignment) {
fprintf(stderr, "[%zu] ", attrs.colorSetIndex);
for (uint16_t colorIndex : colorSets[attrs.colorSetIndex]) {
fprintf(stderr, "%04" PRIx16 ", ", colorIndex);
}
fprintf(
stderr,
"[%zu] %s ",
attrs.colorSetIndex,
listCGBColors(colorSets[attrs.colorSetIndex]).c_str()
);
}
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
+27 -53
View File
@@ -200,11 +200,11 @@ struct Image {
if (std::pair fused{color.toCSS(), other->toCSS()};
fusions.find(fused) == fusions.end()) {
warnx(
"Colors #%08x and #%08x both reduce to the same Game Boy color $%04x "
"Colors #%08x and #%08x both reduce to the same RGB555 color %s "
"(first seen at (%" PRIu32 ", %" PRIu32 "))",
fused.first,
fused.second,
color.cgbColor(),
toCGB(color.cgbColor()).c_str(),
x,
y
);
@@ -411,15 +411,6 @@ static std::pair<std::vector<size_t>, std::vector<Palette>>
}
}
auto listColors = [](auto const &list) {
static char buf[sizeof(", $xxxx, $xxxx, $xxxx, $xxxx")];
char *ptr = buf;
for (uint16_t color : list) {
ptr += snprintf(ptr, sizeof(", $xxxx"), ", $%04x", color);
}
return &buf[literal_strlen(", ")];
};
// Iterate through color sets, and try mapping them to the specified palettes
std::vector<size_t> mappings(colorSets.size());
bool bad = false;
@@ -435,7 +426,10 @@ static std::pair<std::vector<size_t>, std::vector<Palette>>
if (iter == palettes.end()) {
assume(!colorSet.empty());
error("Failed to fit tile colors [%s] in specified palettes", listColors(colorSet));
error(
"Failed to fit tile colors [%s] in specified palettes",
listCGBColors(colorSet).c_str()
);
bad = true;
}
mappings[i] = iter - palettes.begin(); // Bogus value, but whatever
@@ -447,7 +441,7 @@ static std::pair<std::vector<size_t>, std::vector<Palette>>
palettes.size() == 1 ? " was" : "s were"
);
for (Palette const &pal : palettes) {
fprintf(stderr, " [%s]\n", listColors(pal));
fprintf(stderr, " - [%s]\n", listCGBColors(pal).c_str());
}
giveUp();
}
@@ -459,11 +453,7 @@ static void outputPalettes(std::vector<Palette> const &palettes) {
// LCOV_EXCL_START
verboseDo(VERB_INFO, [&]() {
for (Palette const &palette : palettes) {
fputs("{ ", stderr);
for (uint16_t colorIndex : palette) {
fprintf(stderr, "%04" PRIx16 ", ", colorIndex);
}
fputs("}\n", stderr);
fprintf(stderr, "- { %s }\n", listCGBColors(palette).c_str());
}
});
// LCOV_EXCL_STOP
@@ -1048,23 +1038,16 @@ void process() {
case ColorSet::STRICT_SUPERSET:
// Override the previous color set that this one is a strict superset of
verboseDo(VERB_DEBUG, [&]() {
fprintf(
stderr,
"- Tile (%" PRIu32 ", %" PRIu32 ") overrides color set #%zu: [",
tile.x,
tile.y,
n
);
for (uint16_t color : colorSets[n]) {
fprintf(stderr, "$%04x, ", color);
}
fputs("] becomes [", stderr);
for (uint16_t color : colorSet) {
fprintf(stderr, "$%04x, ", color);
}
fputs("]\n", stderr);
});
verbosePrint(
VERB_DEBUG,
"- Tile (%" PRIu32 ", %" PRIu32
") overrides color set #%zu: [%s] becomes [%s]\n",
tile.x,
tile.y,
n,
listCGBColors(colorSets[n]).c_str(),
listCGBColors(colorSet).c_str()
);
colorSets[n] = colorSet;
// Remove any other color sets that we are also a strict superset of
@@ -1112,19 +1095,14 @@ void process() {
attrs.colorSetID = colorSets.size();
colorSets.push_back(colorSet);
verboseDo(VERB_DEBUG, [&]() {
fprintf(
stderr,
"- Tile (%" PRIu32 ", %" PRIu32 ") adds color set #%zu: [",
tile.x,
tile.y,
attrs.colorSetID
);
for (uint16_t color : colorSet) {
fprintf(stderr, "$%04x, ", color);
}
fputs("]\n", stderr);
});
verbosePrint(
VERB_DEBUG,
"- Tile (%" PRIu32 ", %" PRIu32 ") adds color set #%zu: [%s]\n",
tile.x,
tile.y,
attrs.colorSetID,
listCGBColors(colorSet).c_str()
);
continue_visiting_tiles:;
}
@@ -1138,11 +1116,7 @@ continue_visiting_tiles:;
// LCOV_EXCL_START
verboseDo(VERB_INFO, [&]() {
for (ColorSet const &colorSet : colorSets) {
fputs("[ ", stderr);
for (uint16_t color : colorSet) {
fprintf(stderr, "$%04x, ", color);
}
fputs("]\n", stderr);
fprintf(stderr, "- [%s]\n", listCGBColors(colorSet).c_str());
}
});
// LCOV_EXCL_STOP
+13
View File
@@ -4,6 +4,7 @@
#include <algorithm>
#include <array>
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
@@ -11,6 +12,18 @@
#include "gfx/main.hpp" // options
std::string toCGB(uint16_t color) {
if (color == Rgba::transparent) {
return "transparent"; // same length as "GB:rr,gg,bb"
}
uint8_t red = color & 0b11111;
uint8_t green = color >> 5 & 0b11111;
uint8_t blue = color >> 10 & 0b11111;
char buf[sizeof("GB:rr,gg,bb")];
snprintf(buf, sizeof(buf), "GB:%02" PRIu8 ",%02" PRIu8 ",%02" PRIu8, red, green, blue);
return buf;
}
// Based on inverting the "Modern - Accurate" formula used by SameBoy
// since commit b5a611c5db46d6a0649d04d24d8d6339200f9ca1 (Dec 2020),
// with gaps in the scale curve filled by polynomial interpolation.