Last active
December 15, 2015 09:29
-
-
Save davemackintosh/5238776 to your computer and use it in GitHub Desktop.
JS function to lighten or darken a HEX colour.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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