const namesRGB = [ 'r' , 'g' , 'b' ] ; /**
 * this function converts figma color to RGB(A) (array)
 */ function figmaRGBToWebRGB ( color ) ; function figmaRGBToWebRGB ( color ) ; function figmaRGBToWebRGB ( color ) { const rgb = [ ] ; namesRGB . forEach ( ( e , i ) => { rgb [ i ] = Math . round ( color [ e ] * 255 ) ; } ) ; if ( color [ 'a' ] !== undefined ) rgb [ 3 ] = Math . round ( color [ 'a' ] * 100 ) / 100 ; return rgb ; } /**
 * this function converts RGB(A) color (array) to figma color
 */ function webRGBToFigmaRGB ( color ) ; function webRGBToFigmaRGB ( color ) ; function webRGBToFigmaRGB ( color ) { const rgb = { } ; namesRGB . forEach ( ( e , i ) => { rgb [ e ] = color [ i ] / 255 ; } ) ; if ( color [ 3 ] !== undefined ) rgb [ 'a' ] = color [ 3 ] ; return rgb ; } /**
 * this function converts figma color to HEX (string)
 */ function figmaRGBToHex ( color ) { let hex = '#' ; const rgb = figmaRGBToWebRGB ( color ) as webRGB | webRGBA ; hex += ( ( 1 << 24 ) + ( rgb [ 0 ] << 16 ) + ( rgb [ 1 ] << 8 ) + rgb [ 2 ] ) . toString ( 16 ) . slice ( 1 ) ; if ( rgb [ 3 ] !== undefined ) { const a = Math . round ( rgb [ 3 ] * 255 ) . toString ( 16 ) ; if ( a . length == 1 ) { hex += '0' + a ; } else { if ( a !== 'ff' ) hex += a ; } } return hex ; } /**
 * this function converts HEX color (string) to figma color
 */ function hexToFigmaRGB ( color ) { let opacity = '' ; color = color . toLowerCase ( ) ; if ( color [ 0 ] === '#' ) color = color . slice ( 1 ) ; if ( color . length === 3 ) { color = color . replace ( /(.)(.)(.)?/g , '$1$1$2$2$3$3' ) ; } else if ( color . length === 8 ) { const arr = color . match ( /(.{6})(.{2})/ ) ; color = arr [ 1 ] ; opacity = arr [ 2 ] ; } const num = parseInt ( color , 16 ) ; const rgb = [ num >> 16 , ( num >> 8 ) & 255 , num & 255 ] ; if ( opacity ) { rgb . push ( parseInt ( opacity , 16 ) / 255 ) ; return webRGBToFigmaRGB ( rgb as webRGBA ) ; } else { return webRGBToFigmaRGB ( rgb as webRGB ) ; } } export { figmaRGBToWebRGB , webRGBToFigmaRGB , figmaRGBToHex , hexToFigmaRGB } ; type webRGB = [ number , number , number ] ; type webRGBA = [ number , number , number , number ] ; export function getCorrectTextColor ( hex ) { const threshold = 160 ; const hRed = hexToR ( hex ) ; const hGreen = hexToG ( hex ) ; const hBlue = hexToB ( hex ) ; function hexToR ( h ) { return parseInt ( ( cutHex ( h ) ) . substring ( 0 , 2 ) , 16 ) ; } function hexToG ( h ) { return parseInt ( ( cutHex ( h ) ) . substring ( 2 , 4 ) , 16 ) ; } function hexToB ( h ) { return parseInt ( ( cutHex ( h ) ) . substring ( 4 , 6 ) , 16 ) ; } function cutHex ( h ) { return ( h . charAt ( 0 ) == "#" ) ? h . substring ( 1 , 7 ) : h ; } const cBrightness = ( ( hRed * 299 ) + ( hGreen * 587 ) + ( hBlue * 114 ) ) / 1000 ; if ( cBrightness > threshold ) { return "#000000" ; } else { return "#ffffff" ; } }