mirror of
https://github.com/janishutz/color-thief.git
synced 2026-01-13 01:08:23 +00:00
Make colorCount arg optional in getPalette function.
This commit is contained in:
@@ -17,14 +17,14 @@ getColor(sourceImage[, quality])
|
|||||||
returns {r: num, g: num, b: num}
|
returns {r: num, g: num, b: num}
|
||||||
```
|
```
|
||||||
|
|
||||||
###Build a color palette from an image
|
###Build a 8 color palette from an image
|
||||||
```js
|
```js
|
||||||
var colorThief = new ColorThief();
|
var colorThief = new ColorThief();
|
||||||
colorThief.getPalette(sourceImage);
|
colorThief.getPalette(sourceImage, 8);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
getPalette(sourceImage, colorCount[, quality])
|
getPalette(sourceImage[, colorCount, quality])
|
||||||
returns [ [num, num, num], [num, num, num], ... ]
|
returns [ [num, num, num], [num, num, num], ... ]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
4
demo.js
4
demo.js
@@ -30,8 +30,6 @@ $(document).ready(function () {
|
|||||||
|
|
||||||
var colorThief = new ColorThief();
|
var colorThief = new ColorThief();
|
||||||
|
|
||||||
var PALETTE_COLOR_COUNT = 10;
|
|
||||||
|
|
||||||
// Run Color Thief functions and display results below image.
|
// Run Color Thief functions and display results below image.
|
||||||
// We also log execution time of functions for display.
|
// We also log execution time of functions for display.
|
||||||
var showColorsForImage = function($image, $imageSection ) {
|
var showColorsForImage = function($image, $imageSection ) {
|
||||||
@@ -39,7 +37,7 @@ $(document).ready(function () {
|
|||||||
var start = Date.now();
|
var start = Date.now();
|
||||||
var color = colorThief.getColor(image);
|
var color = colorThief.getColor(image);
|
||||||
var elapsedTimeForGetColor = Date.now() - start;
|
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 elapsedTimeForGetPalette = Date.now() - start + elapsedTimeForGetColor;
|
||||||
|
|
||||||
var colorThiefOutput = {
|
var colorThiefOutput = {
|
||||||
|
|||||||
@@ -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}, ...]
|
* 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.
|
* 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
|
* 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.
|
* 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) {
|
ColorThief.prototype.getPalette = function(sourceImage, colorCount, quality) {
|
||||||
|
|
||||||
|
if (typeof colorCount === 'undefined') {
|
||||||
|
colorCount = 10;
|
||||||
|
};
|
||||||
if (typeof quality === 'undefined') {
|
if (typeof quality === 'undefined') {
|
||||||
quality = 10;
|
quality = 10;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create custom CanvasImage object
|
// Create custom CanvasImage object
|
||||||
var image = new CanvasImage(sourceImage);
|
var image = new CanvasImage(sourceImage);
|
||||||
var imageData = image.getImageData();
|
var imageData = image.getImageData();
|
||||||
|
|||||||
Reference in New Issue
Block a user