Skip to content

Commit

Permalink
saturate, desaturate, lighten, and darken functions now allow absolute
Browse files Browse the repository at this point in the history
adjustments if the second argument is a unitless number between 0 and 1.
  • Loading branch information
chriseppstein committed Nov 12, 2009
1 parent ebb4757 commit 1dc77b7
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/compass-colors/sass_extensions.rb
Expand Up @@ -4,28 +4,44 @@ module Sass::Script::Functions
# Takes a color object and amount by which to lighten it (0 to 100).
def lighten(color, amount)
hsl = Compass::Colors::HSL.from_color(color)
hsl.l += (1 - hsl.l) * (amount.value / 100.0)
if percentage?(amount)
hsl.l += (1 - hsl.l) * (amount.value / 100.0)
else
hsl.l += amount.value
end
hsl.to_color
end

# Takes a color object and amount by which to darken it (0 to 100).
def darken(color, amount)
hsl = Compass::Colors::HSL.from_color(color)
hsl.l *= 1.0 - (amount.value / 100.0)
if percentage?(amount)
hsl.l *= 1.0 - (amount.value / 100.0)
else
hsl.l -= amount.value
end
hsl.to_color
end

# Saturate (make a color "richer") a color by the given amount (0 to 100)
def saturate(color, amount)
hsl = Compass::Colors::HSL.from_color(color)
hsl.s += (1 - hsl.s) * (amount.value / 100.0)
if percentage?(amount)
hsl.s += (1 - hsl.s) * (amount.value / 100.0)
else
hsl.s += amount.value
end
hsl.to_color
end

# Desaturate (make a color "grayer") a color by the given amount (0 to 100)
def desaturate(color, amount)
hsl = Compass::Colors::HSL.from_color(color)
hsl.s *= (1.0 - (amount.value / 100.0))
if percentage?(amount)
hsl.s *= (1.0 - (amount.value / 100.0))
else
hsl.s -= amount.value
end
hsl.to_color
end

Expand Down Expand Up @@ -70,4 +86,10 @@ def complement(color)
adjust_hue color, 180
end

private

def percentage?(amount)
amount.numerator_units == ["%"] || (amount.unitless? && amount.value > 1 && amount.value < 100)
end

end

0 comments on commit 1dc77b7

Please sign in to comment.