Skip to content

Using the color databases

Mr-PauI edited this page Jan 5, 2026 · 4 revisions

The sgb.h file contains 32-bit rgb888 approximations of official and community sourced Super Gameboy palettes as well as a structure to ease the process of mapping individual titles to their various palettes. The cgb.h file similarly contains 32-bit rgb888 approximations of official Gameboy Color palettes. Mapping of the various color databases is the same logic, so the super game boy case will be covered here.

Below is an example of helper functions that can be used to convert/copy the palettes to a 12 index palette array in a potential native color format:

RGB565 Example

// Used for converting sgb.h palettes 
static inline uint16_t rgb888_to_rgb565(uint32_t rgb)
{
    uint8_t r8 = (rgb >> 16) & 0xFF;
    uint8_t g8 = (rgb >>  8) & 0xFF;
    uint8_t b8 = (rgb >>  0) & 0xFF;

    // Scale 8-bit to 5/6-bit with rounding
    uint16_t r5 = (r8 * 31 + 127) / 255;
    uint16_t g6 = (g8 * 63 + 127) / 255;
    uint16_t b5 = (b8 * 31 + 127) / 255;

    return (r5 << 11) | (g6 << 5) | b5;
}

/**
 * Converts the SGB palette at a given index to rgb565 and
 * copies to the dst array
 */
void sgb_palette_to_rgb565(
	uint32_t palette_index,
	uint16_t* dst_rgb565
)
{
	const uint32_t* src =
		&default32sgb_palettes[palette_index * 12];

	for (unsigned i = 0; i < 12; i++)
	{
		dst_rgb565[i] = rgb888_to_rgb565(src[i]);
	}
}

RGB555 Example helper functions

// Used for converting sgb.h palettes 
static inline uint16_t rgb888_to_rgb555(uint32_t rgb)
{
	uint8_t r8 = (rgb >> 16) & 0xFF;
	uint8_t g8 = (rgb >> 8) & 0xFF;
	uint8_t b8 = (rgb >> 0) & 0xFF;

	// Scale 8-bit to 5-bit with rounding
	uint16_t r5 = (r8 * 31 + 127) / 255;
	uint16_t g5 = (g8 * 31 + 127) / 255;
	uint16_t b5 = (b8 * 31 + 127) / 255;

	return (r5 << 10) | (g5 << 5) | b5;
}

/**
 * Converts the SGB palette at a given index to rgb555 and
 * copies to the dst array
 */
void sgb_palette_to_rgb555(
	uint32_t palette_index,
	uint16_t* dst_rgb555
)
{
	const uint32_t* src =
		&default32sgb_palettes[palette_index * 12];

	for (unsigned i = 0; i < 12; i++)
	{
		dst_rgb555[i] = rgb888_to_rgb555(src[i]);
	}
}

Mapping structure

Using function such as these, or addressing the palettes directly are both options. The index for each game is stored in the const structure SGB_PALETTE, with names/indexes for mapped titles. This simplifies the process of mapping specific games to their respective palettes. The 12 color format is used for both future extension, compatibility with the existing 12 color modes, and consistency with the cgb.h.

uint16_t selected_palette[12];
sgb_palette_to_rgb565(SGB_PALETTE.POKEMON_RED, selected_palette); // or sgb_palette_to_rgb555/etc

Clone this wiki locally