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 🙏

© 2025 – Pkg Stats / Ryan Hefner

pigmnts

v0.7.0

Published

Generate a color palette from an image using K-means++

Readme

🎨 Pigmnts

Pigmnts is a library to create a color palette from an image built using Rust, compiled to WebAssembly. This allows for super-fast color palette extraction from an image on the web. It uses the K-means++ clustering algorithm to select the most commonly occuring colors from the image.

Examples with Web Assembly

As a JavaScript module

<script type="module">
  // Import the functions from CDN
  import init, { pigments } from 'https://unpkg.com/pigmnts/pigmnts.js';

  async function run() {
    /**
     * Load the wasm file.
     * Replace the URL with a different path if you want to use
     * self hosted wasm file 
     */
    await init('https://unpkg.com/pigmnts/pigmnts_bg.wasm');

    // Canvas element from which palette should be created
    const canvas = document.querySelector('canvas');

    // Call the pigments wasm function
    const palette = pigments(canvas, 5);
  }
  run();

</script>

In Node.js

  1. Start by installing the npm package
npm install pigmnts
  1. Configure your build process to copy the wasm file in the your build directory.

  2. Use it in code

import init, { pigments } from 'pigmnts';

async function run() {
  // Load the wasm file from path where wasm file was copied by the bundler
  await init('<path to the wasm file>');

  // Canvas element from which palette should be created
  const canvas = document.querySelector('canvas');

  // Call the pigments wasm function
  const palette = pigments(canvas, 5);
}
run();

Functions

Pigmnts exposes following function in WebAssembly

pigments(canvas: HtmlCanvasElement, k: number, mood: Mood|number, batch_size: number)

Arguments
  • canvas canvas element which has the image to be processed. Internally, the pixel data is taken from the canvas, and then clustered to create the color palette.
  • k defines the number of colors to be gathered from the image.
  • mood defines the weight function to use. Only 'dominant' mood is supported which has a value of 0
  • batch_size (optional) defines the number of pixels to randomly sample from the image. It should be greater than the total number of pixels in the image and the k. By default, all the pixels in the image are processed.
Return

Returns an Array of Objects where each Object is a color of the following format.

[
  {
    dominance: 0.565    // Dominance of color in image(From 0 to 1)
    hex: '#6DDAD0'      // 6-digit Hex color code
    rgb: {              // Equivalent RGB color
      r: 109,
      g: 218,
      b: 208
    },
    hsl: {             // Equivalent HSL color (Normalized to 0-1)
      h: 0.48333,
      s: 0.6,
      l: 0.64,
    }
  },
  // Other colors
  {
    ...
  }
]

If this crate is used in some Rust projects, then following function is also available

pigments_pixels(pixels: &Vec<LAB>, k: u8, weight: fn(&LAB) -> f32, max_iter: Option<u16>) -> Vec<(LAB, f32)>

This function can be used when color data is gathered from an image decoded using image-rs.

Arguments
  • pixels reference to a Vector of colors in LAB format.
  • k defines the number of colors to be gathered from the image.
  • weight defines the weight function to use. src/weights.rs file has few implemented weight functions.
  • max_iter defines the maximum iterations that algorithm makes, default is 300
Return

Returns a vector of tuples with colors as LAB and dominance(as percentage) of each color found in the image.

License

Pigmnts is MIT Licensed