Palette (cgram)

Last Update: 2010/11/01 23:04 +0900

Overview

cgram variable in zsnes source has complete palette at the time. (source code browser by cvs repository).

NEWSYM cgram, times 512 db 0 ; CGRAM

Palette spec

bullet256 colors in a palette.
bulletA color uses 2 bytes long

Sample decode code

Next code is to extract a color components from the palette to Win32 RGBQUAD/COLORREF form.

extern "C" BYTE cgram[];

COLORREF getPaletteAt(BYTE index)
{
    static const BYTE table[32] = {
        0,8,16,24,32,41,49,57,65,74,82,90,98,106,115,123,131,139,148,156,164,172,180,189,197,205,213,222,230,238,246,255,
    };
    WORD v = ((WORD *)cgram)[index];
    return RGB(
        table[(v >> 0) & 31],
        table[(v >> 5) & 31],
        table[(v >>10) & 31]
        );
}

void getPaletteAt(BYTE index, RGBQUAD &rgb)
{
    static const BYTE table[32] = {
        0,8,16,24,32,41,49,57,65,74,82,90,98,106,115,123,131,139,148,156,164,172,180,189,197,205,213,222,230,238,246,255,
    };
    WORD v = ((WORD *)cgram)[index];
    rgb.rgbRed   = table[(v >> 0) & 31];
    rgb.rgbGreen = table[(v >> 5) & 31];
    rgb.rgbBlue  = table[(v >>10) & 31];
    rgb.rgbReserved = 0;
}