Skip to content

Instantly share code, notes, and snippets.

@patelnwd
Created October 10, 2018 08:40
Show Gist options
  • Save patelnwd/1f99a16022cfb5c84285aadc2aac661d to your computer and use it in GitHub Desktop.
Save patelnwd/1f99a16022cfb5c84285aadc2aac661d to your computer and use it in GitHub Desktop.
You can convert Hex to RBG color code and vice versa.
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function hexToRgb(hex) {
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 LightenColor = function(color, percent) {
var num = parseInt(color,16),
amt = Math.round(2.55 * percent),
R = (num >> 16) + amt,
B = (num >> 8 & 0x00FF) + amt,
G = (num & 0x0000FF) + amt;
return (0x1000000 + (R<255?R<1?0:R:255)*0x10000 + (B<255?B<1?0:B:255)*0x100 + (G<255?G<1?0:G:255)).toString(16).slice(1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment