More than a century after Rudyard Kipling offered his own explanation in the Just So Stories, scientists have revealed how the leopard got his spots.

The animals' dark, rosette-like markings, and those of other wild cats, are evolution's response to the creatures' surroundings and to whether they hunt by day or night, say researchers at Bristol University.

Cats that hunt on open, rocky ground by daylight tend to have evolved plain-coloured coats, while those that pounce from rainforest tree branches typically sport dappled fur. In each habitat, the cat's markings improve its camouflage and make it a more effective predator.

For smaller cats, fur colour can help them hide from larger carnivores.

Will Allen, a behavioural ecologist, studied the coat patterns of 35 wild cat species and compiled details of their habitats, hunting styles and when they went on the prowl.

Cats with complex and irregular markings, such as the familiar spotted leopard, were commonly found in dense, dark forests and hunted at night.

In Kipling's 1902 tale, an Ethiopian hunter paints spots on a leopard to help it blend into the "speckly, patchy-blatchy shadows" of the forest.

"Apart from the painting part, Kipling was quite right," said Allen. "The leopard got its spots from a life in forested habitats, where it made use of the trees and nocturnal hunting."

Ten cats in Allen's study had plain coats and lived in open, often barren landscapes. The sand cat is found in the arid deserts of Asia and Africa and has particularly furry feet to protect them from the scorching sands.

The plain-furred Pallas's cat melts into the treeless steppes of central Asia, while the small Andean mountain cat has a silver-grey coat that matches the rocky landscape.

In some parts of the world, jaguars and leopards are completely black, an adaptation that only seems to arise in species that live in a diverse range of habitats.

In the study, Allen asked volunteers to match the fur of different wild cats to computer-generated coat patterns that varied from plain and simple to complex and irregular markings. When Allen compared the markings across the cat family tree, he found that similar patterns emerged quickly and several times during feline evolution.

Some cats appear to have markings that are not suited to their natural stalking grounds. The cheetah, for example, has a distinctive spotted coat but lives in the sparse deserts of sub-Saharan Africa.

But the animal's impressive athleticism means it can reach more than 60 miles per hour in three seconds, and so it may rely less on camouflage than other cats.