npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

color-robin

v3.0.0

Published

Image colors extraction library (for browsers only)

Downloads

5

Readme

Welcome to color-robin

Color-robin is a small library for extracting the colors from an image. It is based on the canvas element and it requires the browser environment.

🚀 Getting started

Installation

npm install color-robin

Usage example

HTML

<div>
    <img id="image1" src="image1.jpg" alt="..." />
</div>

JavaScript

import { analyzeImage } from "color-robin";

const image1 = document.getElementById("image1");

analyzeImage(image1).then(histogram => {
    // Get the top 3 colors
    const colors = histogram.getColors(3);

    // Print colors in rgb() form
    for (const color of colors) {
        console.log(color.toRGB());
    }
});

❔ How it works

Thumbnail

The first step of the process is to create a low resolution version of the image in order to speed up the analysis. The default resolution of the resized image is 20x20 pixels but this can be configured accordingly.

Note: The resized image has always a 1:1 ratio. Keeping the original aspect ratio of the input images could be a nice future improvement.

Histogram

The next step after generating the thumbnail is the generation of the colors' histogram. The color space is divided into a number of buckets where each bucket represent a group of similar colors. Then the thumbnail is scanned and each pixel color is thrown into one of the buckets. The amount of colors in each bucket represent the usage of the specific color in the image.

📑 Reference

Function analyzeImage(image, resolution, maxClasses)

This is the main function of the library. A wrapper of the whole process that given an image element it will:

  • wait for the image to load,
  • generate a Thumbnail of the image,
  • generate a Histogram of the thumbnail
  • and then return the histogram through a Promise.

| Argument | Type | Description | |----------|------|-------------| | image | HTMLImageElement | The image element to analyze. | | resolution | number (default 20) | The resolution of the Thumbnail that will be created. This means that for the default value the thumbnail will be 20x20 pixels. | | maxClasses | number (default 2) | This number represent the divisions of each color channel for creating the color buckets. The higher the number, the more buckets will be created but the result will be less diverse |

Method Histogram.getColors(max)

This function returns a ColorArray which is an iterable object holding a collection of Color objects. The collection is ordered and the first color returned is the most frequent inside the image while the last one is the less frequent.

Arguments

| Argument | Type | Description | |----------|------|-------------| | max (optional) | number | Can be used to limit the amount of colors that will be returned, always starting from the most frequent color. |

Returns | Type | Description | |------|-------------| | ColorArray | An iterable collection of Color objects ordered from the most frequent colors to the less ones. |

Class Color

A class representing a color.

Methods

| Method | Description | |--------|-------------| | toRGB() | Return the color in rgb() form e.g. rgb(255,0,0) | | getLightness() | Returns the lightness of the color (0 to 1). | | isLight() | Return true if the color has more than 50% lightness. | | isDark() | The opossite of isLight(). | | isLighterThan(Color) | Returns true if the current color is lighter than the given one. | | isDarkerThan(Color) | Returns true if the current color is darker than the given one. | | compareLightness(Color) | A lightness comparison method that can be used for sorting colors. |