Class: ColorCode::HSV

Inherits:
Object
  • Object
show all
Includes:
HueSaturation
Defined in:
lib/color_code/hsv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HueSaturation

#to_rgb, #to_s

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

#hObject

Returns the value of attribute h.



5
6
7
# File 'lib/color_code/hsv.rb', line 5

def h
  @h
end

#sObject

Returns the value of attribute s.



5
6
7
# File 'lib/color_code/hsv.rb', line 5

def s
  @s
end

#vObject

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_aObject



21
22
23
# File 'lib/color_code/hsv.rb', line 21

def to_a
  [@h, @s, @v]
end

#to_hashObject



25
26
27
# File 'lib/color_code/hsv.rb', line 25

def to_hash
  { h: @h, s: @s, v: @v }
end