Module: Sass::Script::Functions

Defined in:
lib/compass-colors/sass_extensions.rb

Instance Method Summary collapse

Instance Method Details

#adjust_hue(color, degrees) ⇒ Object

adjust the hue of a color by the given number of degrees.



62
63
64
65
66
67
# File 'lib/compass-colors/sass_extensions.rb', line 62

def adjust_hue(color, degrees)
  hsl = Compass::Colors::HSL.from_color(color)
  degrees = degrees.value.to_f.round if degrees.is_a?(Sass::Script::Literal)
  hsl.h += degrees
  hsl.to_color
end

#complement(color) ⇒ Object



69
70
71
# File 'lib/compass-colors/sass_extensions.rb', line 69

def complement(color)
  adjust_hue color, 180
end

#darken(color, amount) ⇒ Object

Takes a color object and amount by which to darken it (0 to 100).



12
13
14
15
16
# File 'lib/compass-colors/sass_extensions.rb', line 12

def darken(color, amount)
  hsl = Compass::Colors::HSL.from_color(color)
  hsl.l *= 1.0 - (amount.value / 100.0)
  hsl.to_color
end

#desaturate(color, amount) ⇒ Object

Desaturate (make a color “grayer”) a color by the given amount (0 to 100)



26
27
28
29
30
# File 'lib/compass-colors/sass_extensions.rb', line 26

def desaturate(color, amount)
  hsl = Compass::Colors::HSL.from_color(color)
  hsl.s *= (1.0 - (amount.value / 100.0))
  hsl.to_color
end

#grayscale(color) ⇒ Object

Returns the grayscale equivalent color for the given color



55
56
57
58
59
# File 'lib/compass-colors/sass_extensions.rb', line 55

def grayscale(color)
  hsl = Compass::Colors::HSL.from_color(color)
  g = (hsl.l * 255).round
  Sass::Script::Color.new([g, g, g])
end

#hue(color) ⇒ Object

Return the hue of a color as a number between 0 and 360



33
34
35
# File 'lib/compass-colors/sass_extensions.rb', line 33

def hue(color)
  Sass::Script::Number.new(Compass::Colors::HSL.from_color(color).h.round)
end

#lighten(color, amount) ⇒ Object

Takes a color object and amount by which to lighten it (0 to 100).



5
6
7
8
9
# File 'lib/compass-colors/sass_extensions.rb', line 5

def lighten(color, amount)
  hsl = Compass::Colors::HSL.from_color(color)
  hsl.l += (1 - hsl.l) * (amount.value / 100.0)
  hsl.to_color
end

#luminosity(color) ⇒ Object

Return the luminosity of a color as a number between 0 and 100



43
44
45
# File 'lib/compass-colors/sass_extensions.rb', line 43

def luminosity(color)
  Sass::Script::Number.new((Compass::Colors::HSL.from_color(color).l * 100).round)
end

#mix(color1, color2, amount = nil) ⇒ Object

Mixes two colors by some amount (0 to 100). Defaults to 50.



48
49
50
51
52
# File 'lib/compass-colors/sass_extensions.rb', line 48

def mix(color1, color2, amount = nil)
  percent = amount ? amount.value.round / 100.0 : 0.5
  new_colors = color1.value.zip(color2.value).map{|c1, c2| (c1 * percent) + (c2 * (1 - percent))}
  Sass::Script::Color.new(new_colors)
end

#saturate(color, amount) ⇒ Object

Saturate (make a color “richer”) a color by the given amount (0 to 100)



19
20
21
22
23
# File 'lib/compass-colors/sass_extensions.rb', line 19

def saturate(color, amount)
  hsl = Compass::Colors::HSL.from_color(color)
  hsl.s += (1 - hsl.s) * (amount.value / 100.0)
  hsl.to_color
end

#saturation(color) ⇒ Object

Return the saturation of a color as a number between 0 and 100



38
39
40
# File 'lib/compass-colors/sass_extensions.rb', line 38

def saturation(color)
  Sass::Script::Number.new((Compass::Colors::HSL.from_color(color).s * 100).round)
end