This question featured a PDF where the heading and background colors had different CMYK values, but what if they had the same CMYK values? Then how can I change only the background color, withotu affecting the headings' and subheadings' colors?
There is no answer to your question series. The methods you have been proposed just happen to work because of circumstances. Ultimately you have to consider what the data model is.
First, theres no such thing as background color. That's not how a PDF works. There are just objects that have stroke color, a fill color or both. Now it so happens that text is generally a special text object type. So it is easy to distinguish when a color is applied to text. However, not all text like things are necessarily text objects. One can decompose text into normal beerier paths, which would be bulk of all other objects.
So the easiest, slightly more robust detection method would be to use the area of path objects, that have fill, to determine if they are backgrounds or not. Possibly that they are square, or not too.
But ultimately what you ask can not be automatically detected in a general sense, since there is a chance that this would eliminate figures. There simply is no such thing as a background.