Skip to content

Instantly share code, notes, and snippets.

@frankyonnetti
Last active February 17, 2021 00:35
Show Gist options
  • Select an option

  • Save frankyonnetti/1a62db26854d7812f11e14eabd0ed0f4 to your computer and use it in GitHub Desktop.

Select an option

Save frankyonnetti/1a62db26854d7812f11e14eabd0ed0f4 to your computer and use it in GitHub Desktop.
jQuery - colorLuminance #jquery #color
// Adjust value of a HEX color
// jsfiddle.net/y2mgW
function colorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
var rgb = '#', c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ('00'+c).substr(c.length);
}
return rgb;
}
console.log(
colorLuminance("#00ff00", -0.5) // = #008000
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment