-
Notifications
You must be signed in to change notification settings - Fork 212
/
index.ts
32 lines (26 loc) · 821 Bytes
/
index.ts
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
import {
getImageData,
getCounts
} from './helpers'
interface Opts {
ignore?: string[]
scale?: number
skipTransparentPixels?: boolean
}
const defaultOpts: Opts = {
ignore: [],
scale: 1
}
export default async function (src: string, opts: Opts = defaultOpts): Promise<{ color: string, count: number }[] > {
opts = { ...defaultOpts, ...opts }
const {
ignore, // for example, to ignore white and black: [ 'rgb(0,0,0)', 'rgb(255,255,255)' ]
scale // 0 = best performance, lowest fidelity
// 1 = best fidelity, worst performance
} = opts
if (scale > 1 || scale <= 0) {
console.warn(`You set scale to ${scale}, which isn't between 0-1. This is either pointless (> 1) or a no-op (≤ 0)`)
}
const data = await getImageData(src, scale)
return getCounts(data, ignore)
}