Skip to content

Instantly share code, notes, and snippets.

@magicznyleszek
Last active August 22, 2022 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save magicznyleszek/9d98aa95e2286a197dcd to your computer and use it in GitHub Desktop.
Save magicznyleszek/9d98aa95e2286a197dcd to your computer and use it in GitHub Desktop.
Hex to RGB
function hexToRgb(hex) {
// Expand shorthand form ("#FFF") to full form ("#FFFFF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
// return hex values
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
var hexes = ['#d9d9d9', '#cbcbcb', '#d7d7d7', '#ededed', '#aaaaaa', '#cdcdcd', '#c1c1c1', '#e5e5e5', '#c8c8c8', '#9d9d9d', '#d0d0d0', '#f2f2f2', '#e1e1e1', '#808080', '#03101a', '#6c9600', '#a6c500', '#739900', '#608000', '#a6a6a6', '#656565', '#666666', '#333333', '#bfbfbf', '#4c4c4c', '#404040', '#d4d4d4'];
hexes.forEach(function (color) {
console.log(color, hexToRgb(color));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment