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

get-rgba-palette

v2.0.1

Published

gets a palette of prominent colors from an array of pixels

Downloads

64,163

Readme

get-rgba-palette

stable

Gets a palette of prominent colors from a flat array of RGBA pixels.

img

Full example:

var palette = require('get-rgba-palette')
var pixels = require('get-image-pixels')
var load = require('img')
var baboon = require('baboon-image-uri')

load(baboon, function(err, img) {
    //get flat RGBA pixels array
    var px = pixels(img)

    //get 5 prominent colors from our image
    var colors = palette(px, 5)
})

RequireBin demo

Returns RGB colors in the form of:

[ [255, 0, 0], [128, 23, 52], [124, 0, 62], etc.. ]

Usage

NPM

palette(pixels[, count, quality, filter])

Gets an array of RGB colors from an image (pixels is a flat RGBA array). Defaults to a count of 5 colors and a quality setting of 10.

quality determines the step between each pixel when computing the quantization; higher number means fuzzier quality. Must be > 0.

filter allows you to threshold the RGBA values given to the quantize function. It passes (pixels, index) -- the flat array and index to the RGBA component. Return true to accept the color, or false to reject.

If not specified, the filter function defaults to:

function filter (pixels, index) {
  // offset 3 --> alpha
  return pixels[index + 3] >= 127
}

palette.bins(pixels[, count, quality, filter])

Instead of returning RGB colors, this returns the "bins" for each computed color. This can give you a very rough overview of the distribution of colours in the image. This is fairly arbitrary as the bin size does not always represent the exact number of pixels for that color in the image.

Returns an array of objects:

{
    color: [255, 0, 0],
    size: 12414,
    amount: 0.65
}

Where amount is a percentage for that bin, normalized from the sum of the sizes in the returned bins.

License

MIT, see LICENSE.md for details.