Rainbow Machine

Sunlight on a sun-tracking Ø450 mm round breadboard · prism spectrum or Descartes sphere rainbow · real dispersion, 0.53° solar disk

In short: Sunlight is split into its colours in two ways: a lens and a glass prism spread it into a straight rainbow stripe, and a single clear ball acts like one giant raindrop, throwing a real rainbow ring onto a card. The page shows how glass type, distance and stray daylight change what you see.

© 2026 Fabian Magaña-Sandoval, PhD. All rights reserved.
fabianoptics@gmail.com
drag to orbit · scroll to zoom · right-drag to pan · the plate follows the sun; tracking error tilts it

Design

Sun and tracking

deviation / bow angle
—
colour spread
—
sharpness
—
material
—

The sun is 9.3 mrad across, so a tracker only needs to hold a few mrad. A few mrad of error just shifts the band or arc; beyond about 10 mrad the light starts missing the optics.

What the screen shows

White paper card, lit by the rainbow plus whatever daylight already falls on it. More daylight washes the colours toward white; shade the card (a box or a hood) for the most saturated rainbow.

Colours come from tracing rays at 31 wavelengths (400–700 nm) through the real glass, weighted by a 5778 K sun spectrum and the CIE 1931 eye response. Brightness is normalised, so a dim bow still looks visible here.

How to choose

Straight rainbow (prism): the cylindrical lens squeezes the sunlight into a thin vertical line, and the prism fans each colour sideways. Colour purity depends on the glass's dispersion compared with the sun's 9.3 mrad size. With the 250 mm lens and the screen 120 mm after the prism, N-SF11 spreads 400–700 nm over 22 mm, about 10 sun-image widths, so the colours separate cleanly. N-F2 gives about 4 widths. N-BK7 and acrylic give about 1.5, so the band looks mostly white with coloured edges.

Arced rainbow (sphere): a real rainbow is a cone around the antisolar point, made by every drop. One clear sphere makes the same cone. Put a card with a hole on the sun side and you see the arc. Water gives the classic 42° bow, which is big (a 270 mm ring at 150 mm). Acrylic gives 23° and N-BK7 21°, both compact. Glasses with n ≈ 1.8 or more push the bow into the sphere's own shadow, so the cheapest sphere (a water-filled round flask, or a clear acrylic ball) is also the best one.

Safety: a lens focusing sunlight burns skin and can ignite paper. Never look along the beam or at the focus. Mount the screen before uncovering the lens.

Detailed abstract

Purpose

Shows, with real glass and water dispersion, why a prism and a raindrop make rainbows, and what limits how vivid they look outdoors. It answers build questions: which prism glass separates colours, which sphere gives a visible bow, and how well the tracker must point.

What it can do

Limitations

How realistic is it?

The modeling: core physics and how it is handled

Core physics and how it is handled

1. Dispersion. Refractive index versus wavelength from the three-term Sellmeier form n² = 1 + Σ Bᵢλ²/(λ² − Cᵢ), λ in µm. Handled: closed form in rb_model.js with SCHOTT coefficients for N-BK7, N-F2, N-SF11, a PMMA fit for acrylic, and a four-term Daimon & Masumura fit for water at 20 °C. Air index fixed at n_air = 1.000278; all indices are divided by it.

2. Refraction and Fresnel transmission. Vector Snell's law d_t = η d + (η cos θᵢ − cos θ_t) N, with η = n₁/n₂ and cos θ_t = √(1 − η²(1 − cos²θᵢ)); TIR when the root is negative. Transmission T = 1 − (r_s² + r_p²)/2 with r_s = (n₁cos θᵢ − n₂cos θ_t)/(n₁cos θᵢ + n₂cos θ_t), r_p = (n₂cos θᵢ − n₁cos θ_t)/(n₂cos θᵢ + n₁cos θ_t). Handled: applied at every lens and prism surface; each ray's weight is the product of T values.

3. Prism at minimum deviation. For apex A = 60°, minimum deviation occurs at incidence i₀ = arcsin(n sin(A/2)). Handled: rotation 0 sets i₀ using n(550 nm) of the chosen glass; the slider adds a rotation (degrees). Faces are planes clipped to the 25 mm face width and height; the prism is placed so the beam axis hits the entrance-face centre. Deviation and spread are found by tracing an on-axis ray (no lens) at 400, 550 and 700 nm: spread (mm) = |δ(400) − δ(700)| × screen distance.

4. Lens imaging and the solar-disk limit. The sun subtends 0.533° (≈ 9.3 mrad), so the line focus is at least f·9.3 mrad ≈ 2.3 mm wide for f ≈ 250 mm. Handled: real thick N-BK7 plano-convex lenses (cylindrical R = 129.2 mm, t_c = 3.5 mm; spherical R = 128.8 mm, t_c = 2.8 mm; Ø25.4 mm) traced surface by surface with the shared engine's sag-surface hit routine, so chromatic aberration is included. The "distinct colour bands" readout is spread / (250 mm × 0.0093).

