Implement grayscale DMG palette specs (#1709)

This commit is contained in:
Rangi
2025-06-30 14:53:05 -04:00
committed by GitHub
parent 5942117ac3
commit 7054d81650
21 changed files with 166 additions and 76 deletions

View File

@@ -58,7 +58,15 @@ uint16_t Rgba::cgbColor() const {
uint8_t Rgba::grayIndex() const {
assume(isGray());
// Convert from 0..<256 to hasTransparentPixels..<nbColorsPerPal
// 2bpp shades are inverted from RGB PNG; %00 = white, %11 = black
uint8_t gray = 255 - red;
if (options.palSpecType == Options::DMG) {
assume(!options.hasTransparentPixels);
// Reduce gray shade from 0..<256 to 0..<4, then map to color index,
// then reduce to 0..<nbColorsPerPal
return options.dmgColors[gray * 4 / 256] * options.nbColorsPerPal / 4;
}
// Reduce gray shade from 0..<256 to hasTransparentPixels..<nbColorsPerPal
// Note that `maxOpaqueColors()` already takes `hasTransparentPixels` into account
return (255 - red) * options.maxOpaqueColors() / 256 + options.hasTransparentPixels;
return gray * options.maxOpaqueColors() / 256 + options.hasTransparentPixels;
}