Fermant’s principle lead to the law of refraction. The dispersion, which means that the phase velocity of a lightwave depends on its frequency, is the reason for different strength of refraction. Now we just need water (rain) and light propagation trough it to create the beauty of a rainbow.