Hex Color to rgba


Published on January 21, 2020

Hex to RGBA color in JS

Let's create a function in JS to convert the Hex Color Code to RGBA with Opacity.


HexToRgba.js

const hexToRgba = (hex, opacity) => {
  hex = hex.replace("#", "")
  r = parseInt(hex.substring(0, 2), 16)
  g = parseInt(hex.substring(2, 4), 16)
  b = parseInt(hex.substring(4, 6), 16)
  result = "rgba(" + r + "," + g + "," + b + "," + opacity / 100 + ")"
  return result
}

export default hexToRgba

If you like it, share it!