/**
 *  RGB object type with red, green and blue components.
 */ export type RGB = { r : number ; g : number ; b : number ; } ; /**
 *  RGB list (array) type with red, green and blue components.
 */ export type RgbArray = [ number , number , number ] ; /**
 *  Hexadecimal representation of a color.
 */ export type HexColor = string ; /**
 *  Color represented as hexadecimal value or as RGB object or list.
 */ export type Color = RGB | RgbArray | HexColor ; /**
 *  Interface for defining black and white colors; used to amplify the contrast
 *  of the color inversion.
 */ export interface BlackWhite { black : HexColor ; white : HexColor ; threshold ? : number ; } const DEFAULT_THRESHOLD = Math . sqrt ( 1.05 * 0.05 ) - 0.05 ; const RE_HEX = /^(?:[0-9a-f]{3}){1,2}$/i ; const DEFAULT_BW = { black : '#000000' , white : '#ffffff' , threshold : DEFAULT_THRESHOLD } ; function padz ( str , len = 2 ) { return ( new Array ( len ) . join ( '0' ) + str ) . slice ( - len ) ; } function hexToRgbArray ( hex ) { if ( hex . slice ( 0 , 1 ) === '#' ) hex = hex . slice ( 1 ) ; if ( ! RE_HEX . test ( hex ) ) throw new Error ( `Invalid HEX color: "${ hex }"` ) ; if ( hex . length === 3 ) { hex = hex [ 0 ] + hex [ 0 ] + hex [ 1 ] + hex [ 1 ] + hex [ 2 ] + hex [ 2 ] ; } return [ parseInt ( hex . slice ( 0 , 2 ) , 16 ) , parseInt ( hex . slice ( 2 , 4 ) , 16 ) , parseInt ( hex . slice ( 4 , 6 ) , 16 ) ] ; } function toRGB ( c ) { return { r : c [ 0 ] , g : c [ 1 ] , b : c [ 2 ] } ; } function toRgbArray ( c ) { if ( ! c ) throw new Error ( 'Invalid color value' ) ; if ( Array . isArray ( c ) ) return c as RgbArray ; return typeof c === 'string' ? hexToRgbArray ( c ) : [ c . r , c . g , c . b ] ; } function getLuminance ( c ) { let i , x ; const a = [ ] ; for ( i = 0 ; i < c . length ; i ++ ) { x = c [ i ] / 255 ; a [ i ] = x <= 0.03928 ? x / 12.92 : Math . pow ( ( x + 0.055 ) / 1.055 , 2.4 ) ; } return 0.2126 * a [ 0 ] + 0.7152 * a [ 1 ] + 0.0722 * a [ 2 ] ; } function invertToBW ( color , bw , asArr ? ) { const options = ( bw === true ) ? DEFAULT_BW : Object . assign ( { } , DEFAULT_BW , bw ) ; return getLuminance ( color ) > options . threshold ? ( asArr ? hexToRgbArray ( options . black ) : options . black ) : ( asArr ? hexToRgbArray ( options . white ) : options . white ) ; } param { Color } color param { BlackWhite | boolean } bw returns { HexColor } function invert ( color , bw = false ) { color = toRgbArray ( color ) ; if ( bw ) return invertToBW ( color , bw ) as HexColor ; return '#' + color . map ( c => padz ( ( 255 - c ) . toString ( 16 ) ) ) . join ( '' ) ; } namespace namespace invert { alias param { Color } color param { BlackWhite | boolean } bw returns { RGB } export function asRGB ( color , bw ? ) { color = toRgbArray ( color ) ; const list = bw ? invertToBW ( color , bw , true ) as RgbArray : color . map ( c => 255 - c ) as RgbArray ; return toRGB ( list ) ; } param { Color } color param { BlackWhite | boolean } bw returns { RGB } export function asRgbArray ( color , bw ? ) { color = toRgbArray ( color ) ; return bw ? invertToBW ( color , bw , true ) as RgbArray : color . map ( c => 255 - c ) as RgbArray ; } type { number } export const defaultThreshold = DEFAULT_THRESHOLD ; /**
     *  Alias of `.asRGB()`
     */ export const asRgbObject = asRGB ; } export default invert ;