From bc8b6c8cc4d019bc7cbd3f600121af3d158ecbe6 Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:00:38 -0700 Subject: [PATCH 01/13] added dominateColor definition to exports.args --- lib/defs.js | 426 ++++++++++++++++++++++++++-------------------------- 1 file changed, 215 insertions(+), 211 deletions(-) diff --git a/lib/defs.js b/lib/defs.js index df9feec3..8ef7c92d 100644 --- a/lib/defs.js +++ b/lib/defs.js @@ -192,214 +192,218 @@ exports.args = { name: 'callback', type: 'function' }], - darken: [{ - name: 'delta', - type: 'number' - }, { - name: 'callback', - type: 'function' - }], - fade: [{ - name: 'delta', - type: 'number' - }, { - name: 'callback', - type: 'function' - }], - opacify: [{ - name: 'callback', - type: 'function' - }], - hue: [{ - name: 'shift', - type: 'number' - }, { - name: 'callback', - type: 'function' - }], - crop: [{ - name: 'left', - type: 'nn-number' - }, { - name: 'top', - type: 'nn-number' - }, { - name: 'right', - type: 'nn-number', - optional: true - }, { - name: 'bottom', - type: 'nn-number', - optional: true - }, { - name: 'callback', - type: 'function' - }], - mirror: [{ - name: 'axes', - type: 'axes' - }, { - name: 'callback', - type: 'function' - }], - exec: [{ - name: 'callback', - type: 'function' - }], - toBuffer: [{ - name: 'type', - type: 'string' - }, { - name: 'params', - type: 'hash', - optional: true, - default: {} - }, { - name: 'callback', - type: 'function' - }], - getPixel: [{ - name: 'left', - type: 'nn-int' - }, { - name: 'top', - type: 'nn-int' - }], - border: [{ - name: 'width', - type: 'nn-number' - }, { - name: 'color', - type: 'color', - optional: true, - default: defaults.DEF_BORDER_COLOR - }, { - name: 'callback', - type: 'function' - }], - pad: [{ - name: 'left', - type: 'nn-number' - }, { - name: 'top', - type: 'nn-number' - }, { - name: 'right', - type: 'nn-number' - }, { - name: 'bottom', - type: 'nn-number' - }, { - name: 'color', - type: 'color', - optional: true, - default: defaults.DEF_PAD_COLOR - }, { - name: 'callback', - type: 'function' - }], - sharpen: [{ - name: 'amplitude', - type: 'number' - }, { - name: 'callback', - type: 'function' - }], - clone: [{ - name: 'callback', - type: 'function' - }], - paste: [{ - name: 'left', - type: 'nn-number' - }, { - name: 'top', - type: 'nn-number' - }, { - name: 'image', - type: 'image' - }, { - name: 'callback', - type: 'function' - }], - extract: [{ - name: 'left', - type: 'nn-number' - }, { - name: 'top', - type: 'nn-number' - }, { - name: 'right', - type: 'nn-number' - }, { - name: 'bottom', - type: 'nn-number' - }, { - name: 'callback', - type: 'function' - }], - writeFile: [{ - name: 'path', - type: 'string' - }, { - name: 'type', - type: 'string', - optional: true - }, { - name: 'params', - type: 'hash', - optional: true, - default: {} - }, { - name: 'callback', - type: 'function' - }], - setPixel: [{ - name: 'left', - type: 'nn-int' - }, { - name: 'top', - type: 'nn-int' - }, { - name: 'color', - type: 'color' - }, { - name: 'callback', - type: 'function' - }], - contain: [{ - name: 'width', - type: 'p-number' - }, { - name: 'height', - type: 'p-number' - }, { - name: 'color', - type: 'color', - optional: true, - default: defaults.DEF_CONTAIN_COLOR - }, { - name: 'interpolation', - type: 'interpolation', - optional: true, - default: defaults.DEF_INTERPOLATION - }, { - name: 'callback', - type: 'function' - }], - cover: [{ - name: 'width', - type: 'p-number' - }, { - name: 'height', - type: 'p-number' - }, { - name: 'interpolation', - type: 'interpolation', - optional: true, - default: defaults.DEF_INTERPOLATION - }, { - name: 'callback', - type: 'function' - }], -}; + dominateColor: [{ + name: 'type', + type: 'number' + }], + darken: [{ + name: 'delta', + type: 'number' + }, { + name: 'callback', + type: 'function' + }], + fade: [{ + name: 'delta', + type: 'number' + }, { + name: 'callback', + type: 'function' + }], + opacify: [{ + name: 'callback', + type: 'function' + }], + hue: [{ + name: 'shift', + type: 'number' + }, { + name: 'callback', + type: 'function' + }], + crop: [{ + name: 'left', + type: 'nn-number' + }, { + name: 'top', + type: 'nn-number' + }, { + name: 'right', + type: 'nn-number', + optional: true + }, { + name: 'bottom', + type: 'nn-number', + optional: true + }, { + name: 'callback', + type: 'function' + }], + mirror: [{ + name: 'axes', + type: 'axes' + }, { + name: 'callback', + type: 'function' + }], + exec: [{ + name: 'callback', + type: 'function' + }], + toBuffer: [{ + name: 'type', + type: 'string' + }, { + name: 'params', + type: 'hash', + optional: true, + default: {} + }, { + name: 'callback', + type: 'function' + }], + getPixel: [{ + name: 'left', + type: 'nn-int' + }, { + name: 'top', + type: 'nn-int' + }], + border: [{ + name: 'width', + type: 'nn-number' + }, { + name: 'color', + type: 'color', + optional: true, + default: defaults.DEF_BORDER_COLOR + }, { + name: 'callback', + type: 'function' + }], + pad: [{ + name: 'left', + type: 'nn-number' + }, { + name: 'top', + type: 'nn-number' + }, { + name: 'right', + type: 'nn-number' + }, { + name: 'bottom', + type: 'nn-number' + }, { + name: 'color', + type: 'color', + optional: true, + default: defaults.DEF_PAD_COLOR + }, { + name: 'callback', + type: 'function' + }], + sharpen: [{ + name: 'amplitude', + type: 'number' + }, { + name: 'callback', + type: 'function' + }], + clone: [{ + name: 'callback', + type: 'function' + }], + paste: [{ + name: 'left', + type: 'nn-number' + }, { + name: 'top', + type: 'nn-number' + }, { + name: 'image', + type: 'image' + }, { + name: 'callback', + type: 'function' + }], + extract: [{ + name: 'left', + type: 'nn-number' + }, { + name: 'top', + type: 'nn-number' + }, { + name: 'right', + type: 'nn-number' + }, { + name: 'bottom', + type: 'nn-number' + }, { + name: 'callback', + type: 'function' + }], + writeFile: [{ + name: 'path', + type: 'string' + }, { + name: 'type', + type: 'string', + optional: true + }, { + name: 'params', + type: 'hash', + optional: true, + default: {} + }, { + name: 'callback', + type: 'function' + }], + setPixel: [{ + name: 'left', + type: 'nn-int' + }, { + name: 'top', + type: 'nn-int' + }, { + name: 'color', + type: 'color' + }, { + name: 'callback', + type: 'function' + }], + contain: [{ + name: 'width', + type: 'p-number' + }, { + name: 'height', + type: 'p-number' + }, { + name: 'color', + type: 'color', + optional: true, + default: defaults.DEF_CONTAIN_COLOR + }, { + name: 'interpolation', + type: 'interpolation', + optional: true, + default: defaults.DEF_INTERPOLATION + }, { + name: 'callback', + type: 'function' + }], + cover: [{ + name: 'width', + type: 'p-number' + }, { + name: 'height', + type: 'p-number' + }, { + name: 'interpolation', + type: 'interpolation', + optional: true, + default: defaults.DEF_INTERPOLATION + }, { + name: 'callback', + type: 'function' + }], + }; From dace4e90fcfddcb88a1fd8b415ecb43e5cc17fde Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:03:36 -0700 Subject: [PATCH 02/13] added colors.jpg to exports --- tests/imgs.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/imgs.js b/tests/imgs.js index 0c1d0f39..3e6e0efd 100644 --- a/tests/imgs.js +++ b/tests/imgs.js @@ -7,7 +7,9 @@ module.exports = { gs: join(__dirname, imbase, 'gs.jpg'), rgb: join(__dirname, imbase, 'rgb.jpg'), noex: join(__dirname, imbase, 'rgbjpg'), - inv: join(__dirname, imbase, 'invalid.jpg') + inv: join(__dirname, imbase, 'invalid.jpg'), + colors: join(__dirname, imbase, 'colors.jpg') + }, png: { gs: join(__dirname, imbase, 'gs.png'), From 9900ac9bdbddf2d695325cae237031241a330bcf Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:04:11 -0700 Subject: [PATCH 03/13] added tests for dominateColor --- tests/01.getters/index.js | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/01.getters/index.js b/tests/01.getters/index.js index 9d8be348..2a2a4b55 100644 --- a/tests/01.getters/index.js +++ b/tests/01.getters/index.js @@ -103,3 +103,45 @@ describe('lwip.getMetadata', () => { }); }); }); + +describe('lwip.getDominateColor',function(){ + var t_image; + before(function(done){ + lwip.open(imgs.jpg.colors, function(err, img) { + if (err) return done(err); + t_image = img; + console.log('see this once before'); + done(); + }); + }); + + it('should return the color that occurs the most frequently',function(done){ + var color = t_image.dominateColor(10); + assert( color.r === 255); + assert( color.g === 252); + assert( color.b === 0); + assert( color.a === 100); + done(); + }); + + function shouldFail(fail){ + it('should fail when: '+fail+' is passed as a parameter',function(done){ + var err_count = 0; + try{ + t_image.dominateColor(fail); + } + catch(error){ + err_count++; + } + finally{ + assert( err_count === 1); + } + done(); + }); + } + + var fails = [ -1, 3.3, '\'tree\'']; + for(var i = 0; i < fails.length; i++) + shouldFail(fails[i]); + +}); From f026d8790ad8405f8254b9cbda252f75d30d9104 Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:08:10 -0700 Subject: [PATCH 04/13] used to test dominateColor --- tests/images/colors.jpg | Bin 0 -> 4844 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/images/colors.jpg diff --git a/tests/images/colors.jpg b/tests/images/colors.jpg new file mode 100644 index 0000000000000000000000000000000000000000..772a62ff8c8e91d72e388c8769cb0b24d8ddfcf5 GIT binary patch literal 4844 zcmcIo2Ut_d7M>&!0)hkx9TY^Wf^?d%JJnd^dOIo-*g$f6koDx#ZX6VSv_DJD~<(P_u$r z0YL5tDmzcxl)u}yC;7X&-}J~efB^-51_T6*0l*nx2nHCr4zPc54*c4vQB*XP+bkFi zAfVrOjS5ALq#^eKS_BNh(FimEu%#_P^_aGLZaP$HQW3x=6!d(;tzAD313<6Fezm`g z=4yK&K(t;-T4bykTB-tIPoxA@Q5rMVBLJxGZ_17G`&Ui`0wBj`NAGGZLaz)d9$mjw zr@0Z(23hvQHt!ky$#_o)r0B7{78bWk21&rgqC`6o{?n^VuDR*U0{~E4dW)z@W@Ez; zj%t)XfOg8cVos>a<5MdfpOcgkt=d}RCU95NuC@Ys@4z&-Y&qLtQZ*tZ#wc&jr zv%3N&_E$V*%i}k5Tj_zeQcdG&i`isc*)?g zPq6QF7A=^IXRhFZSqsq#4#Xu?0svPUn#sC0+Y$`?QKRaBW=zZDy{n>RBmj)KsdECn z54K^Uyg)2jD|*QB9KxB?GoK0F)(yb|FEJe0=x?X9fgFu0wV)2OpMhY3olJG{pPNMH zK(D3tMwFsXV&y*MO{Q^?{OU3SS_K%N3nBOn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Mmr$C{36gSr14-w@_v4JS|l3ex%OwA31 ziUhZYYvq;AraNLMOD>Ex5qu|-0u1RFvx4eRTx+UoT6Y|FGZB8z)F%7hs@!j7rYFm) z{L((va7LyXqr?0ted=ad`o6WWVk`z8Uzc7n2yws{Ye8@-$cddI9Bl6|XtM?G+<_ug z9zp~js3?Ovhz^H3hyv}c{o?dfSDu|TN$g@#q1 zDW^VDu%9Vx0M`$hqA!HF|83hokFYWP{`&$9dn(B7TTy~eIp39T^3e9tz7iSo^J zMKK=_CLi38EbLa8CKy1_8t&J)xeeO>^J!^mEyjW#!)#|f@`^Pls<5pe6Cj%BRL2wD z#A9DN4>q|j+d@^`D%w4>$tlbk;@xHZI)%p0!m~0!pFMu_8jx3k-_AkkT)hYKmp?8| zv?Xn?T5oXDeBJ&+cG@!|Px^BLMG51RP0&h)(qM%2A1Q~@C;3i9C^=i?h4||xHx^8w z);~@p$#~E87rDGC^PcM?YQ~Ag-;Pd~>)@?)e?n(+iKOChNYqR-PKEAqBF3pk+c=@2 z^V)A*f-!t`2?|mL^jP873865oybK%1n>pQJccE$#$?oMj+z%0VAcBxm>vt}EWnq&! zQb;P#AF`}<)^RTE=fpYn=GnE}!#NE>@w#!Y)rpiKdfhw_%M~DHq{E_5Yg|?YF`(D^ J`c~xLe*sBA`Zxdp literal 0 HcmV?d00001 From 4672dd2c959b04d942db9889835f306abb7a0d09 Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:10:04 -0700 Subject: [PATCH 05/13] added definition for dominateColor --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 7d88ba79..e13e7d1c 100644 --- a/README.md +++ b/README.md @@ -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 dominate color](#dominate-color) 0. [Batch operations](#batch-operations) 0. [Copyrights](#copyrights) @@ -739,6 +740,12 @@ tEXt chunks in PNG images, and will get the first tEXt chunk found with the key `image.getMetadata()` +### Get dominate color + +Get the pixel color that occurs most frequently in the picture. +`image.dominateColor(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 From 57ca166ea7f714ed82669eb142384f1f65eb6ef6 Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:12:26 -0700 Subject: [PATCH 06/13] added definition for dominateColor --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e13e7d1c..8b1af19d 100644 --- a/README.md +++ b/README.md @@ -740,7 +740,7 @@ tEXt chunks in PNG images, and will get the first tEXt chunk found with the key `image.getMetadata()` -### Get dominate color +#### Get dominate color Get the pixel color that occurs most frequently in the picture. `image.dominateColor(pixels_to_skip)` From 61a2f87e63f6f7f6ad11052b45646375b5dadf56 Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:30:03 -0700 Subject: [PATCH 07/13] fixed dominateColor link --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8b1af19d..742c32a0 100644 --- a/README.md +++ b/README.md @@ -740,10 +740,12 @@ tEXt chunks in PNG images, and will get the first tEXt chunk found with the key `image.getMetadata()` -#### Get dominate color +### Get dominate color + +Get the pixel color that occurs most frequently in a picture. -Get the pixel color that occurs most frequently in the picture. `image.dominateColor(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 From 6c513d80f1878f6d4110845b0594853a70593b73 Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:31:07 -0700 Subject: [PATCH 08/13] fixed dominateColor link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 742c32a0..9144e4f0 100644 --- a/README.md +++ b/README.md @@ -59,7 +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 dominate color](#dominate-color) + 0. [Get dominate color](#get-dominate-color) 0. [Batch operations](#batch-operations) 0. [Copyrights](#copyrights) From 135ecbc5f0b45e666c7783497178f1f9f36850ef Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 12:52:19 -0700 Subject: [PATCH 09/13] removed log --- tests/01.getters/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/01.getters/index.js b/tests/01.getters/index.js index 2a2a4b55..c0dfb277 100644 --- a/tests/01.getters/index.js +++ b/tests/01.getters/index.js @@ -110,7 +110,6 @@ describe('lwip.getDominateColor',function(){ lwip.open(imgs.jpg.colors, function(err, img) { if (err) return done(err); t_image = img; - console.log('see this once before'); done(); }); }); From e8cb568cf858e6bbe3cb142cf71f868ca88c5aad Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Fri, 10 Jun 2016 13:12:18 -0700 Subject: [PATCH 10/13] adjusted name --- tests/01.getters/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/01.getters/index.js b/tests/01.getters/index.js index c0dfb277..89e80f3e 100644 --- a/tests/01.getters/index.js +++ b/tests/01.getters/index.js @@ -104,7 +104,7 @@ describe('lwip.getMetadata', () => { }); }); -describe('lwip.getDominateColor',function(){ +describe('lwip.dominateColor',function(){ var t_image; before(function(done){ lwip.open(imgs.jpg.colors, function(err, img) { From d99d82dee281988cb0e23f108049ee3b38f812ea Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Wed, 6 Jul 2016 18:41:38 -0700 Subject: [PATCH 11/13] renamed dominateColor to dominantColor --- lib/defs.js | 2 +- tests/01.getters/index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/defs.js b/lib/defs.js index 8ef7c92d..4523fc24 100644 --- a/lib/defs.js +++ b/lib/defs.js @@ -192,7 +192,7 @@ exports.args = { name: 'callback', type: 'function' }], - dominateColor: [{ + dominantColor: [{ name: 'type', type: 'number' }], diff --git a/tests/01.getters/index.js b/tests/01.getters/index.js index 89e80f3e..7c95fe75 100644 --- a/tests/01.getters/index.js +++ b/tests/01.getters/index.js @@ -104,7 +104,7 @@ describe('lwip.getMetadata', () => { }); }); -describe('lwip.dominateColor',function(){ +describe('lwip.dominantColor',function(){ var t_image; before(function(done){ lwip.open(imgs.jpg.colors, function(err, img) { @@ -115,7 +115,7 @@ describe('lwip.dominateColor',function(){ }); it('should return the color that occurs the most frequently',function(done){ - var color = t_image.dominateColor(10); + var color = t_image.dominantColor(10); assert( color.r === 255); assert( color.g === 252); assert( color.b === 0); @@ -127,7 +127,7 @@ describe('lwip.dominateColor',function(){ it('should fail when: '+fail+' is passed as a parameter',function(done){ var err_count = 0; try{ - t_image.dominateColor(fail); + t_image.dominantColor(fail); } catch(error){ err_count++; From 02df8f64203a5683178e3251c2f1aad40ee66bef Mon Sep 17 00:00:00 2001 From: MarkOtten Date: Wed, 6 Jul 2016 18:46:27 -0700 Subject: [PATCH 12/13] renamed dominateColor to dominantColor --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9144e4f0..34e0a84e 100644 --- a/README.md +++ b/README.md @@ -59,7 +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 dominate color](#get-dominate-color) + 0. [Get dominant color](#get-dominant-color) 0. [Batch operations](#batch-operations) 0. [Copyrights](#copyrights) @@ -740,11 +740,11 @@ tEXt chunks in PNG images, and will get the first tEXt chunk found with the key `image.getMetadata()` -### Get dominate color +### Get dominant color Get the pixel color that occurs most frequently in a picture. -`image.dominateColor(pixels_to_skip)` +`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. From 7cd33d5e6b39ae9f383acd89381dd811abf9ee00 Mon Sep 17 00:00:00 2001 From: Randy Tarampi Date: Mon, 27 Apr 2020 20:35:41 +0200 Subject: [PATCH 13/13] 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],