How do chameleons change color?

How/what?

Chameleons change color by manipulating the chromatophores in their sub-dermal layer. Each chromatophore contains one pigment, and a sphincter muscle. When contracted, the pigment is squeezed up into a flat region above the chromatophore, showing that pigment. As you can imagine, this works much like an RGB display; an animal can have only a few pigments, and make many different colors by combining them selectively. Chameleons have four different layers to their skin, in order of increasing depth: the protective epidermis, the chromatophore layer (which contains red and yellow pigments), the melanophore layer which can display brown or black pigments or reflect blue, and the nether layer, which is white. Any given color morph of a species of chameleon only has a few different patterns and color combinations in its palette.

So say the chameleon is frightened by a Boomslang or a Shrike (both predators of chameleons), a combination of hormone action and nerve impulses will selectively expand and contract some chromatophores of the chameleon into either a camouflage pattern or a bright pattern which often means "I taste bad" or "I'm poisonous" in the animal world. These transitions can take as little as a few seconds, and a viewer will see a gradual shift from one color pattern to the other.

Diagrams of Chromatophores:

Pigment showing:
  _____________
{______  ______}
          |  |
          |  | (Sphincter squeezing in this region)
          |  |
          |_|

Pigment hidden:
     _
    /  \
   /    \
  /      \
 |        |
  \____/

Why/when?

There is much contention over why chameleons change color, however. I've seen several claims that chameleons change color not based on camouflage but based on their mood, which seems patently ridiculous to me; what's the evolutionary selection pressure for a mood ring? What has been shown, however, is that several species of chameleon (including the Panther Chameleon (Chamaeleo Pardalis)) use their color changing abilities in communication with other chameleons; when two males confront each other, they will face off and have a color fight, changing colors rapidly and trying to intimidate the other away. When a chameleon is hurt, it will change into dark and complicated patterns. When a chameleon is cold, it will move into the sunlight and turn a dark color, to absorb the energy better. A chameleon can also use its color changing as a defense against predators, as noted above.

Color changing is used extensively in the courting rituals of some chameleons, so the selection pressure could be that it's a selection attribute for reproduction, so the chameleon that can change color the best gets the girl, and spreads his genes farther in the next generation. Since color is partly hormonally controlled, it could vary based on the chameleon's 'mood', in that hormone balances will be different in different situations, but I don't think it's proper to anthropomorphize human moods onto animals.

Who?

Cuttlefish, and some species of octopi and squid also exhibit chromatophoric color-changing.