Skip to content

Instantly share code, notes, and snippets.

@edmondyip
Created January 23, 2018 03:04
Show Gist options
  • Save edmondyip/eb9dd43377ce830a3b79cc6cb21c8d0e to your computer and use it in GitHub Desktop.
Save edmondyip/eb9dd43377ce830a3b79cc6cb21c8d0e to your computer and use it in GitHub Desktop.
Convert Hex to RGB in Vue Method
data: {
return {
opacity: 50
}
},
methods: {
convertHex: function (color) {
color = color.replace('#', '')
let r = parseInt(color.substring(0, 2), 16)
let g = parseInt(color.substring(2, 4), 16)
let b = parseInt(color.substring(4, 6), 16)
let result = 'rgba(' + r + ',' + g + ',' + b + ',' + this.opacity / 100 + ')'
return result
}
}
@flockast
Copy link

flockast commented Jan 25, 2021

Some refactoring changes

  methods: {
    convertHex (color) {
      color = color.replace('#', '')
      const r = parseInt(color.substring(0, 2), 16)
      const g = parseInt(color.substring(2, 4), 16)
      const b = parseInt(color.substring(4, 6), 16)
      return `rgba(${r}, ${g}, ${b}, ${this.opacity / 100})`
    }
  }

@neitony
Copy link

neitony commented Jun 1, 2022

Thank you, this is very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment