GithubHelp home page GithubHelp logo

jamiefang / get-rgba-palette Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattdesl/get-rgba-palette

0.0 2.0 0.0 10 KB

gets a palette of prominent colors from an array of pixels

License: MIT License

JavaScript 100.00%

get-rgba-palette's Introduction

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.

get-rgba-palette's People

Contributors

cheapsteak avatar jimkang avatar mattdesl avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.