Every single element has a spectra of light that is unique to that element. Think of this like a visual “fingerprint” for elements. Scientists have conducted experiments on all of the planetary elements and empirically figured out the different emission spectra for each element.
These emission spectra https://en.m.wikipedia.org/wiki/Hydrogen_spectral_series are what scientists are measuring when trying to determine the composition of a distant planetary body. The telescope revives the light from the object by observing it, and then the scientists can break down that information to figure out what the object is made of.
Latest Answers