In medieval times, the retainers and servants of any given nobleman were clothed in livery of distinctive color and design for easy recognition.
If, as might happen under the stress of war or of sharp political differences, it became expedient that a man not be recognized as associated with another, it was a simple matter for him to slip his coat inside out, thus disguising his allegiance.
One who so turned his coat was, literally, a turncoat, and the designation soon came to be applied in the figurative sense as well.
Legend has it that one astute landholder, whose estate lay on the border between two warring factions, had a coat fashioned with the colors of one army on one side and those of the other on the reverse.
According to the tides of battle, he wore first one side out, then the other.