[
  {
    "path": ".gitignore",
    "content": ".DS_Store\nnode_modules\n*.sock\nnode_modules\nout.png\n"
  },
  {
    "path": ".npmignore",
    "content": "support\ntest\nexamples\n*.sock\n"
  },
  {
    "path": "History.md",
    "content": "\n0.0.1 / 2011-12-18 \n==================\n\n  * Initial release\n"
  },
  {
    "path": "Makefile",
    "content": "\nbenchmark:\n\t@node benchmarks\n\n.PHONY: benchmarks"
  },
  {
    "path": "Readme.md",
    "content": "\n# Palette\n\n  Image color palette extraction with node-canvas for [node.js](http://nodejs.org)\n\n  ![image color palette example](http://f.cl.ly/items/3i0v0u251O3D0M020e20/Grab.png)\n\n## Installation\n\n```\n$ npm install palette\n```\n\n  *Note:* Palette's dependency, [node-canvas](https://github.com/Automattic/node-canvas), requires that Cairo be installed. Please see the [installation guide](https://github.com/Automattic/node-canvas#installation) for node-canvas for further details.\n\n## API\n\n Palette's public API consists of a single function, the one returned by `require()`. This function accepts the `canvas` you wish to compute a color palette for, and an optional number of samples defaulting to `5`.\n\n The following example is taken from the `./test` script, showing you how you may re-draw the palette onto the original canvas, however it is of course possible to save these values in a database etc.\n\n```js\nvar colors = palette(canvas, 10);\ncolors.forEach(function(color){\n  var r = color[0]\n    , g = color[1]\n    , b = color[2]\n    , val = r << 16 | g << 8 | b\n    , str = '#' + val.toString(16);\n\n  ctx.fillStyle = str;\n  ctx.fillRect(x += 31, canvas.height - 40, 30, 30);\n});\n```\n\n## Running the examples\n\n```\n$ ./test examples/cat.jpg && open /tmp/out.png\n```\n\n## Full example\n\n  This is the contents of `./test`. The means of loading the image data and drawing it to the Canvas is up to you, they could be from a database, the file system, fetched from the web, however here we simply use `img.src = path`.\n\n```js\n#!/usr/bin/env node\n\nvar palette = require('./')\n  , fs = require('fs')\n  , Canvas = require('canvas')\n  , Image = Canvas.Image\n  , canvas = new Canvas\n  , ctx = canvas.getContext('2d')\n  , path = process.argv[2]\n  , out = '/tmp/out.png';\n\nif (!path) {\n  console.error('Usage: test <image>');\n  process.exit(1);\n}\n\nvar img = new Image;\n\nimg.onload = function(){\n  canvas.width = img.width;\n  canvas.height = img.height + 50;\n  ctx.fillStyle = 'white';\n  ctx.fillRect(0, 0, canvas.width, canvas.height);\n  ctx.drawImage(img, 0, 0);\n  paintPalette();\n  save();\n};\n\nimg.src = path;\n\nfunction paintPalette() {\n  var x = 0;\n  var colors = palette(canvas);\n  colors.forEach(function(color){\n    var r = color[0]\n      , g = color[1]\n      , b = color[2]\n      , val = r << 16 | g << 8 | b\n      , str = '#' + val.toString(16);\n\n    ctx.fillStyle = str;\n    ctx.fillRect(x += 31, canvas.height - 40, 30, 30);\n  });\n}\n\nfunction save() {\n  fs.writeFile(out, canvas.toBuffer(), function(err){\n    if (err) throw err;\n    console.log('saved %s', out);\n  });\n}\n```\n\n## Booyah\n\n ![learnboost](http://f.cl.ly/items/3K3C1Z1006083Q00231q/Grab.png)\n\n## License\n\n(The MIT License)\n\nCopyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "benchmarks/index.js",
    "content": "\nvar vbench = require('vbench')\n  , palette = require('../')\n  , Canvas = require('canvas')\n  , Image = Canvas.Image\n  , canvas = new Canvas\n  , ctx = canvas.getContext('2d')\n  , suite = vbench.createSuite();\n\nvar img = new Image;\n\nimg.onload = function(){\n  canvas.width = img.width;\n  canvas.height = img.height;\n  ctx.fillStyle = 'white';\n  ctx.fillRect(0, 0, canvas.width, canvas.height);\n  ctx.drawImage(img, 0, 0);\n};\n\nimg.src = __dirname + '/../examples/cat.jpg';\n\nsuite.bench('5 swatches', function(next){\n  palette(canvas, 5);\n  next();\n});\n\nsuite.bench('10 swatches', function(next){\n  palette(canvas, 10);\n  next();\n});\n\nsuite.bench('20 swatches', function(next){\n  palette(canvas, 20);\n  next();\n});\n\nsuite.run();"
  },
  {
    "path": "index.js",
    "content": "\nmodule.exports = require('./lib/palette');"
  },
  {
    "path": "lib/palette.js",
    "content": "\n/*!\n * palette\n * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\n\nvar quantize = require('./quantize');\n\n/**\n * Expose `palette`.\n */\n\nmodule.exports = palette;\n\n/**\n * Library version.\n */\n\nexports.version = '0.0.1';\n\n/**\n * Return the color palette for the given `canvas`\n * consisting of `n` RGB color values, defaulting to 5.\n *\n * @param {Canvas} canvas\n * @param {Number} n\n * @return {Array}\n * @api public\n */\n\nfunction palette(canvas, n) {\n  var ctx = canvas.getContext('2d')\n    , imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)\n    , data = imageData.data\n    , len = data.length\n    , n = n || 5\n    , arr = [];\n\n  for (var i = 0; i < len; i += 4) {\n    // semi-transparent\n    if (data[i + 3] < 0xaa) continue;\n    // TODO: skip stark white\n    arr.push([data[i], data[i + 1], data[i + 2]]);\n  }\n\n  return quantize(arr, n).palette();\n}\n"
  },
  {
    "path": "lib/quantize.js",
    "content": "/*! \n * quantize.js Copyright 2008 Nick Rabinowitz.\n * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php\n */\n\n// fill out a couple protovis dependencies\n/*!\n * Block below copied from Protovis: http://mbostock.github.com/protovis/\n * Copyright 2010 Stanford Visualization Group\n * Licensed under the BSD License: http://www.opensource.org/licenses/bsd-license.php\n */\n\nmodule.exports = quantize;\n\nvar pv = {\n    map: function(array, f) {\n      var o = {};\n      return f\n          ? array.map(function(d, i) { o.index = i; return f.call(o, d); })\n          : array.slice();\n    },\n    naturalOrder: function(a, b) {\n        return (a < b) ? -1 : ((a > b) ? 1 : 0);\n    },\n    sum: function(array, f) {\n      var o = {};\n      return array.reduce(f\n          ? function(p, d, i) { o.index = i; return p + f.call(o, d); }\n          : function(p, d) { return p + d; }, 0);\n    },\n    max: function(array, f) {\n      return Math.max.apply(null, f ? pv.map(array, f) : array);\n    }\n}\n\nvar sigbits = 5,\n  rshift = 8 - sigbits,\n  maxIterations = 1000,\n  fractByPopulations = 0.75;\n\n// get reduced-space color index for a pixel\nfunction getColorIndex(r, g, b) {\n    return (r << (2 * sigbits)) + (g << sigbits) + b;\n}\n\n// Simple priority queue\nfunction PQueue(comparator) {\n    var contents = [],\n        sorted = false;\n    \n    function sort() {\n        contents.sort(comparator);\n        sorted = true;\n    }\n    \n    return {\n        push: function(o) {\n            contents.push(o);\n            sorted = false;\n        },\n        peek: function(index) {\n            if (!sorted) sort();\n            if (index===undefined) index = contents.length - 1;\n            return contents[index];\n        },\n        pop: function() {\n            if (!sorted) sort();\n            return contents.pop();\n        },\n        size: function() {\n            return contents.length;\n        },\n        map: function(f) {\n            return contents.map(f);\n        },\n        debug: function() {\n            if (!sorted) sort();\n            return contents;\n        }\n    };\n}\n\n// 3d color space box\nfunction VBox(r1, r2, g1, g2, b1, b2, histo) {\n    var vbox = this;\n    vbox.r1 = r1;\n    vbox.r2 = r2;\n    vbox.g1 = g1;\n    vbox.g2 = g2;\n    vbox.b1 = b1;\n    vbox.b2 = b2;\n    vbox.histo = histo;\n}\nVBox.prototype = {\n    volume: function(force) {\n        var vbox = this;\n        if (!vbox._volume || force) {\n            vbox._volume = ((vbox.r2 - vbox.r1 + 1) * (vbox.g2 - vbox.g1 + 1) * (vbox.b2 - vbox.b1 + 1));\n        }\n        return vbox._volume;\n    },\n    count: function(force) {\n        var vbox = this,\n            histo = vbox.histo;\n        if (!vbox._count_set || force) {\n            var npix = 0,\n                i, j, k;\n            for (i = vbox.r1; i <= vbox.r2; i++) {\n                for (j = vbox.g1; j <= vbox.g2; j++) {\n                    for (k = vbox.b1; k <= vbox.b2; k++) {\n                         index = getColorIndex(i,j,k);\n                         npix += (histo[index] || 0);\n                    }\n                }\n            }\n            vbox._count = npix;\n            vbox._count_set = true;\n        }\n        return vbox._count;\n    },\n    copy: function() {\n        var vbox = this;\n        return new VBox(vbox.r1, vbox.r2, vbox.g1, vbox.g2, vbox.b1, vbox.b2, vbox.histo);\n    },\n    avg: function(force) {\n        var vbox = this,\n            histo = vbox.histo;\n        if (!vbox._avg || force) {\n            var ntot = 0,\n                mult = 1 << (8 - sigbits),\n                rsum = 0,\n                gsum = 0,\n                bsum = 0,\n                hval,\n                i, j, k, histoindex;\n            for (i = vbox.r1; i <= vbox.r2; i++) {\n                for (j = vbox.g1; j <= vbox.g2; j++) {\n                    for (k = vbox.b1; k <= vbox.b2; k++) {\n                         histoindex = getColorIndex(i,j,k);\n                         hval = histo[histoindex] || 0;\n                         ntot += hval;\n                         rsum += (hval * (i + 0.5) * mult);\n                         gsum += (hval * (j + 0.5) * mult);\n                         bsum += (hval * (k + 0.5) * mult);\n                    }\n                }\n            }\n            if (ntot) {\n                vbox._avg = [~~(rsum/ntot), ~~(gsum/ntot), ~~(bsum/ntot)];\n            } else {\n//                    console.log('empty box');\n                vbox._avg = [\n                    ~~(mult * (vbox.r1 + vbox.r2 + 1) / 2),\n                    ~~(mult * (vbox.g1 + vbox.g2 + 1) / 2),\n                    ~~(mult * (vbox.b1 + vbox.b2 + 1) / 2)\n                ];\n            }\n        }\n        return vbox._avg;\n    },\n    contains: function(pixel) {\n        var vbox = this,\n            rval = pixel[0] >> rshift;\n            gval = pixel[1] >> rshift;\n            bval = pixel[2] >> rshift;\n        return (rval >= vbox.r1 && rval <= vbox.r2 &&\n                gval >= vbox.g1 && rval <= vbox.g2 &&\n                bval >= vbox.b1 && rval <= vbox.b2);\n    }\n};\n\n// Color map\nfunction CMap() {\n    this.vboxes = new PQueue(function(a,b) { \n        return pv.naturalOrder(\n            a.vbox.count()*a.vbox.volume(), \n            b.vbox.count()*b.vbox.volume()\n        ) \n    });;\n}\nCMap.prototype = {\n    push: function(vbox) {\n        this.vboxes.push({\n            vbox: vbox,\n            color: vbox.avg()\n        });\n    },\n    palette: function() {\n        return this.vboxes.map(function(vb) { return vb.color });\n    },\n    size: function() {\n        return this.vboxes.size();\n    },\n    map: function(color) {\n        var vboxes = this.vboxes;\n        for (var i=0; i<vboxes.size(); i++) {\n            if (vboxes.peek(i).vbox.contains(color)) {\n                return vboxes.peek(i).color;\n            }\n        }\n        return this.nearest(color);\n    },\n    nearest: function(color) {\n        var vboxes = this.vboxes,\n            d1, d2, pColor;\n        for (var i=0; i<vboxes.size(); i++) {\n            d2 = Math.sqrt(\n                Math.pow(color[0] - vboxes.peek(i).color[0], 2) +\n                Math.pow(color[1] - vboxes.peek(i).color[1], 2) +\n                Math.pow(color[1] - vboxes.peek(i).color[1], 2)\n            );\n            if (d2 < d1 || d1 === undefined) {\n                d1 = d2;\n                pColor = vboxes.peek(i).color;\n            }\n        }\n        return pColor;\n    },\n    forcebw: function() {\n        // XXX: won't  work yet\n        var vboxes = this.vboxes;\n        vboxes.sort(function(a,b) { return pv.naturalOrder(pv.sum(a.color), pv.sum(b.color) )});\n        \n        // force darkest color to black if everything < 5\n        var lowest = vboxes[0].color;\n        if (lowest[0] < 5 && lowest[1] < 5 && lowest[2] < 5)\n            vboxes[0].color = [0,0,0];\n        \n        // force lightest color to white if everything > 251\n        var idx = vboxes.length-1,\n            highest = vboxes[idx].color;\n        if (highest[0] > 251 && highest[1] > 251 && highest[2] > 251)\n            vboxes[idx].color = [255,255,255];\n    }\n};\n\n// histo (1-d array, giving the number of pixels in\n// each quantized region of color space), or null on error\nfunction getHisto(pixels) {\n    var histosize = 1 << (3 * sigbits),\n        histo = new Array(histosize),\n        index, rval, gval, bval;\n    pixels.forEach(function(pixel) {\n        rval = pixel[0] >> rshift;\n        gval = pixel[1] >> rshift;\n        bval = pixel[2] >> rshift;\n        index = getColorIndex(rval, gval, bval);\n        histo[index] = (histo[index] || 0) + 1;\n    });\n    return histo;\n}\n\nfunction vboxFromPixels(pixels, histo) {\n    var rmin=1000000, rmax=0, \n        gmin=1000000, gmax=0, \n        bmin=1000000, bmax=0, \n        rval, gval, bval;\n    // find min/max\n    pixels.forEach(function(pixel) {\n        rval = pixel[0] >> rshift;\n        gval = pixel[1] >> rshift;\n        bval = pixel[2] >> rshift;\n        if (rval < rmin) rmin = rval;\n        else if (rval > rmax) rmax = rval;\n        if (gval < gmin) gmin = gval;\n        else if (gval > gmax) gmax = gval;\n        if (bval < bmin) bmin = bval;\n        else if (bval > bmax)  bmax = bval;\n    });\n    return new VBox(rmin, rmax, gmin, gmax, bmin, bmax, histo);\n}\n\nfunction medianCutApply(histo, vbox) {\n    if (!vbox.count()) return;\n    \n    var rw = vbox.r2 - vbox.r1 + 1,\n        gw = vbox.g2 - vbox.g1 + 1,\n        bw = vbox.b2 - vbox.b1 + 1,\n        maxw = pv.max([rw, gw, bw]);\n    // only one pixel, no split\n    if (vbox.count() == 1) {\n        return [vbox.copy()]\n    }\n    /* Find the partial sum arrays along the selected axis. */\n    var total = 0,\n        partialsum = [],\n        lookaheadsum = [],\n        i, j, k, sum, index;\n    if (maxw == rw) {\n        for (i = vbox.r1; i <= vbox.r2; i++) {\n            sum = 0;\n            for (j = vbox.g1; j <= vbox.g2; j++) {\n                for (k = vbox.b1; k <= vbox.b2; k++) {\n                    index = getColorIndex(i,j,k);\n                    sum += (histo[index] || 0);\n                }\n            }\n            total += sum;\n            partialsum[i] = total;\n        }\n    }\n    else if (maxw == gw) {\n        for (i = vbox.g1; i <= vbox.g2; i++) {\n            sum = 0;\n            for (j = vbox.r1; j <= vbox.r2; j++) {\n                for (k = vbox.b1; k <= vbox.b2; k++) {\n                    index = getColorIndex(j,i,k);\n                    sum += (histo[index] || 0);\n                }\n            }\n            total += sum;\n            partialsum[i] = total;\n        }\n    }\n    else {  /* maxw == bw */\n        for (i = vbox.b1; i <= vbox.b2; i++) {\n            sum = 0;\n            for (j = vbox.r1; j <= vbox.r2; j++) {\n                for (k = vbox.g1; k <= vbox.g2; k++) {\n                    index = getColorIndex(j,k,i);\n                    sum += (histo[index] || 0);\n                }\n            }\n            total += sum;\n            partialsum[i] = total;\n        }\n    }\n    partialsum.forEach(function(d,i) { \n        lookaheadsum[i] = total-d \n    });\n    function doCut(color) {\n        var dim1 = color + '1',\n            dim2 = color + '2', \n            left, right, vbox1, vbox2, d2, count2=0;\n        for (i = vbox[dim1]; i <= vbox[dim2]; i++) {\n            if (partialsum[i] > total / 2) {\n                vbox1 = vbox.copy();\n                vbox2 = vbox.copy();\n                left = i - vbox[dim1];\n                right = vbox[dim2] - i;\n                if (left <= right)\n                    d2 = Math.min(vbox[dim2] - 1, ~~(i + right / 2));\n                else d2 = Math.max(vbox[dim1], ~~(i - 1 - left / 2));\n                // avoid 0-count boxes\n                while (!partialsum[d2]) d2++;\n                count2 = lookaheadsum[d2];\n                while (!count2 && partialsum[d2-1]) count2 = lookaheadsum[--d2];\n                // set dimensions\n                vbox1[dim2] = d2;\n                vbox2[dim1] = vbox1[dim2] + 1;\n//                    console.log('vbox counts:', vbox.count(), vbox1.count(), vbox2.count());\n                return [vbox1, vbox2];\n            }\n        }\n    \n    }\n    // determine the cut planes\n    return maxw == rw ? doCut('r') :\n        maxw == gw ? doCut('g') :\n        doCut('b');\n}\n\nfunction quantize(pixels, maxcolors) {\n    // short-circuit\n    if (!pixels.length || maxcolors < 2 || maxcolors > 256) {\n//            console.log('wrong number of maxcolors');\n        return false;\n    }\n    \n    // XXX: check color content and convert to grayscale if insufficient\n    \n    var histo = getHisto(pixels),\n        histosize = 1 << (3 * sigbits);\n    \n    // check that we aren't below maxcolors already\n    var nColors = 0;\n    histo.forEach(function() { nColors++ });\n    if (nColors <= maxcolors) {\n        // XXX: generate the new colors from the histo and return\n    }\n    \n    // get the beginning vbox from the colors\n    var vbox = vboxFromPixels(pixels, histo),\n        pq = new PQueue(function(a,b) { return pv.naturalOrder(a.count(), b.count()) });\n    pq.push(vbox);\n    \n    // inner function to do the iteration\n    function iter(lh, target) {\n        var ncolors = 1,\n            niters = 0,\n            vbox;\n        while (niters < maxIterations) {\n            vbox = lh.pop();\n            if (!vbox.count())  { /* just put it back */\n                lh.push(vbox);\n                niters++;\n                continue;\n            }\n            // do the cut\n            var vboxes = medianCutApply(histo, vbox),\n                vbox1 = vboxes[0],\n                vbox2 = vboxes[1];\n                \n            if (!vbox1) {\n//                    console.log(\"vbox1 not defined; shouldn't happen!\");\n                return;\n            }\n            lh.push(vbox1);\n            if (vbox2) {  /* vbox2 can be null */\n                lh.push(vbox2);\n                ncolors++;\n            }\n            if (ncolors >= target) return;\n            if (niters++ > maxIterations) {\n//                    console.log(\"infinite loop; perhaps too few pixels!\");\n                return;\n            }\n        }\n    }\n    \n    // first set of colors, sorted by population\n    iter(pq, fractByPopulations * maxcolors);\n    // console.log(pq.size(), pq.debug().length, pq.debug().slice());\n    \n    // Re-sort by the product of pixel occupancy times the size in color space.\n    var pq2 = new PQueue(function(a,b) { \n        return pv.naturalOrder(a.count()*a.volume(), b.count()*b.volume()) \n    });\n    while (pq.size()) {\n        pq2.push(pq.pop());\n    }\n    \n    // next set - generate the median cuts using the (npix * vol) sorting.\n    iter(pq2, maxcolors - pq2.size());\n    \n    // calculate the actual colors\n    var cmap = new CMap();\n    while (pq2.size()) {\n        cmap.push(pq2.pop());\n    }\n    \n    return cmap;\n}"
  },
  {
    "path": "npm-shrinkwrap.json",
    "content": "{\n  \"name\": \"palette\",\n  \"version\": \"0.1.0\",\n  \"dependencies\": {\n    \"vbench\": {\n      \"version\": \"0.1.0\",\n      \"from\": \"vbench@0.1.x\",\n      \"dependencies\": {\n        \"canvas\": {\n          \"version\": \"1.5.0\",\n          \"from\": \"canvas@^1.5.0\"\n        }\n      }\n    }\n  }\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"palette\",\n  \"version\": \"0.1.0\",\n  \"description\": \"Image color palette with node-canvas\",\n  \"keywords\": [\n    \"palette\",\n    \"color\",\n    \"image\",\n    \"sample\",\n    \"canvas\",\n    \"photo\"\n  ],\n  \"author\": \"TJ Holowaychuk <tj@vision-media.ca>\",\n  \"devDependencies\": {\n    \"canvas\": \"^1.5.0\",\n    \"vbench\": \"0.1.x\"\n  },\n  \"main\": \"index\",\n  \"engines\": {\n    \"node\": \"2.x.x\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"https://github.com/visionmedia/palette.git\"\n  },\n  \"dependencies\": {\n    \"canvas\": \"^1.5.0\"\n  }\n}\n"
  },
  {
    "path": "test",
    "content": "#!/usr/bin/env node\n\nvar palette = require('./')\n  , fs = require('fs')\n  , Canvas = require('canvas')\n  , Image = Canvas.Image\n  , canvas = new Canvas\n  , ctx = canvas.getContext('2d')\n  , path = process.argv[2]\n  , n = ~~process.argv[3] || 5\n  , out = '/tmp/out.png';\n\nif (!path) {\n  console.error('Usage: test <image> [colors]');\n  process.exit(1);\n}\n\nvar img = new Image;\n\nimg.onload = function(){\n  canvas.width = img.width;\n  canvas.height = img.height + 50;\n  ctx.fillStyle = 'white';\n  ctx.fillRect(0, 0, canvas.width, canvas.height);\n  ctx.drawImage(img, 0, 0);\n  paintPalette();\n  save();\n};\n\nimg.src = path;\n\nfunction paintPalette() {\n  var x = 0;\n  var colors = palette(canvas, n);\n  colors.forEach(function(color){\n    var r = color[0]\n      , g = color[1]\n      , b = color[2]\n      , val = r << 16 | g << 8 | b\n      , str = '#' + val.toString(16);\n\n    ctx.fillStyle = str;\n    ctx.fillRect(x += 31, canvas.height - 40, 30, 30);\n  });\n}\n\nfunction save() {\n  fs.writeFile(out, canvas.toBuffer(), function(err){\n    if (err) throw err;\n    console.log('saved %s', out);\n  });\n}"
  }
]