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

better-dominant-colors

v1.0.1

Published

A fast library written in C++ to obtain the dominant colors of an image in a meaningful way to get usable results.

Downloads

5

Readme

better-dominant-colors

A fast NPM library written in C++ to obtain the dominant colors of an image in a meaningful way to get usable results.

To process images, I used the fantastic stb_image library.
To find peaks in my diagrams, I adapted the code from Clayder Gonzalez, which is also an adaptation of the original code from Matlab.
Lastly, to turn my C++ code into a Node library, I used the node-addon-api, or napi for short.

The "meaningful way" for usable results

What do I mean by "meaningful way for usable results" ?
To better understand, here's a comparison of the analysis of the image below by a fairly widely used library: dominant-colors versus the analysis of my library.
I'll explain how I do it later in this readme, but it's not rocket science.

How to use

Installation

You can install better-dominant-colors using npm:

npm install -g better-dominant-colors

Usage

I recommend you to take a look at the example.js file at the root of this project for more examples and to better understand the use of certain parameters, even if the library is really simple to use.

var betterDominantColors = require('better-dominant-colors');

/*
    Array getDominantColors(String imagePath, Integer dominantCount, Boolean enliven = true);

    Returns an array of strings containing the hexadecimal codes of the dominantCount colors of
    the image given in the imagePath argument.

    The enliven option allows you to increase the saturation and value of the image's HSV values
    by 50% to obtain brighter colors. It is enabled by default.
*/

dominantColorsSmall = betterDominantColors.getDominantColors("images/small.png", 3)

console.log(dominantColorsSmall)
/* Ouput : ['#ff04', '#3c719c', '#950eef'] */

How does it work?

General functioning of the algorithm

The algorithm is quite simple. Here are the steps the image goes through to obtain its dominant colors:

  • The image is resized if it exceeds 65536 pixels to speed up its analysis.
  • Each pixel's RGB value is recovered and transferred as an HSV value to obtain the Hue value for transposing the colors onto a chromatic circle in the correct order.
  • A diagram is then made of each pixel's Hue value, and a local maximum search algorithm creates a table of the diagram's peaks corresponding to the colors.
  • Finally, the values are sent in order of appearance, and the algorithm tries to find values that are sufficiently different from each other to make up the final selection.

Enliven option

During the retrieval of all pixel values, each Hue value is associated with a saturation S and a value V. The Hue value is usually a float, but in order to reduce color diversity the algorithm cast Hue as an integer, and thus sometimes (quite often in reality on a fairly large image) two values with the same Hue but different S and V values appear. The algorithm averages the S and V values for each same Hue. The enliven option simply increases the S and V values by 50% once the algorithm has finished recovering all the pixels.

Value conversions

This part isn't necessarily very interesting, but as there are different approaches to converting RGB values to HSV and vice versa, I'm putting here the formulas I've personally used.

Compile source code

You can if you want get the sources of this project use or modify them, if you find a problem make a push request on this repo or open an issue.
Here's how to compile the sources to suit your needs:

C++ source code

At the root of the project run the following commands :

cmake -B "build"

Move to the build folder:

cd build/

And compile :

cmake --build .

The binary is in the folder : build/Debug/better_dominant_colors.exe and the main function in src/example.cpp.

Compile the Node library

At the root of the project run the following commands :
First install all dependencies with :

npm install

Then run this 2 commands to build the library in Node.

node-gyp configure
node-gyp build

Binaries are in the folder : build/Release/