compass-colors 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,4 +2,4 @@
2
2
  :build:
3
3
  :major: 0
4
4
  :minor: 3
5
- :patch: 0
5
+ :patch: 1
@@ -8,7 +8,8 @@ module Compass
8
8
  attr_reader :s, :l
9
9
 
10
10
  def self.from_color(color)
11
- from_rgb(*color.value)
11
+ rgb = color.respond_to?(:rgb) ? color.rgb : color.value
12
+ from_rgb(*rgb)
12
13
  end
13
14
 
14
15
  def self.from_rgb(r, g, b)
@@ -1,47 +1,69 @@
1
1
  require 'sass'
2
2
 
3
3
  module Sass::Script::Functions
4
+ module Colors
5
+ extend self
6
+ def rgb_value(color)
7
+ if color.respond_to?(:rgb)
8
+ color.rgb
9
+ else
10
+ color.value
11
+ end
12
+ end
13
+ end
4
14
  # Takes a color object and amount by which to lighten it (0 to 100).
5
15
  def lighten(color, amount)
6
16
  hsl = Compass::Colors::HSL.from_color(color)
7
- if percentage?(amount)
8
- hsl.l += (1 - hsl.l) * (amount.value / 100.0)
9
- else
10
- hsl.l += amount.value
11
- end
17
+ hsl.l += amount.value / 100.0
18
+ hsl.to_color
19
+ end
20
+
21
+ # Takes a color object and percent by which to lighten it (0 to 100).
22
+ def lighten_percent(color, amount)
23
+ hsl = Compass::Colors::HSL.from_color(color)
24
+ hsl.l += (1 - hsl.l) * (amount.value / 100.0)
12
25
  hsl.to_color
13
26
  end
14
27
 
15
28
  # Takes a color object and amount by which to darken it (0 to 100).
16
29
  def darken(color, amount)
17
30
  hsl = Compass::Colors::HSL.from_color(color)
18
- if percentage?(amount)
19
- hsl.l *= 1.0 - (amount.value / 100.0)
20
- else
21
- hsl.l -= amount.value
22
- end
31
+ hsl.l -= amount.value / 100.0
32
+ hsl.to_color
33
+ end
34
+
35
+ # Takes a color object and percent by which to darken it (0 to 100).
36
+ def darken_percent(color, amount)
37
+ hsl = Compass::Colors::HSL.from_color(color)
38
+ hsl.l *= 1.0 - (amount.value / 100.0)
23
39
  hsl.to_color
24
40
  end
25
41
 
26
42
  # Saturate (make a color "richer") a color by the given amount (0 to 100)
27
43
  def saturate(color, amount)
28
44
  hsl = Compass::Colors::HSL.from_color(color)
29
- if percentage?(amount)
30
- hsl.s += (1 - hsl.s) * (amount.value / 100.0)
31
- else
32
- hsl.s += amount.value
33
- end
45
+ hsl.s += amount.value / 100.0
46
+ hsl.to_color
47
+ end
48
+
49
+ # Saturate (make a color "richer") a color by the given percent (0 to 100)
50
+ def saturate_percent(color, amount)
51
+ hsl = Compass::Colors::HSL.from_color(color)
52
+ hsl.s += (1 - hsl.s) * (amount.value / 100.0)
34
53
  hsl.to_color
35
54
  end
36
55
 
37
56
  # Desaturate (make a color "grayer") a color by the given amount (0 to 100)
38
57
  def desaturate(color, amount)
39
58
  hsl = Compass::Colors::HSL.from_color(color)
40
- if percentage?(amount)
41
- hsl.s *= (1.0 - (amount.value / 100.0))
42
- else
43
- hsl.s -= amount.value
44
- end
59
+ hsl.s -= amount.value / 100.0
60
+ hsl.to_color
61
+ end
62
+
63
+ # Desaturate (make a color "grayer") a color by the given percent (0 to 100)
64
+ def desaturate_percent(color, amount)
65
+ hsl = Compass::Colors::HSL.from_color(color)
66
+ hsl.s *= (1.0 - (amount.value / 100.0))
45
67
  hsl.to_color
46
68
  end
47
69
 
@@ -59,11 +81,12 @@ module Sass::Script::Functions
59
81
  def luminosity(color)
60
82
  Sass::Script::Number.new((Compass::Colors::HSL.from_color(color).l * 100).round)
61
83
  end
84
+ alias lightness luminosity
62
85
 
63
86
  # Mixes two colors by some amount (0 to 100). Defaults to 50.
64
87
  def mix(color1, color2, amount = nil)
65
88
  percent = amount ? amount.value.round / 100.0 : 0.5
66
- new_colors = color1.value.zip(color2.value).map{|c1, c2| (c1 * percent) + (c2 * (1 - percent))}
89
+ new_colors = Colors.rgb_value(color1).zip(Colors.rgb_value(color2)).map{|c1, c2| (c1 * percent) + (c2 * (1 - percent))}
67
90
  Sass::Script::Color.new(new_colors)
68
91
  end
69
92
 
@@ -86,10 +109,4 @@ module Sass::Script::Functions
86
109
  adjust_hue color, 180
87
110
  end
88
111
 
89
- private
90
-
91
- def percentage?(amount)
92
- amount.numerator_units == ["%"] || (amount.unitless? && amount.value > 1 && amount.value < 100)
93
- end
94
-
95
112
  end
@@ -6,7 +6,7 @@ module BeApproximatelyTheSameColorAsMatcher
6
6
 
7
7
  def matches?(target)
8
8
  @target = target
9
- @target.value.zip(@expected.value).all?{|e,t| (e-t).abs <= 1}
9
+ @target.rgb.zip(@expected.rgb).all?{|e,t| (e-t).abs <= 1}
10
10
  end
11
11
 
12
12
  def failure_message
@@ -10,12 +10,22 @@ require 'compass-colors'
10
10
 
11
11
  describe "sass extensions" do
12
12
  it "should lighten red into pink" do
13
- pink = invoke(:lighten, color(255,0,0), number(50))
13
+ pink = invoke(:lighten, color(255,0,0), number(25))
14
+ pink.should be_approximately_the_same_color_as(color(255,127,127))
15
+ end
16
+
17
+ it "should lighten red into pink (percentage)" do
18
+ pink = invoke(:lighten_percent, color(255,0,0), number(50))
14
19
  pink.should be_approximately_the_same_color_as(color(255,127,127))
15
20
  end
16
21
 
17
22
  it "should darken red into maroon" do
18
- maroon = invoke(:darken, color(255,0,0), number(50))
23
+ maroon = invoke(:darken, color(255,0,0), number(25))
24
+ maroon.should be_approximately_the_same_color_as(color(127,0,0))
25
+ end
26
+
27
+ it "should darken red into maroon (percentage)" do
28
+ maroon = invoke(:darken_percent, color(255,0,0), number(50))
19
29
  maroon.should be_approximately_the_same_color_as(color(127,0,0))
20
30
  end
21
31
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compass-colors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Eppstein
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-14 00:00:00 -08:00
12
+ date: 2009-11-19 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency