Speaking specifically about visas for the USA, as far as I can tell, it is not acceptable (it's not illegal, it just means they won't accept the photo).
The US Department of State's page on photos in visa applications has a section near the bottom, "Detailed Examples of Visa Photos", under which there's a tab "Digital Alterations and Retouching" which includes an example that fails because the sitter has red-eye. The note under this photo says
Digitally remove the red eye effect, or retake a photo that does not include the red eye effect. (This is the only digital retouching that is acceptable for passport photos)
From which I conclude that recolouring the background is not acceptable. They do give another specific case where someone's made a white background by cropping out the existing one, but badly - the grounds for refusal are "Background is cropped out using a photo retouching tool, altering the outline of the head, face, and neck". However, the red-eye comment makes me think that all such alterations, even if done well, are unacceptable. That doesn't mean they'll know you did it, so you might get away with it if you do a good job, but strictly speaking, State doesn't like it.