Fading colors

Palette fades
Palettes can be faded to white or black by respectively incrementing or decrementing each color component of each color individually. Of course, care has to be taken so that each value stops at $1F or 0.

Fading to a specific color can also be done this way, by comparing each actual components to the target ones and incrementing/decrementing them accordingly (see example code).

Simulated fades




Since it has the highest priority on the screen, the fix layer can be used to fade the underlaying display by using pre-dithered tiles. This can only be used if the fix doesn't have to show important information.

This is also used in Metal Slug 3 as transitions between levels sections.

This method is quite popular, as fading the full 256 palettes range of the system can be quite CPU intensive, plus vBlank is too short to rewrite all palettes anyway.

Color handling code
If not using pre-calculated palette tables for fades, each color component of each color needs to be modified.

RGB components can then be compared with a target palette in RAM and adjusted until they match, for example: