Skip to content

Instantly share code, notes, and snippets.

@roykolak
Created December 20, 2017 17:53
Show Gist options
  • Save roykolak/9604140af3214dfffd13d5d7950ab524 to your computer and use it in GitHub Desktop.
Save roykolak/9604140af3214dfffd13d5d7950ab524 to your computer and use it in GitHub Desktop.
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;
}
function shadeHex(color, percent) {
var f=parseInt(color.slice(1),16),t=percent<0?0:255,p=percent<0?percent*-1:percent,R=f>>16,G=f>>8&0x00FF,B=f&0x0000FF;
return "#"+(0x1000000+(Math.round((t-R)*p)+R)*0x10000+(Math.round((t-G)*p)+G)*0x100+(Math.round((t-B)*p)+B)).toString(16).slice(1);
}
const brightness = 5
const shadedHex = shadeHex('#FFFFFF', -((100 - brightness) / 100)
hexToRgb(shadedHex);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment