What causes the colors in flames?

When you gaze at a wood fire, you might notice different colors in the flames. What causes them?

If you look into a wood fire, then up in the night sky, you might see the same colors in the flames as you see in the stars. But is there a correlation between these fire colors and the colors of the stars?

The colors of stars indicate their temperatures. Blue-white Vega is hotter than red Aldebaran. Star colors stem from “black-body radiation”, the same sort of radiation you see in metal heated to red, orange, or white heat. The orange glow seen between logs in the heart of a fire is also black-body radiation

But the orange seen in the actual tongues of flame is not. Instead, the colors of flames in a wood fire are due to different substances in the flames. The bright orange of most wood flames is due to the presence of sodium, which, when heated, emits light strongly in the orange. The blue in wood flames comes from carbon and hydrogen, which emit in the blue and violet. Copper compounds make green or blue, lithium makes red.