11import _ from 'lodash' ;
22import { Category , matchString , loadClasses } from './classes' ;
3- const Color = require ( 'color' ) ;
4- const d3 = require ( 'd3' ) ;
3+ import Color from 'color' ;
4+ import * as d3 from 'd3' ;
55
66// TODO: Move elsewhere
77interface Event {
@@ -24,7 +24,9 @@ const COLOR_UNCAT = '#CCC';
2424const scale = d3 . scaleOrdinal ( [ '#90CAF9' , '#FFE082' , '#EF9A9A' , '#A5D6A7' ] ) ;
2525
2626// Needed to prewarm the color table
27- scale . domain ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 ] ) ;
27+ scale . domain (
28+ '0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20' . split ( / , / )
29+ ) ;
2830
2931const customColors = {
3032 afk : '#EEE' ,
@@ -52,9 +54,12 @@ const customColors = {
5254 // Social media sites
5355 'messenger.com' : Color ( '#3b5998' ) . lighten ( 0.5 ) ,
5456 'facebook.com' : Color ( '#3b5998' ) . lighten ( 0.5 ) ,
57+
58+ // Categories
59+ uncategorized : COLOR_UNCAT ,
5560} ;
5661
57- function hashcode ( str : string ) {
62+ function hashcode ( str : string ) : number {
5863 let hash = 0 ;
5964 if ( str . length === 0 ) {
6065 return hash ;
@@ -70,7 +75,7 @@ function hashcode(str: string) {
7075export function getColorFromString ( appname : string ) {
7176 appname = appname || '' ;
7277 appname = appname . toLowerCase ( ) ;
73- return customColors [ appname ] || scale ( Math . abs ( hashcode ( appname ) % 20 ) ) ;
78+ return customColors [ appname ] || scale ( Math . abs ( hashcode ( appname ) % 20 ) . toString ( ) ) ;
7479}
7580
7681// TODO: Move into vuex?
@@ -84,7 +89,6 @@ export function getColorFromCategory(c: Category, allCats: Category[]): string {
8489 const parentCat = allCats . find ( cc => _ . isEqual ( cc . name , parent ) ) ;
8590 return getColorFromCategory ( parentCat , allCats ) ;
8691 } else {
87- // TODO: Fix reasonable fallback
8892 return COLOR_UNCAT ;
8993 }
9094}
@@ -97,7 +101,17 @@ export function getCategoryColorFromString(str: string): string {
97101 if ( c !== null ) {
98102 return getColorFromCategory ( c , allCats ) ;
99103 } else {
100- // TODO: Fix reasonable fallback
104+ return fallbackColor ( str ) ;
105+ }
106+ }
107+
108+ function fallbackColor ( str : string ) : string {
109+ // Get fallback color
110+ // TODO: Fetch setting from somewhere better, where defaults are respected
111+ const useColorFallback = localStorage !== undefined ? localStorage . useColorFallback : true ;
112+ if ( useColorFallback ) {
113+ return getColorFromString ( str ) ;
114+ } else {
101115 return COLOR_UNCAT ;
102116 }
103117}
0 commit comments