hex_to_rgb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b183e00bf03da4227c59ea5de39a8966ba48e53
4
- data.tar.gz: c182bf56998f3c24163580d248aa7151c10600ce
3
+ metadata.gz: 7c2cad3ed6450f932f663ce4d69efc8bc4cba63d
4
+ data.tar.gz: b3e05080f41325f6151ea5d6db71ca8a35d43247
5
5
  SHA512:
6
- metadata.gz: d73278f9465386325cc992bc43d32de97d99d3442295c78d9c2b00165344d0b327d3ade3694c062fc7ce171fc6350b04e6242494e2956d4b71887a4b8d7c98ba
7
- data.tar.gz: 612ed30e7cdcef2b281defd7e9659e8c99bd6d6b3fbd6bdfc40fa6c975144734c488c19298eca5fd4bfd974ca4520d21736828376883591fc71e7fa6e160605e
6
+ metadata.gz: 033fadd217e0a836d1087cfe73778fdbd35937a7137a34b87c3b41b57bb4df917b6fd26dc86f194d8c07c81435bbaef11cfd4900328e32eb1a5554b800899181
7
+ data.tar.gz: 46d562cf7c1f335f4230fab43baae5031e55f38bcb0cff48f21dc3dff46f15b6dccb170c867be97a9128adf52c1fe49371c6d3d854c373de892ba21046e619b2
data/lib/hex_to_rgb.rb CHANGED
@@ -17,6 +17,24 @@ class HexToRgb
17
17
  end
18
18
  end
19
19
 
20
+ def r
21
+ if valid?
22
+ rgb_digits[0]
23
+ end
24
+ end
25
+
26
+ def g
27
+ if valid?
28
+ rgb_digits[1]
29
+ end
30
+ end
31
+
32
+ def b
33
+ if valid?
34
+ rgb_digits[2]
35
+ end
36
+ end
37
+
20
38
  private
21
39
  # Based on the article: http://www.mkyong.com/regular-expressions/how-to-validate-hex-color-code-with-regular-expression/
22
40
  HEX_COLOR_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
@@ -38,7 +56,18 @@ private
38
56
  end
39
57
 
40
58
  def rgb_digits
41
- hex_digits.map{|d| d.to_i(16) }
59
+ @rgb_digits ||=
60
+ hex_digits.map do |d|
61
+ digit =
62
+ case just_digits.length
63
+ when 3
64
+ d*2
65
+ when 6
66
+ d
67
+ end
68
+
69
+ digit.to_i(16)
70
+ end
42
71
  end
43
72
  end
44
73
 
@@ -14,6 +14,10 @@ describe HexToRgb do
14
14
  end
15
15
  end
16
16
 
17
+ shared_context "with invalid hex color" do
18
+ let(:initial_color) { '#XYZ' }
19
+ end
20
+
17
21
  describe "#valid?" do
18
22
  subject { hex_to_rgb.valid? }
19
23
 
@@ -57,7 +61,7 @@ describe HexToRgb do
57
61
  end
58
62
 
59
63
  describe "with non-hexadecimal digits" do
60
- let(:initial_color) { '#XYZ' }
64
+ include_context "with invalid hex color"
61
65
 
62
66
  include_examples "is not a valid color"
63
67
  end
@@ -68,10 +72,22 @@ describe HexToRgb do
68
72
  subject { hex_to_rgb.rgb }
69
73
 
70
74
  describe "with a valid hex color" do
71
- let(:expected_rgb_colors) { [ 255, 255, 255 ] }
75
+ describe "with 6 digits" do
76
+ let(:expected_rgb_colors) { [ 255, 255, 255 ] }
77
+
78
+ it "returns array of rgb decimal values" do
79
+ expect(subject).to eq expected_rgb_colors
80
+ end
81
+ end
82
+
83
+ describe "with 3 digits" do
84
+ let(:initial_color) { '#FFF' }
72
85
 
73
- it "returns array of rgb decimal values" do
74
- expect(subject).to eq expected_rgb_colors
86
+ let(:expected_rgb_colors) { [ 255, 255, 255 ] }
87
+
88
+ it "returns array of rgb decimal values" do
89
+ expect(subject).to eq expected_rgb_colors
90
+ end
75
91
  end
76
92
  end
77
93
 
@@ -83,5 +99,101 @@ describe HexToRgb do
83
99
  end
84
100
  end
85
101
  end
102
+
103
+ describe "#r" do
104
+ subject { hex_to_rgb.r }
105
+
106
+ describe "with a valid hex color" do
107
+ let(:expected_r) { 170 }
108
+
109
+ describe "with 6 digits" do
110
+ let(:initial_color) { '#AABBCC' }
111
+
112
+ it "is the decimal value of the R hex digit" do
113
+ expect(subject).to eq expected_r
114
+ end
115
+ end
116
+
117
+ describe "with 3 digits" do
118
+ let(:initial_color) { '#ABC' }
119
+
120
+ it "is the decimal value of the R hex digit" do
121
+ expect(subject).to eq expected_r
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "with an invalid hex color" do
127
+ include_context "with invalid hex color"
128
+
129
+ it "is nil" do
130
+ expect(subject).to be_nil
131
+ end
132
+ end
133
+ end
134
+
135
+ describe "#g" do
136
+ subject { hex_to_rgb.g }
137
+
138
+ describe "with a valid hex color" do
139
+ let(:expected_g) { 187 }
140
+
141
+ describe "with 6 digits" do
142
+ let(:initial_color) { '#AABBCC' }
143
+
144
+ it "is the decimal value of the G hex digit" do
145
+ expect(subject).to eq expected_g
146
+ end
147
+ end
148
+
149
+ describe "with 3 digits" do
150
+ let(:initial_color) { '#ABC' }
151
+
152
+ it "is the decimal value of the G hex digit" do
153
+ expect(subject).to eq expected_g
154
+ end
155
+ end
156
+ end
157
+
158
+ describe "with an invalid hex color" do
159
+ include_context "with invalid hex color"
160
+
161
+ it "is nil" do
162
+ expect(subject).to be_nil
163
+ end
164
+ end
165
+ end
166
+
167
+ describe "#b" do
168
+ subject { hex_to_rgb.b }
169
+
170
+ describe "with a valid hex color" do
171
+ let(:expected_b) { 204 }
172
+
173
+ describe "with 6 digits" do
174
+ let(:initial_color) { '#AABBCC' }
175
+
176
+ it "is the decimal value of the B hex digit" do
177
+ expect(subject).to eq expected_b
178
+ end
179
+ end
180
+
181
+ describe "with 3 digits" do
182
+ let(:initial_color) { '#ABC' }
183
+
184
+ it "is the decimal value of the B hex digit" do
185
+ expect(subject).to eq expected_b
186
+ end
187
+ end
188
+ end
189
+
190
+ describe "with an invalid hex color" do
191
+ include_context "with invalid hex color"
192
+
193
+ it "is nil" do
194
+ expect(subject).to be_nil
195
+ end
196
+ end
197
+ end
86
198
  end
87
199
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hex_to_rgb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Tolle