Class: ColorPicker::Color

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Color

Returns a new instance of Color.



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

def initialize(code)
  @hex_code = code.to_str if code.respond_to?(:to_str)
  @rgb_code = code.to_ary unless @hex_code
end

Instance Attribute Details

#hex_codeObject

Returns the value of attribute hex_code.



3
4
5
# File 'lib/color_picker/color.rb', line 3

def hex_code
  @hex_code
end

#rgb_codeObject

Returns the value of attribute rgb_code.



3
4
5
# File 'lib/color_picker/color.rb', line 3

def rgb_code
  @rgb_code
end

Instance Method Details

#to_hexObject



16
17
18
19
# File 'lib/color_picker/color.rb', line 16

def to_hex
  return self.to_s unless @rgb_code
  @rgb_code.map{|number| number.to_s(16)}.join.rjust(6, '0')
end

#to_rgbObject



11
12
13
14
# File 'lib/color_picker/color.rb', line 11

def to_rgb
  return self.to_s unless @hex_code
  @hex_code.scan(/.{2}/).map{|color| color.to_i(16)}
end

#to_s(type = nil) ⇒ Object



21
22
23
24
# File 'lib/color_picker/color.rb', line 21

def to_s(type=nil)
  return "rgb(#{rgb_code.join(', ')})" if type == :rgb || !rgb_code.nil?
  "##{hex_code}"
end