5. Monte Carlo spectrum on the screen. Handled: 15 × 15 jittered aperture samples inside the iris circle, 7 sun directions on the disk (Fibonacci spiral, radius 0.2665°), and 45 jittered wavelength strata over 400–700 nm per ray. Each hit on the screen plane (normal to the 550 nm central ray, at the set distance) carries XYZ weight; hits are bilinearly splatted to a 320 × 200 image and blurred by one pixel.

6. Descartes rainbow. A ray with impact parameter b = sin i refracts to r = arcsin(b/n); after k internal reflections the total deviation is D = 2(i − r) + k(π − 2r). The bow is where dD/di = 0 (light piles up). Handled: sphereProfile sweeps 40 000 impact parameters for each of 31 wavelengths (10 nm grid, 400–700 nm), k = 1 and 2. Weight per ray: ½[(1−R_s)²R_sᵏ + (1−R_p)²R_pᵏ]. Rays are binned in 0.02° bins of angle from the antisolar point, weighted by b/sin θ (per unit solid angle). The readout takes each colour's profile maximum within ±1.5° of its Descartes angle; after the 0.53° solar-disk blur and the 0.02° binning it sits about 0.2° inside the Descartes value (40.68° / 42.14° for water vs 40.87° / 42.33°). For N-SF11 (n ≈ 1.8) the bow is at 3–6°, inside the card hole, and the page says so.

7. Solar-disk blur and card projection. Handled: each angular profile is convolved with a semicircular kernel ±13 bins (±0.26°). The flat card sits at the set distance on the sun side; each pixel's angle θ is computed from the sphere centre, and irradiance is scaled by cos³ of the angle from the card normal. A hole of radius R_sphere + 3 mm shows dark.

8. Colour science. Each wavelength is weighted by a 5778 K Planck spectrum times CIE 1931 colour-matching functions (multi-lobe Gaussian fit after Wyman et al.). Handled: XYZ is white-balanced (von Kries, solar white → D65), an ambient term (fraction × peak) is added to model daylight on white paper, luminance is compressed with a Reinhard curve, then XYZ → linear sRGB, negatives clipped, over-range scaled as a whole (hue-preserving), and sRGB gamma applied.

How the simulation is built

How to recreate it

  1. Code the Sellmeier functions for each material (coefficients in rb_model.js) and check n_d for N-BK7 ≈ 1.5168.
  2. Write vector refraction with Fresnel transmission (equations in item 2).
  3. Prism mode: build two planes at the minimum-deviation orientation, add a plano-convex lens, and trace rays sampled over the aperture, solar disk and 400–700 nm; record hits on a screen normal to the 550 nm ray.
  4. Accumulate hits weighted by solar spectrum × CIE functions into an XYZ image; convert to sRGB with white balance and hue-preserving scaling.
  5. Sphere mode: for each wavelength, sweep b from 0 to 1, compute D for k = 1, 2, histogram θ = π − D (folded) with Fresnel and b/sin θ weights, blur by the 0.53° disk, and map onto a flat card with cos³.
  6. Check: water primary ≈ 42° (red) and secondary ≈ 51°; N-SF11 prism ≈ 67° deviation and ≈ 10.5° spread.
  7. To rerun the existing code: python build.py in the source folder, then open rainbow_machine.html (three.js loads from the CDN).

Key parameters

Parameter Value Source
Sun angular diameter 0.533° (9.3 mrad) design choice (standard value)
Sun spectrum 5778 K blackbody design choice
Glass dispersion Sellmeier N-BK7, N-F2, N-SF11 SCHOTT datasheet
Water dispersion 4-term fit, 20 °C Daimon & Masumura 2007
Air index 1.000278 design choice
Prism 25 mm equilateral, A = 60° design choice
Lens (cylindrical) R = 129.2 mm, t_c = 3.5 mm, f ≈ 250 mm design choice
Lens (spherical) R = 128.8 mm, t_c = 2.8 mm (LA1461-type) datasheet
Default iris / screen distance Ø20 mm / 120 mm design choice
Prism-mode sampling 15 × 15 aperture, 7 sun directions, 45 λ strata design choice
Sphere-mode sampling 40 000 rays/λ, 31 λ, 0.02° bins design choice
Spheres Ø25 mm balls, Ø50 mm water flask design choice
Daylight on card 10 % (prism), 40 % (sphere) of peak ASSUMED
Plate Ø450 mm, M6 on 25 mm grid design choice
The shared 3D bench kit (how the table, mounts, beams and parts list are drawn)

What the shared bench engine is

