• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    JavaScript
  • Created almost 11 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Color extractor

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.

More Repositories

1

clipanion

Type-safe CLI library / framework with no runtime dependencies
TypeScript
882
star
2

pnp-webpack-plugin

Transparently adds support for Plug'n'Play to Webpack
JavaScript
259
star
3

virtjs

JS collection of standardized devices, useful for emulating engines
JavaScript
213
star
4

typanion

Static and runtime type assertion library with no dependencies
TypeScript
208
star
5

secretsanta

Host secret santa without leaking your guests' informations πŸŽ„
HTML
190
star
6

sherlock

GitHub Actions toolkit to monitor a repository and run testcases
TypeScript
125
star
7

ts-pnp

Transparently adds support for Plug'n'Play to TypeScript
JavaScript
65
star
8

cpp-loader

A C++ loader for Webpack
C++
58
star
9

sparkle

WebGL particle engine
JavaScript
49
star
10

graphql-typescript-integration

TypeScript
35
star
11

eslint-plugin-arca

Various ESLint rules tailored to the author's preferred style
TypeScript
33
star
12

mono-layout

Fast implementation of a browser-like text layout engine, for Node and browsers
C++
27
star
13

pokelib

A library allowing to control & change the runtime values of a Pokemon game
JavaScript
26
star
14

pxeger

Reverse regular expression engine - generate all possible values for a regular expression
JavaScript
25
star
15

jest-pnp-resolver

Transparently adds support for Plug'n'Play to Jest
JavaScript
24
star
16

pnp-rust

JavaScript
23
star
17

ohui

Javascript curses library
JavaScript
22
star
18

rollup-plugin-pnp-resolve

Transparently adds support for Plug'n'Play to Rollup
JavaScript
19
star
19

node-sea

Pack entire packages and application into portable scripts
TypeScript
18
star
20

emmagic

Easily transport your data structures between your C++ and Javascript
C++
16
star
21

trivia.ff9

Extraction & conversion tools for Final Fantasy IX
C++
16
star
22

js.voxel

[DEPRECATED, cf readme for an alternative] Javascript voxel engine
JavaScript
15
star
23

peg.arcanis.fr

PEG.js web editor
JavaScript
14
star
24

gist-proxy-server

JavaScript
14
star
25

datefuzz

jQuery plugin for simple fuzzy date management
CSS
12
star
26

magicbin

Simple process manager; alternative to pm2
TypeScript
12
star
27

mpllvm

Metaprogramming type resolver for LLVM
C++
11
star
28

js.perlin

Javascript perlin's noise implementation - Demo on the arcanis/voxplode repository
JavaScript
9
star
29

pikasprite

A set of pokemon icons
CSS
8
star
30

voxplode

HTML5 game powered by the SWAT framework
JavaScript
8
star
31

babel-plugin-lazy-import

JavaScript
7
star
32

git-smart-project

Collection of smarter Git utilities
TypeScript
7
star
33

pokequery

Prolog library for issuing complex query on the Pokemon databases
JavaScript
6
star
34

RTWorldReader

Javascript library for importing .rtw files into webgl applications
JavaScript
6
star
35

astar-wasm

JavaScript
5
star
36

node-webpack

Isomorphic module loading via Webpack
JavaScript
5
star
37

boost.log

C++
5
star
38

embin

Shell
4
star
39

slice-ansi

JavaScript
4
star
40

arcanis

4
star
41

trivia.gol404

JavaScript
4
star
42

chenille

Keep master green at all times
JavaScript
3
star
43

epitech.pfa

JavaScript
3
star
44

build-pnm

Generate package-name-maps files by consuming the PnP API
JavaScript
3
star
45

tsc-with-workspaces

TypeScript
2
star
46

willikins

JavaScript
2
star
47

is-pnp

Detects whether PnP is enabled or not when running your scripts
JavaScript
2
star
48

tppserver

JavaScript
2
star
49

fe13-planificator

A planificator for Fire Emblem Awakening
JavaScript
2
star
50

arcanis.github.io

HTML
2
star
51

react-tailwind-select

TypeScript
2
star
52

epitech.generic-linked-list

A generic linked list. With bits of preprocessing metaprogrammation.
C
2
star
53

require-context-node

JavaScript
2
star
54

node-tput

tput for node.js
C++
1
star
55

multisweeper

Multiplayer minesweeper
CSS
1
star
56

term-capture

Capture your terminal as HTML
Python
1
star
57

presentation.jsmontreal-20130312

JavaScript
1
star
58

js.spark

Yet another javascript particle engine
JavaScript
1
star
59

tco

C++
1
star
60

ghost-theme

My own ghost theme
CSS
1
star
61

nitro

Advanced HTML5 mobile development framework, for Angular aficionados
JavaScript
1
star
62

arca-rs

Just a couple of Rust utilities I use
Rust
1
star
63

jest-coverage-ts

TypeScript
1
star
64

rollup-node-resolve-regression

JavaScript
1
star
65

tinylogic

JavaScript
1
star
66

webpack-project-generator

JavaScript
1
star
67

esfuse

TypeScript
1
star
68

ts-monorepo-example

JavaScript
1
star