colver 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.0.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.0.pre5"
12
+ gem "simplecov", '~> 0.3.5', :require => false
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.0.pre5)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rspec (2.0.1)
12
+ rspec-core (~> 2.0.1)
13
+ rspec-expectations (~> 2.0.1)
14
+ rspec-mocks (~> 2.0.1)
15
+ rspec-core (2.0.1)
16
+ rspec-expectations (2.0.1)
17
+ diff-lcs (>= 1.1.2)
18
+ rspec-mocks (2.0.1)
19
+ rspec-core (~> 2.0.1)
20
+ rspec-expectations (~> 2.0.1)
21
+ simplecov (0.3.7)
22
+ simplecov-html (>= 0.3.7)
23
+ simplecov-html (0.3.9)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.0.0)
30
+ jeweler (~> 1.5.0.pre5)
31
+ rspec (~> 2.0.0)
32
+ simplecov (~> 0.3.5)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Rafał Cieślak
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = colver
2
+
3
+ If you are a <b>col</b>or lo<b>ver</b>, then this gem is for you! Play with colors and make them happy.
4
+
5
+ Color.new [127, 63, 255] #=> #<Color: hex: 7f3fff, rgb: [127, 63, 255]>
6
+ Color.new 'a4cf13' #=> #<Color: hex: a4cf13, rgb: [164, 207, 19]>
7
+ Color.new 'colver' #=> #<Color: hex: 16b558, rgb: [22, 181, 88]>
8
+
9
+ Yeah - if you pass a non-hex string, colver will generate a color from it! You can also invert colors:
10
+
11
+ color = Color.new 'github' #=> #<Color: hex: 64b2b6, rgb: [100, 178, 182]>
12
+ color.invert #=> #<Color: hex: 9b4d49, rgb: [155, 77, 73]>
13
+
14
+ More features coming soon.
15
+
16
+ == Contributing to colver
17
+
18
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
19
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
20
+ * Fork the project
21
+ * Start a feature/bugfix branch
22
+ * Commit and push until you are happy with your contribution
23
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
24
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
25
+
26
+ == Copyright
27
+
28
+ Copyright (c) 2010 Rafał Cieślak. See LICENSE.txt for
29
+ further details.
30
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "colver"
17
+ gem.summary = %Q{Simple gem for playing with colors}
18
+ gem.description = %Q{Simple gem for playing with colors}
19
+ gem.email = "ravicious@gmail.com"
20
+ gem.homepage = "http://github.com/ravicious/colver"
21
+ gem.authors = ["Rafał Cieślak"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # spec.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # spec.add_development_dependency 'rspec', '> 1.2.3'
26
+ gem.add_development_dependency "rspec", "~> 2.0.0"
27
+ gem.add_development_dependency "bundler", "~> 1.0.0"
28
+ gem.add_development_dependency "jeweler", "~> 1.5.0.pre5"
29
+ gem.add_development_dependency "simplecov", "~> 0.3.5"
30
+ end
31
+ Jeweler::RubygemsDotOrgTasks.new
32
+
33
+ require 'rspec/core'
34
+ require 'rspec/core/rake_task'
35
+ RSpec::Core::RakeTask.new(:spec) do |spec|
36
+ spec.pattern = FileList['spec/**/*_spec.rb']
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "colver #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+ require "digest"
3
+
4
+ class Color
5
+ attr_reader :red, :green, :blue, :hex
6
+ alias :to_s :hex
7
+
8
+ def initialize(color)
9
+ if color.class == Array
10
+ from_array color
11
+ elsif color.class == String
12
+ from_string color
13
+ else
14
+ raise ArgumentError, "Color#new argument must be an array or a string!"
15
+ end
16
+ end
17
+
18
+ def rgb
19
+ [@red, @green, @blue]
20
+ end
21
+ alias :to_a :rgb
22
+
23
+ def inspect
24
+ "#<Color: hex: #{hex}, rgb: #{rgb}>"
25
+ end
26
+
27
+ # Inverts a color
28
+ def invert
29
+ Color.new rgb.map{|value| 255 - value}
30
+ end
31
+
32
+ private
33
+
34
+ # Converts a string to a color. Also checks if the string is a hex:
35
+ #
36
+ # - if yes: returns it
37
+ # - if not: generates a hash and truncates it to the first six chars
38
+ #
39
+ # If string looks like "ab" or "1cb67", makes it longer
40
+ def from_string(string)
41
+ string = (string =~ /\A[a-fA-F0-9]{1,6}\z/ ? string : Digest::SHA1.hexdigest(string)[0..5]).downcase
42
+
43
+ until string[0..5].length == 6
44
+ string += string
45
+ end
46
+
47
+ @hex = string[0..5]
48
+ make_rgb_from_hex
49
+ end
50
+
51
+ def make_rgb_from_hex
52
+ rgb_array = @hex.split(/([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/)
53
+ rgb_array.shift
54
+ @red, @green, @blue = rgb_array.map{|color| color.to_i(16) }
55
+ end
56
+
57
+ # Converts an array to a color. Also validates the length of the array and the RGB values.
58
+ def from_array(array)
59
+ [0, 1, 2].each do |index|
60
+ raise ArgumentError, "insert proper RGB values! (x ∈ <0;255>)" unless (0..255).include? array[index]
61
+ end
62
+
63
+ @red, @green, @blue = array
64
+ make_hex_from_rgb
65
+ end
66
+
67
+ def make_hex_from_rgb
68
+ @hex = rgb.map do |color|
69
+ color < 16 ? "0#{color.to_s(16)}" : color.to_s(16)
70
+ end.join
71
+ end
72
+
73
+ end
data/lib/colver.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "colver/color"
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe Color do
5
+
6
+ describe 'instance with invalid argument' do
7
+ subject { lambda { described_class.new(Hash.new) } }
8
+ it { should raise_error(ArgumentError, "Color#new argument must be an array or a string!")}
9
+ end
10
+
11
+ describe 'instance with an array' do
12
+ context 'with valid RGB values' do
13
+ subject { Color.new([55, 3, 169]) }
14
+
15
+ its(:hex) { should == "3703a9" }
16
+ its(:rgb) { should == [55, 3, 169] }
17
+ end
18
+
19
+ context 'with low RGB values' do
20
+ subject { Color.new([1, 2, 3]) }
21
+
22
+ its(:hex) { should == "010203" }
23
+ its(:rgb) { should == [1, 2, 3] }
24
+ end
25
+
26
+ context 'with invalid values' do
27
+ subject { lambda { described_class.new([1337, String.new, Hash.new]) } }
28
+ it { should raise_error(ArgumentError, "insert proper RGB values! (x ∈ <0;255>)") }
29
+ end
30
+ end
31
+
32
+ describe 'instance with an string' do
33
+ context 'which is a word (for example "ravicious")' do
34
+ subject { Color.new 'ravicious' }
35
+
36
+ its(:hex) { should == "fd3f3a" }
37
+ its(:rgb) { should == [253, 63, 58] }
38
+ end
39
+
40
+ context 'which is already a hex value' do
41
+ context 'with 6 chars' do
42
+ subject { Color.new '1b4fcd' }
43
+
44
+ its(:hex) { should == '1b4fcd' }
45
+ its(:rgb) { should == [27, 79, 205] }
46
+ end
47
+
48
+ context 'with less than 6 chars' do
49
+ subject { Color.new '1b' }
50
+
51
+ describe "hex" do
52
+ before { @hex = Color.new('1b').hex }
53
+
54
+ it "should get longer" do
55
+ @hex.should == '1b1b1b'
56
+ end
57
+ end
58
+
59
+ its(:rgb) { should == [27, 27, 27] }
60
+
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ describe "valid instance" do
67
+ before { @color = Color.new([234, 71, 63]) }
68
+
69
+ it "should invert itself to the other color" do
70
+ @color.invert.should be_instance_of Color
71
+ @color.invert.rgb.should == [21, 184, 192]
72
+ end
73
+
74
+ describe "#inspect" do
75
+ it "should respond with human-readable representation of a color" do
76
+ @color.inspect.should == "#<Color: hex: ea473f, rgb: [234, 71, 63]>"
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,13 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'colver'
5
+ require 'rspec'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colver
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - "Rafa\xC5\x82 Cie\xC5\x9Blak"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-13 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 0
31
+ version: 2.0.0
32
+ type: :development
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: jeweler
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 5
60
+ - 0
61
+ - pre5
62
+ version: 1.5.0.pre5
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: simplecov
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ - 3
76
+ - 5
77
+ version: 0.3.5
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 2
90
+ - 0
91
+ - 0
92
+ version: 2.0.0
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: *id005
96
+ - !ruby/object:Gem::Dependency
97
+ name: bundler
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 1
105
+ - 0
106
+ - 0
107
+ version: 1.0.0
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: *id006
111
+ - !ruby/object:Gem::Dependency
112
+ name: jeweler
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ segments:
119
+ - 1
120
+ - 5
121
+ - 0
122
+ - pre5
123
+ version: 1.5.0.pre5
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: *id007
127
+ - !ruby/object:Gem::Dependency
128
+ name: simplecov
129
+ requirement: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ segments:
135
+ - 0
136
+ - 3
137
+ - 5
138
+ version: 0.3.5
139
+ type: :development
140
+ prerelease: false
141
+ version_requirements: *id008
142
+ description: Simple gem for playing with colors
143
+ email: ravicious@gmail.com
144
+ executables: []
145
+
146
+ extensions: []
147
+
148
+ extra_rdoc_files:
149
+ - LICENSE.txt
150
+ - README.rdoc
151
+ files:
152
+ - .document
153
+ - .rspec
154
+ - Gemfile
155
+ - Gemfile.lock
156
+ - LICENSE.txt
157
+ - README.rdoc
158
+ - Rakefile
159
+ - VERSION
160
+ - lib/colver.rb
161
+ - lib/colver/color.rb
162
+ - spec/color_spec.rb
163
+ - spec/spec_helper.rb
164
+ has_rdoc: true
165
+ homepage: http://github.com/ravicious/colver
166
+ licenses: []
167
+
168
+ post_install_message:
169
+ rdoc_options: []
170
+
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ none: false
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ hash: 1034222001
179
+ segments:
180
+ - 0
181
+ version: "0"
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ none: false
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ segments:
188
+ - 0
189
+ version: "0"
190
+ requirements: []
191
+
192
+ rubyforge_project:
193
+ rubygems_version: 1.3.7
194
+ signing_key:
195
+ specification_version: 3
196
+ summary: Simple gem for playing with colors
197
+ test_files:
198
+ - spec/color_spec.rb
199
+ - spec/spec_helper.rb