Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:43
Show Gist options
  • Save rfprod/bb979f15b47225ed476393d42fd8a15d to your computer and use it in GitHub Desktop.
Save rfprod/bb979f15b47225ed476393d42fd8a15d to your computer and use it in GitHub Desktop.
HEX Colour Code To RGB
function hexColourToRGB(hexString) {
const HEXA_DECIMAL = '0123456789ABCDEF';
let output = { r:0, g:0, b:0 };
let hex = [];
for (let i = 1, max = hexString.length; i < max; i += 2) {
hex.push(hexString.slice(i, i + 2));
}
// convert base16 to base10
for (let i = 0, max = hex.length; i < max; i++) {
let base10 = 0;
let power = 0;
const input = hex[i];
for (let j = input.length - 1; j >= 0; j--) {
base10 += HEXA_DECIMAL.indexOf(input[j].toUpperCase()) * Math.pow(16, power);
power++;
}
if (i === 0) { output.r = base10; }
if (i === 1) { output.g = base10; }
if (i === 2) { output.b = base10; }
}
return output;
}
hexColourToRGB("#FF9933") // {r:255, g:153, b:51}

HEX Colour Code To RGB

Converts HEX colour code to RGB.

A script by V.

License.

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