Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull dominantColors per https://github.com/randytarampi/lwip/pull/28 #30

Merged
merged 13 commits into from
Apr 28, 2020
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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

judges.dominantColor(
arguments,
skips => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

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 = {};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

for (let i = 0; i < this.width(); i++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

for (let j = 0; j < this.height(); j+= skips+1) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

const pixel = this.__lwip.getPixel(i,j),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

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',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

count = 0;
for (let key in colorCounter) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

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