Created
May 30, 2015 07:31
-
-
Save anhkind/49c75acf162d84c19931 to your computer and use it in GitHub Desktop.
Use SASS functions to lighten/ darken a (hex) color Rails
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
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