import { Colors } from '../../../common/colors' ; import { getPredicateFn } from '../../../common/predicate' ; import { safeFormat , ValueFormatter } from '../../../utils/common' ; import { ColorBand , HeatmapBandsColorScale } from '../specs/heatmap' ; import { ColorScale } from '../state/selectors/get_color_scale' ; function defaultColorBandFormatter ( valueFormatter ? ) { return ( startValue , endValue ) => { const finiteStart = Number . isFinite ( startValue ) ; const finiteEnd = Number . isFinite ( endValue ) ; const start = safeFormat ( startValue , valueFormatter ) ; const end = safeFormat ( endValue , valueFormatter ) ; return ! finiteStart && finiteEnd ? `< ${ end }` : finiteStart && ! finiteEnd ? `≥ ${ start }` : `${ start } - ${ end }` ; } ; } internal export function getBandsColorScale ( colorScale , valueFormatter ? ) { const labelFormatter = colorScale . labelFormatter ?? defaultColorBandFormatter ( valueFormatter ) ; const ascendingSortFn = getPredicateFn ( 'numAsc' , 'start' ) ; const bands = colorScale . bands . reduce < Required < ColorBand > [ ] > ( ( acc , { start , end , color , label } ) => { if ( start < end ) acc . push ( { start , end , color , label : label ?? labelFormatter ( start , end ) } ) ; return acc ; } , [ ] ) . sort ( ascendingSortFn ) ; const scale = getBandScale ( bands ) ; return { scale , bands } ; } function getBandScale ( bands ) { return ( value ) => { for ( let i = 0 ; i < bands . length ; i ++ ) { const { start , end , color } = bands [ i ] ; if ( start <= value && value < end ) { return color ; } } return Colors . Transparent . keyword ; } ; }