import * as colors from '../constants' ; import { ILoggerColorParams } from '../interfaces' ; see https param { string } str returns { string } export const stringToColor = ( str ) : string => { let hash = 0 ; let color = '#' ; for ( let i = 0 ; i < str . length ; i ++ ) { hash = str . charCodeAt ( i ) + ( ( hash << 5 ) - hash ) ; } for ( let i = 0 ; i < 3 ; i ++ ) { const value = ( hash >> ( i * 8 ) ) & 0xff ; color += ( '00' + value . toString ( 16 ) ) . substr ( - 2 ) ; } return color ; } ; param { string } hexadecimal returns { string } export function getInvertColor ( hex ) { let value = hex . slice ( 1 ) ; if ( ! [ 6 , 3 ] . includes ( value . length ) ) { throw new Error ( 'Invalid HEX color.' ) ; } if ( value . length === 3 ) { const [ r , g , b ] = value ; value = r + r + g + g + b + b ; } const [ r , g , b ] = value . match ( /.{1,2}/g ) . map ( ( c ) : number => parseInt ( c , 16 ) ) ; return r * 0.299 + g * 0.587 + b * 0.114 > 186 ? colors . loggerLuminosityColorsConstant . BLACK : colors . loggerLuminosityColorsConstant . WHITE ; } export const getColorsFromText = ( context ) : ILoggerColorParams => { const colorName = stringToColor ( context ) ; return { BACKGROUND_COLOR : colorName , TEXT_COLOR : getInvertColor ( colorName ) , } ; } ; param { ILoggerColorParams } color returns { Array < string > } export const getStyle = ( color ) : string => [ 'font-size: 11px;' , 'font-weight: 400;' , 'margin: 1px;' , 'padding: 2px;' , 'border-radius: 3px;' , `color: ${ color ? color . TEXT_COLOR : colors . loggerLuminosityColorsConstant . WHITE };` , `background-color: ${ color ? color . BACKGROUND_COLOR : colors . loggerLuminosityColorsConstant . BLACK };` , ] . join ( '\n' ) ;