import type { Handler } from '../types' ; import { clampDimensionsPreservingAspectRatio , VArg } from './utils' ; export const quality = { args : [ VArg ] , order : - 1 , apply : ( context , _pipe , quality ) => { context . quality = quality ; } } ; export const fit = { args : [ VArg ] , order : - 1 , apply : ( context , _pipe , fit ) => { context . fit = fit ; } } ; const HEX_RE = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i ; const SHORTHEX_RE = /^([a-f\d])([a-f\d])([a-f\d])$/i ; export const background = { args : [ VArg ] , order : - 1 , apply : ( context , _pipe , background ) => { background = String ( background ) ; if ( ! background . startsWith ( '#' ) && ( HEX_RE . test ( background ) || SHORTHEX_RE . test ( background ) ) ) { background = '#' + background ; } context . background = background ; } } ; export const enlarge = { args : [ ] , apply : ( context ) => { context . enlarge = true ; } } ; export const width = { args : [ VArg ] , apply : ( context , pipe , width ) => { return pipe . resize ( width , null , { withoutEnlargement : ! context . enlarge } ) ; } } ; export const height = { args : [ VArg ] , apply : ( context , pipe , height ) => { return pipe . resize ( null , height , { withoutEnlargement : ! context . enlarge } ) ; } } ; export const resize = { args : [ VArg , VArg , VArg ] , apply : ( context , pipe , size ) => { let [ width , height ] = String ( size ) . split ( 'x' ) . map ( v => Number ( v ) ) ; if ( ! context . enlarge ) { const clamped = clampDimensionsPreservingAspectRatio ( context . meta , { width , height } ) ; width = clamped . width ; height = clamped . height ; } return pipe . resize ( width , height , { fit : context . fit , background : context . background } ) ; } } ; export const trim = { args : [ VArg ] , apply : ( _context , pipe , threshold ) => { return pipe . trim ( threshold ) ; } } ; export const extend = { args : [ VArg , VArg , VArg , VArg ] , apply : ( context , pipe , top , right , bottom , left ) => { return pipe . extend ( { top , left , bottom , right , background : context . background } ) ; } } ; export const extract = { args : [ VArg , VArg , VArg , VArg ] , apply : ( context , pipe , top , right , bottom , left ) => { return pipe . extend ( { top , left , bottom , right , background : context . background } ) ; } } ; export const rotate = { args : [ VArg ] , apply : ( context , pipe , angel ) => { return pipe . rotate ( angel , { background : context . background } ) ; } } ; export const flip = { args : [ ] , apply : ( _context , pipe ) => { return pipe . flip ( ) ; } } ; export const flop = { args : [ ] , apply : ( _context , pipe ) => { return pipe . flop ( ) ; } } ; export const sharpen = { args : [ VArg , VArg , VArg ] , apply : ( _context , pipe , sigma , flat , jagged ) => { return pipe . sharpen ( sigma , flat , jagged ) ; } } ; export const median = { args : [ VArg , VArg , VArg ] , apply : ( _context , pipe , size ) => { return pipe . median ( size ) ; } } ; export const blur = { args : [ VArg , VArg , VArg ] , apply : ( _context , pipe ) => { return pipe . blur ( ) ; } } ; export const flatten = { args : [ VArg , VArg , VArg ] , apply : ( context , pipe ) => { return pipe . flatten ( { background : context . background } ) ; } } ; export const gamma = { args : [ VArg , VArg , VArg ] , apply : ( _context , pipe , gamma , gammaOut ) => { return pipe . gamma ( gamma , gammaOut ) ; } } ; export const negate = { args : [ VArg , VArg , VArg ] , apply : ( _context , pipe ) => { return pipe . negate ( ) ; } } ; export const normalize = { args : [ VArg , VArg , VArg ] , apply : ( _context , pipe ) => { return pipe . normalize ( ) ; } } ; export const threshold = { args : [ VArg ] , apply : ( _context , pipe , threshold ) => { return pipe . threshold ( threshold ) ; } } ; export const modulate = { args : [ VArg ] , apply : ( _context , pipe , brightness , saturation , hue ) => { return pipe . modulate ( { brightness , saturation , hue } ) ; } } ; export const tint = { args : [ VArg ] , apply : ( _context , pipe , rgb ) => { return pipe . tint ( rgb ) ; } } ; export const grayscale = { args : [ VArg ] , apply : ( _context , pipe ) => { return pipe . grayscale ( ) ; } } ; export const crop = extract ; export const q = quality ; export const b = background ; export const w = width ; export const h = height ; export const s = resize ;