4.0.4 • Published 11 days ago

extract-colors v4.0.4

Weekly downloads
50
License
GPL-3.0
Repository
github
Last release
11 days ago

Extract Colors

package version npm min bundle size npm gzip bundle size zero dependency CI code coverage GNU GPL software License Downloaded

Extract color palettes from images.
Simple use, < 6kB minified, gzip ≈ 2kB, fast process and no dependencies for browser.
Need image reader dependence for node.js

Website | Demo | Guide

3 examples of colors extraction

Requirements

Browsers

  • Firefox: 29+
  • Chrome: 33+
  • Edge: 12+
  • Opera: 19+
  • Safari: 8+
  • Webview Android: 4.4.3+
  • Samsung Internet: 2.0+
  • Internet Explorer

Node

  • Node.js: 6.0+

Install

For browser

npm install --save extract-colors

For node.js

Need to install an ImageData extractor like get-pixels

npm install --save extract-colors get-pixels

Usage

Browser example

import { extractColors } from 'extract-colors'

const src = 'my-image.jpg'

extractColors(src)
  .then(console.log)
  .catch(console.error)

You can use different types for src param (String for a path of image, HTMLImageElement or ImageData).

Node.js example

const path = require('path')
const getPixels = require("get-pixels")
const { extractColors } = require('extract-colors')

const src = path.join(__dirname, './my-image.jpg')

getPixels(src, (err, pixels) => {
  if(!err) {
    const data = [...pixels.data]
    const width = Math.round(Math.sqrt(data.length / 4))
    const height = width

    extractColors({ data, width, height })
      .then(console.log)
      .catch(console.log)
  }
})

This example use get-pixels but you can change the lib. Just send and ImageData object to extractColors(imageData).

ExtractorOptions

const options = {
  pixels: 64000,
  distance: 0.22,
  colorValidator: (red, green, blue, alpha = 255) => alpha > 250,
  saturationDistance: 0.2,
  lightnessDistance: 0.2,
  hueDistance: 0.083333333
}

extractColors(src, options)
  .then(console.log)
  .catch(console.error)

pixels
Total pixel number of the resized picture for calculation
Type: Integer
Default: 64000

distance
From 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
Type: Number
Default: 0.22

colorValidator
Test function to enable only some colors
Type: Function
Default: (red, green, blue, alpha = 255) => alpha > 250

crossOrigin
Only for browser, can be 'Anonymous' to avoid client side CORS (the server side images need authorizations too)
Type: String
Default: null

saturationDistance
Minimum saturation value between two colors otherwise the colors will be merged (from 0 to 1) Type: String
Default: 0.2

lightnessDistance
Minimum lightness value between two colors otherwise the colors will be merged (from 0 to 1) Type: String
Default: 0.2

hueDistance
Minimum hue value between two colors otherwise the colors will be merged (from 0 to 1) Type: String
Default: 0.083333333

Return of the promise

Array of colors with the followed properties:

[
  {
    hex: "#858409",​​
    red: 133,​​
    green: 132,​​
    blue: 9,​​
    hue: 0.16532258064516128,​​
    intensity: 0.4862745098039216,​​
    lightness: 0.2784313725490196,​​
    saturation: 0.8732394366197184,
    area: 0.0004
  },
  ...
]
FieldExampleTypeDescription
hex#858409Stringcolor in hexadecimal string
red133Integerred canal from 0 to 255
green132Integergreen canal from 0 to 255
blue9Integerblue canal from 0 to 255
hue0.1653Numbercolor tone from 0 to 1
intensity0.4862Numbercolor intensity from 0 to 1
lightness0.2784Numbercolor lightness from 0 to 1
saturation0.8732Numbercolor saturation from 0 to 1
area0.0004Numberarea of the color and his neighbouring colors from 0 to 1

License

Copyright (C) 2019 Damien Doussaud

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

4.0.4

11 days ago

4.0.3

13 days ago

4.0.1

8 months ago

4.0.0

8 months ago

4.0.2

8 months ago

3.0.0

1 year ago

2.0.6

1 year ago

2.0.5

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.4

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.1.23

1 year ago

1.1.22

2 years ago

1.1.19

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.9

4 years ago

1.1.12

4 years ago

1.1.10

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago