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

added dominantColor function #255

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
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 @@ -706,6 +707,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
1 change: 1 addition & 0 deletions lib/BatchPrototypeInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
exec: decree(defs.args.exec),
toBuffer: decree(defs.args.toBuffer),
writeFile: decree(defs.args.writeFile)

};

var undefinedFilter = util.undefinedFilter,
Expand Down
49 changes: 48 additions & 1 deletion lib/ImagePrototypeInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
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)
};

Image.prototype.__lock = function() {
Expand Down Expand Up @@ -68,6 +69,51 @@
return this.__lwip.height();
};



Image.prototype.dominantColor = function() {
var that = this,
ret_pixel;
judges.dominantColor(
arguments,
function(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.");

var hash = {};

for(var i = 0; i < that.width(); i++){
for(var j = 0; j < that.height(); j+= skips+1){
var pixel = that.__lwip.getPixel(i,j),
key = pixel[0].toString()+','+pixel[1].toString()+','+pixel[2].toString()+','+pixel[3].toString(),
occur = hash[key];
if( occur === undefined)
hash[key] = 1;
else
hash[key]++;
}
}

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

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

Image.prototype.size = function() {
return {
width: this.__lwip.width(),
Expand Down Expand Up @@ -114,6 +160,7 @@
);
};


Image.prototype.resize = function() {
this.__lock();
var that = this;
Expand Down
4 changes: 4 additions & 0 deletions lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@
name: 'callback',
type: 'function'
}],
dominantColor: [{
name: 'type',
type: 'number'
}],
darken: [{
name: 'delta',
type: 'number'
Expand Down
Binary file modified tests/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions tests/01.getters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,44 @@ describe('lwip.getMetadata', function() {
});
});
});

describe('lwip.dominantColor',function(){
var t_image;
before(function(done){
lwip.open(imgs.jpg.colors, function(err, img) {
if (err) return done(err);
t_image = img;
done();
});
});

it('should return the color that occurs the most frequently',function(done){
var color = t_image.dominantColor(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.dominantColor(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]);

});
Binary file added tests/images/colors.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/imgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down