1- import { hex2rgb , rgb2hsv , rgba } from '@swiftcarrot/color-fns' ;
1+ import { hex2rgb , rgb2hsv , rgba , rgb2hex } from '@swiftcarrot/color-fns' ;
22
33export function hex2alpha ( aa ) {
44 return Math . round ( ( parseInt ( '0x' + aa , 16 ) / 255 ) * 100 ) ;
55}
66
7+ export function alpha2hex ( a ) {
8+ return ( Math . round ( ( a / 100 ) * 255 ) + 0x10000 ) . toString ( 16 ) . substr ( - 2 ) ;
9+ }
10+
711export function parseColor ( hexColor ) {
812 hexColor = hexColor . toLowerCase ( ) ;
913 const hex = hexColor ;
@@ -15,32 +19,9 @@ export function parseColor(hexColor) {
1519 return { ...hsv , r, g, b, a, hex, rgba : rgba ( r , g , b , a ) } ;
1620}
1721
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 ( '' ) ;
22+ export function rgba2hex ( r , g , b , a ) {
23+ const hex = rgb2hex ( r , g , b ) ;
24+ return hex + alpha2hex ( a ) ;
4425}
4526
4627export {
0 commit comments