color_conversion 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/.gitignore +20 -0
  2. data/Gemfile +4 -0
  3. data/Guardfile +9 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +67 -0
  6. data/Rakefile +22 -0
  7. data/color_conversion.gemspec +31 -0
  8. data/lib/color_conversion.rb +20 -0
  9. data/lib/color_conversion/color.rb +10 -0
  10. data/lib/color_conversion/color_converter.rb +124 -0
  11. data/lib/color_conversion/converters/cmyk_converter.rb +26 -0
  12. data/lib/color_conversion/converters/hex_converter.rb +26 -0
  13. data/lib/color_conversion/converters/hsl_converter.rb +51 -0
  14. data/lib/color_conversion/converters/hsl_string_converter.rb +24 -0
  15. data/lib/color_conversion/converters/hsv_converter.rb +35 -0
  16. data/lib/color_conversion/converters/name_converter.rb +167 -0
  17. data/lib/color_conversion/converters/null_converter.rb +14 -0
  18. data/lib/color_conversion/converters/rgb_converter.rb +21 -0
  19. data/lib/color_conversion/converters/rgb_string_converter.rb +25 -0
  20. data/lib/color_conversion/errors.rb +4 -0
  21. data/lib/color_conversion/version.rb +3 -0
  22. data/spec/color_converter_spec.rb +63 -0
  23. data/spec/color_spec.rb +94 -0
  24. data/spec/converters/cmyk_converter_spec.rb +31 -0
  25. data/spec/converters/hex_converter_spec.rb +33 -0
  26. data/spec/converters/hsl_converter_spec.rb +41 -0
  27. data/spec/converters/hsl_string_converter_spec.rb +31 -0
  28. data/spec/converters/hsv_converter_spec.rb +36 -0
  29. data/spec/converters/name_converter_spec.rb +26 -0
  30. data/spec/converters/null_converter_spec.rb +19 -0
  31. data/spec/converters/rgb_converter_spec.rb +41 -0
  32. data/spec/converters/rgb_string_converter_spec.rb +45 -0
  33. data/spec/spec_helper.rb +9 -0
  34. data/spec/version_spec.rb +9 -0
  35. metadata +174 -0
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe CmykConverter do
4
+ describe ".matches?" do
5
+ it "should match args with cmyk hash" do
6
+ expect(CmykConverter.matches?(c: 87, m: 69, y: 13, k: 1)).to be_true
7
+ end
8
+
9
+ it "should not match args without cmyk hash" do
10
+ expect(CmykConverter.matches?(h: 225, s: 73, v: 57)).to be_false
11
+ end
12
+
13
+ it "should not match a string" do
14
+ expect(CmykConverter.matches?("#ffffff")).to be_false
15
+ end
16
+ end
17
+
18
+ describe ".rgba" do
19
+ it "should convert to rgba" do
20
+ conv = CmykConverter.new(c: 74, m: 58, y: 22, k: 3)
21
+ rgba = {r: 64, g: 104, b: 193, a: 1.0}
22
+ expect(conv.rgba).to eq rgba
23
+ end
24
+
25
+ it "should convert cymk strings to rgba" do
26
+ conv = CmykConverter.new(c: "74", m: "58", y: "22", k: "3")
27
+ rgba = {r: 64, g: 104, b: 193, a: 1.0}
28
+ expect(conv.rgba).to eq rgba
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe HexConverter do
4
+ describe ".matches?" do
5
+ it "should match args with hex string" do
6
+ expect(HexConverter.matches?("#FFFFFF")).to be_true
7
+ end
8
+
9
+ it "should match args with short hex string" do
10
+ expect(HexConverter.matches?("#FFF")).to be_true
11
+ end
12
+
13
+ it "should match args with lower case hex string" do
14
+ expect(HexConverter.matches?("#ffffff")).to be_true
15
+ end
16
+
17
+ it "should not match args without hex string" do
18
+ expect(HexConverter.matches?("asdf")).to be_false
19
+ end
20
+
21
+ it "should not match args without hex string" do
22
+ expect(HexConverter.matches?(h: 225, s: 73, l: 57)).to be_false
23
+ end
24
+ end
25
+
26
+ describe ".rgba" do
27
+ it "should convert to rgba" do
28
+ conv = HexConverter.new("#3366cc")
29
+ rgba = {r: 51, g: 102, b: 204, a: 1.0}
30
+ expect(conv.rgba).to eq rgba
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe HslConverter do
4
+ describe ".matches?" do
5
+ it "should match args with hsl hash" do
6
+ expect(HslConverter.matches?(h: 225, s: 73, l: 57)).to be_true
7
+ end
8
+
9
+ it "should match args with hsla hash" do
10
+ expect(HslConverter.matches?(h: 225, s: 73, l: 57, a: 0.5)).to be_true
11
+ end
12
+
13
+ it "should not match args without hsl hash" do
14
+ expect(HslConverter.matches?(h: 225, s: 73, v: 57)).to be_false
15
+ end
16
+
17
+ it "should not match a string" do
18
+ expect(HslConverter.matches?("#ffffff")).to be_false
19
+ end
20
+ end
21
+
22
+ describe ".rgba" do
23
+ it "should convert hsl to rgba" do
24
+ conv = HslConverter.new(h: 225, s: 73, l: 57)
25
+ rgba = {r: 65, g: 105, b: 225, a: 1.0}
26
+ expect(conv.rgba).to eq rgba
27
+ end
28
+
29
+ it "should convert hsla to rgba" do
30
+ conv = HslConverter.new(h: 225, s: 73, l: 57, a: 0.5)
31
+ rgba = {r: 65, g: 105, b: 225, a: 0.5}
32
+ expect(conv.rgba).to eq rgba
33
+ end
34
+
35
+ it "should convert hsl string to rgba" do
36
+ conv = HslConverter.new(h: "225", s: "73%", l: "57%", a: "0.5")
37
+ rgba = {r: 65, g: 105, b: 225, a: 0.5}
38
+ expect(conv.rgba).to eq rgba
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe HslStringConverter do
4
+ describe ".matches?" do
5
+ it "should match args with hsl string" do
6
+ expect(HslStringConverter.matches?("hsl(225, 73%, 57%)")).to be_true
7
+ end
8
+
9
+ it "should match args with hsla string" do
10
+ expect(HslStringConverter.matches?("hsla(225, 73%, 57%, 0.5)")).to be_true
11
+ end
12
+
13
+ it "should not match args without hsl string" do
14
+ expect(HslStringConverter.matches?(h: 225, s: 73, l: 57)).to be_false
15
+ end
16
+ end
17
+
18
+ describe ".rgba" do
19
+ it "should convert hsl to rgba" do
20
+ conv = HslStringConverter.new("hsl(225, 73%, 57%)")
21
+ rgba = {r: 65, g: 105, b: 225, a: 1.0}
22
+ expect(conv.rgba).to eq rgba
23
+ end
24
+
25
+ it "should convert hsla to rgba" do
26
+ conv = HslStringConverter.new("hsla(225, 73%, 57%, 0.5)")
27
+ rgba = {r: 65, g: 105, b: 225, a: 0.5}
28
+ expect(conv.rgba).to eq rgba
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe HsvConverter do
4
+ describe ".matches?" do
5
+ it "should match args with hsv hash" do
6
+ expect(HsvConverter.matches?(h: 225, s: 73, v: 57)).to be_true
7
+ end
8
+
9
+ it "should match args with hsb hash" do
10
+ expect(HsvConverter.matches?(h: 225, s: 73, b: 57)).to be_true
11
+ end
12
+
13
+ it "should not match args without hsv hash" do
14
+ expect(HsvConverter.matches?(h: 225, s: 73, l: 57)).to be_false
15
+ end
16
+
17
+ it "should not match a string" do
18
+ expect(HsvConverter.matches?("#ffffff")).to be_false
19
+ end
20
+ end
21
+
22
+ describe ".rgba" do
23
+ it "should convert hsv to rgba" do
24
+ conv = HsvConverter.new(h: 220, s: 75, v: 80)
25
+ rgba = {r: 51, g: 102, b: 204, a: 1.0}
26
+ expect(conv.rgba).to eq rgba
27
+ end
28
+
29
+ it "should convert hsb to rgba" do
30
+ conv = HsvConverter.new(h: 220, s: 75, b: 80)
31
+ rgba = {r: 51, g: 102, b: 204, a: 1.0}
32
+ expect(conv.rgba).to eq rgba
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe NameConverter do
4
+ describe ".matches?" do
5
+ it "should match args with name string" do
6
+ expect(NameConverter.matches?("blue")).to be_true
7
+ end
8
+
9
+ it "should match args with color insensitive string" do
10
+ expect(NameConverter.matches?("RoyalBlue")).to be_true
11
+ expect(NameConverter.matches?("royalblue")).to be_true
12
+ end
13
+
14
+ it "should not match args without name string" do
15
+ expect(NameConverter.matches?("#ffffff")).to be_false
16
+ end
17
+ end
18
+
19
+ describe ".rgb" do
20
+ it "should convert to rgb" do
21
+ conv = NameConverter.new("blue")
22
+ rgba = {r: 0, g: 0, b: 255, a: 1.0}
23
+ expect(conv.rgba).to eq rgba
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe NullConverter do
4
+ describe ".matches?" do
5
+ it "should always match" do
6
+ expect(NullConverter.matches?("blue")).to be_true
7
+ expect(NullConverter.matches?(r: 51, g: 102, b: 204)).to be_true
8
+ end
9
+ end
10
+
11
+ describe ".rgba" do
12
+ it "should throw an error" do
13
+ expect {
14
+ conv = NullConverter.new("asdf")
15
+ conv.rgba
16
+ }.to raise_error(InvalidColorError)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe RgbConverter do
4
+ describe ".matches?" do
5
+ it "should match args with rgb hash" do
6
+ expect(RgbConverter.matches?(r: 51, g: 102, b: 204)).to be_true
7
+ end
8
+
9
+ it "should match args with rgba hash" do
10
+ expect(RgbConverter.matches?(r: 51, g: 102, b: 204, a: 0.5)).to be_true
11
+ end
12
+
13
+ it "should not match args without rgb hash" do
14
+ expect(RgbConverter.matches?(h: 225, s: 73, l: 57)).to be_false
15
+ end
16
+
17
+ it "should not match a string" do
18
+ expect(RgbConverter.matches?("#ffffff")).to be_false
19
+ end
20
+ end
21
+
22
+ describe ".rgb" do
23
+ it "should convert rgb to rgba" do
24
+ conv = RgbConverter.new(r: 51, g: 102, b: 204)
25
+ rgba = {r: 51, g: 102, b: 204, a: 1.0}
26
+ expect(conv.rgba).to eq rgba
27
+ end
28
+
29
+ it "should convert rgba to rgba" do
30
+ conv = RgbConverter.new(r: 51, g: 102, b: 204, a: 0.5)
31
+ rgba = {r: 51, g: 102, b: 204, a: 0.5}
32
+ expect(conv.rgba).to eq rgba
33
+ end
34
+
35
+ it "should convert rgba strings to rgba" do
36
+ conv = RgbConverter.new(r: "51", g: "102", b: "204", a: "0.5")
37
+ rgba = {r: 51, g: 102, b: 204, a: 0.5}
38
+ expect(conv.rgba).to eq rgba
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe RgbStringConverter do
4
+ describe ".matches?" do
5
+ it "should match args with rgb string" do
6
+ expect(RgbStringConverter.matches?("rgb(51, 102, 204)")).to be_true
7
+ end
8
+
9
+ it "should match args with rgba string" do
10
+ expect(RgbStringConverter.matches?("rgba(51, 102, 204, 0.2)")).to be_true
11
+ end
12
+
13
+ it "should not match args without rgb string" do
14
+ expect(RgbStringConverter.matches?(r: 51, g: 102, b: 204)).to be_false
15
+ end
16
+ end
17
+
18
+ describe ".rgb" do
19
+ it "should convert rgb to rgba" do
20
+ conv = RgbStringConverter.new("rgb(51, 102, 204)")
21
+ rgba = {r: 51, g: 102, b: 204, a: 1.0}
22
+ expect(conv.rgba).to eq rgba
23
+ end
24
+
25
+ it "should convert rgba to rgba" do
26
+ conv = RgbStringConverter.new("rgba(51, 102, 204, 0.5)")
27
+ rgba = {r: 51, g: 102, b: 204, a: 0.5}
28
+ expect(conv.rgba).to eq rgba
29
+ end
30
+
31
+ it "should raise error for invalid color values" do
32
+ expect {
33
+ conv = RgbStringConverter.new("rgba(foo)")
34
+ conv.rgba
35
+ }.to raise_error(InvalidColorError)
36
+ end
37
+
38
+ it "should raise error for missing values" do
39
+ expect {
40
+ conv = RgbStringConverter.new("rgba(51, 102)")
41
+ conv.rgba
42
+ }.to raise_error(InvalidColorError)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require_relative '../lib/color_conversion.rb'
5
+
6
+ include ColorConversion
7
+
8
+ RSpec.configure do |config|
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe VERSION do
4
+ describe "version" do
5
+ it "should be present" do
6
+ expect(ColorConversion::VERSION).not_to be_nil
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: color_conversion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Derek DeVries
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.9'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.9'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.7'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.5'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.5'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rb-fsevent
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '0.9'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '0.9'
94
+ description: Convert colors to hex/rgb/hsv
95
+ email:
96
+ - derek@sportspyder.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - Guardfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - color_conversion.gemspec
108
+ - lib/color_conversion.rb
109
+ - lib/color_conversion/color.rb
110
+ - lib/color_conversion/color_converter.rb
111
+ - lib/color_conversion/converters/cmyk_converter.rb
112
+ - lib/color_conversion/converters/hex_converter.rb
113
+ - lib/color_conversion/converters/hsl_converter.rb
114
+ - lib/color_conversion/converters/hsl_string_converter.rb
115
+ - lib/color_conversion/converters/hsv_converter.rb
116
+ - lib/color_conversion/converters/name_converter.rb
117
+ - lib/color_conversion/converters/null_converter.rb
118
+ - lib/color_conversion/converters/rgb_converter.rb
119
+ - lib/color_conversion/converters/rgb_string_converter.rb
120
+ - lib/color_conversion/errors.rb
121
+ - lib/color_conversion/version.rb
122
+ - spec/color_converter_spec.rb
123
+ - spec/color_spec.rb
124
+ - spec/converters/cmyk_converter_spec.rb
125
+ - spec/converters/hex_converter_spec.rb
126
+ - spec/converters/hsl_converter_spec.rb
127
+ - spec/converters/hsl_string_converter_spec.rb
128
+ - spec/converters/hsv_converter_spec.rb
129
+ - spec/converters/name_converter_spec.rb
130
+ - spec/converters/null_converter_spec.rb
131
+ - spec/converters/rgb_converter_spec.rb
132
+ - spec/converters/rgb_string_converter_spec.rb
133
+ - spec/spec_helper.rb
134
+ - spec/version_spec.rb
135
+ homepage: https://github.com/devrieda/color_conversion
136
+ licenses:
137
+ - MIT
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: 1.9.3
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.25
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: Convert colors to hex/rgb/hsv
160
+ test_files:
161
+ - spec/color_converter_spec.rb
162
+ - spec/color_spec.rb
163
+ - spec/converters/cmyk_converter_spec.rb
164
+ - spec/converters/hex_converter_spec.rb
165
+ - spec/converters/hsl_converter_spec.rb
166
+ - spec/converters/hsl_string_converter_spec.rb
167
+ - spec/converters/hsv_converter_spec.rb
168
+ - spec/converters/name_converter_spec.rb
169
+ - spec/converters/null_converter_spec.rb
170
+ - spec/converters/rgb_converter_spec.rb
171
+ - spec/converters/rgb_string_converter_spec.rb
172
+ - spec/spec_helper.rb
173
+ - spec/version_spec.rb
174
+ has_rdoc: