Created
January 25, 2014 03:11
-
-
Save kwoods/8611265 to your computer and use it in GitHub Desktop.
Lower brightness for a given CSS hex value, used for gradients
This file contains hidden or 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
# Reduces a color's brightness by a percentage | |
# returns a hex value in the format "#x0x0x0" | |
def self.darken(hex,percent) | |
# convert to r,g,b | |
rgb_version = hex.to_s.gsub("#","").scan(/../).map {|color| color.to_i(16)} | |
# reduce values by 'percent' | |
new_rgb_color = rgb_version.map {|rgb| (rgb - (rgb * percent.to_f/100)).ceil} | |
# convert back to hex | |
"#%02x%02x%02x" % new_rgb_color | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment