import '@/extensions' ; /**
 * 加0补位
 */ const fAddZero = ( v ) : string => { let newV = `00${ v }` ; return newV . substring ( newV . length - 2 , newV . length ) ; } ; /**
 * rgb转hex
 */ const fColorToHex = ( r , g , b ) : string => `#${ fAddZero ( r . toString ( 16 ) ) }${ fAddZero ( g . toString ( 16 ) ) }${ fAddZero ( b . toString ( 16 ) ) }` ; /**
 * 解析rgb格式
 */ const fAnalysisRGB = ( temp ) : Array < number > => { let colorTemp = temp . toLowerCase ( ) . substring ( 1 , temp . length ) ; let colors = [ ] ; colors . push ( parseInt ( colorTemp . substring ( 0 , 2 ) , 16 ) ) ; colors . push ( parseInt ( colorTemp . substring ( 2 , 4 ) , 16 ) ) ; colors . push ( parseInt ( colorTemp . substring ( 4 , 6 ) , 16 ) ) ; return colors ; } ; description param startColor param endColor param num returns const fColorGradualChange = ( startColor , endColor , num ) : Array < string > => { let rgb = /^rgb|RGB\((([0-9]|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])),){2}([0-9]|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\)$/ ; let hex = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i ; let startRGB = [ ] ; let endRGB = [ ] ; if ( startColor . test ( hex ) ) startRGB = fAnalysisRGB ( startColor ) ; else if ( startColor . test ( rgb ) ) startRGB = startColor . substring ( 3 , 15 ) . split ( ',' ) . map ( ( item ) : number => parseInt ( item ) ) ; if ( endColor . test ( hex ) ) endRGB = fAnalysisRGB ( endColor ) ; else if ( startColor . test ( rgb ) ) endRGB = endColor . substring ( 3 , 15 ) . split ( ',' ) . map ( ( item ) : number => parseInt ( item ) ) ; let [ startR , startG , startB ] = startRGB ; let [ endR , endG , endB ] = endRGB ; let sR = ( endR - startR ) / num ; let sG = ( endG - startG ) / num ; let sB = ( endB - startB ) / num ; let colors = [ ] ; for ( let i = 0 ; i < num ; i ++ ) { colors . push ( fColorToHex ( Math . floor ( sR * i + startR ) , Math . floor ( sG * i + startG ) , Math . floor ( sB * i + startB ) ) ) ; } return colors ; } ; description param hexColor returns const hexToRgb = ( hexColor ) : number [ ] => { let sColor = hexColor . toLowerCase ( ) ; let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/ ; if ( sColor && sColor . test ( reg ) ) { if ( sColor . length === 4 ) { let sColorNew = '#' ; for ( let i = 1 ; i < 4 ; i += 1 ) { sColorNew += sColor . slice ( i , i + 1 ) . concat ( sColor . slice ( i , i + 1 ) ) ; } sColor = sColorNew ; } let sColorChange = [ ] ; for ( let i = 1 ; i < 7 ; i += 2 ) { sColorChange . push ( parseInt ( `0x${ sColor . slice ( i , i + 2 ) }` ) ) ; } return sColorChange ; } return [ 255 , 0 , 0 ] ; } ; description param number returns const rgbToHsl = ( rgb ) : Array < number > => { let [ r , g , b ] = rgb ; r /= 255 ; g /= 255 ; b /= 255 ; let max = Math . max ( r , g , b ) ; let min = Math . min ( r , g , b ) ; let h = 0 ; let s = 0 ; let l = ( max + min ) / 2 ; if ( max === min ) { h = 0 ; s = 0 ; } else { let d = max - min ; s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min ) ; switch ( max ) { case r : h = ( g - b ) / d + ( g < b ? 6 : 0 ) ; break ; case g : h = ( b - r ) / d + 2 ; break ; case b : h = ( r - g ) / d + 4 ; break ; } h /= 6 ; } return [ h , s , l ] ; } ; const colorArray = [ '#FF0000' , '#FF9A00' , '#CCFF00' , '#33FF00' , '#00FF67' , '#00FFFF' , '#0066FF' , '#3300FF' , '#CC00FF' , '#FF0099' , '#FF0000' ] ; param value returns export const getHslColor = ( value ) : Array < number > => { let step = 10 ; let colorZone = Math . floor ( Math . abs ( value - 1 ) / step ) ; return rgbToHsl ( hexToRgb ( fColorGradualChange ( colorArray [ colorZone ] , colorArray [ colorZone + 1 ] , step ) [ value % step ] ) ) ; } ; param value returns export const getHexColor = ( value ) : string => { let step = 10 ; let colorZone = Math . floor ( Math . abs ( value - 1 ) / step ) ; return fColorGradualChange ( colorArray [ colorZone ] , colorArray [ colorZone + 1 ] , step ) [ value % step ] ; } ;