Why the sky is blue and the sunset is red
Sunlight entering the atmosphere scatters off air molecules far smaller than its wavelength, a regime called Rayleigh scattering. The key result, derived by Lord Rayleigh in the 1870s, is that the scattering intensity depends on wavelength as 1/λ⁴ — blue light (~450 nm) scatters roughly 5–6 times more strongly than red light (~700 nm) purely because of that fourth-power dependence. Looking at the sky away from the Sun, you are seeing scattered light, and it is disproportionately blue.
I_scattered ∝ 1 / lambda^4 // Rayleigh scattering intensity blue (450nm) : red (700nm) scattering ratio ≈ (700/450)^4 ≈ 5.8
At sunset the geometry changes the story: sunlight travels through far more atmosphere at a low sun angle than at noon, so almost all the blue (and much of the green) light gets scattered away before reaching your eye, leaving the direct beam dominated by the reds and oranges that survive the long path — the same physics, a longer optical path length.
The rainbow: refraction, one internal reflection, dispersion
A rainbow forms from sunlight entering a spherical raindrop, refracting, reflecting once off the droplet's inner back surface, and refracting again on the way out. Because the refractive index of water depends slightly on wavelength (dispersion), each colour bends by a slightly different angle, and the light exiting a droplet is not spread evenly but is concentrated in a narrow band around a specific angle — roughly 42° from the point directly opposite the Sun for red, about 40° for violet. That angular concentration (a mathematical caustic) is why you see a sharp-edged arc rather than a diffuse glow, and why the rainbow is always centred on the antisolar point — the shadow of your own head, extended to the horizon.
primary rainbow: 1 internal reflection, exit angle ~ 42deg (red) .. 40deg (violet)
secondary rainbow: 2 internal reflections, exit angle ~ 51deg (red) .. 53deg (violet)
=> colour order reversed, and noticeably dimmer
A secondary rainbow, when visible, comes from light that reflects twice inside the droplet before exiting, emerging at a wider angle (~51°) with the colour order reversed (red on the inside edge instead of the outside) and noticeably dimmer, since each extra reflection loses some light to transmission out through the droplet wall instead of staying reflected.
The 22° halo: ice, not water
A halo forms the same way conceptually — refraction through a transparent crystal — but through hexagonal ice crystals high in cirrus clouds rather than spherical raindrops, which changes the geometry entirely. Light entering one face of a hexagonal prism and exiting through a non-adjacent face bends by a minimum deviation angle of almost exactly 22° for the commonest crystal orientation, and because that 22° angle is a minimum of the deviation function, light bunches up there for the same caustic reason a rainbow bunches at 42° — producing a faint ring of that radius around the Sun or Moon, tinted reddish on the inner edge where the dispersion first separates the colours.
Tracing a single droplet: how the simulation gets the angles right
Rather than looking the final rainbow angle up as a constant, the simulation on this page ray-traces the actual path through a droplet cross-section for many entry points and wavelengths, which is what makes it able to show the caustic build-up rather than just draw a pre-baked arc:
for each entry point b (impact parameter, 0..1 of droplet radius): theta_i = asin(b) // angle of incidence theta_t = asin(sin(theta_i) / n(lambda)) // Snell's law, n depends on wavelength deviation = 2*(theta_i - theta_t) + (pi - 2*theta_t) // in + 1 internal bounce + out accumulate deviation into a histogram per wavelength // the histogram peak (turning point of deviation vs b) is the rainbow angle
Because n(λ) is slightly different for each wavelength, this per-wavelength histogram peaks at a slightly different angle for each colour — exactly reproducing the 40–42° spread that separates violet from red without hard-coding the rainbow angle anywhere in the code.
Frequently asked questions
Why is the sky blue but sunsets are red, if it's the same physics?
Both come from Rayleigh scattering, which favours blue light roughly 5-6 times more than red. At midday you mostly see freshly scattered blue light filling the sky. At sunset, sunlight travels through far more atmosphere at a low angle, so almost all the blue is scattered away before reaching you, leaving the direct beam dominated by the reds and oranges that survive that longer path.
Why is a rainbow always a circle centred opposite the Sun?
Because the concentration of exiting light at ~42 degrees from the antisolar point is a purely geometric consequence of refraction, one internal reflection and dispersion inside a spherical raindrop, and that geometry is rotationally symmetric around the line from the Sun through your eye. Every raindrop at the right angle around that line contributes to the arc, which is why the rainbow's centre is always the shadow of your own head, extended to the horizon.
Is a 22-degree halo the same phenomenon as a rainbow?
No. A rainbow forms in spherical liquid water droplets via one internal reflection; a halo forms in hexagonal ice crystals via simple refraction through two crystal faces with no reflection at all, which produces a much smaller minimum-deviation angle of about 22 degrees instead of 42.
Try it live
Everything above runs in your browser — open Atmospheric Optics and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Atmospheric Optics simulation