sugarcube 0.20.16 → 0.20.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,13 @@ class Symbol
14
14
  end
15
15
  else
16
16
  # css colors
17
- color = sugarcube_look_in(Symbol.css_colors).uicolor(alpha)
17
+ color = sugarcube_look_in(Symbol.css_colors).uicolor
18
+ Symbol.css_colors[self] = color
19
+ if alpha.nil?
20
+ color
21
+ else
22
+ color.uicolor(alpha)
23
+ end
18
24
  end
19
25
 
20
26
  color
@@ -19,6 +19,21 @@ class UIColor
19
19
  mix_with(color.uicolor, 0.5)
20
20
  end
21
21
 
22
+ # blends two colors by adding the colors, with an upper maximum of 255.
23
+ # Adding white to any color will create white, adding black will do nothing.
24
+ # Also takes transparency into account; adding a transparent color has no
25
+ # effect, adding an opaque color has the most effect.
26
+ # @example
27
+ # :red.uicolor << :blue.uicolor == '#ff00ff'.uicolor (:magenta)
28
+ # :red.uicolor << :blue.uicolor(0.5) == '#ff0080'.uicolor (pinkish)
29
+ def <<(color)
30
+ r = [1.0, color.red * color.alpha + self.red].min
31
+ g = [1.0, color.green * color.alpha + self.green].min
32
+ b = [1.0, color.blue * color.alpha + self.blue].min
33
+ a = self.alpha
34
+ UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
35
+ end
36
+
22
37
  # a more generic color mixing method. mixes two colors, but a second
23
38
  # parameter determines how much of each. 0.5 means equal parts, 0.0 means use
24
39
  # all of the first, and 1.0 means use all of the second
@@ -28,21 +43,30 @@ class UIColor
28
43
  # make amount between 0 and 1
29
44
  amount = [[0, amount].max, 1].min
30
45
  # start with precise amounts: 0, 0.5, and 1.
31
- if amount == 0
46
+ if amount == 0 && self.alpha == color.alpha
32
47
  self
33
- elsif amount == 1
48
+ elsif amount == 1 && self.alpha == color.alpha
34
49
  color
35
- elsif amount == 0.5
50
+ elsif amount == 0.5 && self.alpha == color.alpha
36
51
  r = (self.red + color.red) / 2
37
52
  g = (self.green + color.green) / 2
38
53
  b = (self.blue + color.blue) / 2
39
54
  a = (self.alpha + color.alpha) / 2
40
55
  UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
41
56
  else
42
- r = (color.red - self.red) * amount + self.red
43
- g = (color.green - self.green) * amount + self.green
44
- b = (color.blue - self.blue) * amount + self.blue
45
57
  a = (color.alpha - self.alpha) * amount + self.alpha
58
+ return UIColor.clearColor if a == 0
59
+
60
+ color_red = color.red * color.alpha + self.red * (1 - color.alpha)
61
+ self_red = self.red * self.alpha + color.red * (1 - self.alpha)
62
+ color_green = color.green * color.alpha + self.green * (1 - color.alpha)
63
+ self_green = self.green * self.alpha + color.green * (1 - self.alpha)
64
+ color_blue = color.blue * color.alpha + self.blue * (1 - color.alpha)
65
+ self_blue = self.blue * self.alpha + color.blue * (1 - self.alpha)
66
+
67
+ r = (color_red - self_red) * amount + self_red
68
+ g = (color_green - self_green) * amount + self_green
69
+ b = (color_blue - self_blue) * amount + self_blue
46
70
  UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
47
71
  end
48
72
  end
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.20.16'
2
+ Version = '0.20.17'
3
3
  end
@@ -8,11 +8,71 @@ describe 'UIColor' do
8
8
  UIColor.redColor.uicolor(0.5).alpha.should == 0.5
9
9
  end
10
10
 
11
- it "should have a #+(color) method" do
12
- new_color = UIColor.whiteColor + UIColor.blackColor
13
- new_color.red.should == 0.5
14
- new_color.green.should == 0.5
15
- new_color.blue.should == 0.5
11
+ describe "should have a #+(color) method" do
12
+
13
+ it 'should add white and black to make gray' do
14
+ new_color = UIColor.whiteColor + UIColor.blackColor
15
+ new_color.red.should == 0.5
16
+ new_color.green.should == 0.5
17
+ new_color.blue.should == 0.5
18
+ new_color.alpha.should == 1.0
19
+ end
20
+
21
+ it 'should add black and white to make gray' do
22
+ new_color = UIColor.blackColor + UIColor.whiteColor
23
+ new_color.red.should == 0.5
24
+ new_color.green.should == 0.5
25
+ new_color.blue.should == 0.5
26
+ new_color.alpha.should == 1.0
27
+ end
28
+
29
+ it 'should add alpha channel (white + clear)' do
30
+ new_color = UIColor.whiteColor + UIColor.clearColor
31
+ new_color.red.should == 1.0
32
+ new_color.green.should == 1.0
33
+ new_color.blue.should == 1.0
34
+ new_color.alpha.should == 0.5
35
+ end
36
+
37
+ it 'should add alpha channel (clear + white)' do
38
+ new_color = UIColor.clearColor + UIColor.whiteColor
39
+ new_color.red.should == 1.0
40
+ new_color.green.should == 1.0
41
+ new_color.blue.should == 1.0
42
+ new_color.alpha.should == 0.5
43
+ end
44
+
45
+ end
46
+
47
+ describe "should have a #<<(color) method" do
48
+ it "should return white no matter what" do
49
+ new_color = UIColor.whiteColor << UIColor.blackColor
50
+ new_color.red.should == 1.0
51
+ new_color.green.should == 1.0
52
+ new_color.blue.should == 1.0
53
+ end
54
+
55
+ it "should have no effect with black" do
56
+ new_color = UIColor.blueColor << UIColor.blackColor
57
+ new_color.red.should == 0.0
58
+ new_color.green.should == 0.0
59
+ new_color.blue.should == 1.0
60
+ end
61
+
62
+ it "should add blue to red" do
63
+ new_color = UIColor.redColor << UIColor.blueColor
64
+ new_color.red.should == 1.0
65
+ new_color.green.should == 0.0
66
+ new_color.blue.should == 1.0
67
+ end
68
+
69
+ it "should use opacity when adding blue to red" do
70
+ new_color = UIColor.redColor << UIColor.blueColor.colorWithAlphaComponent(0.5)
71
+ new_color.red.should == 1.0
72
+ new_color.green.should == 0.0
73
+ new_color.blue.should == 0.5
74
+ end
75
+
16
76
  end
17
77
 
18
78
  it "should have a #invert method" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.16
4
+ version: 0.20.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-04-25 00:00:00.000000000 Z
16
+ date: 2013-04-27 00:00:00.000000000 Z
17
17
  dependencies: []
18
18
  description: ! '== Description
19
19