import { timed } from "@thi.ng/bench/timed" ; import { lch } from "@thi.ng/color/lch/lch" ; import { srgb } from "@thi.ng/color/srgb/srgb" ; import { dominantColors } from "@thi.ng/pixel/dominant-colors" ; import { floatBuffer } from "@thi.ng/pixel/float" ; import { FLOAT_RGB } from "@thi.ng/pixel/format/float-rgb" ; import { intBufferFromImage } from "@thi.ng/pixel/int" ; import { map } from "@thi.ng/transducers/map" ; import { minMax } from "@thi.ng/transducers/min-max" ; import { transduce } from "@thi.ng/transducers/transduce" ; import { type , DominantColor , type , SortMode , SORT_MODES } from "./api" ; param img param num param minChroma export const processImage = ( img , num , minChroma ) => timed ( ( ) => { let buf = intBufferFromImage ( img ) ; buf = buf . scale ( 256 / Math . max ( buf . width , buf . height ) , "nearest" ) ; const colors = dominantColors ( floatBuffer ( buf , FLOAT_RGB ) , num , { filter : ( p ) => lch ( srgb ( p ) ) . c >= minChroma , } ) . map ( ( c ) => < DominantColor > { col : lch ( srgb ( c . color ) ) , area : c . area } ) ; return { buf , colors } ; } ) ; export const postProcess = ( colors , minArea , sortMode ) => { minArea *= 0.01 ; colors = colors . filter ( ( c ) => c . area >= minArea ) . sort ( SORT_MODES [ sortMode ] ) ; const hues = transduce ( map ( ( c ) => c . col . h ) , minMax ( ) , colors ) ; return { colors , hues } ; } ;