A soap bubble can contain swirling bands of purple, green, blue, yellow, and red even when the soap solution itself is colorless.
Those colors come from light.
More specifically, they come from a process called thin-film interference.
A soap bubble is an extremely thin film
The bubble wall contains a thin layer of water held between soap molecules.
That film can be much thinner than a human hair.
When white light reaches it, some light reflects from the outside surface.
Some travels into the film and reflects from the inside surface.
The reflected waves meet again
Light behaves like a wave.
The two reflected waves have traveled slightly different distances.
When they meet, they can reinforce or cancel one another.
Whether they reinforce depends partly on the thickness of the soap film and the wavelength of the light.
Different colors have different wavelengths
Red light has a different wavelength from blue light.
That means a certain film thickness may strengthen blue while weakening red.
A slightly different thickness may strengthen green or yellow instead.
The bubble therefore shows different colors in different places.
Why do the colors move?
The film is constantly changing.
Gravity pulls liquid downward.
Water evaporates.
Air moves around the bubble.
All of those changes alter the thickness of the film.
As thickness changes, the color pattern changes too.
Why does the top of a bubble sometimes turn dark?
As the film becomes extremely thin, the reflected waves can cancel much of the visible light.
A dark patch near the top can be a sign that the bubble is very close to popping.
The film has nearly drained away.
Is the rainbow the same as a rainbow in the sky?
No.
A sky rainbow mainly involves refraction, reflection, and dispersion inside water droplets.
Bubble colors mainly come from interference in a thin film.
Both involve light.
The mechanisms are different.
So the rainbow is not dye hiding in the soap.
It is light interfering with itself across a film thinner than a human hair.
A related explanation is Why does a straw look bent in water?, which looks at another familiar effect from the same general system.
And that’s the short explanation.
Related explanations: Why does a straw look bent in water?, Why do mirrors reverse images?, and Why is the sky blue?