1

Each pixel is composed by two bits, allowing up to 4 shades of gray. From the LCD Monochrome Palettes section of the pandocs we can develop the algorithm for getting the color (in the case I've understood it correctly):

COLOR_NUMBER_PALETTE_BITS = {
    0: (1, 0),
    1: (3, 2),
    2: (5, 4),
    3: (7, 6)
}
COLORS = {0: WHITE, 1: LIGHT_GRAY, 2: DARK_GRAY, 3: BLACK}


def get_pixel_color(palette_address, color_number):
    palette = read_memory(palette_address)
    high_bit, low_bit = COLOR_NUMBER_PALETTE_BITS[color_number]

    color_high_bit = get_bit(palette, high_bit)
    color_low_bit = get_bit(palette, low_bit)

    color = (color_high_bit << 1) | color_low_bit

    return COLORS[color]

But just looking at the function signature we can deduce that same color numbers may result in different colors; it depends on the palette we're using.

My question is, why do we need multiple color palettes when two of them are identical, and the only difference with the third one is that 0 is transparent instead of white? Why do the palette definition change, instead of the color number used to get the color from the palette?

Julen
  • 1,024
  • 1
  • 13
  • 29

1 Answers1

2

What do you mean "two of them are identical"? You have three different color palettes. One for background and window. Two for sprites which you can select for each sprite individually using special bit in sprite attributes. Why changing palette instead of color numbers? It's much cheaper to change color palette or sprite attributes than to rewrite each tile of a sprite or window to achieve some effect on the screen. You can even change palettes in realtime as the Gameboy draws the image on the screen.

For example, let's say I wanted to implement flashing effect where whole background flashes between normal colors and white or black. Instead of rewriting every background tile on every frame I could just change the palette and time it exactly to only affect parts of the screen I need. There's even an article describing similar technique.

creker
  • 9,400
  • 1
  • 30
  • 47