Pyrotechnics, the art of firework creating, probably originated back in 1st century China when black powder was invented by mixing charcoal, sulphur and potassium nitrate. However, up until the 19th century the science of fireworks remained pretty unchanged and fairly monochrome with only yellow or orange flashes possible to accompany the bangs.
Then, with the discovery of more and more elements came a move towards experimenting with producing a greater range of coloured flames. First reds and greens were developed, followed by blues and purples.
The dazzling displays of colour can be seen as a demonstration of the first law of thermodynamics: Nature conserves energy.
Energy released by the burning of the fuel (charcoal and sulphur) in the oxidiser (potassium nitrate) is transferred to the atoms of the colorant chemicals (e.g. strontium, copper, barium, sodium). That gets some of the electrons orbiting the nucleus rather excited and they venture further away from the centre of the atom.
As they take a chill pill though the electrons move back to where they were originally, but that excess energy they received isn’t destroyed – it gets released as a burst of light corresponding to the initial amount of energy that was transferred. So you actually see the colours as a firework is cooling down.
Getting the electrons of different colorant chemicals excited takes different amounts of energy. To tickle the fancy of strontium and see a red flash involves a different amount of energy compared to the blushing burst of green from that of a barium atom, or blue of a cocky copper.
Pyro technicians, like kids with coloured crayons, can combine the colorant chemicals to make even more colours: A mixture of strontium and copper gives purple.
Reaction time is slowed for a longer-lasting firework effect by increasing the size of the grains of chemicals that have been added. The final composition of fuel, oxidising agent, and colorant chemical has to be considered carefully in order for the firework to be both stable in storage and free from impurities to give the spectators the pure, explosive colours that’ll accompany their ‘oohs’ and ‘aahs’.