Created
December 29, 2015 15:32
-
-
Save szanata/e97a42e64f2274740171 to your computer and use it in GitHub Desktop.
This function creates change the luminescence of given color (0..1)
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 generate(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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment