When a male panther chameleon is calm, he is usually green. But if a macho male approaches he might become competitive or angry, and often changes to a bright yellow and red.
Some species, like the panther, can change colour in under two minutes.
Many lizards are known to have several layers of cells with different pigments in them, which can make their skin brighter or darker.
Now it has been shown that chameleons do not use this same process when they change from, say, green to yellow.
Instead, tiny crystals called iridophores, found just under the surface of their skin, are the key. These crystals contract and expand when a chameleon is excited or under duress.
The research is published in the journal Nature Communications.
Depending on whether they are shrunken or expanded, the crystals reflect different wavelengths of light.
They are like selective mirrors, explains co-author Michel Milinkovitch at the University of Geneva in Switzerland.
"Light will bounce on them only for specific wavelengths," says Milinkovitch. "The other wavelengths will not bounce on these cells."
The different colours are created by the sizes of the crystals, and their spacing in the skin.
The crystals are efficiently organised, so the chameleon's colour is pure and intense, says Milinkovitch.
A male chameleon will often change colour to impress a rival male, or to chase him away. "It's really a social display," Milinkovitch told BBC Earth.
To observe the colour change in real time, the researchers filmed chameleons when they were faced with a rival male.
They also extracted the top layer of their skin, and exposed it to chemicals that made the crystals change size.
"We observed exactly the same change of colour in the dish," says Milinkovitch. "It really demonstrates that the colour change is happening due to the modification of these crystals."
The colour change is not caused by pigments in their skin, he adds.
The team also discovered a deeper layer of skin that reflects near-infrared sunlight. This could help chameleons regulate their body temperature.
This discovery of this second layer of cells is extremely exciting, says Devi Stuart-Fox of the University of Melbourne in Australia.
"Near-infrared is the part of the spectrum of sunlight that we and chameleons don't see," says Stuart-Fox.
"If they absorb that energy they would heat up more." So by reflecting it, chameleons can keep cool.
Follow Melissa Hogenboom and BBC Earth on twitter.