import { IHSLA , IRGBA } from '../ts-interfaces/interfaces' ; import { named } from '../data/named' ; interface IcolorToHsla { color : string ; format : string ; } export const colorToHsla = ( { color , format } ) : IHSLA | string => { color = color . toLowerCase ( ) ; if ( format === 'keyword' ) { return color ; } if ( format . includes ( 'hsl' ) ) { return hslaFromString ( color ) ; } if ( format . includes ( 'rgb' ) ) { return RGBToHSL ( rgbaFromString ( color ) ) ; } if ( format === 'named' ) { return RGBToHSL ( rgbaFromString ( < string > named [ color ] ) ) ; } return RGBToHSL ( hexToRGB ( color ) ) ; } ; const hslaFromString = ( str ) : IHSLA => { const colorMatch = < string [ ] > str . match ( /\(((.*))\)/ ) ; const colorStr = colorMatch [ 1 ] ; const colorParts = colorStr . split ( ',' ) ; const colorPartsAsNums = colorParts . map ( item => { return + item . trim ( ) . replace ( '%' , '' ) ; } ) ; const [ h , s , l , a ] = colorPartsAsNums ; return { h , s , l , a } ; } ; const rgbaFromString = ( str ) : IRGBA => { const colorMatch = < string [ ] > str . match ( /\(((.*))\)/ ) ; const colorStr = colorMatch [ 1 ] ; const colorParts = colorStr . split ( ',' ) ; const colorPartsAsNums = colorParts . map ( item => { return + item . trim ( ) . replace ( '%' , '' ) ; } ) ; const [ r , g , b , a ] = colorPartsAsNums ; return { r , g , b , a } ; } ; const hexToRGB = ( hex ) : IRGBA => { let r = '' ; let g = '' ; let b = '' ; let a = '' ; if ( hex . length < 7 ) { r = '0x' + hex [ 1 ] + hex [ 1 ] ; g = '0x' + hex [ 2 ] + hex [ 2 ] ; b = '0x' + hex [ 3 ] + hex [ 3 ] ; if ( hex [ 4 ] ) { a = '0x' + hex [ 4 ] + hex [ 4 ] ; } } else { r = '0x' + hex [ 1 ] + hex [ 2 ] ; g = '0x' + hex [ 3 ] + hex [ 4 ] ; b = '0x' + hex [ 5 ] + hex [ 6 ] ; if ( hex [ 7 ] ) { a = '0x' + hex [ 7 ] + hex [ 8 ] ; } } if ( a ) { a = ( parseInt ( a , 16 ) / 255 ) . toFixed ( 1 ) ; } return { r : + r , g : + g , b : + b , a : + a } ; } ; const RGBToHSL = ( { r , g , b , a } ) : IHSLA => { r /= 255 ; g /= 255 ; b /= 255 ; const cmin = Math . min ( r , g , b ) ; const cmax = Math . max ( r , g , b ) ; const delta = cmax - cmin ; let h = 0 ; let s = 0 ; let l = 0 ; if ( delta === 0 ) { h = 0 ; } else if ( cmax === r ) { h = ( ( g - b ) / delta ) % 6 ; } else if ( cmax === g ) { h = ( b - r ) / delta + 2 ; } else { h = ( r - g ) / delta + 4 ; } h = Math . round ( h * 60 ) ; if ( h < 0 ) { h += 360 ; } l = ( cmax + cmin ) / 2 ; s = delta === 0 ? 0 : delta / ( 1 - Math . abs ( 2 * l - 1 ) ) ; s = + ( s * 100 ) . toFixed ( 1 ) ; l = + ( l * 100 ) . toFixed ( 1 ) ; return { h , s , l , a } ; } ;