Class: Savio::HsvColor

Inherits:
Struct
  • Object
show all
Defined in:
lib/savio/hsv2rgb.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hObject

Returns the value of attribute h

Returns:

  • (Object)

    the current value of h



39
40
41
# File 'lib/savio/hsv2rgb.rb', line 39

def h
  @h
end

#sObject

Returns the value of attribute s

Returns:

  • (Object)

    the current value of s



39
40
41
# File 'lib/savio/hsv2rgb.rb', line 39

def s
  @s
end

#vObject

Returns the value of attribute v

Returns:

  • (Object)

    the current value of v



39
40
41
# File 'lib/savio/hsv2rgb.rb', line 39

def v
  @v
end

Class Method Details

.newFromRGB(rgb) ⇒ Object



40
41
42
43
# File 'lib/savio/hsv2rgb.rb', line 40

def self.newFromRGB(rgb)
  hsv = rgb.to_hsv()
  return HsvColor.new(hsv.h,hsv.s,hsv.v)
end

Instance Method Details

#to_rgbObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/savio/hsv2rgb.rb', line 44

def to_rgb()
  hue = h.to_f
  saturation = s.to_f
  value = v.to_f

  chroma = (value * saturation).to_f
  hPrime = hue/60.0
  x = (chroma * (1 - (hPrime % 2 - 1).abs)).to_f

  if 0 <= hPrime && hPrime < 1
    rgb = [chroma, x, 0]
  elsif 1 <= hPrime && hPrime < 2
    rgb = [x, chroma, 0]
  elsif 2 <= hPrime && hPrime < 3
    rgb = [0, chroma, x]
  elsif 3 <= hPrime && hPrime < 4
    rgb = [0, x, chroma]
  elsif 4 <= hPrime && hPrime < 5
    rgb = [x, 0, chroma]
  elsif 5 <= hPrime && hPrime < 6
    rgb = [chroma, 0, x]
  else
    rgb = [0,0,0]
  end

  match = (value - chroma).to_f

  rgb[0] = (rgb[0] + match).to_f
  rgb[1] = (rgb[1] + match).to_f
  rgb[2] = (rgb[2] + match).to_f

  return RgbColor.new(rgb[0],rgb[1],rgb[2])
end