domcolor

Dominant color

Usage no npm install needed!

<script type="module">
  import domcolor from 'https://cdn.skypack.dev/domcolor';
</script>

README

domcolor

Get the dominant color from an image.

Installation

npm install domcolor

Usage

domcolor (args)

  • args: <string> | <Buffer> (Required)

    The string path to the source image or the source image in buffer form.

    args is a required parameter.

  • Returns: <Object>

    • rgb: <Array> color RGB values
    • hex: <string> color in hex format
    • count: <integer> the number of pixels are the dominant color
    • total: <integer> the total number of pixels in the image

Sample Code

/* example usage */
;(async function whatColor () {
  try {
    let domcolor = require('domcolor')

    let fromPath = await domcolor('/path/to/my/cat_pic.jpg')

    /* OR */

    let imgBuffer = fs.readFileSync('/path/to/my/cat_pic.jpg')
    let fromBuffer = await domcolor(imgBuffer)

    console.log(fromPath, fromBuffer)
  } catch (error) {
    console.error(error)
  }
})()

/* getting hex from file -- condensed */
;(function getBackgroundColor (cb) {
  require('domcolor')(require('fs').readFileSync('/path/to/img.jpeg'))
    .then(cb)
    .catch(console.error)
})(({ hex }) => console.log('hex:', hex))

Example

gopher

{ rgb: [ 208, 182, 152 ], hex: '#d0b698', count: 23210, total: 65536 }

gopher-color

Contributing

See our guidelines, in Contributing.