Why are there seven colors in the rainbow?
Short answer: There aren’t.
Long answer: Isaac Newton was one of the first people to rigorously experiment with light by observing how white light can be decomposed into a full rainbow spectrum using a prism. He observed how objects would absorb and transmit certain parts of the spectrum by separating out colors from the spectrum for experiments. By shining those colored rays on different objects he concluded that those different parts of the spectrum would not change their color if scattered. This overturned the classical theory of light, which claimed that sunlight was “pure” and was converted into different colors when scattered from objects. In short, Newton showed that white sunlight already contained those colors.
Newton’s theory of color also has one more weird component, owing to Newton’s interest in alchemy and the ancient Greeks, who had a bit of an obsession with the number seven. For example they developed a correspondence between the seven known planets in the night sky and the seven days of the week (Sun-day, Moon-day, … , Saturn-day). They also only knew of seven metals, and believed each had an associated planet so the number seven is pretty ubiquitous in ancient texts.
Basically, the number seven shows up a lot in alchemy, and Newton was a bit of an alchemist. Newton originally subdivided the spectrum he observed into just five colors – red, yellow, green, blue, and violet. He revised it to include seven colors which he published in his treatise on light, “Opticks,” where he argues for a correspondence between the seven colors and the seven musical notes. This isn’t actually that weird – Newton believed the colors were cyclical (like the musical scale) so he placed red adjacent to violet, thereby inventing the color wheel. I guess when you invent as much science as Newton you’re allowed to stylize it however you want.
Anyway, to make the point one last time, take a look at this spectrum:
With our modern understanding of the electromagnetic spectrum we know that visible light is any light with wavelengths between about 390 and 700 nm. This means that light comes in a continuum, with smooth transitions between colors. Furthermore, the human eye is capable of distinguishing hundreds of colors! The “Seven Colors of the Rainbow” is completely arbitrary.