|Photographer: Benny Trapp (via Wikimedia Commons)|
According to Dr. John Friel of the Cornell University Museum of Invertebrates, true chameleons (as opposed to the little color-changing anoles that are often referred to as chameleons) change color primarily for communication. They'll turn dark colors or black if scared or angry, and bright colors if they're ready to mate or defend their territory. The light level and weather conditions in their environment also influence the chameleon's color. However, there is one particular species of chameleon, the dwarf chameleon, that changes color for camouflage. These guys can adjust their colors in accordance to the vision of the type of predator!
How do chameleons change color, anyway? Just below their transparent outer layer of skin, they have layers of pigmented cells called chromatophores. The upper layer cells (xanthophores and erythophores) have yellow or red pigment. The middle layer cells (iridophores and guanohores) have guanine, which is blue or white. Below those is a layer of cells with melanin, which can create dark colors. The chameleon's brain sends signals to the cells, causing them to shrink or expand. The colors can even mix like paint.