Skip to content

Instantly share code, notes, and snippets.

@smakosh
Created December 5, 2019 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smakosh/9b677299f8c98423e1b2a46f68b6f0ad to your computer and use it in GitHub Desktop.
Save smakosh/9b677299f8c98423e1b2a46f68b6f0ad to your computer and use it in GitHub Desktop.
Hex to RGBA - SC
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