Litmus is a mixture of several different dyes extracted from the Roccella tinctoria or Lecanora tartarea, which are special types of organisms known as "lichens." These dyes (most notably erythrolitmin and azolitmin) have massive molecules, which feature special structural groups called "chromophores." These chromophores consist of several rings filled with carbon-carbon and carbon-oxygen double bonds. This combination of double bonds and rings creates a network of corridors through which electrons can travel.
When visible light hits a dye molecule, the chromophore's electron network allows it to absorb a specific range of colours. The colour of light that is not absorbed is reflected back to the observer, giving the molecule its observed colour. For example, litmus's natural colour is blue, meaning that the network absorbs every wavelength of light except blue.
However, the hydrogen ions in acids attack carbon-carbon and carbon-oxygen double bonds, converting them into single bonds. With each double bond that is destroyed, the electron network decreases in size. This alters the range of wavelengths the molecule can absorb, thus changing its observed colour.
Blue Litmus Paper
Used to test for acidity, blue litmus paper consists of cellulose filter paper infused with a solution of litmus dye freshly extracted from lichens.
In an alkaline solution, the hydrogen ion levels are too low to significantly damage the chromophore's double bonds, preserving the natural blue colour. In a neutral solution like distilled water, the hydrogen ion levels are significant enough to begin damaging a significant number of dye molecules. For those molecules that are affected, the hydrogen-degraded chromophores cause the dye molecules to turn from blue to red. At the microscopic level, the litmus paper is a mixture of unaffected blue molecules and degraded red molecules. To the observer, however, the paper appeared to be a uniform shade of purple.
Finally, in an acidic solution, the excess of hydrogen ions manage to degrade the vast majority of chromophore double bonds, turning the litmus paper entirely red.
How Red Litmus Paper Works
Used to test for basicity, red litmus paper consists of cellulose paper infused with dye molecules that have already been degraded by acid (hence the red colour). Red litmus paper essentially functions as blue litmus paper in reverse.
As a solution's pH increases, the concentration of hydrogen ions decreases but the concentration of hydroxide ions [OH-] increases. These negatively-charge ions "steal" hydrogen atoms from the degraded dye molecules to form water. The dye molecule respond to the lost hydrogen by forming double bonds between their carbon and oxygen atoms. In this way, the dye molecule "rebuild" their electron networks and regain their original blue color.