-
-
Save simonneutert/baacc99cffa864e19cb4ebe93d29948f to your computer and use it in GitHub Desktop.
Convert Hex Color to rgba with opacity
This file contains hidden or 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
/** | |
* ECMA2015 | |
*/ | |
function convertHex(hexCode,opacity){ | |
var hex = hexCode.replace('#',''); | |
if (hex.length === 3) { | |
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; | |
} | |
var r = parseInt(hex.substring(0,2), 16), | |
g = parseInt(hex.substring(2,4), 16), | |
b = parseInt(hex.substring(4,6), 16); | |
return 'rgba('+r+','+g+','+b+','+opacity/100+')'; | |
} | |
/** | |
* ECMA2016 / ES6 | |
*/ | |
const convertHexToRGBA = (hexCode, opacity) => { | |
let hex = hexCode.replace('#', ''); | |
if (hex.length === 3) { | |
hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`; | |
} | |
const r = parseInt(hex.substring(0, 2), 16); | |
const g = parseInt(hex.substring(2, 4), 16); | |
const b = parseInt(hex.substring(4, 6), 16); | |
return `rgba(${r},${g},${b},${opacity / 100})`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment