A library for retrieving a colorpalette from an image. The library is based on an improved version of the fast data-clustering algorithm k-means for use with images.
- Works with every image that can be used with the HTML5 canvas element.
- Supports every browser that has support for the canvas element.
Improvements/Changes from the original algorithm include, but is not limited, to:
- If a datapoint (color) is too far away from the clusters it will be added to the set of clusters.
- With the possible addition of clusters as of 1. and a user specified input 'numberOfClusters' there's a greater dataset to pick our final palette from.
- With a possible greater dataset as of 1. and 2. the algorithm will trim the dataset down to the final palette based on the population of the clusters.
Include the file somewhere in your HTML document:
<script type="text/javascript" src="Colormeans.js"></script>
In your javascript file:
var colorMeans = new Colormeans();
var image = document.getElementById('#imageID');
var palette = colorMeans.getPalette(image, 4);
//Returns the palette as an array with RGB-values.
//Ex: [[84,121,128], [69, 173, 168], [157, 224, 169], [229, 255, 194]]
or alternatively:
var colorMeans = new Colormeans();
var image = new Image();
image.src = "http://www.somewebsite.com/image.jpg"
image.onload = function() {
var palette = colorMeans.getPalette(this, 4);
}
var colorMeans = new Colormeans();
var image = document.getElementById('#imageID');
var options = {
numberOfClusters:10, //The number of initial clusters.
minimumDifference: 0.1, //The minimum difference when calculating centerpoints
clusterThreshold:25 //The minimum euclidean-distance between initial clusters.
}
var palette = colorMeans.getPalette(image, 4, options);
- Coming soon! Meanwhile, look at the jsdoc within colormeans.js
Features I or contributers will hopefully soon implement.
- Further improve the algorithm for retrieving the initial clusters.
- Remove dependency of the canvas element to further improve loading time.
- Further improve the algorithm for adding extra clusters when necessary.
- Add support for node.js
- Add resize option for larger images.
- Add option to calculate euclidean distance with triangle inequality for possible speed improvement.
- Minified version.
- Will fail when the number of colors to be retrieved from an image exceeds the number of unique colors in the image.
- The canvas may be tained in Chrome if you run the script locally without a web server.
- (Workaround) Run the script on a local server.
To Anna Päärni and Christian Fischer for discussing the algorithm and possible improvements.
Copyright (c) 2015 Simon Johansson