GitXplorerGitXplorer
a

colibrijs

public
156 stars
13 forks
3 issues

Commits

List of commits on branch master.
Unverified
0025c391be76e735c2195e51034dfd037051d5c3

v1.1.0

aarcanis committed 5 years ago
Unverified
a36df6f20fbb3e8c4e71f24beda4807eb1ea162c

Improves the package

aarcanis committed 5 years ago
Unverified
be0a2b44ba05142baf064bdbb8fe8f945052378f

Moves a note a bit higher

aarcanis committed 8 years ago
Unverified
a5676bf614e805cb8a72dea61fb74f87d123d957

Updates package

aarcanis committed 8 years ago
Unverified
c31e38d914174b5f9000c490b28a6ac5a7f45bee

Adds 'hex' filter

aarcanis committed 10 years ago
Unverified
e74f5b79a4ea427d6b09b5cb9dacabac01eec267

Updates README

aarcanis committed 11 years ago

README

The README file for this repository.

Colibrijs (demo)

Colibrijs is a small library that can extract the dominant colors from any image.

The algorithm used in the library has been ported over from the following StackOverflow answer.

Examples

Installation

$> npm install colibrijs

Usage

Node:

let fs = require('fs');

let Colibri = require('colibrijs');
let Image = require('canvas').Image;

let img = new Image();
img.src = fs.readFileSync('myImage.jpg');

console.log(Colibri.extractImageColors(img, 'css'));

Browser:

let img = new Image();
img.src = 'myImage.jpg';

img.addEventListener('load', function () {
    alert(Colibri.extractImageColors(img, 'css'));
});

Note that it won't work as expected if you try to load a remote image subject to cross-origin restrictions. However, if the target host is properly configured (and if you're not using IE), you should be able to do the following :

let img = new Image();

img.crossOrigin = 'Anonymous';
img.src = 'http://i.imgur.com/OUmDIOqb.jpg';

img.addEventListener('load', function () {
    alert(Colibri.extractImageColors(img, 'css'));
});

Performances

  • Performance issues might arise if you try to extract dominant colors from a lot of images (dozen of them). If you happen to hit this issue, maybe generating these colors the server side might be a better.

  • Since we're iterating on every pixel of the images, large ones might take a long time to complete. In order to avoid this issue, try to only extract colors from small images. If necessary, you can resize them at runtime before giving them to Colibrijs:

    let img = new Image();
    
    img.crossOrigin = 'Anonymous';
    img.src = 'http://i.imgur.com/OUmDIOq.jpg';
    
    img.addEventListener('load', function () {
    
        var canvas = document.createElement( 'canvas' );
        var context = canvas.getContext( '2d' );
    
        var maxWidth = 200, maxHeight = 200;
        var widthRatio = Math.max( 1, img.width / maxWidth );
        var heightRatio = Math.max( 1, img.height / maxHeight );
        var maxRatio = Math.max( widthRatio, heightRatio );
    
        canvas.width = img.width / maxRatio;
        canvas.height = img.height / maxRatio;
    
        context.drawImage( img, 0, 0, canvas.width, canvas.height );
    
        alert( Colibri.extractImageColors( canvas, 'css' ) );
    
    });

License (MIT)

Copyright © 2016 Maël Nison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.