Details

To write to the palette in the middle of the screen, the following sequence of operations need to be done:

Disable rendering ($2001)

Set PPU address ($2006)

Write new palette ($2007)

Restore PPU address ($2006)

Re-enable rendering ($2001)

Ideally, these should be done within or mostly within horizontal blanking to minimize the appearance of visual errors. There are several problems:

NTSC horizontal blank only lasts 21 cycles, which is not enough time to fit all of these steps at once.

Half of the address may be written to $2006 early.

Three values can be loaded into the A, X, Y registers in advance of blanking to avoid extra load cycles.

Most IRQ or $2001 polling techniques for timing are subject to jitter, varying by about 7 cycles, which cuts into the usable horizontal blank time significantly.

Because sprites are evaluated one line in advance, turning off rendering will cause the first line of sprites to be corrupted after re-enabling.

The write to disable rendering ($2001) should be done after pixel 240 on a scanline to avoid corrupting all sprites on the next frame (see: Errata).

After the change, one line with background rendering enabled but sprites hidden can be used to flush the invalid sprite evaluation buffer. (With background rendering on, sprite evaluation will resume but remain hidden.)

Whenever the PPU address is pointing at a palette entry, this will become the current background color whether rendering is on or off.

If the background color is unchanged and stored redundantly at PPU $3F00/4/8/C... BIT $2007 can be used to skip over the background color, followed by 3 writes to $2007, returning the PPU address to point at the background color again. This can be used to change 3 colors in a single horizontal blank, and if rendering is kept off 3 more colors may be updated in each subsequent line. An extra empty line is needed after the palette writes are finished, since there is not enough time in horizontal blank to write 3 colors and restore the PPU address and return to rendering.

Because of these issues, it is best not to use this techniques where sprites may overlap the affected lines. Indiana Jones hides some potential visual errors by only working where the background is already blank, and only changing the background color. Games that change colors for a status bar can use blank lines at the top of the status bar to similarly avoid visual errors.