Many animals change their body pigmentation according to illumination of their environment. In aquatic vertebrates, this reaction is mediated through aggregation or dispersion of melanin-filled vesicles (melanosomes) in dermal pigment cells (melanophores). The adaptive value of this behavior is usually seen in camouflage by allowing the animal to visually blend into the background. When exposed to visible light from below, however, dark-adapted zebrafish embryos at the age of 2 days post fertilization (dpf) surprisingly display dispersal instead of aggregation of melanosomes, i.e. their body coloration becomes dark on a bright background. Melanosomes of older embryos and early larvae (3-5 dpf) on the other hand aggregate as expected under these conditions. Here we provide an explanation to this puzzling finding: Melanosome dispersion in larvae 3 dpf and older is efficiently triggered by ultraviolet (UV) light, irrespective of the visual background, suggesting that the extent of pigmentation is a trade-off between threats from predation and UV irradiation. The UV light-induced dispersion of melanosomes thereby is dependent on input from retinal short wavelength-sensitive (SWS) cone photoreceptors. In young embryos still lacking a functional retina, protection from UV light predominates, and light triggers a dispersal of melanosomes via photoreceptors intrinsic to the melanophores, regardless of the actual UV content. In older embryos and early larvae with functional retinal photoreceptors in contrast, this light-induced dispersion is counteracted by a delayed aggregation in the absence of UV light. These data suggest that the primary function of melanosome dispersal has evolved as a protective adaption to prevent UV damage, which was only later co-opted for camouflage.