@@ -6,7 +6,7 @@ export function hex2alpha(aa) {
66
77export function parseColor ( hexColor ) {
88 hexColor = hexColor . toLowerCase ( ) ;
9- const hex = hexColor . substr ( 0 , 7 ) ;
9+ const hex = hexColor ;
1010 const rgb = hex2rgb ( hex ) ;
1111 const { r, g, b } = rgb ;
1212 const hsv = rgb2hsv ( r , g , b ) ;
@@ -15,6 +15,34 @@ export function parseColor(hexColor) {
1515 return { ...hsv , r, g, b, a, hex, rgba : rgba ( r , g , b , a ) } ;
1616}
1717
18+ export function trim ( str ) {
19+ return str . replace ( / ^ \s + | \s + $ / gm, '' ) ;
20+ }
21+
22+ export function rgbaToHex ( rgba ) {
23+ let inParts = rgba . substring ( rgba . indexOf ( '(' ) ) . split ( ',' ) ,
24+ r = parseInt ( trim ( inParts [ 0 ] . substring ( 1 ) ) , 10 ) ,
25+ g = parseInt ( trim ( inParts [ 1 ] ) , 10 ) ,
26+ b = parseInt ( trim ( inParts [ 2 ] ) , 10 ) ,
27+ a = parseFloat (
28+ trim ( inParts [ 3 ] . substring ( 0 , inParts [ 3 ] . length - 1 ) ) ,
29+ ) . toFixed ( 2 ) ;
30+ let outParts = [
31+ r . toString ( 16 ) ,
32+ g . toString ( 16 ) ,
33+ b . toString ( 16 ) ,
34+ Math . round ( a * 255 )
35+ . toString ( 16 )
36+ . substring ( 0 , 2 ) ,
37+ ] ;
38+ outParts . forEach ( function ( part , i ) {
39+ if ( part . length === 1 ) {
40+ outParts [ i ] = '0' + part ;
41+ }
42+ } ) ;
43+ return '#' + outParts . join ( '' ) ;
44+ }
45+
1846export {
1947 rgb2hsv ,
2048 hsv2hex ,
0 commit comments