If I were to do this "from the ground up," I might make each country shape its own layer black only, with the layers named uniquely, and apply a layer effect "color overlay." To achieve the desired color, and a stroke to define the outline. If there is more detail to the map, and you only want to alter the background color, you can overlay other items with a different layer.
Flood fill can be tricky depending on non-contiguous areas and anti-aliasing of the item, so the layer effect would probably give you an easier time.
To view all countries, you toggle all layers visible. To programmatically change a color, you can now reference the layer by its unique ID, and then alter the color overlay property.
The specifics of how to do this will depend on your need and the program you use. I know that both GIMP and PS are scriptable (and AFAIK, support color overlay and stroke).
Knowing open source, GIMP probably gives you deeper access to the backend for scripting.