Skip to content

Instantly share code, notes, and snippets.

@davemackintosh
Last active December 15, 2015 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davemackintosh/5238776 to your computer and use it in GitHub Desktop.
Save davemackintosh/5238776 to your computer and use it in GitHub Desktop.
JS function to lighten or darken a HEX colour.
/**
* Util function for lightening the colour with a %
* @param - string - colour with leading #
* @param - number - percentage to lighten by
*/
function lighten (c,p) {
var n = parseInt(c.slice(1),16)
, a = Math.round(2.55 * p)
// Bitshift 16 bits to the left
, r = (n >> 16) + a
// Bitshift 8 bits to the left based on blue
, b = (n >> 8 & 0x00FF) + a
//
, g = (n & 0x0000FF) + a;
// Calculate
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