hex2rgb 0.0.0 → 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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hex2rgb.rb +33 -2
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f7bfb8b7cc04b46dc5cb13c4cff4369d77f59d1
4
- data.tar.gz: cf68b5a5366077901bf3bf0da88df27110855b0c
3
+ metadata.gz: 545ad7139fd9cb3b38849e4af47298eede452086
4
+ data.tar.gz: e94f55f78dbe2b66cca7bc72cb4f967bc8410c3f
5
5
  SHA512:
6
- metadata.gz: a086b39903776a090f519f3d9e570f56e28d75af339391a74e3c725b196a60f95cac11d75395c7467a0c880da7c6ed89b4942932a6aa05f1ff2d1ccae30d41b4
7
- data.tar.gz: 4a5e39ed9589de29a1ec9c2f929b029b61bac831b384feaa411d0830523df16849fb3d5ac8aaaaf57ae8de7afbd31d202c4f21ebdbfa9dea86bf6b28df960b01
6
+ metadata.gz: b6872cb1f9412583247fcf897db973e2813aeec6d0002087ace242e7bfffedf9e2e6cf56757b618dfa6811c893a8b466891115b7b2774a88044712710cc19461
7
+ data.tar.gz: ee736295e2ad89a73114cbb025c2e81fd8ecfb803d177e271554b015ae12c2ca09fd407f3c1f0553400f7f66f56fa125149fabb75e3aeef117f48e840f4c876c
@@ -1,5 +1,36 @@
1
+ # Class with single class method which does convertion
1
2
  class Hex2Rgb
2
- def self.convert(hex)
3
- "[0, 0, 0]"
3
+ # Produces an RGB triplet value out of HEX value.
4
+ #
5
+ # Hex2Rgb accepts both long (6 chars) and short (3 chars) formats
6
+ # of HEX values. Leading `#` char is optional as well. Empty method
7
+ # call defaults to HEX value of black `#000000`
8
+ #
9
+ # Example:
10
+ # >> Hex2Rgb("#fff")
11
+ # => [255, 255, 255]
12
+ #
13
+ # Arguments:
14
+ # hex: (String)
15
+
16
+ def self.convert(hex = "#000000")
17
+ result = []
18
+ hex.delete! "#"
19
+ hex.downcase!
20
+ hex.gsub!(/./) {|s| s + s} if (hex.length == 3)
21
+
22
+ Hex2Rgb.validate(hex)
23
+
24
+ hex.scan(/../) { |s| result << s.hex }
25
+ "[" + result.join(", ") + "]"
26
+ end
27
+
28
+ private
29
+
30
+ def self.validate (hex)
31
+ raise ArgumentError, "wrong input argument length", caller unless hex.length == 6
32
+ hex.each_char { |s|
33
+ raise ArgumentError, "only chars in ranges 0-9 and a-f are allowed", caller unless s.match(/[0-9a-f]/)
34
+ }
4
35
  end
5
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hex2rgb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Zelenkov