Permalink
Browse files

implemented

  • Loading branch information...
1 parent fb0d38e commit 58b3661832973ccd92e8ae3cb95755a8f78e417d @tj committed Dec 18, 2011
Showing with 17 additions and 6 deletions.
  1. +16 −5 lib/palette.js
  2. +1 −1 test
View
@@ -24,18 +24,29 @@ module.exports = palette;
exports.version = '0.0.1';
/**
- * Return the color palette for the given `canvas`.
+ * Return the color palette for the given `canvas`
+ * consisting of `n` color values, defaulting to 5.
*
* @param {Canvas} canvas
+ * @param {Number} n
* @return {Array}
* @api public
*/
-function palette(canvas) {
+function palette(canvas, n) {
var ctx = canvas.getContext('2d')
, imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
, data = imageData.data
- , pixels = data.length / 4;
-
- // console.log(quantize(data, 5));
+ , len = data.length
+ , n = n || 5
+ , arr = [];
+
+ for (var i = 0; i < len; i += 4) {
+ // semi-transparent
+ if (data[i + 3] < 0xaa) continue;
+ // TODO: skip stark white
+ arr.push(data[i], data[i + 1], data[i + 2]);
+ }
+
+ return quantize(arr, n).palette();
}
View
2 test
@@ -22,7 +22,7 @@ img.onload = function(){
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
- palette(canvas);
+ var colors = palette(canvas);
fs.writeFile(out, canvas.toBuffer(), function(err){
if (err) throw err;
console.log('saved %s', out);

0 comments on commit 58b3661

Please sign in to comment.