Skip to content

Commit

Permalink
feat(Image): Fix conflicts per #28.
Browse files Browse the repository at this point in the history
Specifically 3c22027, 55fdda7...e6c898e.
  • Loading branch information
randytarampi committed Apr 27, 2020
1 parent 02df8f6 commit 7cd33d5
Showing 1 changed file with 43 additions and 1 deletion.
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

0 comments on commit 7cd33d5

Please sign in to comment.