Evolution within the reptile class:
Color change is a delicate balance between crypsis, or avoiding dectection, and thermoregulation. For example, it is now believed that color change in chameoleons was originally for communication, not camouflage. However, selection for the ability to change colors to optimize thermoregulation seems to have driven the evolution of color change in chameleons and anoles (Macedonia 2001). Yet some believe that conspecific communication is the real cause of the evolution of color change in some reptiles (Stuart-Fox and Adnan Moussalli 2008).
Color change in other speices:
Interestingly, the ability to change color is found throughout the animal (and even plant) kingdom. Chromatophores, the pigment containing cells, are found in bacteria, amphibians, birds, cephlapods, and mammals, though only some species are known to actively change color, as has been discussed for chameloeons and anoles (Bagnara and Hadley 1973Stuart-Fox and Adnan Moussalli 2008
Interestingly, there seems to be few studies concerning the phylogeny of color change or chromatophores in the animal kingdom.