Class: ColorCode::HSV
- Inherits:
-
Object
- Object
- ColorCode::HSV
- Includes:
- HueSaturation
- Defined in:
- lib/color_code/hsv.rb
Instance Attribute Summary collapse
-
#h ⇒ Object
Returns the value of attribute h.
-
#s ⇒ Object
Returns the value of attribute s.
-
#v ⇒ Object
Returns the value of attribute v.
Instance Method Summary collapse
- #distance(hsv) ⇒ Object
-
#initialize(code = nil, h: 0, s: 0, v: 0) ⇒ HSV
constructor
A new instance of HSV.
- #to_a ⇒ Object
- #to_hash ⇒ Object
Methods included from HueSaturation
Constructor Details
#initialize(code = nil, h: 0, s: 0, v: 0) ⇒ HSV
Returns a new instance of HSV.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/color_code/hsv.rb', line 7 def initialize(code=nil, h: 0, s: 0, v: 0) if code hsv = ColorCode::RGB.new(code).to_hsv h, s, v = hsv.to_a end @h = h @s = s @v = v raise if @h > 360 || @s > 100 || @v > 100 rescue raise ArgumentError.new('invalid value') end |
Instance Attribute Details
#h ⇒ Object
Returns the value of attribute h.
5 6 7 |
# File 'lib/color_code/hsv.rb', line 5 def h @h end |
#s ⇒ Object
Returns the value of attribute s.
5 6 7 |
# File 'lib/color_code/hsv.rb', line 5 def s @s end |
#v ⇒ Object
Returns the value of attribute v.
5 6 7 |
# File 'lib/color_code/hsv.rb', line 5 def v @v end |
Instance Method Details
#distance(hsv) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/color_code/hsv.rb', line 29 def distance(hsv) diff = if @h > hsv.h [@h - hsv.h, hsv.h - @h + 360].min else [hsv.h - @h, @h - hsv.h + 360].min end Math.sqrt(diff**2 + (@s - hsv.s)**2 + (@v - hsv.v)**2) end |
#to_a ⇒ Object
21 22 23 |
# File 'lib/color_code/hsv.rb', line 21 def to_a [@h, @s, @v] end |
#to_hash ⇒ Object
25 26 27 |
# File 'lib/color_code/hsv.rb', line 25 def to_hash { h: @h, s: @s, v: @v } end |