88 max ,
99 min ,
1010 extent ,
11+ rgb ,
1112} from 'd3' ;
1213import fromPairs from 'lodash/fromPairs' ;
1314import isEqual from 'lodash/isEqual' ;
@@ -22,6 +23,7 @@ import { TimeCell } from './components/cells/time';
2223import { NumberCell } from './components/cells/number' ;
2324import { RawNumberCell } from './components/cells/raw-number' ;
2425import { StringCell } from './components/cells/string' ;
26+ import { ColorCell } from './components/cells/color' ;
2527import { CategoryCell } from './components/cells/category' ;
2628import { StringFilter } from './components/filters/string' ;
2729import { CategoryFilter } from './components/filters/category' ;
@@ -571,6 +573,17 @@ function generateSchema(data: any[]) {
571573 return false ;
572574 }
573575 } ;
576+ const isColor = ( value : any ) => {
577+ try {
578+ if ( typeof value === 'string' ) {
579+ const color = rgb ( value ) ;
580+ return ! ! color ;
581+ }
582+ return false ;
583+ } catch ( e ) {
584+ return false ;
585+ }
586+ } ;
574587 const isFirstValueADate = isDate ( value ) ;
575588 if ( isFirstValueADate ) {
576589 const values = data . map ( ( d ) => d [ metric ] ) . filter ( ( d ) => d ) ;
@@ -598,6 +611,17 @@ function generateSchema(data: any[]) {
598611 const areMultipleValuesTimes = ! values . find ( ( d ) => ! isTime ( d ) ) ;
599612 if ( areMultipleValuesTimes ) return [ metric , 'time' ] ;
600613 }
614+
615+ const isFirstValueAColor = isColor ( value ) ;
616+ if ( isFirstValueAColor ) {
617+ const values = data
618+ . map ( ( d ) => d [ metric ] )
619+ . filter ( ( d ) => d )
620+ . slice ( 0 , 30 ) ;
621+ const areMultipleValuesColors = ! values . find ( ( d ) => ! isColor ( d ) ) ;
622+ if ( areMultipleValuesColors ) return [ metric , 'color' ] ;
623+ }
624+
601625 const isFirstValueAnArray = Array . isArray ( value ) ;
602626 if ( isFirstValueAnArray ) {
603627 const values = data . map ( ( d ) => d [ metric ] ) . filter ( ( d ) => d ) ;
@@ -610,6 +634,7 @@ function generateSchema(data: any[]) {
610634 : 'short-array' ,
611635 ] ;
612636 }
637+
613638 const isObject = typeof value === 'object' ;
614639 if ( isObject ) {
615640 return [ metric , 'object' ] ;
@@ -729,6 +754,13 @@ export const cellTypeMap = {
729754 shortFormat : ( d : string ) => d ,
730755 sortValueType : 'string' ,
731756 } ,
757+ color : {
758+ cell : ColorCell ,
759+ filter : StringFilter ,
760+ format : ( d : string ) => d ,
761+ shortFormat : ( d : string ) => d ,
762+ sortValueType : 'string' ,
763+ } ,
732764 object : {
733765 cell : StringCell ,
734766 filter : StringFilter ,
0 commit comments