Skip to content

Instantly share code, notes, and snippets.

@mrsoftware
Last active December 2, 2018 09:29
Show Gist options
  • Save mrsoftware/f365aa20b400ea09b1fc9d7fbf9d27db to your computer and use it in GitHub Desktop.
Save mrsoftware/f365aa20b400ea09b1fc9d7fbf9d27db to your computer and use it in GitHub Desktop.
Functions for convert HEX to RGBA
// function for calculating 6 letters hex value
export const hexToRgbaFull = (hex, opacity = 1) => {
hex = ( hex.charAt(0) === "#" ? hex.substr(1) : hex );
var r = parseInt(hex.substring(0, 2), 16);
var g = parseInt(hex.substring(2, 4), 16);
var b = parseInt(hex.substring(4, 6), 16);
return `rgba( ${r}, ${g}, ${b}, ${opacity} )`;
}
// function for calculating 3 letters hex value
export const hexToRgbaPartial = (hex, opacity = 1) => {
hex = ( hex.charAt(0) === "#" ? hex.substr(1) : hex );
var r = parseInt(hex.substring(0, 1) + hex.substring(0, 1), 16);
var g = parseInt(hex.substring(1, 2) + hex.substring(1, 2), 16);
var b = parseInt(hex.substring(2, 3) + hex.substring(2, 3), 16);
return `rgba( ${r}, ${g}, ${b}, ${opacity} )`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment