Class: ColorPicker::Color
- Inherits:
-
Object
- Object
- ColorPicker::Color
- Defined in:
- lib/color_picker/color.rb
Instance Attribute Summary collapse
-
#hex_code ⇒ Object
readonly
Returns the value of attribute hex_code.
-
#rgb_code ⇒ Object
readonly
Returns the value of attribute rgb_code.
Instance Method Summary collapse
-
#initialize(code) ⇒ Color
constructor
A new instance of Color.
- #to_hex ⇒ Object
- #to_rgb ⇒ Object
- #to_s(type = nil) ⇒ Object
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_code ⇒ Object
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_code ⇒ Object
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_hex ⇒ Object
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_rgb ⇒ Object
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 |