Skip to content

Instantly share code, notes, and snippets.

@veiko
Created March 23, 2013 17:53
Show Gist options
  • Select an option

  • Save veiko/5228723 to your computer and use it in GitHub Desktop.

Select an option

Save veiko/5228723 to your computer and use it in GitHub Desktop.
A function to adjust the lightness of a color in Javascript. Based on a solution from http://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color
adjustLightness = (color,percent) ->
num = parseInt(color.slice(1),16)
amt = Math.round(2.55 * percent)
R = (num >> 16) + amt
B = (num >> 8 & 0x00FF) + amt
G = (num & 0x0000FF) + amt
"#" + (0x1000000 +
(if R<255 then (if R<1 then 0 else R) else 255)*0x10000 +
(if B<255 then (if B<1 then 0 else B) else 255)*0x100 +
(if G<255 then (if G<1 then 0 else G) else 255)
).toString(16).slice(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment