wdd-ruby-ext 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use system
1
+ rvm use ree-head@wdd
data/Gemfile CHANGED
@@ -1 +1,10 @@
1
- gem 'wdd-ruby-ext', :path=>"."
1
+ source :gemcutter, :github
2
+
3
+ gem 'wdd-ruby-ext', :path=>"."
4
+ gem "nokogiri", "~> 1.4.1"
5
+ gem "eventmachine", "~> 0.12.10"
6
+ gem "jeweler"
7
+
8
+ group :development, :test do
9
+ gem 'rspec', '~> 1.3'
10
+ end
data/Gemfile.lock CHANGED
@@ -1,13 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wdd-ruby-ext (0.2.3)
4
+ wdd-ruby-ext (0.4.4)
5
+ eventmachine (~> 0.12.10)
6
+ nokogiri (~> 1.4.1)
7
+ wdd-ruby-ext
5
8
 
6
9
  GEM
10
+ remote: http://rubygems.org/
7
11
  specs:
12
+ eventmachine (0.12.10)
13
+ git (1.2.5)
14
+ jeweler (1.8.4)
15
+ bundler (~> 1.0)
16
+ git (>= 1.2.5)
17
+ rake
18
+ rdoc
19
+ json (1.7.5)
20
+ nokogiri (1.4.7)
21
+ rake (0.8.7)
22
+ rdoc (3.12)
23
+ json (~> 1.4)
24
+ rspec (1.3.2)
8
25
 
9
26
  PLATFORMS
10
27
  ruby
11
28
 
12
29
  DEPENDENCIES
30
+ eventmachine (~> 0.12.10)
31
+ jeweler
32
+ nokogiri (~> 1.4.1)
33
+ rspec (~> 1.3)
13
34
  wdd-ruby-ext!
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.4
1
+ 0.5.0
@@ -0,0 +1,162 @@
1
+ if $0 == __FILE__
2
+ require 'rubygems'
3
+ require 'wdd-ruby-ext'
4
+ end
5
+
6
+ module WDD
7
+ module Utils
8
+ class RGBColor
9
+ attr :red, true
10
+ attr :green, true
11
+ attr :blue, true
12
+
13
+ def initialize( *args )
14
+ if args.length == 0
15
+ raise "Must supply initializer arguments"
16
+ elsif args.length ==1
17
+ raise "Single argument must be a string" if !args[0].is_a?( String )
18
+ hex_color_string = args[0]
19
+
20
+ if matches = hex_color_string.match( /#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/ )
21
+ @red = ("%02d" % "0x#{matches[1]}").to_i
22
+ @green = ("%02d" % "0x#{matches[2]}").to_i
23
+ @blue = ("%02d" % "0x#{matches[3]}").to_i
24
+ elsif matches = hex_color_string.match( /#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})/ )
25
+ @red = ("%02d" % "0x#{matches[1]}#{matches[1]}").to_i
26
+ @green = ("%02d" % "0x#{matches[2]}#{matches[2]}").to_i
27
+ @blue = ("%02d" % "0x#{matches[3]}#{matches[3]}").to_i
28
+ else
29
+ raise "Invalid RGB hex color code #{hex_color_string}" if !matches
30
+ end
31
+ elsif args.length == 3
32
+ args.each do |arg|
33
+ arg = arg.to_i
34
+ raise "Invalid color value #{arg}" if !arg.is_a?( Fixnum ) || arg<0 || arg > 255
35
+ end
36
+ @red = args[0]
37
+ @green = args[1]
38
+ @blue = args[2]
39
+ end
40
+ end
41
+
42
+ def to_hex
43
+ ("%02x" % @red) + ("%02x" % @green) + ("%02x" % @blue)
44
+ end
45
+
46
+ def to_hex!
47
+ '#' + to_hex
48
+ end
49
+
50
+ # Lightens the color by +percentage+
51
+ def lighten percentage
52
+ raise "Invalid multiplier #{multiplier}. Should be >= 0 and <= 100." if percentage < 0 || percentage > 100
53
+ multiplier = percentage.to_f / 100
54
+ new_red = (@red+(multiplier*(255-@red))).min(255).to_i
55
+ new_green = (@green+(multiplier*(255-@green))).min(255).to_i
56
+ new_blue = (@blue+(multiplier*(255-@blue))).min(255).to_i
57
+ return RGBColor.new( new_red, new_green, new_blue )
58
+ end
59
+
60
+ # Darkens the color by +percentage+
61
+ def darken percentage
62
+ raise "Invalid multiplier #{multiplier}. Should be >= 0 and <= 100." if percentage < 0 || percentage > 100
63
+ multiplier = percentage.to_f / 100
64
+ new_red = (@red-multiplier*@red).min(255).to_i
65
+ new_green = (@green-multiplier*@green).min(255).to_i
66
+ new_blue = (@blue-multiplier*@blue).min(255).to_i
67
+ return RGBColor.new( new_red, new_green, new_blue )
68
+ end
69
+
70
+ def saturation
71
+ least_val = @red.min(@green).min(@blue)
72
+ most_val = @red.max(@green).max(@blue)
73
+ saturation_level = (most_val - least_val).to_f / most_val * 100
74
+ end
75
+
76
+ def saturate percentage
77
+ min_val = 0
78
+ multiplier = percentage.to_f / 100
79
+ most_val = @red.max(@green).max(@blue)
80
+ return self.dup if most_val <= min_val
81
+ least_val = @red.min(@green).min(@blue)
82
+ red_ratio = 1.0 - @red.to_f / most_val
83
+ green_ratio = 1.0 - @green.to_f / most_val
84
+ blue_ratio = 1.0 - @blue.to_f / most_val
85
+ new_red = @red - red_ratio * (@red - min_val) * multiplier
86
+ new_green = @green - green_ratio * (@green - min_val) * multiplier
87
+ new_blue = @blue - blue_ratio * (@blue - min_val) * multiplier
88
+ # puts "@red: #{@red}"
89
+ # puts "@green: #{@green}"
90
+ # puts "@blue: #{@blue}"
91
+ # puts "new_red: #{new_red}"
92
+ # puts "new_green: #{new_green}"
93
+ # puts "new_blue: #{new_blue}"
94
+ return RGBColor.new( new_red.to_i, new_green.to_i, new_blue.to_i )
95
+ end
96
+
97
+ def desaturate percentage
98
+ multiplier = (100.0 - percentage.to_f) / 100
99
+ most_val = @red.max(@green).max(@blue)
100
+ least_val = @red.min(@green).min(@blue)
101
+ range = most_val
102
+ red_ratio = 1.0 - @red.to_f / most_val
103
+ green_ratio = 1.0 - @green.to_f / most_val
104
+ blue_ratio = 1.0 - @blue.to_f / most_val
105
+ highest_ratio = red_ratio.max(green_ratio).max(blue_ratio)
106
+ new_red = most_val - red_ratio * range * multiplier
107
+ new_green = most_val - green_ratio * range * multiplier
108
+ new_blue = most_val - blue_ratio * range * multiplier
109
+ return RGBColor.new( new_red.to_i, new_green.to_i, new_blue.to_i )
110
+ end
111
+
112
+ def self.color_cycle start, arc_step_in_degrees, iterations, desaturation=0, darken=0
113
+ angle = 0
114
+ two_thirds_PI = 2.0 * Math::PI / 3
115
+ four_thirds_PI = 4.0 * Math::PI / 3
116
+ half_PI = Math::PI / 2
117
+ colors = []
118
+ iterations.times do |i|
119
+ angle = ((start + i*arc_step_in_degrees) * Math::PI * 2) / 360
120
+ red = (Math.cos(angle) + 1) / 2 * 255
121
+ green = (Math.cos(angle + two_thirds_PI) + 1) / 2 * 255
122
+ blue = (Math.cos(angle + four_thirds_PI) +1) / 2 * 255
123
+ desat = (Math.cos(i)+1) / 2 * 60
124
+ dark = (Math.cos(i*2)+1) / 2 * 20
125
+ color = RGBColor.new(red, green, blue).saturate(desat).darken(dark).desaturate(desaturation).darken(darken)
126
+ colors << color
127
+ yield color if block_given?
128
+ end
129
+ colors
130
+ end
131
+ end
132
+
133
+
134
+ if $0 == __FILE__
135
+ color = RGBColor.new( "FF8800" )
136
+ puts color.to_hex
137
+ puts color.to_hex!
138
+ color = RGBColor.new( 0xAA, 0x88, 0x44 )
139
+ puts color.to_hex
140
+ puts color.to_hex!
141
+ puts color.saturation
142
+ puts
143
+ puts color.saturate( 0 ).to_hex
144
+ puts color.saturate( 50 ).to_hex
145
+ puts color.saturate( 100 ).to_hex
146
+ puts
147
+ puts color.desaturate( 0 ).to_hex
148
+ puts color.desaturate( 50 ).to_hex
149
+ puts color.desaturate( 100 ).to_hex
150
+ puts color.lighten( 0 ).to_hex
151
+ puts color.lighten( 50 ).to_hex
152
+ puts color.lighten( 100 ).to_hex
153
+ puts color.darken( 0 ).to_hex
154
+ puts color.darken( 50 ).to_hex
155
+ puts color.darken( 100 ).to_hex
156
+ color = RGBColor.new( "#888" )
157
+ puts color.to_hex
158
+
159
+ colors = RGBColor.color_cycle( 0, 20, 16) {|rgb| puts rgb.to_hex!}
160
+ end
161
+ end
162
+ end
data/wdd-ruby-ext.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wdd-ruby-ext}
8
- s.version = "0.4.4"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["shock"]
12
- s.date = %q{2011-10-19}
12
+ s.date = %q{2012-09-14}
13
13
  s.description = %q{Some of these are borrowed. Some are original. This gem simply provides a single place to source control them all for incorporation into other projects.}
14
14
  s.email = %q{github@wdoughty.net}
15
15
  s.extra_rdoc_files = [
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  "lib/wdd-ruby-ext/utils/helpers.rb",
42
42
  "lib/wdd-ruby-ext/utils/miscellany.rb",
43
43
  "lib/wdd-ruby-ext/utils/pretty_xml.rb",
44
+ "lib/wdd-ruby-ext/utils/rgb_color.rb",
44
45
  "lib/wdd-ruby-ext/utils/simpledebug.rb",
45
46
  "lib/wdd-ruby-ext/utils/spinner.rb",
46
47
  "lib/wdd-ruby-ext/utils/test_server.rb",
@@ -56,12 +57,6 @@ Gem::Specification.new do |s|
56
57
  s.require_paths = ["lib"]
57
58
  s.rubygems_version = %q{1.3.7}
58
59
  s.summary = %q{Handy extensions to the Ruby base classes and other utilities.}
59
- s.test_files = [
60
- "spec/lib/utils/fixed_point_spec.rb",
61
- "spec/lib/utils/pretty_xml_spec.rb",
62
- "spec/spec_helper.rb",
63
- "spec/wdd-ruby-ext_spec.rb"
64
- ]
65
60
 
66
61
  if s.respond_to? :specification_version then
67
62
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -69,17 +64,29 @@ Gem::Specification.new do |s|
69
64
 
70
65
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
71
66
  s.add_runtime_dependency(%q<wdd-ruby-ext>, [">= 0"])
67
+ s.add_runtime_dependency(%q<nokogiri>, ["~> 1.4.1"])
68
+ s.add_runtime_dependency(%q<eventmachine>, ["~> 0.12.10"])
69
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
70
+ s.add_development_dependency(%q<rspec>, ["~> 1.3"])
72
71
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
73
72
  s.add_runtime_dependency(%q<nokogiri>, ["~> 1.4.1"])
74
73
  s.add_runtime_dependency(%q<eventmachine>, ["~> 0.12.10"])
75
74
  else
76
75
  s.add_dependency(%q<wdd-ruby-ext>, [">= 0"])
76
+ s.add_dependency(%q<nokogiri>, ["~> 1.4.1"])
77
+ s.add_dependency(%q<eventmachine>, ["~> 0.12.10"])
78
+ s.add_dependency(%q<jeweler>, [">= 0"])
79
+ s.add_dependency(%q<rspec>, ["~> 1.3"])
77
80
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
78
81
  s.add_dependency(%q<nokogiri>, ["~> 1.4.1"])
79
82
  s.add_dependency(%q<eventmachine>, ["~> 0.12.10"])
80
83
  end
81
84
  else
82
85
  s.add_dependency(%q<wdd-ruby-ext>, [">= 0"])
86
+ s.add_dependency(%q<nokogiri>, ["~> 1.4.1"])
87
+ s.add_dependency(%q<eventmachine>, ["~> 0.12.10"])
88
+ s.add_dependency(%q<jeweler>, [">= 0"])
89
+ s.add_dependency(%q<rspec>, ["~> 1.3"])
83
90
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
84
91
  s.add_dependency(%q<nokogiri>, ["~> 1.4.1"])
85
92
  s.add_dependency(%q<eventmachine>, ["~> 0.12.10"])
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wdd-ruby-ext
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 4
10
- version: 0.4.4
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - shock
@@ -15,13 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-19 00:00:00 -05:00
18
+ date: 2012-09-14 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  prerelease: false
23
- type: :runtime
24
- name: wdd-ruby-ext
25
23
  version_requirements: &id001 !ruby/object:Gem::Requirement
26
24
  none: false
27
25
  requirements:
@@ -31,12 +29,73 @@ dependencies:
31
29
  segments:
32
30
  - 0
33
31
  version: "0"
32
+ name: wdd-ruby-ext
34
33
  requirement: *id001
34
+ type: :runtime
35
35
  - !ruby/object:Gem::Dependency
36
36
  prerelease: false
37
- type: :development
38
- name: rspec
39
37
  version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 5
43
+ segments:
44
+ - 1
45
+ - 4
46
+ - 1
47
+ version: 1.4.1
48
+ name: nokogiri
49
+ requirement: *id002
50
+ type: :runtime
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 59
59
+ segments:
60
+ - 0
61
+ - 12
62
+ - 10
63
+ version: 0.12.10
64
+ name: eventmachine
65
+ requirement: *id003
66
+ type: :runtime
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ name: jeweler
79
+ requirement: *id004
80
+ type: :runtime
81
+ - !ruby/object:Gem::Dependency
82
+ prerelease: false
83
+ version_requirements: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ hash: 9
89
+ segments:
90
+ - 1
91
+ - 3
92
+ version: "1.3"
93
+ name: rspec
94
+ requirement: *id005
95
+ type: :development
96
+ - !ruby/object:Gem::Dependency
97
+ prerelease: false
98
+ version_requirements: &id006 !ruby/object:Gem::Requirement
40
99
  none: false
41
100
  requirements:
42
101
  - - ">="
@@ -47,12 +106,12 @@ dependencies:
47
106
  - 2
48
107
  - 9
49
108
  version: 1.2.9
50
- requirement: *id002
109
+ name: rspec
110
+ requirement: *id006
111
+ type: :development
51
112
  - !ruby/object:Gem::Dependency
52
113
  prerelease: false
53
- type: :runtime
54
- name: nokogiri
55
- version_requirements: &id003 !ruby/object:Gem::Requirement
114
+ version_requirements: &id007 !ruby/object:Gem::Requirement
56
115
  none: false
57
116
  requirements:
58
117
  - - ~>
@@ -63,12 +122,12 @@ dependencies:
63
122
  - 4
64
123
  - 1
65
124
  version: 1.4.1
66
- requirement: *id003
125
+ name: nokogiri
126
+ requirement: *id007
127
+ type: :runtime
67
128
  - !ruby/object:Gem::Dependency
68
129
  prerelease: false
69
- type: :runtime
70
- name: eventmachine
71
- version_requirements: &id004 !ruby/object:Gem::Requirement
130
+ version_requirements: &id008 !ruby/object:Gem::Requirement
72
131
  none: false
73
132
  requirements:
74
133
  - - ~>
@@ -79,7 +138,9 @@ dependencies:
79
138
  - 12
80
139
  - 10
81
140
  version: 0.12.10
82
- requirement: *id004
141
+ name: eventmachine
142
+ requirement: *id008
143
+ type: :runtime
83
144
  description: Some of these are borrowed. Some are original. This gem simply provides a single place to source control them all for incorporation into other projects.
84
145
  email: github@wdoughty.net
85
146
  executables: []
@@ -114,6 +175,7 @@ files:
114
175
  - lib/wdd-ruby-ext/utils/helpers.rb
115
176
  - lib/wdd-ruby-ext/utils/miscellany.rb
116
177
  - lib/wdd-ruby-ext/utils/pretty_xml.rb
178
+ - lib/wdd-ruby-ext/utils/rgb_color.rb
117
179
  - lib/wdd-ruby-ext/utils/simpledebug.rb
118
180
  - lib/wdd-ruby-ext/utils/spinner.rb
119
181
  - lib/wdd-ruby-ext/utils/test_server.rb
@@ -158,8 +220,5 @@ rubygems_version: 1.3.7
158
220
  signing_key:
159
221
  specification_version: 3
160
222
  summary: Handy extensions to the Ruby base classes and other utilities.
161
- test_files:
162
- - spec/lib/utils/fixed_point_spec.rb
163
- - spec/lib/utils/pretty_xml_spec.rb
164
- - spec/spec_helper.rb
165
- - spec/wdd-ruby-ext_spec.rb
223
+ test_files: []
224
+