Skip to content

Instantly share code, notes, and snippets.

@smakosh
Last active December 22, 2020 18:51
Show Gist options
  • Save smakosh/a33f39aab14a55132c82fc10e058dfc8 to your computer and use it in GitHub Desktop.
Save smakosh/a33f39aab14a55132c82fc10e058dfc8 to your computer and use it in GitHub Desktop.
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})`
}
// background: ${rgba(color, 0.2)};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment