hex_to_rgb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 861c8c35b07f9de0bb250e81c62e08789b851cfb
4
+ data.tar.gz: d1aca23066a92a3ae996a8b0381299183bec0275
5
+ SHA512:
6
+ metadata.gz: 516c724e828b02a7038121e9a4f0abe77789a00a6562ad9f7cc6f1643a10fbd1607482cc594dd537d0d47d89f5c4441e78443466e4b6536b56f258c4bbc3a168
7
+ data.tar.gz: 86442a66ab8b82ab2708576ed9c4764bce041d2e1de434447e5448fc815283e27b9a3c6a6fc9119f4049c95b50d7aa4f5a2571f26187b84d4a21a63a400c6b9f
@@ -0,0 +1,44 @@
1
+ class HexToRgb
2
+ def initialize(hex_color)
3
+ @hex = hex_color
4
+ end
5
+
6
+ def hex
7
+ @hex
8
+ end
9
+
10
+ def valid?
11
+ !!(hex =~ HEX_COLOR_REGEX)
12
+ end
13
+
14
+ def rgb
15
+ if valid?
16
+ rgb_digits
17
+ end
18
+ end
19
+
20
+ private
21
+ # Based on the article: http://www.mkyong.com/regular-expressions/how-to-validate-hex-color-code-with-regular-expression/
22
+ HEX_COLOR_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
23
+
24
+ def just_digits
25
+ hex.gsub('#','')
26
+ end
27
+
28
+ def hex_digits
29
+ digit_pattern =
30
+ case just_digits.length
31
+ when 3
32
+ /./
33
+ when 6
34
+ /../
35
+ end
36
+
37
+ just_digits.scan digit_pattern
38
+ end
39
+
40
+ def rgb_digits
41
+ hex_digits.map{|d| d.to_i(16) }
42
+ end
43
+ end
44
+
@@ -0,0 +1,86 @@
1
+ require_relative '../lib/hex_to_rgb.rb'
2
+
3
+ describe HexToRgb do
4
+ let(:expected_color) { '#FFFFFF' }
5
+ let(:initial_color) { expected_color }
6
+ let(:hex_to_rgb) { described_class.new(initial_color) }
7
+
8
+ describe "#hex" do
9
+ subject { hex_to_rgb.hex }
10
+
11
+ it "returns the color it was initialized with" do
12
+ expect(subject).to eq expected_color
13
+ end
14
+ end
15
+
16
+ describe "#valid?" do
17
+ subject { hex_to_rgb.valid? }
18
+
19
+ describe "for a valid hex color" do
20
+ shared_examples "is a valid color" do
21
+ it "is true" do
22
+ expect(subject).to eq true
23
+ end
24
+ end
25
+
26
+ describe "with 3 digits" do
27
+ let(:initial_color) { "#FFF" }
28
+
29
+ include_examples "is a valid color"
30
+ end
31
+
32
+ describe "with 6 digits" do
33
+ let(:initial_color) { "#FFFFFF" }
34
+
35
+ include_examples "is a valid color"
36
+ end
37
+ end
38
+
39
+ describe "for an invalid hex color" do
40
+ shared_examples "is not a valid color" do
41
+ it "is false" do
42
+ expect(subject).to eq false
43
+ end
44
+ end
45
+
46
+ describe "with no leading pound" do
47
+ let(:initial_color) { 'AAAAAA' }
48
+
49
+ include_examples "is not a valid color"
50
+ end
51
+
52
+ describe "with other than 3 or 6 digits" do
53
+ let(:initial_color) { '#AAAA' }
54
+
55
+ include_examples "is not a valid color"
56
+ end
57
+
58
+ describe "with non-hexadecimal digits" do
59
+ let(:initial_color) { '#XYZ' }
60
+
61
+ include_examples "is not a valid color"
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#rgb" do
67
+ subject { hex_to_rgb.rgb }
68
+
69
+ describe "with a valid hex color" do
70
+ let(:expected_rgb_colors) { [ 255, 255, 255 ] }
71
+
72
+ it "returns array of rgb decimal values" do
73
+ expect(subject).to eq expected_rgb_colors
74
+ end
75
+ end
76
+
77
+ describe "with an invalid hex color" do
78
+ let(:initial_color) { 'kyle' }
79
+
80
+ it "returns nil" do
81
+ expect(subject).to be_nil
82
+ end
83
+ end
84
+ end
85
+ end
86
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hex_to_rgb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Tolle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: kyle@nullsix.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/hex_to_rgb.rb
20
+ - spec/hex_to_rgb_spec.rb
21
+ homepage: https://github.com/kyletolle/hex_to_rgb
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Convert hexadecimal colors to their RGB values.
45
+ test_files: []
46
+ has_rdoc: