Gameboy Color PalettesOctober 2, 2013
The Gameboy Color has the ability to draw tiles that each use a total of four colors. Obviously, this is a big restriction on how the artwork was created for those games. It's also responsible for giving Gameboy games the unique aesthetic many of us remember from our childhood. This article will go over the details of how color palettes are represented and stored in Gameboy Color games.
Gameboy Color palettes are represented in BGR form, or "Blue-Green-Red" form. This means that each color is represented using three numeric values. One for the Blue value, one for the Green value, and one for the Red value. The most common method for representing colors in this way is RGB form, meaning "Red-Green-Blue". Typically, the numeric values range from 0 to 255, where 255 is the highest intensity of that color. Let's look at some examples of colors represented by RGB values.
The color red's RGB value is (255, 0, 0). As you can see from that RGB tuple, the red component has a value of 255, meaning the red is "turned all the way up". Green and blue are not contributing at all to the color. Of course, the RGB value of (0, 255, 0) would be the color green, and (0, 0, 255) would be blue. If we vary the values some more, we can create any color you can imagine. Purple is (128, 0, 128), orange is 255, 127, 0), and periwinkle is (204, 204, 255).
Recall that the Gameboy palettes are represented in BGR form. So, red would be represented at (0, 0, 255) in BGR form. The other difference is that the values only range from 0 to 31. This means that the total number of possible colors a Gameboy can display is significantly less than the number of possible colors your laptop screen can display. However, the same principles hold, so on a Gameboy, red would be represented as (0, 0, 31).
Representation in ROM
Now that we know that each color in a Gameboy palette is represented with three numeric values in BGR form, we can talk about how the values are actually stored in a game. Each color is represented with 2 bytes, or 16 total bits. Three numeric values need to be stored in those 16 bits, so each color will use 5 bits. Notice that 25=32, which is exactly how many possible values each component can have (recall that the values range from 0-31). Also notice that we're only using a total of 15 bits to represent a color. This means that 1 bit is unused, since we have 16 total bits to work with.
To obtain the final representation of a color, we form a bitstring of length 16 (2 bytes), as follows. Left-most bit is unused, so it's set to 0. The next 5 bits are the numerical representation of the blue component. Then, we tack on the 5 bits used by the green component. Next, add on the 5 bits used by the red component. Now that the 16-bit bitstring is created, we split it into 2 bytes (8 bits for the first, and 8 bits for the second). Since the Gameboy is a little-endian system, the second byte actually comes first when it's stored in the game.
Let's do an example! We'll do periwinkle because it's fun to say. As stated above, periwinkle's standard RGB form is (204, 204, 255). If we convert those numbers to be on a scale between 0-31, we get (25, 25, 31). Now, flip the first and last values to convert it to BGR form, and we get (31, 25, 25). Now, let's construct the bitstring.
First is '0' | 31 in binary | 25 in binary | 25 in binary --------------------------------------------------------- 0 | 11111 | 11001 | 11001 Now, concatenate them in order: Bitstring = 0111111100111001 Byte 1 = 01111111 = 0x7F in hexadecimal Byte 2 = 00111001 = 0x39 in hexadecimal Little-endian form = 39 7F
There we have it. Periwinkle's Gameboy representation is 39 7F in hexadecimal format. Recall that a palette is made up of 4 colors. This means that is you were viewing a Gameboy game ROM in a hex editor, you will probably see 8 bytes grouped together that define each of the 4 colors.
Slide the bars left and right to choose RGB values for 4 colors. The resulting 8-byte Gameboy representation of the palette is displayed. Fun fact: the default values are Pallet Town's palette.