The Short Explanation
This feat of magic is common to most chameleons. They do this by expanding or contracting cells in their skin that contain different pigments. They can thus blend into their surroundings by altering the colour of their skin to match the background.
Chameleons will also use their ability to switch colour during social displays when they alter their colouration and pattern dramatically from the background.
In addition to this miraculous ability their body shape also helps with camouflage as well as the way they move. The high arched back gives their body a leaf-like shape and their slow movements make it difficult for predators and prey to detect them. Some chameleons, for example the stump-tailed chameleon, rock back and forward to look like leaves being blown by the wind.
The Long Explanation
The way that chameleons actually do this is on a molecular level – you could say that they’re molecular masterminds, really. If you look at the skin of a chameleon, you find that they have several layers of specialised cells called chromatophores and these are cells that can change colour. On the outer surface of the chameleon, the skin is transparent and just below that is the first layer of these cells, and they contain various pigments. These are xanthophores, containing particular specialised pigments that have a yellow colour. Beneath that are pigment cells which are called erythrophores which have a red colour in them. Beneath that, another layer of cells called iridiphores have a blue coloured pigment called guanine, which is actually also used in making DNA. And underneath that is another layer of cells called melanophores which have a brown pigment – melanin – in them.
Now, how does the chameleon change colour? Well those chromatophores are wired up to the nervous system. They are also sensitive to chemicals that are washing around in the blood stream of the chameleon. What happens is that the colours are locked away in tiny vesicles, little sacs inside the cells that keep them in one place, so the cells don’t look coloured. But when a signal comes in from the nervous system or from the blood stream, the granules or vesicles can discharge, allowing the colour to spread out across the cell, and this alters the colour of the cell. It’s rather like giving the cell a coat of paint. By varying the relative amount of activity of the different chromatophores in different layers of the skin, it’s like mixing different paints together. So if you mix red and yellow, you get orange for example, and this is how chameleons do this. They mix different contributions of these chromatophores. It’s a bit like on your television screen. When you mix different colours together on the screen to get the colour that the eye ultimately perceives and so, that’s how the chameleon changes colour, and usually does so to convey mood.
So a calm chameleon is a pale greeny colour. When it gets angry, it might go bright yellow, and when it wants to mate, it basically turns on every possible colour it can which shows that it’s in the mood. This is not unique to chameleons. Other animals also have these chromatophores. Cuttlefish are another very elegant example of how this works.