Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhasbach committed May 16, 2015
0 parents commit 062178d
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea/
126 changes: 126 additions & 0 deletions lib/extractColors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
var _ = require('lodash'),
path = require('path'),
async = require('async'),
imageJS = require('imagejs'),
Pageres = require('pageres');

var extractColors = module.exports = {
all: function(opt, cb) {
if (!_.isFunction(cb)) {
throw new TypeError('cb must be a function');
}
if (!_.isObject(opt)) {
cb(new TypeError('opt must be an object'));
return extractColors;
}
if (!_.isArray(opt.pages)) {
cb(new TypeError('opt.pages must be an array'));
return extractColors;
}

var colors = [],
pageres = new Pageres(opt.pageresOpt);

_.each(opt.pages, function(page) {
pageres.src.apply(pageres, page);
});

pageres.run(function(err, imgStreams) {
if (err) {
cb(err);
return;
}

async.each(imgStreams, function(imgStream, done) {
var pageColors = [],
filename = imgStream.filename,
image = new imageJS.Bitmap();

image.read(imgStream, filename.substr(filename.length - 3, filename.length - 1) === 'png' ? 2 : 1)
.then(function() {
_.times(image.width, function(x) {
_.times(image.height, function(y) {
pageColors.push(image.getPixel(x, y));
});
});

colors.push(pageColors);

if (_.isString(opt.dest)) {
image.writeFile(path.join(opt.dest, imgStream.filename), {quality: opt.quality})
.then(function() {
done();
}).error(done);
}
else {
done();
}
}).error(done);
}, function(err) {
cb(err, colors);
});
});

return extractColors;
},
unique: function(opt, cb) {
if (!_.isFunction(cb)) {
throw new TypeError('cb must be a function');
}

extractColors.all(opt, function(err, colors) {
cb(err, _.unique(colors));
});

return extractColors;
},
frequent: function(opt, cb) {
if (!_.isFunction(cb)) {
throw new TypeError('cb must be a function');
}
if (!_.isObject(opt)) {
cb(new TypeError('opt must be an object'));
return extractColors;
}
if (!_.isNumber(opt.amount)) {
cb(new TypeError('opt.amount must be a number'));
return extractColors;
}

var freqColors = [];

extractColors.all(opt, function(err, pageColors) {
_.each(pageColors, function(colors) {
if (err) {
cb(err);
return;
}

var pageFreqColors = [],
freqDist = _.chain(colors)
.countBy(function(color) { return JSON.stringify(color); })
.reduce(function(arr, freq, color) {
var obj = {};

obj[color] = freq;

arr.push(obj);

return arr;
}, [])
.sortBy(function(obj) { return -obj[_.keys(obj)[0]]; })
.value();

_.times(opt.amount, function(i) {
pageFreqColors.push(JSON.parse(_.keys(freqDist[i])[0]));
});

freqColors.push(pageFreqColors);
});

cb(err, freqColors);
});

return extractColors;
}
};
19 changes: 19 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Matthew Hasbach

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "website-color-extractor",
"version": "1.0.0",
"description": "Extract colors from websites",
"license": "MIT",
"homepage": "https://github.com/mjhasbach/node-website-color-extractor",
"dependencies": {
"async": "~0.9.0",
"imagejs": "~0.0.9",
"lodash": "~3.8.0",
"pageres": "~1.3.0"
},
"keywords": [
"web",
"page",
"site",
"website",
"screenshot",
"extract",
"color"
],
"repository": {
"type": "git",
"url": "https://github.com/mjhasbach/node-website-color-extractor.git"
},
"author": {
"name": "Matthew Hasbach",
"email": "hasbach.git@gmail.com",
"url": "https://github.com/mjhasbach"
},
"main": "lib/extractColors.js",
"directories": {
"lib": "lib"
}
}
112 changes: 112 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# node-website-color-extractor

