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.
- checksums.yaml +4 -4
- data/lib/hex2rgb.rb +33 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 545ad7139fd9cb3b38849e4af47298eede452086
|
|
4
|
+
data.tar.gz: e94f55f78dbe2b66cca7bc72cb4f967bc8410c3f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b6872cb1f9412583247fcf897db973e2813aeec6d0002087ace242e7bfffedf9e2e6cf56757b618dfa6811c893a8b466891115b7b2774a88044712710cc19461
|
|
7
|
+
data.tar.gz: ee736295e2ad89a73114cbb025c2e81fd8ecfb803d177e271554b015ae12c2ca09fd407f3c1f0553400f7f66f56fa125149fabb75e3aeef117f48e840f4c876c
|
data/lib/hex2rgb.rb
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
|
+
# Class with single class method which does convertion
|
|
1
2
|
class Hex2Rgb
|
|
2
|
-
|
|
3
|
-
|
|
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
|