Butterfly collectors have known for over a century that the background color of the environment can affect the color of the chrysalis (pupa) of some species of butterfly. The Black Swallowtail butterfly, Papilo polyxenes, has a range of color morphs from light green to dark brown. On green foliage, the pupa is a light green color. On dark stems (often in the fall) the pupae are brown. The color variation helps the pupa better blend into the environment and provides some measure of protection from predators.
How does the Black Swallowtail know how to match the color of its pupa to the environment? Caterpillars have photo receptors (stemmata) with pigments that presumably can detect the surrounding color when it starts to pupate. The color triggers or inhibits the release of a hormone that controls pupal coloration. High levels of hormone will produce a brown pupa, low levels produce a green pupa. Injection of caterpillars on a green background with a nerve extract from caterpillars that are on a brown background will produce brown pupae.
Adjusting their color so they are harder to detect helps Black Swallowtail pupae avoid predation.