1+ import type { DitherMethod } from '../image.ts' ;
12
23// perceptual channel weights for squared color distances
34const WR = 3 , WG = 4 , WB = 2 ;
@@ -14,9 +15,9 @@ export interface QuantizeOptions {
1415 transparent ?: boolean ;
1516}
1617
17- /** The dithering methods of Image.quantize() and the GIF encoders */
18- export const DITHER_METHODS = [ 'none' , 'floyd-steinberg' , 'atkinson' , 'ordered' ] as const ;
19- export type DitherMethod = typeof DITHER_METHODS [ number ] ;
18+ // every DitherMethod (a missing key is a type error)
19+ const DITHER : Record < DitherMethod , true > = { 'none' : true , 'floyd-steinberg' : true , 'atkinson' : true , 'ordered' : true } ;
20+ export const DITHER_METHODS = Object . keys ( DITHER ) as DitherMethod [ ] ;
2021
2122/**
2223 * Checks palette options (`colors` from 2 to 256, a dither method, a strength 0-1) before anything changes, with the
@@ -185,7 +186,7 @@ const ATKINSON = [[1, 0, 1 / 8], [2, 0, 1 / 8], [-1, 1, 1 / 8], [0, 1, 1 / 8], [
185186 * the neighbors that come after it (serpentine: rows alternate direction), `'ordered'` offsets each pixel by a 4x4
186187 * Bayer threshold. `strength` (0-1) scales the spread error and the threshold offset; 0 changes nothing but rounding
187188 */
188- export function applyPalette ( pixels : Uint8Array , width : number , height : number , palette : Uint8Array , dither : 'none' | 'floyd-steinberg' | 'atkinson' | 'ordered' , strength : number ) : void {
189+ export function applyPalette ( pixels : Uint8Array , width : number , height : number , palette : Uint8Array , dither : DitherMethod , strength : number ) : void {
189190 const entries = palette . length / 3 ;
190191 if ( ! entries ) return ;
191192 const nearest = nearestSearch ( palette , entries ) ;
0 commit comments