There’s a class of instruments called spectrographs which put the light through a prism or grating to separate the starlight into a rainbow. Here’s the clever part: rather than a continuous smear of colours, starlight has thin black lines missing. It looks like a fine barcode printed on top of the spectrum. These dark lines are called absorption lines, and each chemical element produces it’s own, distinctive barcode. They’re a result of quantum physics in the structure of the atom. (Please don’t ask me to explain the maths, or I’ll start to whimper.) So by studying the spectrum of a star, an astronomer can tell which chemical elements make up the star. By looking at the relative intensity of different lines on the same barcode, they can also tell how hot it is. And there’s more.
If you listen to an approaching police car or ambulance, you’ll hear the pitch of the siren change as it passes you. As the car comes towards you, the sound waves get squished together, making the pitch higher. Then as the car passes the pitch drops, because now the sound waves are being stretched out, creating a deeper note. This is called the Doppler effect. The faster the car’s moving, the bigger the effect.
The same thing happens with light. If a star is moving towards us, the light waves get compressed, and so the star looks bluer. If it’s moving away, the light waves get stretched, so the star looks redder. The amount its light gets shifted depends on its speed.
How do we know what colour the star was before the light got shifted?
You need a barcode that you know hasn’t been shifted to compare with the same barcode from the star. That makes the amount of shift very obvious. So practically all spectrographs have special lamps, called calibration lamps, to provide this.
So that’s the chemical composition of the star, the temperature, and its movement, all from a boring-looking barcode.