Created
July 4, 2018 16:28
-
-
Save LB-Digital/1711f25f989533cd27293dcf705fbb6a to your computer and use it in GitHub Desktop.
JS function for converting a `HEX` colour code string with an alpha component, to `RGBA`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hexToRGBA(hex, opacity){ | |
var rgba = "rgba("; | |
var inc = Math.abs(hex.length - 5); | |
for (var i = 1; i < hex.length; i+=inc){ | |
var component = hex[i] + hex[i + (inc-1)]; | |
rgba += parseInt(component, 16) + ","; | |
} | |
return rgba + opacity + ")"; | |
} | |
// abs() gives absolute val (modulus); -5 just produces correct increment (1 or 2, based on length of 4 or 7... #f0f vs #ff00ff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment