They don't change the color depending on a surrounding background at all.
Never changed and will never be. Absolute myth. Full invention. Bald lie.
The color of a chameleon depends on his emotional state. And if color suddenly coincides with a surrounding background, then it exactly coincidence, nothing more.
The chameleon changes color when he is scared, or he was taken in hand or if he has won a fight of other chameleon. They change color when the representative of an opposite sex gets to their field of vision, and sometimes — owing to changes of temperature or lighting.