import { coloursCode } from '@src/constants' ; export const determineColorIntensity = ( userColor ) : 'light' | 'dark' => { let color = userColor ; let r , g , b , hsp ; if ( coloursCode . hasOwnProperty ( color ) ) { color = coloursCode [ color ] ; } if ( color . match ( /^rgb/ ) ) { color = color . match ( /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/ ) || '' ; r = parseInt ( color [ 1 ] ) ; g = parseInt ( color [ 2 ] ) ; b = parseInt ( color [ 3 ] ) ; } else { color = + ( '0x' + color . slice ( 1 ) . replace ( ( color . length < 5 && /./g ) || '' , '$&$&' ) ) ; r = color >> 16 ; g = ( color >> 8 ) & 255 ; b = color & 255 ; } hsp = Math . sqrt ( 0.299 * ( r * r ) + 0.587 * ( g * g ) + 0.114 * ( b * b ) ) ; if ( hsp > 127.5 ) return 'light' ; else return 'dark' ; } ;