Skip to content

Commit

Permalink
Merge pull request #30 from randytarampi/feat/pull-dominantColors
Browse files Browse the repository at this point in the history
Pull `dominantColor`s per #28

Closes #28 and EyalAr#255
  • Loading branch information
randytarampi authored Apr 28, 2020
2 parents 9db9c4a + 7cd33d5 commit 0df3162
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 213 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ This is a branch based off of [@kant2002/lwip](https://www.npmjs.com/package/@ka
0. [GIF](#gif)
0. [Write to file](#write-to-file)
0. [Get metadata](#get-metadata)
0. [Get dominant color](#get-dominant-color)
0. [Batch operations](#batch-operations)
0. [Copyrights](#copyrights)

Expand Down Expand Up @@ -739,6 +740,14 @@ tEXt chunks in PNG images, and will get the first tEXt chunk found with the key

`image.getMetadata()`

### Get dominant color

Get the pixel color that occurs most frequently in a picture.

`image.dominantColor(pixels_to_skip)`

0. `pixels_to_skip {Int}`: the number of pixels to skip while iterating through the image. Ex. supplying 1 will skip every other pixel, 0 will skip none. The greater the number the less accuracy the result will have.

### Batch operations

Each of the [image operations](#image-operations) above can be done as part of
Expand Down
44 changes: 43 additions & 1 deletion lib/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const judges = {
toBuffer: decree(defs.args.toBuffer),
writeFile: decree(defs.args.writeFile),
setPixel: decree(defs.args.setPixel),
getPixel: decree(defs.args.getPixel)
getPixel: decree(defs.args.getPixel),
dominantColor: decree(defs.args.dominantColor)
};

module.exports = class Image {
Expand Down Expand Up @@ -82,6 +83,47 @@ module.exports = class Image {
};
}

dominantColor () {
let dominantColor;
judges.dominantColor(
arguments,
skips => {
if (typeof skips !== 'number' || skips < 0 || parseInt(skips,10) !== skips) {
throw Error('Pass a positive integer argument for number of pixels to skip over each iteration');
}

const colorCounter = {};
for (let i = 0; i < this.width(); i++) {
for (let j = 0; j < this.height(); j+= skips+1) {
const pixel = this.__lwip.getPixel(i,j),
pixelColor = pixel[0].toString()+','+pixel[1].toString()+','+pixel[2].toString()+','+pixel[3].toString(),
occurence = colorCounter[pixelColor];
if (occurence === undefined) colorCounter[pixelColor] = 1;
else colorCounter[pixelColor]++;
}
}

let the_key = '0,0,0,0',
count = 0;
for (let key in colorCounter) {
if (colorCounter[key] > count) {
the_key = key;
count = colorCounter[key];
}
}
the_key = the_key.split(',');

dominantColor = {
r: parseInt(the_key[0],10),
g: parseInt(the_key[1],10),
b: parseInt(the_key[2],10),
a: parseInt(the_key[3],10)
};
}
);
return dominantColor;
}

getPixel () {
const args = judges.getPixel(arguments),
left = args[0],
Expand Down
Loading

0 comments on commit 0df3162

Please sign in to comment.