forked from colorjs/get-image-colors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (51 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const getPixels = require('get-pixels')
const getRgbaPalette = require('get-rgba-palette')
const chroma = require('chroma-js')
const getSvgColors = require('get-svg-colors')
const pify = require('pify')
const patterns = {
image: /\.(gif|jpg|png|svg)$/i,
raster: /\.(gif|jpg|png)$/i,
svg: /svg$/i
}
function colorPalette (input, options, callback) {
if (typeof options === 'function') {
callback = options
options = {
type: undefined,
count: 5
}
} else if (typeof options === 'string') {
options = {
type: options,
count: 5
}
}
// SVG
if (!Buffer.isBuffer(input)) {
if (input.match(patterns.svg)) {
return callback(null, getSvgColors(input, { flat: true }))
}
} else if (options.type === 'image/svg+xml') {
return callback(null, getSvgColors(input, { flat: true }))
}
// PNG, GIF, JPG
return paletteFromBitmap(input, options, callback)
}
function paletteFromBitmap (filename, options, callback) {
if (!callback) {
callback = options
options = {
type: undefined,
count: 5
}
}
getPixels(filename, options.type, function (err, pixels) {
if (err) return callback(err)
const palette = getRgbaPalette(pixels.data, options.count).map(function (rgba) {
return chroma(rgba)
})
return callback(null, palette)
})
}
module.exports = pify(colorPalette)