@@ -180,7 +180,11 @@ export type TransformMode = 'nearest-neighbor' | 'bilinear' | 'bicubic';
180180export type AffineTransform = readonly [ a : number , b : number , c : number , d : number , e : number , f : number ] ;
181181
182182// the sampling modes of Image.transform()
183- const TRANSFORM_MODES : readonly string [ ] = [ 'nearest-neighbor' , 'bilinear' , 'bicubic' ] ;
183+ const TRANSFORM_MODES : readonly TransformMode [ ] = [ 'nearest-neighbor' , 'bilinear' , 'bicubic' ] ;
184+ const RESIZE_FITS : readonly ResizeFit [ ] = [ 'fill' , 'inside' , 'contain' , 'cover' ] ;
185+ const SUBSAMPLINGS : readonly string [ ] = [ '4:4:4' , '4:2:0' ] ;
186+ /** @internal */
187+ export const TONE_MAPPINGS : readonly ToneMapping [ ] = [ 'reinhard' , 'clamp' ] ;
184188/** @internal */
185189export const FLIP_DIRECTIONS : readonly FlipDirection [ ] = [ 'horizontal' , 'vertical' ] ;
186190
@@ -1751,7 +1755,6 @@ export class Image {
17511755 */
17521756 quantize ( options : QuantizeOptions = { } ) : this {
17531757 checkOptions ( options , 'image.quantize({ colors: 16 })' ) ;
1754- if ( typeof options !== 'object' || options === null ) throw new TypeError ( 'quantize takes an options object' ) ;
17551758 // (JavaScript callers can give both: checked below)
17561759 const { colors, palette, dither = 'floyd-steinberg' , ditherStrength = 0.85 } = options as DitherOptions & { colors ?: number , palette ?: Color [ ] } ;
17571760
@@ -1844,9 +1847,8 @@ export class Image {
18441847 if ( matrix === null || typeof matrix !== 'object' || matrix . length !== 6 ) throw new RangeError ( 'A transform has 6 values: [a, b, c, d, e, f]' ) ;
18451848 // (numbers only: a string is a mistake in data like this)
18461849 const values = Array . from ( { length : 6 } , ( _ , i ) => finite ( typeof matrix [ i ] === 'number' ? matrix [ i ] : NaN , 'transform value' ) ) ;
1847- if ( typeof options !== 'object' || options === null ) throw new TypeError ( 'transform takes an options object' ) ;
18481850 const { origin = { } , expand = true , mode = 'bilinear' } = options ;
1849- if ( ! TRANSFORM_MODES . includes ( mode ) ) throw new TypeError ( `transform mode must be one of ${ TRANSFORM_MODES . map ( name => `' ${ name } '` ) . join ( ' , ') } , not ${ String ( mode ) } ` ) ;
1851+ oneOf ( mode , TRANSFORM_MODES , 'transform mode' ) ;
18501852 const { x : pivotX = this . width / 2 , y : pivotY = this . height / 2 } = origin ;
18511853 finite ( pivotX , 'origin x' ) ;
18521854 finite ( pivotY , 'origin y' ) ;
@@ -2205,7 +2207,7 @@ export class Image {
22052207 rotate ( angle : number , options : RotateOptions = { } ) : this {
22062208 checkOptions ( options , 'rotate(angle, { expand: false })' ) ;
22072209 const { expand = true , mode = 'bilinear' } = options ;
2208- if ( ! TRANSFORM_MODES . includes ( mode ) ) throw new TypeError ( `rotate mode must be one of ${ TRANSFORM_MODES . map ( name => `' ${ name } '` ) . join ( ' , ') } , not ${ String ( mode ) } ` ) ;
2210+ oneOf ( mode , TRANSFORM_MODES , 'rotate mode' ) ;
22092211 // (counterclockwise from here on: the image's y axis points down, so this turns it clockwise on screen)
22102212 angle = ( ( - finite ( angle , 'angle' ) % 360 ) + 360 ) % 360 ;
22112213 if ( angle === 0 ) return this ;
@@ -2448,7 +2450,7 @@ export class Image {
24482450 checkOptions ( options , 'encodeJPEG({ quality: 80 })' ) ;
24492451 const { quality : qualityOption = 90 , subsampling, background = 0x000000ff , metadata = { } , gainMap : gainMapOption = true } = options ;
24502452 const quality = checkQuality ( qualityOption ) ;
2451- if ( subsampling !== undefined && subsampling !== '4:4:4' && subsampling !== '4:2:0' ) throw new TypeError ( ` subsampling must be '4:4:4' or '4:2:0', not ${ String ( subsampling ) } ` ) ;
2453+ if ( subsampling !== undefined ) oneOf ( subsampling , SUBSAMPLINGS , ' subsampling' ) ;
24522454 const backgroundColor = checkColor ( background , 'background' ) ;
24532455 const m = resolveMetadata ( metadata ) ;
24542456 const base = { exif : exifData ( m ) , comment : m . comment ? utf8 ( m . comment ) : undefined , subsampling } ;
@@ -2554,7 +2556,7 @@ export class Image {
25542556 const { quality = 60 , alphaQuality = 100 , subsampling = '4:4:4' , effort = 1 , threads = defaultThreads ( ) , metadata = { } , gainMap = true } = options ;
25552557 checkEffort ( effort , 2 ) ;
25562558 checkThreads ( threads ) ;
2557- if ( subsampling !== '4:4:4' && subsampling !== '4:2:0' ) throw new TypeError ( `subsampling must be '4:4:4' or '4:2:0', not ${ String ( subsampling ) } ` ) ;
2559+ oneOf ( subsampling , SUBSAMPLINGS , ' subsampling' ) ;
25582560 const exif = exifData ( resolveMetadata ( metadata ) ) ;
25592561 return avifEnc . encode ( this . __bytes__ ( ) , this . width , this . height , {
25602562 quality : checkQuality ( quality ) , alphaQuality : checkQuality ( alphaQuality , 'alphaQuality' ) , subsampling, effort, exif, threads, gainMap : this . __gain_map_pixels__ ( gainMap )
@@ -2628,7 +2630,7 @@ export class Image {
26282630 const { applyOrientation = true , applyColorProfile = true , maxPixels = MAX_PIXELS , toneMapping = 'reinhard' , threads = defaultThreads ( ) , gainMap = true } = options ;
26292631 checkThreads ( threads ) ;
26302632 if ( ! ( Number ( maxPixels ) > 0 ) ) throw new RangeError ( `maxPixels must be a positive number, not ${ maxPixels } ` ) ;
2631- if ( toneMapping !== 'reinhard' && toneMapping !== 'clamp' ) throw new TypeError ( `toneMapping must be 'reinhard' or 'clamp', not ${ String ( toneMapping ) } ` ) ;
2633+ oneOf ( toneMapping , TONE_MAPPINGS , ' toneMapping' ) ;
26322634 const data = view ( input ) ;
26332635 const dataView = new DataView ( data . buffer , data . byteOffset , data . byteLength ) ;
26342636
@@ -2973,7 +2975,7 @@ export function resizeArguments(widthOrOptions: number | ResizeToOptions, height
29732975 }
29742976 const { width, height : optionsHeight , fit = 'fill' , ...rest } = widthOrOptions ;
29752977 if ( width === undefined && optionsHeight === undefined ) throw new TypeError ( 'resize() needs a width, a height or both' ) ;
2976- if ( ! [ 'fill' , 'inside' , 'contain' , 'cover' ] . includes ( fit ) ) throw new TypeError ( `fit must be 'fill', 'inside' , 'contain' or 'cover', not ${ String ( fit ) } ` ) ;
2978+ oneOf ( fit , RESIZE_FITS , 'fit' ) ;
29772979 return [ width === undefined ? undefined : finite ( width , 'width' ) , optionsHeight === undefined ? undefined : finite ( optionsHeight , 'height' ) , rest , fit ] ;
29782980}
29792981
0 commit comments