The story
From the naval military practice of displaying one's colours (signal flags or insignia) from the mast of a ship during battle to show loyalty. To surrender, one would strike one's colours, i.e. take down one's flag. If a ship nailed its colours to the mast, it would fight to the death and not surrender.
Etymology from Wiktionary, CC BY-SA 4.0.