Class: Abachrome::Parsers::Hex
- Inherits:
-
Object
- Object
- Abachrome::Parsers::Hex
- Defined in:
- lib/abachrome/parsers/hex.rb
Constant Summary collapse
- HEX_PATTERN =
/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{8})$/
Class Method Summary collapse
- .parse(input) ⇒ Object
- .parse_full_hex(hex) ⇒ Object
- .parse_full_hex_with_alpha(hex) ⇒ Object
- .parse_short_hex(hex) ⇒ Object
- .parse_short_hex_with_alpha(hex) ⇒ Object
Class Method Details
.parse(input) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/abachrome/parsers/hex.rb', line 8 def self.parse(input) hex = input.gsub(/^#/, "") return nil unless hex.match?(HEX_PATTERN) case hex.length when 3 parse_short_hex(hex) when 4 parse_short_hex_with_alpha(hex) when 6 parse_full_hex(hex) when 8 parse_full_hex_with_alpha(hex) end end |
.parse_full_hex(hex) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/abachrome/parsers/hex.rb', line 34 def self.parse_full_hex(hex) r = hex[0, 2].to_i(16) g = hex[2, 2].to_i(16) b = hex[4, 2].to_i(16) Color.from_rgb(r / 255.0, g / 255.0, b / 255.0) end |
.parse_full_hex_with_alpha(hex) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/abachrome/parsers/hex.rb', line 41 def self.parse_full_hex_with_alpha(hex) r = hex[0, 2].to_i(16) g = hex[2, 2].to_i(16) b = hex[4, 2].to_i(16) a = hex[6, 2].to_i(16) Color.from_rgb(r / 255.0, g / 255.0, b / 255.0, a / 255.0) end |
.parse_short_hex(hex) ⇒ Object
24 25 26 27 |
# File 'lib/abachrome/parsers/hex.rb', line 24 def self.parse_short_hex(hex) r, g, b = hex.chars.map { |c| (c + c).to_i(16) } Color.from_rgb(r / 255.0, g / 255.0, b / 255.0) end |
.parse_short_hex_with_alpha(hex) ⇒ Object
29 30 31 32 |
# File 'lib/abachrome/parsers/hex.rb', line 29 def self.parse_short_hex_with_alpha(hex) r, g, b, a = hex.chars.map { |c| (c + c).to_i(16) } Color.from_rgb(r / 255.0, g / 255.0, b / 255.0, a / 255.0) end |