Class: Falu::Colors::HEX

Inherits:
Base
  • Object
show all
Defined in:
lib/falu/colors/hex.rb

Instance Attribute Summary

Attributes inherited from Base

#color

Instance Method Summary collapse

Methods inherited from Base

#as_json

Constructor Details

#initialize(*color, rgb: nil, hsl: nil) ⇒ HEX

Returns a new instance of HEX.



6
7
8
9
10
# File 'lib/falu/colors/hex.rb', line 6

def initialize(*color, rgb: nil, hsl: nil)
  color = color[0] if color.length == 1
  raise "Invalid HEX Color: #{color.to_s}" unless Falu::Color.hex?(color)
  @color = color.is_a?(Array) ? color.join : color
end

Instance Method Details

#blueObject



27
28
29
# File 'lib/falu/colors/hex.rb', line 27

def blue
  colors[2]
end

#colorsObject



12
13
14
15
16
17
# File 'lib/falu/colors/hex.rb', line 12

def colors
  @colors ||= begin
    match = color.to_s.match(/#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/)
    match[1..3] unless match.nil?
  end
end

#greenObject



23
24
25
# File 'lib/falu/colors/hex.rb', line 23

def green
  colors[1]
end

#redObject



19
20
21
# File 'lib/falu/colors/hex.rb', line 19

def red
  colors[0]
end

#to_aObject



35
36
37
# File 'lib/falu/colors/hex.rb', line 35

def to_a
  colors
end

#to_hObject



39
40
41
# File 'lib/falu/colors/hex.rb', line 39

def to_h
  {red: red, green: green, blue: blue}
end

#to_hexObject



43
44
45
# File 'lib/falu/colors/hex.rb', line 43

def to_hex
  self
end

#to_hslObject



51
52
53
# File 'lib/falu/colors/hex.rb', line 51

def to_hsl
  to_rgb.to_hsl
end

#to_rgbObject



47
48
49
# File 'lib/falu/colors/hex.rb', line 47

def to_rgb
  Falu::Colors::RGB.new(colors.map { |c| c.to_i(16) })
end

#to_sObject



31
32
33
# File 'lib/falu/colors/hex.rb', line 31

def to_s
  "##{colors.join}"
end