Class: HSV

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

Overview

Hue Saturation and Value. Hue can range from [0,360). Saturation and Value are [0,1]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colour

#analogous, #complementary, #gradient_to, #hex, #rotate_hue, #split_complementary, #to_hsv, #to_s, #triadic, #web_hex, #web_safe

Constructor Details

#initialize(h = 0.0, s = 0.0, v = 0.0) ⇒ HSV



8
9
10
11
12
# File 'lib/hsv.rb', line 8

def initialize(h=0.0, s=0.0, v=0.0)
  @h = h.abs.to_f % 360
  @s = s.abs.to_f
  @v = v.abs.to_f 
end

Instance Attribute Details

#hObject

Returns the value of attribute h.



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

def h
  @h
end

#sObject

Returns the value of attribute s.



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

def s
  @s
end

#vObject

Returns the value of attribute v.



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

def v
  @v
end

Instance Method Details

#to_cmykObject



41
42
43
# File 'lib/hsv.rb', line 41

def to_cmyk
  self.to_rgb.to_cmyk
end

#to_rgbObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hsv.rb', line 14

def to_rgb
  if (@s == 0) then #Shade of grey
    RGB.new(@v, @v, @v)
  else
    @h /= 60
    i = @h.floor
    f = @h - i
    p = @v * (1 - @s)
    q = @v * (1 - @s * f)
    t = @v * (1 - @s * (1 - f))
    case i
      when 0 then
        RGB.new(v,t,p)
      when 1 then
        RGB.new(q,v,p)
      when 2 then
        RGB.new(p,v,t)
      when 3 then
        RGB.new(p,q,v)
      when 4 then
        RGB.new(t,p,v)
      else
        RGB.new(v,p,q)
    end
  end
end