Make colorCount arg optional in getPalette function.

This commit is contained in:
Lokesh Dhakar
2013-06-23 22:32:28 -07:00
parent 0f590b91ee
commit 0adb00daf1
3 changed files with 16 additions and 9 deletions

View File

@@ -17,14 +17,14 @@ getColor(sourceImage[, quality])
returns {r: num, g: num, b: num}
```
###Build a color palette from an image
###Build a 8 color palette from an image
```js
var colorThief = new ColorThief();
colorThief.getPalette(sourceImage);
colorThief.getPalette(sourceImage, 8);
```
```js
getPalette(sourceImage, colorCount[, quality])
getPalette(sourceImage[, colorCount, quality])
returns [ [num, num, num], [num, num, num], ... ]
```

View File

@@ -30,8 +30,6 @@ $(document).ready(function () {
var colorThief = new ColorThief();
var PALETTE_COLOR_COUNT = 10;
// Run Color Thief functions and display results below image.
// We also log execution time of functions for display.
var showColorsForImage = function($image, $imageSection ) {
@@ -39,7 +37,7 @@ $(document).ready(function () {
var start = Date.now();
var color = colorThief.getColor(image);
var elapsedTimeForGetColor = Date.now() - start;
var palette = colorThief.getPalette(image, PALETTE_COLOR_COUNT);
var palette = colorThief.getPalette(image);
var elapsedTimeForGetPalette = Date.now() - start + elapsedTimeForGetColor;
var colorThiefOutput = {

View File

@@ -78,22 +78,31 @@ ColorThief.prototype.getColor = function(sourceImage, quality) {
/*
* getPalette(sourceImage, colorCount[, quality])
* getPalette(sourceImage[, colorCount, quality])
* returns array[ {r: num, g: num, b: num}, {r: num, g: num, b: num}, ...]
*
* Use the median cut algorithm provided by quantize.js to cluster similar colors.
*
* Quality is an optional argument. It needs to be an integer. 0 is the highest quality settings.
* colorCount determines the size of the palette; the number of colors returned. If not set, it
* defaults to 10.
*
* BUGGY: Function does not always return the requested amount of colors. It can be +/- 2.
*
* quality is an optional argument. It needs to be an integer. 0 is the highest quality settings.
* 10 is the default. There is a trade-off between quality and speed. The bigger the number, the
* faster the palette generation but the greater the likelihood that colors will be missed.
*
* BUGGY: Function does not always return the requested amount of colors. It can be +/- 2.
*
*/
ColorThief.prototype.getPalette = function(sourceImage, colorCount, quality) {
if (typeof colorCount === 'undefined') {
colorCount = 10;
};
if (typeof quality === 'undefined') {
quality = 10;
};
// Create custom CanvasImage object
var image = new CanvasImage(sourceImage);
var imageData = image.getImageData();