Class: Camalian::Color
- Inherits:
-
Object
- Object
- Camalian::Color
- Defined in:
- lib/camalian/color.rb
Instance Attribute Summary collapse
-
#b ⇒ Object
readonly
Returns the value of attribute b.
-
#g ⇒ Object
readonly
Returns the value of attribute g.
-
#h ⇒ Object
readonly
Returns the value of attribute h.
-
#hsv ⇒ Object
readonly
Returns the value of attribute hsv.
-
#l ⇒ Object
readonly
Returns the value of attribute l.
-
#r ⇒ Object
readonly
Returns the value of attribute r.
-
#s ⇒ Object
readonly
Returns the value of attribute s.
Instance Method Summary collapse
- #distance(color) ⇒ Object
- #extract_rgb(color_hash) ⇒ Object
-
#initialize(*value) ⇒ Color
constructor
A new instance of Color.
- #to_hex ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*value) ⇒ Color
Returns a new instance of Color.
6 7 8 9 10 11 12 13 |
# File 'lib/camalian/color.rb', line 6 def initialize(*value) if value.size == 1 rgb = extract_rgb(value.first) build_components(rgb[0], rgb[1], rgb[2]) elsif value.size == 3 build_components(value[0], value[1], value[2]) end end |
Instance Attribute Details
#b ⇒ Object (readonly)
Returns the value of attribute b.
4 5 6 |
# File 'lib/camalian/color.rb', line 4 def b @b end |
#g ⇒ Object (readonly)
Returns the value of attribute g.
4 5 6 |
# File 'lib/camalian/color.rb', line 4 def g @g end |
#h ⇒ Object (readonly)
Returns the value of attribute h.
4 5 6 |
# File 'lib/camalian/color.rb', line 4 def h @h end |
#hsv ⇒ Object (readonly)
Returns the value of attribute hsv.
4 5 6 |
# File 'lib/camalian/color.rb', line 4 def hsv @hsv end |
#l ⇒ Object (readonly)
Returns the value of attribute l.
4 5 6 |
# File 'lib/camalian/color.rb', line 4 def l @l end |
#r ⇒ Object (readonly)
Returns the value of attribute r.
4 5 6 |
# File 'lib/camalian/color.rb', line 4 def r @r end |
#s ⇒ Object (readonly)
Returns the value of attribute s.
4 5 6 |
# File 'lib/camalian/color.rb', line 4 def s @s end |
Instance Method Details
#distance(color) ⇒ Object
23 24 25 |
# File 'lib/camalian/color.rb', line 23 def distance(color) [(self.h - color.h) % 360, (color.h - self.h) % 360].min end |
#extract_rgb(color_hash) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/camalian/color.rb', line 27 def extract_rgb(color_hash) color_hash = color_hash[0..6] color_hash = color_hash[1..6] if color_hash[0] == '#' r = color_hash[0..1].to_i(16) g = color_hash[2..3].to_i(16) b = color_hash[4..5].to_i(16) [r, g, b] end |
#to_hex ⇒ Object
19 20 21 |
# File 'lib/camalian/color.rb', line 19 def to_hex "##{r.to_s(16).rjust(2, '0')}#{g.to_s(16).rjust(2, '0')}#{b.to_s(16).rjust(2, '0')}" end |
#to_s ⇒ Object
15 16 17 |
# File 'lib/camalian/color.rb', line 15 def to_s "red=#{r} green=#{g} blue=#{b} hue=#{h} saturation=#{s} lightness=#{l}" end |