Skip to content

Instantly share code, notes, and snippets.

@kwoods
Created January 25, 2014 03:11
Show Gist options
  • Save kwoods/8611265 to your computer and use it in GitHub Desktop.
Save kwoods/8611265 to your computer and use it in GitHub Desktop.
Lower brightness for a given CSS hex value, used for gradients
# 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