Skip to content

Instantly share code, notes, and snippets.

@anhkind
Created May 30, 2015 07:31
Show Gist options
  • Save anhkind/49c75acf162d84c19931 to your computer and use it in GitHub Desktop.
Save anhkind/49c75acf162d84c19931 to your computer and use it in GitHub Desktop.
Use SASS functions to lighten/ darken a (hex) color Rails
class ColorUtil
class << self
def lighten hex_color, percent
adjust :lightness, hex_color, percent
end
def darken hex_color, percent
adjust :lightness, hex_color, percent * -1
end
private
def adjust action, hex_color, percent
color = get_color hex_color
color.with(action => Sass::Util.restrict(color.send(action).send(:+, percent * 100) , 0..100)).inspect
end
def get_color hex_color
Sass::Script::Color.new hex_color.gsub('#','').scan(/../).map(&:hex)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment