Created
December 5, 2019 19:47
-
-
Save smakosh/9b677299f8c98423e1b2a46f68b6f0ad to your computer and use it in GitHub Desktop.
Hex to RGBA - SC
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
const hexToRgb = hex => { | |
// http://stackoverflow.com/a/5624139 | |
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i | |
hex = hex.replace(shorthandRegex, (m, r, g, b) => { | |
return r + r + g + g + b + b | |
}) | |
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex) | |
return result | |
? { | |
r: parseInt(result[1], 16), | |
g: parseInt(result[2], 16), | |
b: parseInt(result[3], 16), | |
} | |
: null | |
} | |
const rgba = (hex, alpha) => { | |
const color = hexToRgb(hex) | |
return `rgba(${color.r}, ${color.g}, ${color.b}, ${alpha})` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment