Class: Falu::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/falu/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color) ⇒ Color

Returns a new instance of Color.



53
54
55
# File 'lib/falu/color.rb', line 53

def initialize(color)
  @color = color.is_a?(Falu::Colors::Base) ? color : self.class.color(color)
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



7
8
9
# File 'lib/falu/color.rb', line 7

def color
  @color
end

Class Method Details

.color(color) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/falu/color.rb', line 18

def color(color)
  if hex?(color)
    Falu::Colors::HEX.new(color)
  elsif rgb?(color) && !hsl?(color)
    Falu::Colors::RGB.new(color)
  elsif hsl?(color) && !rgb?(color)
    Falu::Colors::HSL.new(color)
  else
    raise "Unable to determine color type (RGB vs HSL): #{color.to_s}"
  end
end

.dump(color) ⇒ Object



10
11
12
# File 'lib/falu/color.rb', line 10

def dump(color)
  color.as_json
end

.hex?(color) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/falu/color.rb', line 30

def hex?(color)
  color = color.join('') if color.is_a?(Array)
  !!color.to_s.match(/#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/)
end

.hsl?(color) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'lib/falu/color.rb', line 43

def hsl?(color)
  color = color.to_s.split(',') unless color.is_a?(Array)
  color = color.map { |c| c.to_f }
  return false if color.length != 3
  return false if color[0] > 360 || color[0] < 0
  return false if color[1..2].any? { |c| c > 100 || c < 0 }
  true
end

.load(json) ⇒ Object



14
15
16
# File 'lib/falu/color.rb', line 14

def load(json)
  new(json.values.first)
end

.rgb?(color) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/falu/color.rb', line 35

def rgb?(color)
  color = color.to_s.split(',') unless color.is_a?(Array)
  color = color.map { |c| c.to_i }
  return false if color.length != 3
  return false if color.any? { |c| c > 255 || c < 0 }
  true
end

Instance Method Details

#<=>(other) ⇒ Object



74
75
76
# File 'lib/falu/color.rb', line 74

def <=>(other)
  rgb.absolute <=> other.rgb.absolute
end

#as_json(options = {}) ⇒ Object



82
83
84
85
# File 'lib/falu/color.rb', line 82

def as_json(options={})
  # TODO options to control what type to return
  hex.as_json
end

#hexObject



62
63
64
# File 'lib/falu/color.rb', line 62

def hex
  @hex ||= color.to_hex
end

#hslObject



70
71
72
# File 'lib/falu/color.rb', line 70

def hsl
  @hsl ||= color.to_hsl
end

#nameObject



57
58
59
60
# File 'lib/falu/color.rb', line 57

def name
  cname = Falu.colors.find { |c| c[:hex].downcase == hex.to_s.downcase }
  cname.nil? ? cname : cname[:name]
end

#rgbObject



66
67
68
# File 'lib/falu/color.rb', line 66

def rgb
  @rgb ||= color.to_rgb
end

#to_s(stype = :hex) ⇒ Object



78
79
80
# File 'lib/falu/color.rb', line 78

def to_s(stype=:hex)
  send(stype).to_s
end