Retinal opsin photopigments initiate mammalian vision when stimulated by light. Most mammals possess a short wavelength-sensitive opsin 1 (SWS1) pigment that is primarily sensitive to either ultraviolet or violet light, leading to variation in colour perception across species. Despite knowledge of both ultraviolet-and violet-sensitive SWS1 classes in mammals for 25 years, the adaptive significance of this variation has not been subjected to hypothesis testing, resulting in minimal understanding of the basis for mammalian SWS1 spectral tuning evolution. Here, we gathered data on SWS1 for 403 mammal species, including novel SWS1 sequences for 97 species. Ancestral sequence reconstructions suggest that the most recent common ancestor of Theria possessed an ultraviolet SWS1 pigment, and that violet-sensitive pigments evolved at least 12 times in mammalian history. We also observed that ultraviolet pigments, previously considered to be a rarity, are common in mammals. We then used phylogenetic comparative methods to test the hypotheses that the evolution of violet-sensitive SWS1 is associated with increased light exposure, extended longevity and longer eye length. We discovered that diurnal mammals and species with longer eyes are more likely to have violet-sensitive pigments and less likely to possess UV-sensitive pigments. We hypothesize that (i) as mammals evolved larger body sizes, they evolved longer eyes, which limited transmittance of ultraviolet light to the retina due to an increase in Rayleigh scattering, and (ii) as mammals began to invade diurnal temporal niches, they evolved lenses with low UV transmittance to reduce chromatic aberration and/or photo-oxidative damage.