Last active
August 16, 2020 15:14
-
-
Save hz2/88339114b16ec408393f1aaffc219c16 to your computer and use it in GitHub Desktop.
1-line-code-convert-color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert hex color to RGBA color | |
// x => current item | |
// i => index | |
// o => origin value | |
const hex2rgba = h => h.replace('#','').split('').map((x,i,o)=>o.length< 6 ? [x,x]: x).flat().reduce((str,x,i,orig)=>i%2 ? str.replace("x", parseInt( orig[i-1]+ x , 16) ) + ([',',','].concat(orig.length < 7 ? [',1)']:[',',')']))[(i-1)/2] : str + 'x','rgba(') | |
// View Results | |
hex2rgba('#eeff00') | |
// > "rgba(238,255,0,1)" | |
hex2rgba('#eeff0080') | |
// > "rgba(238,255,0,128)" | |
hex2rgba('#f0f') | |
// > "rgba(255,0,255,1)" | |
hex2rgba('#f0f7') | |
// > "rgba(255,0,255,119)" | |
hex2rgba('eeff00') | |
// > "rgba(238,255,0,1)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment