Class: Camalian::Color
- Inherits:
-
Object
- Object
- Camalian::Color
- Defined in:
- lib/camalian/color.rb
Overview
Camalian color object
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.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Used for object comparison.
-
#hash ⇒ Object
Used for array uniqueness.
- #hue_distance(color) ⇒ Object
-
#initialize(r, g, b) ⇒ Color
constructor
A new instance of Color.
- #rgb_distance(color) ⇒ Object
- #to_hex ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(r, g, b) ⇒ Color
Returns a new instance of Color.
8 9 10 |
# File 'lib/camalian/color.rb', line 8 def initialize(r, g, b) build_components(r, g, b) end |
Instance Attribute Details
#b ⇒ Object (readonly)
Returns the value of attribute b.
6 7 8 |
# File 'lib/camalian/color.rb', line 6 def b @b end |
#g ⇒ Object (readonly)
Returns the value of attribute g.
6 7 8 |
# File 'lib/camalian/color.rb', line 6 def g @g end |
#h ⇒ Object (readonly)
Returns the value of attribute h.
6 7 8 |
# File 'lib/camalian/color.rb', line 6 def h @h end |
#hsv ⇒ Object (readonly)
Returns the value of attribute hsv.
6 7 8 |
# File 'lib/camalian/color.rb', line 6 def hsv @hsv end |
#l ⇒ Object (readonly)
Returns the value of attribute l.
6 7 8 |
# File 'lib/camalian/color.rb', line 6 def l @l end |
#r ⇒ Object (readonly)
Returns the value of attribute r.
6 7 8 |
# File 'lib/camalian/color.rb', line 6 def r @r end |
#s ⇒ Object (readonly)
Returns the value of attribute s.
6 7 8 |
# File 'lib/camalian/color.rb', line 6 def s @s end |
Class Method Details
.from_hex(hex_value) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/camalian/color.rb', line 12 def self.from_hex(hex_value) color_hash = hex_value[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) Color.new(r, g, b) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Used for object comparison
36 37 38 |
# File 'lib/camalian/color.rb', line 36 def ==(other) r == other.r && g == other.g && b == other.b end |
#hash ⇒ Object
Used for array uniqueness
31 32 33 |
# File 'lib/camalian/color.rb', line 31 def hash "#{r}#{g}#{b}".to_i end |
#hue_distance(color) ⇒ Object
41 42 43 |
# File 'lib/camalian/color.rb', line 41 def hue_distance(color) [(h - color.h) % 360, (color.h - h) % 360].min end |
#rgb_distance(color) ⇒ Object
45 46 47 |
# File 'lib/camalian/color.rb', line 45 def rgb_distance(color) Math.sqrt(((r - color.r)**2) + ((g - color.g)**2) + ((b - color.b)**2)) end |
#to_hex ⇒ Object
26 27 28 |
# File 'lib/camalian/color.rb', line 26 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
22 23 24 |
# File 'lib/camalian/color.rb', line 22 def to_s "red=#{r} green=#{g} blue=#{b} hue=#{h} saturation=#{s} lightness=#{l}" end |