Skip to content

Commit

Permalink
Add hexadecimal format checking
Browse files Browse the repository at this point in the history
  • Loading branch information
chiting committed Jun 7, 2018
1 parent e4f74cf commit 0880e17
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rainbow/color.rb
Expand Up @@ -36,6 +36,11 @@ def self.build(ground, values)
end

def self.parse_hex_color(hex)
unless hex =~ /^#?[a-f0-9]{6}/i
raise ArgumentError,
"Invalid hexadecimal RGB triplet. Valid format: /^#?[a-f0-9]{6}/i"
end

hex = hex.sub(/^#/, '')
r = hex[0..1].to_i(16)
g = hex[2..3].to_i(16)
Expand Down

0 comments on commit 0880e17

Please sign in to comment.