Skip to content

Instantly share code, notes, and snippets.

@comficker
Created June 6, 2018 01:16
Show Gist options
  • Save comficker/871d378c535854c1c460f7867a191a5a to your computer and use it in GitHub Desktop.
Save comficker/871d378c535854c1c460f7867a191a5a to your computer and use it in GitHub Desktop.
Bellow are javascript code can convert hex to rgb color. Demo: http://aiconverter.com/color/hex-to-rgb
function HEX2RGB (hex) {
"use strict";
if (hex.charAt(0) === '#') {
hex = hex.substr(1);
}
if ((hex.length < 2) || (hex.length > 6)) {
return false;
}
var values = hex.split(''),
r,
g,
b;
if (hex.length === 2) {
r = parseInt(values[0].toString() + values[1].toString(), 16);
g = r;
b = r;
} else if (hex.length === 3) {
r = parseInt(values[0].toString() + values[0].toString(), 16);
g = parseInt(values[1].toString() + values[1].toString(), 16);
b = parseInt(values[2].toString() + values[2].toString(), 16);
} else if (hex.length === 6) {
r = parseInt(values[0].toString() + values[1].toString(), 16);
g = parseInt(values[2].toString() + values[3].toString(), 16);
b = parseInt(values[4].toString() + values[5].toString(), 16);
} else {
return false;
}
return [r, g, b];
}
@bathos
Copy link

bathos commented Aug 19, 2020

@meodai Thanks! I wasn’t aware of the context for this — the color names project looks cool. If I recall correctly, I encountered this gist because it was linked to from another site. Re: adding as a contributor — feel free, but not obligated.

@meodai
Copy link

meodai commented Aug 19, 2020

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