About the color palette...

Discussion of hardware and software development for Super NES and Super Famicom. See the SNESdev wiki for more information.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
Post Reply
Esns68
Posts: 31
Joined: Tue Jan 07, 2020 2:03 pm

About the color palette...

Post by Esns68 »

This is coming from a learning beginner, and this is a noob question.

I'm trying to understand more about the color palette thing,
So I'm just messing around with some starter source codes in attempts to load my own sprite.

While messing around with a Sprite load program I found here
https://wiki.superfamicom.org/snes-sprites
From the included LoadGraphics code, I noticed a call in the source code

LoadPalette SpritePalette, 128, 16 ; Sprite Palettes start at color 128

So knowing that comment, from 128 does it start to go above or below 128?
If it goes up from 128, how far up does it go?
And what if you wanted to load another palette, would you basically write that code again with the other palleted name, and then add (or subtract) that 16 number to the 128?

And in the SNES_Starterkit, I noticed in a source code from it's own LoadGraphics that just says
LoadPalette SpritePalette
So I'm guessing it's the same as the other one, but it automatically puts all the palettes for you?
considering I see the code loads multiple palettes to it.

Also, how does the palette match up with it's corresponding image?

The other question I had was, how do you swap palettes if you ever wanted to do that?
Like for the a certain sprite, how do you load another palette to it?
Do you just create multiple palettes for that exact sprite (The same way you did in the first place with the graphics editor), load them into the code same way as above, then whenever you want, have the program apply any of those palettes to that image?

Thank you for your time.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: About the color palette...

Post by Pokun »

The whole palette or CGRAM (Color Generator RAM) as it's officially called, is a piece of 512 byte large memory where every word (byte-pair) is one color entry in a 15-bit RGB format. This means the palette has 256 color entries numbered from 0 to 255. The sprites uses the later half of CGRAM which means colors 128 to 255. Colors 0 to 127 can only be used by the background and the backdrop. Exactly what background tiles are using what subpalette depends on the background mode you are using, but sprites always works the same in all modes.

How does the palette match up with it's corresponding image? This depends on the format of the image. It can be 2 bpp (2 bits per pixel), 4 bpp, 8 bpp or a non-planar 8 bpp used only in mode 7, but sprites always uses 4 bpp so we can forget about the other ones for now.
4 bpp means that each pattern (the CHR image data used to define how BG tiles and sprites looks like) has 16 colors (15 colors + transparent). These represents the 16 colors in the sprite's assigned sub-palette (assigned in OAM). Sprite sub-palettes are always 16 colors, so color entry 128 to 144 is sprite sup-palette 0, 145 to 161 is sprite sub-palette 1 and so on all the way to sprite sub-palette 7 which is 239 to 255. So you have 8 sub-palettes for sprites with 16 colors each. You assign one of these sub-palette numbers (0 to 7) in OAM to the sprite you want to use it.

To understand the 4 bpp format I will first explain the 2 bpp format (copy-pasta from my own notes):

2 bpp
16 bytes defines a 8x8 CHR pattern allowing 3 colors + transparent.
The first two bytes defines the top pixel line of the pattern, the next two bytes covers the next line and so on. The bits in the two bytes for each line defines the pixels from left to right. Bit 7 defines the leftmost pixel, bit 6 defines the next pixel and so on until bit 0 which defines the rightmost pixel in the row. Both bytes do this and combine into a 2-bit value %00, %01, %02 or %03 for color 0~3. The bits in the first byte are the low bits and the second byte's bits are the high bits in the color number. Thus 2 bits per pixel (2bpp). Color 0 means
transparent, and the other 3 colors are defined in the palette used.
This is the same 2 bpp format used for Game Boy but different from the one used for NES.

4 bpp
32 bytes defines a 8x8 CHR pattern allowing 15 colors + transparent.
The 16 first bytes contains the two low bits of the color number for each row, and the last 16 bytes contains the two high bits. Otherwise everything is the same as the 2bpp format.
For example the topmost pixel row is defined by the 1st, 2nd, 17th and 18th bytes, in the order of least to most significant bit of the color number. The bottom row is defined by the 15th, 16th, 31st and 32nd bytes.

Color number is the number of the color in the sub-palette the tile/sprite is using. For a 4 bpp tile or sprite, the color numbers will be 0 to 15. So if for example the sprite is using sprite sub-palette 0 (128-144) color 0 is CGRAM color entry 128, color 1 is entry 129 and color 15 is entry 144.

You will probably use an image editor and convert the image to 2 bpp or 4 bpp pattern files using a program like SuperFamiConv, but you will probably want to know how the format works anyway.
Esns68
Posts: 31
Joined: Tue Jan 07, 2020 2:03 pm

Re: About the color palette...

Post by Esns68 »

That was very helpful! ありがとうございます Pokun (Seeing that your from Hokkaido, Japan. I study a little 日本語 :D )

I also forgot to ask
Are you supposed to set the all the colors in the whole palette once for the whole entire program/game,
or do you "update" the colors in the whole palette through out the program/game?

And let's say you wanted to palette swap on a certain sprite, do you just load to the same sub-palette location with another palette file,
or do you set the sprite to another another sub-palette?
93143
Posts: 1717
Joined: Fri Jul 04, 2014 9:31 pm

Re: About the color palette...

Post by 93143 »

Esns68 wrote: Thu Feb 04, 2021 10:21 pmAre you supposed to set the all the colors in the whole palette once for the whole entire program/game,
or do you "update" the colors in the whole palette through out the program/game?
There is no "supposed to". You can do either one. If you're making Pong or Asteroids or something like that, loading CGRAM once may well be enough. For a large or complex game, it may be necessary to change colours now and then, or even frequently. You can even use HDMA or an IRQ to change a few colours in between scanlines, if you encounter a situation where that's useful - lots of games do this with COLDATA (the constant colour, or subscreen backdrop) to create vertical gradients, and probably with CGRAM too.

Just make sure you only write to CGRAM during blanking (HBlank, VBlank, or forced blank). This is because if the PPU is rendering pixels, it has control of the CGRAM address, meaning the writes won't go where you want them to - they'll go where the PPU is reading instead of the address in CGADD. It's possible to exploit this to load colours during active scan, but that's a very advanced technique that you don't need to worry about.
And let's say you wanted to palette swap on a certain sprite, do you just load to the same sub-palette location with another palette file,
or do you set the sprite to another another sub-palette?
Again, you can do either one; it depends on what you're trying to accomplish.

Take Mario as an example. If you want red and green Koopas on screen at the same time, you have to use different subpalettes for them. But if you want to play as Luigi, it's probably better to simply overwrite Mario's palette, because you don't need both at the same time, so reserving two subpalettes would be wasteful.
Esns68
Posts: 31
Joined: Tue Jan 07, 2020 2:03 pm

Re: About the color palette...

Post by Esns68 »

93143 wrote: Thu Feb 04, 2021 11:14 pm Take Mario as an example. If you want red and green Koopas on screen at the same time, you have to use different subpalettes for them. But if you want to play as Luigi, it's probably better to simply overwrite Mario's palette, because you don't need both at the same time, so reserving two subpalettes would be wasteful.
Wow, couldn't have a more perfect, and appropriate example, haha.
Thanks!
Esns68
Posts: 31
Joined: Tue Jan 07, 2020 2:03 pm

Re: About the color palette...

Post by Esns68 »

Sorry I forgot to ask,
And where do you write the the desired sub-palette for the sprite? Which address and which bits?
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: About the color palette...

Post by tepples »

As described in Fullsnes, bits 3-1 of byte 3 of the OAM entry choose a subpalette for each sprite.
Post Reply