What colour were dinosaurs and how do scientists know?

What colour was a Tyrannosaurus rex? Until recently, the honest answer was simply: nobody knows, and the colours in pictures are just made up by artists. Now, for a few dinosaurs, we actually have an answer, and it wasn’t found in drawings, but in the fossils themselves.
The one-sentence answer
Tiny grains of paint
Bird feathers and human hair contain melanosomes. These are microscopic grains that hold a pigment called melanin. The long, rod-shaped ones create black and grey colours. The short, wide ones create reds and browns.
Sometimes, these melanosomes survive in a fossil right alongside the imprint of a feather. Scientists take a tiny piece, about the size of a poppy seed, examine it under a powerful microscope, and compare the shape of the grains with the feathers of modern birds whose colours we already know.
Anchiornis with a red crest

The first dinosaur to be fully coloured in from head to tail was Anchiornis. This was a small feathered dinosaur that lived about 150 million years ago. In 2010, scientists from Yale University took 29 feather samples from different parts of its body.
The result was striking. Its body was grey, it had a reddish crest on its head, speckles on its face, and the white feathers on its wings and legs had black spots at the tips. One of the authors compared this pattern to a speckled Hamburg chicken. It’s likely that these colours helped the animals recognise each other and attract mates.
Sinosauropteryx wearing a raccoon mask

Sinosauropteryx lived in what is now China. In 2017, researchers at the University of Bristol studied three of its fossils where the pigment was exceptionally well preserved. Its back turned out to be dark, its belly light, and it had a dark stripe running across its eyes, just like a raccoon’s mask. The tail was striped: near the base, the stripes were narrow and close together, while closer to the tip they were wide and spaced further apart.
A dark back and light belly act as natural camouflage. Light falls on the animal from above, while a shadow forms underneath; these colours balance it out, making the body look flat and harder to spot. Thanks to this pattern, scientists even deduced that Sinosauropteryx lived in open areas with little plant cover. One of the researchers, Jakob Vinther, said it looked like a skinny raccoon crossed with a turkey.
The rusty-red Borealopelta
Borealopelta was found in Alberta, Canada. It was an armoured, plant-eating dinosaur about five and a half metres long, weighing roughly 1.3 tonnes. No melanosomes were found in it, but chemical analysis revealed traces of a rusty-red pigment.
The pigment was heaviest on the back, lighter on the sides, and completely absent from the belly. This is the exact same camouflage pattern seen in Sinosauropteryx. It is surprising that such a huge, armoured animal needed camouflage at all. Scientists believe this points to one thing: the predators of that time were incredibly dangerous, even for a heavily armoured tank of a dinosaur.
What about T. rex?
For the Tyrannosaurus rex, no such traces have been found yet. Fossils where pigment has survived in the skin or feathers are incredibly rare. Plus, this method can only identify black, grey, red, brown, and iridescence—you can’t detect yellow this way. So, the colours of T. rex in books and movies are still just an artist’s best guess.