From 7cd33d5e6b39ae9f383acd89381dd811abf9ee00 Mon Sep 17 00:00:00 2001 From: Randy Tarampi Date: Mon, 27 Apr 2020 20:35:41 +0200 Subject: [PATCH] feat(Image): Fix conflicts per https://github.com/randytarampi/lwip/pull/28. Specifically 3c220279d7cc5d73ce891faca1f3c73d23b5f3f9, 55fdda7f090dace865eb9ca87ea8181f46aab17c...e6c898e38efc47c0ce9548cf38460f584d160193. --- lib/Image.js | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/Image.js b/lib/Image.js index 48c5a2b1..e07624af 100644 --- a/lib/Image.js +++ b/lib/Image.js @@ -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 { @@ -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],