import { APCAContrast } from './apca_color_contrast' ; import { RgbaTuple , RGBATupleToString , RgbTuple } from './color_library_wrappers' ; import { Colors } from './colors' ; import { getWCAG2ContrastRatio } from './wcag2_color_contrast' ; internal export function hueInterpolator ( colors ) { return ( d ) => RGBATupleToString ( colors [ Math . round ( d * 255 ) ] ) ; } internal export function arrayToLookup ( keyFun , array ) { return Object . assign ( { } , ... array . map ( ( d ) => ( { [ keyFun ( d ) ] : d } ) ) ) ; } internal export function combineColors ( [ fgR , fgG , fgB , fgA ] , [ bgR , bgG , bgB , bgA ] ) { if ( fgA === 1 ) { return [ fgR , fgG , fgB , fgA ] ; } const alpha = fgA + bgA * ( 1 - fgA ) ; if ( alpha === 0 ) { return Colors . Transparent . rgba ; } const r = Math . round ( ( fgR * fgA + bgR * bgA * ( 1 - fgA ) ) / alpha ) ; const g = Math . round ( ( fgG * fgA + bgG * bgA * ( 1 - fgA ) ) / alpha ) ; const b = Math . round ( ( fgB * fgA + bgB * bgA * ( 1 - fgA ) ) / alpha ) ; return [ r , g , b , alpha ] ; } function getHighContrastColorWCAG2 ( background ) { const wWhite = getWCAG2ContrastRatio ( Colors . White . rgba , background ) ; const wBlack = getWCAG2ContrastRatio ( Colors . Black . rgba , background ) ; return wWhite >= wBlack ? Colors . White . rgba : Colors . Black . rgba ; } function getHighContrastColorAPCA ( background ) { const wWhiteText = Math . abs ( APCAContrast ( background , Colors . White . rgba ) ) ; const wBlackText = Math . abs ( APCAContrast ( background , Colors . Black . rgba ) ) ; return wWhiteText > wBlackText ? Colors . White . rgba : Colors . Black . rgba ; } const HIGH_CONTRAST_FN = { WCAG2 : getHighContrastColorWCAG2 , WCAG3 : getHighContrastColorAPCA , } ; internal export function highContrastColor ( background , mode = 'WCAG2' ) { return HIGH_CONTRAST_FN [ mode ] ( background ) ; }