Fork me on GitHub

colour.js

Parse, convert and manipulate colours (also known as colors) in JavaScript

colour.js is a small (4KB minified and gzipped) JavaScript library which can parse any CSS colour definition, convert the results between RGB and HSL, and perform various transformations.

For a page which makes extensive use of colour.js, have a look at The Colour Toy.

Download

Download the latest source as a tarball or a zipball:

Install

Download the zipball or tarball, put colour.min.js in your project, and link to it in in your HTML:

<head>
     ...
     <script type="text/javascript" src="path/to/colour.min.js"></script>
     ...
</head>

You knew that already, right?

Documentation

Full API documentation is in the README.

Example

var colourString = "hotpink", // maybe this came from a user preference or something
    baseColour = new Colour(colourString), // create colour object for hotpink
    shaded = baseColour.darken(0.5), // looks like this
    textColour = baseColour.contrast(); // is #111

// using jQuery syntax here, but could apply to anything:
$(".customized").css({
    "background-color": baseColour.toString(), // "#ff69b4"
    "border-color": shaded.toString(),         // "#b30059"
    "color": textColor.toString()              // "#111111"
});

Notice how each operation returns a new Colour object, leaving baseColour untouched.

License

Author

Neil de Carteret (n3dst4@gmail.com)

Tweet your followers about Colour.js

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/n3dst4/colour.js