Use COLOURlovers top patterns as random background for you web application

1 minute read

A couple of days ago I stumbled on the COLOURlovers site from the Android Play Store. This site provides a whole lot of beautiful patterns, colors and palettes and has tools to create them. I downloaded an app that randomly changes the background for my tablet, and really like the result. They really have a very beautiful set of patterns to use as a background:

Browse Patterns __ COLOURlovers.png

When browsing their site I noticed they had an API. From this API you can search through all the content on their site, and retrieve links to the patterns:

For instance a GET on gives the following result:

"dateCreated":"2008-03-03 13:24:06",

As a simple experiment I decided to create a web page that uses this API to set a random background based on the top patterns from COLOURlovers. The first problem though was that this API doesn’t support CORS, so I couldn’t directly access the API. Luckily though, this API supports JSONP, so with JSONP I can call this API directly from the web app. Now what do you need to do to get all this working? Not that much, first thing to do, is get the url of a random pattern and set it as the background:

    // We retrieve a set of the 50 top backgrounds
    $.getJSON('', function(data) {

        // we randomly select one of the elements
        var element = data[Math.floor(Math.random()*50)];

        // we update the body background with the url
            "background-image": "url("+ element.imageUrl +")"

This however, doesn’t make the pattern repeat itself. To do this we need to add the following css directives:

        body {
            background-repeat: repeat;

And that’s it, you’ve now got a random repeating background. I created a simple example which you can see here:

Random background from Colourlovers.png