Physics, not chemistry.
When light hits the oil (which sits on top of the water), some of it reflects and some of it bends and continues. By “bends” I mean changes direction, the effect that makes a pencil inserted into water appear broken.
When the bent light hits the water, more of it reflects upwards.
When it hits the oil/air transition the second time, it bends again.
The result is that a beam of light entering the oil becomes a cone on the way out.
[Diagram](https://media.hswstatic.com/eyJidWNrZXQiOiJjb250ZW50Lmhzd3N0YXRpYy5jb20iLCJrZXkiOiJnaWZcL29pbC1maWxtLmdpZiIsImVkaXRzIjp7InJlc2l6ZSI6eyJ3aWR0aCI6MjkwfX19).
Wish people tried google first. This was asked here before:
ELI5: Why does oil on water turn rainbow colored?
byu/fell_while_reading inexplainlikeimfive
On wet pavement, oil floats and spreads thinner and thinner until the oil layer approaches the thicknesses of the wavelengths of visible light. At that point it starts reflecting only select colors.
More details: https://np.reddit.com/r/askscience/comments/1idrfn/why_are_oil_leak_rainbows_the_way_they_are/
This is a phenomenon called [thin film interference.](https://en.wikipedia.org/wiki/Thin-film_interference#:~:text=Thin-film%20interference%20is%20a,this%20effect%20produces%20colorful%20reflections) It is also the explanation behind why soap bubbles have rainbows on them in sunlight, anti-reflective coatings on on glasses, and iridescent vinyl wraps.
When ocean waves combine, they add together. If a peak (high part) encounters as another peak, they combine to make a peak twice as a high. If a peak encounters a trough (low part) they combine to mostly cancel out (up+down = flat). All waves do this. Light is a wave, but sunlight is many waves which have different distance between the peaks (the wavelength). Most of this light you can’t see. What you can see we call the visible spectrum. You may have heard of infrared and ultraviolet, and these are different wavelengths of light that the sun emits that we can’t see.
When oil floats on water, it makes a very thin layer, often only a molecule think. This thickness is similar to the wavelengths of visible light. When light hits a boundary, even one it can pass through, some of it typically reflects. This is why you get reflection off of glass, even though glass is clear. So when light hits the oil, some of it reflects, and some of it continues through. When the light that continued through hits the water underneath the oils some of it again reflects. This reflected part travels up and combines with the light that reflected off the top of the oil. If the thickness of the oil matches up with the wavelength of the light, then the peak of the light wave that reflected off the oil will line up with the peak that reflected of the water under the oil, and they will combine to make a bigger wave, which your eyes see as brighter. So if the oil thickness matches up with red light, the light you see reflecting from the top of the oil will be red-tinted because it has more red light in it. Colors close to red in wavelength will also get a smaller boost, and colors further away won’t.
The angle matters, because as light goes through the oil at an angle, it travels a longer distance in the oil than if it went straight down and straight back up. This causes a different color to get boosted in brightness.
When you look at the oil, different parts of it are at different angles to your eye, thus boosting different colors at different locations on the oil surface. In addition, the oil can vary a bit in thickness. Since both the angle to your eye and the thickness change smoothly across the oil’s surface, the colors you see will change smoothly through the different colors of visible light as the distance of oil the light passes through matches up with the wavelengths of different colors of light. Red has the longest wavelength, violet has the shortest.
Latest Answers