export type alias = ( string | RegExp ) [ ] ; export type RMSwatch = [ alias , number [ ] ] ; export type RMSwatchGenerator = ( buffer : Buffer ) => Promise < RMSwatch [ ] > ; import { readFileSync } from "fs" ; import { getFromNodeVibrant } from "../palette-generator/NodeVibrant" ; import mime from "mime" ; import Color from "color" ; import { log } from "./log" ; function getSwatch ( filepath , quality = 1 ) { const buffer = readFileSync ( filepath ) ; const mimep = mime . getType ( filepath ) || "image/png" ; if ( ! mimep ) console . log ( "failed to get mime type" ) ; return Promise . all ( [ getFromNodeVibrant ( buffer , { quality } ) ] ) . then ( palette => { const sumPalette = [ ] ; palette . forEach ( v => sumPalette . push ( ... v ) ) ; return sumPalette ; } ) ; } const regex = /\@(\w+)/ ; function parseSwatchStr ( str , swatch ) { const match = str . match ( regex ) ; if ( match && match [ 1 ] ) { const find = swatch . find ( ( [ v ] ) => v [ 0 ] == match [ 1 ] || v [ 1 ] == match [ 1 ] ) ; if ( find ) return Color ( find [ 1 ] ) ; } return undefined ; } export { getSwatch , parseSwatchStr } ;