Every interactive page loads three shared files. engine.js is the optics math, kit.js is the three.js scene, stand-in hardware, beam drawing and parts table, and clamps.js places the posts and forks. Units are mm in a table frame (x, y, z up). Each experiment's physics lives in its own model file. The engine only does the optics that a model explicitly asks for.

How beams are traced

  • Chief ray: exact 3D tracing at infinite planes, spheres and cylinders. Refraction uses vector Snell's law and fails on total internal reflection. Mirrors use d' = d − 2(d·N)N. A 'stop' surface only intersects. Air is n = 1.000278 and N-BK7 comes from its Sellmeier formula. Each wavelength gets its own instance via make(λ).
  • Gaussian beam: a general astigmatic beam in Arnaud form. Complex 2 × 2 matrices X and Θ give W = Θ·X⁻¹ = n·Q⁻¹. Free space is X += (L/n)·Θ, the matrix form of the q-parameter (ABCD) law. At a surface, X' = A·X and Θ' = C·X + D·Θ. A and D come from projecting the beam basis onto the surface tangents, and C = −Δ·P₂⁻ᵀ·K·P₁⁻¹ with Δ = n₂(d₂·N) − n₁(d₁·N) and K the curvature tensor. Tilted and cylindrical surfaces therefore produce correct astigmatism. Gouy phase is accumulated per axis.
  • Sampling: samples are taken about every 6 mm and refined wherever the radius changes by more than 12 % between samples. The 1/e² ellipse at each sample comes from Im(W).
  • Splits: there is no automatic splitting. A model clones the beam (cloneB) at a beamsplitter and traces each branch with its own surface list. Surfaces have no apertures, so the model must handle clipping.
  • Extras: Hermite and Laguerre polynomials, and an FFT angular-spectrum propagator for mode-shape pages.

Where readings come from

The kit's photodiode and camera objects are bodies on posts and compute nothing. Photodiode voltages, quadrant read-outs, spectra and noise are all computed in each model file. These calculations are often analytic and use no engine at all, as in the Faraday, EOM and direction-finder pages. Labels are CSS2D text set by the app.

Parts table

Each stand-in part and post stack registers a row: reference, category, part number, description. A stack registers a TR post, a PH40 or PH50 holder (chosen for the beam height), a BE1/M base, and a CF125C/M fork unless the post sits directly on a hole. partsTable() groups and counts identical rows, sorts them by category, and writes the HTML table and the CSV.

Clamps and obstacles

Each fork's M6 screw must land on a 25 mm-grid hole 26.4–49.6 mm from the post. Forks perpendicular to the beam are preferred. A sliding part keeps its hole while that hole still fits, then re-clamps. Forks may not overlap other forks, pedestals or registered footprints ("obstacles"). Conflicts turn red, and a guard stops posts from colliding. All of this is mechanical layout only; it has no optical effect.

Purely visual

  • Beam tubes draw the ellipse samples, optionally magnified, with opacity on a log scale of power.
  • Some pages draw hand-made tube samples instead of traced ones.
  • Meshes are parametric stand-ins, not CAD.

Parts list (generated from the 3D model)

Suggestions only; verify part numbers. Prices quoted in the README were checked online when this was built.

CSV (select and copy)

References and related literature

Two classic demonstrations: a dispersing prism (Newton) and the single-sphere (Descartes) rainbow. Colours use a solar spectrum and analytic CIE colour-matching functions.

  1. classic demo: sphereR. Descartes, Les Météores (1637), Discourse VIII: the ray theory of the rainbow with a glass sphere as a model raindrop.
  2. classic demo: prismI. Newton, Opticks (1704): dispersion of sunlight by a prism.
  3. H. M. Nussenzveig, "The theory of the rainbow," Sci. Am. 236(4), 116 (1977).
  4. J. A. Adam, "The mathematical physics of rainbows and glories," Phys. Rep. 356, 229 (2002).
  5. J. D. Walker, "Multiple rainbows from single drops of water and other liquids," Am. J. Phys. 44, 421 (1976).
  6. C. B. Boyer, The Rainbow: From Myth to Mathematics (Princeton University Press, 1987).
  7. M. Daimon and A. Masumura, "Measurement of the refractive index of distilled water from the near-infrared region to the ultraviolet region," Appl. Opt. 46, 3811 (2007).
  8. SCHOTT optical glass data sheets (N-BK7, N-F2, N-SF11 Sellmeier coefficients).
  9. C. Wyman, P.-P. Sloan and P. Shirley, "Simple analytic approximations to the CIE XYZ color matching functions," J. Comput. Graph. Tech. 2(2), 1 (2013). link
  10. E. Hecht, Optics, 5th ed. (Pearson, 2017): interference, Fresnel equations, Malus's law, prisms.