## Description
This library takes screenshots of websites and extracts all of their colors or most frequently occurring colors in rgb object format. Another library like [TinyColor](https://github.com/bgrins/TinyColor) can be used to convert to other formats.

## API
#### extractColors.all(```opt```, ```cb```)

Extract the color of every pixel from screenshots of websites

* object `opt` - An options object
* array{array{string | array{string} | object}} `pages` - An array of arrays of [pageres.src](https://github.com/sindresorhus/pageres#pageressrcurl-sizes-options) arguments
* object `pageresOpt` - (Optional) [pageres options](https://github.com/sindresorhus/pageres#pageresoptions)
* string `dest` - (Optional) A directory in which to save screenshots of the websites from which colors are extracted. Screenshots will not be saved if this property is omitted.
* number `quality` - (Optional) A number between 0 and 100 which determines the JPEG encoding quality if `dest` is provided and a [format](https://github.com/sindresorhus/pageres#format) of "jpg" was provided in `pageresOpt` or `pages`. Defaults to 90.
* function(null|object `err`, array{array{object{number}}} `colors`) `cb` - A function to be executed after the colors are extracted

__Example__

```
extractColors.all(
{
pages: [['google.com', ['650x650']]],
pageresOpt: {format: 'jpg'},
dest: path.join(__dirname, 'images'),
quality: 100
},
function(err, colors) {
if (err){ throw err; }
console.log(colors[0].length);
}
);
```

#### extractColors.unique(```opt```, ```cb```)

Extract the unique colors from screenshots of websites

* object `opt` - An options object
* array{array{string | array{string} | object}} `pages` - An array of arrays of [pageres.src](https://github.com/sindresorhus/pageres#pageressrcurl-sizes-options) arguments
* object `pageresOpt` - (Optional) [pageres options](https://github.com/sindresorhus/pageres#pageresoptions)
* string `dest` - (Optional) A directory in which to save screenshots of the websites from which colors are extracted. Screenshots will not be saved if this property is omitted.
* number `quality` - (Optional) A number between 0 and 100 which determines the JPEG encoding quality if `dest` is provided and a [format](https://github.com/sindresorhus/pageres#format) of "jpg" was provided in `pageresOpt` or `pages`. Defaults to 90.
* function(null|object `err`, array{array{object{number}}} `colors`) `cb` - A function to be executed after the colors are extracted

__Example__

```
extractColors.unique(
{
pages: [[
'gamespot.com',
['1367x768'],
{hide: ['.kubrick-content', '.media-figure']}
]],
pageresOpt: {
delay: 2,
userAgent: 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36'
},
dest: path.join(__dirname, 'images')
},
function(err, colors) {
if (err) { throw err; }
console.log(colors[0].length);
}
);
```

#### extractColors.frequent(```opt```, ```cb```)

Extract the most frequently occurring colors from screenshots of websites

* object `opt` - An options object
* number `amount` - The amount of colors to extract
* array{array{string | array{string} | object}} `pages` - An array of arrays of [pageres.src](https://github.com/sindresorhus/pageres#pageressrcurl-sizes-options) arguments
* object `pageresOpt` - (Optional) [pageres options](https://github.com/sindresorhus/pageres#pageresoptions)
* string `dest` - (Optional) A directory in which to save screenshots of the websites from which colors are extracted. Screenshots will not be saved if this property is omitted.
* number `quality` - (Optional) A number between 0 and 100 which determines the JPEG encoding quality if `dest` is provided and a [format](https://github.com/sindresorhus/pageres#format) of "jpg" was provided in `pageresOpt` or `pages`. Defaults to 90.
* function(null|object `err`, array{array{object{number}}} `colors`) `cb` - A function to be executed after the colors are extracted. `colors[n]` are ordered by frequency.

__Example__

```
extractColors.frequent(
{
amount: 10,
pages: [
['stackexchange.com', ['1920x1080'], {crop: true}],
['stackoverflow.com', ['1920x1080'], {crop: true}],
]
},
function(err, colors) {
if (err) { throw err; }
console.log(colors[0]);
console.log(colors[1]);
}
);
```

#### Note

All methods return `extractColors` for chaining.

## Installation
#### Npm
```
npm install website-color-extractor
```

0 comments on commit 062178d

Please sign in to comment.