Created
September 11, 2016 09:36
-
-
Save jp26jp/dc80aa27c0a446145e3115b5db0f51df to your computer and use it in GitHub Desktop.
Programmatically increase color hex brightness
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
function increase_brightness(hex, percent){ | |
// strip the leading # if it's there | |
hex = hex.replace(/^\s*#|\s*$/g, ''); | |
// convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF` | |
if(hex.length == 3){ | |
hex = hex.replace(/(.)/g, '$1$1'); | |
} | |
var r = parseInt(hex.substr(0, 2), 16), | |
g = parseInt(hex.substr(2, 2), 16), | |
b = parseInt(hex.substr(4, 2), 16); | |
return '#' + | |
((0|(1<<8) + r + (256 - r) * percent / 100).toString(16)).substr(1) + | |
((0|(1<<8) + g + (256 - g) * percent / 100).toString(16)).substr(1) + | |
((0|(1<<8) + b + (256 - b) * percent / 100).toString(16)).substr(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment