import { hsl } from 'd3-color' ; import { Column , isNumbersColumn , IDataRow , INumberColumn , isNumberColumn , isMapAbleColumn , DEFAULT_COLOR , SolidColorFunction , } from '../model' ; import { CANVAS_HEIGHT , cssClass } from '../styles' ; import { colorOf } from './impose' ; import { IRenderContext , ERenderMode , ICellRendererFactory , IImposer , ICellRenderer , IGroupCellRenderer , ISummaryRenderer , } from './interfaces' ; import { renderMissingCanvas , renderMissingDOM } from './missing' ; import { noRenderer , setText } from './utils' ; export function toHeatMapColor ( v , row , col , imposer ? ) { if ( v == null || Number . isNaN ( v ) ) { v = 1 ; } if ( imposer || ! isMapAbleColumn ( col ) ) { const color = hsl ( colorOf ( col , row , imposer , v ) || DEFAULT_COLOR ) ; color . l = 1 - v ; return color . toString ( ) ; } const map = col . getColorMapping ( ) ; const valueColor = map . apply ( v ) ; if ( map instanceof SolidColorFunction ) { const color = hsl ( valueColor ) ; color . l = 1 - v ; return color . toString ( ) ; } return valueColor ; } export default class BrightnessCellRenderer implements ICellRendererFactory { readonly title : string = 'Brightness' ; canRender ( col , mode ) { return isNumberColumn ( col ) && mode === ERenderMode . CELL && ! isNumbersColumn ( col ) ; } create ( col , context , imposer ? ) { const width = context . colWidth ( col ) ; return { template : `<div title="">
        <div class="${ cssClass ( 'cat-color' ) }" style="background-color: ${ DEFAULT_COLOR }"></div><div class="${ cssClass ( 'cat-label' ) }"> </div>
      </div>` , update : ( n , d ) => { const missing = renderMissingDOM ( n , col , d ) ; n . title = col . getLabel ( d ) ; ( n . firstElementChild ! as HTMLDivElement ) . style . backgroundColor = missing ? null : toHeatMapColor ( col . getNumber ( d ) , d , col , imposer ) ; setText ( n . lastElementChild ! , n . title ) ; } , render : ( ctx , d ) => { if ( renderMissingCanvas ( ctx , col , d , width ) ) { return ; } ctx . fillStyle = toHeatMapColor ( col . getNumber ( d ) , d , col , imposer ) ; ctx . fillRect ( 0 , 0 , width , CANVAS_HEIGHT ) ; } , } ; } createGroup ( ) { return noRenderer ; } createSummary ( ) { return noRenderer ; } }