Class: Falu::Color
- Inherits:
-
Object
- Object
- Falu::Color
- Defined in:
- lib/falu/color.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
Class Method Summary collapse
- .color(color) ⇒ Object
- .dump(color) ⇒ Object
- .hex?(color) ⇒ Boolean
- .hsl?(color) ⇒ Boolean
- .load(json) ⇒ Object
- .rgb?(color) ⇒ Boolean
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #hex ⇒ Object
- #hsl ⇒ Object
-
#initialize(color) ⇒ Color
constructor
A new instance of Color.
- #name ⇒ Object
- #rgb ⇒ Object
- #to_s(stype = :hex) ⇒ Object
Constructor Details
Instance Attribute Details
#color ⇒ Object (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
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
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
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(={}) # TODO options to control what type to return hex.as_json end |
#hex ⇒ Object
62 63 64 |
# File 'lib/falu/color.rb', line 62 def hex @hex ||= color.to_hex end |
#hsl ⇒ Object
70 71 72 |
# File 'lib/falu/color.rb', line 70 def hsl @hsl ||= color.to_hsl end |
#name ⇒ Object
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 |
#rgb ⇒ Object
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 |