Class: Color::HSB

Inherits:
Object
  • Object
show all
Defined in:
lib/color/hsb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hue, saturation, brightness) ⇒ HSB

Returns a new instance of HSB.



12
13
14
15
16
17
18
19
20
# File 'lib/color/hsb.rb', line 12

def initialize(hue, saturation, brightness)
  Color::check_constraint(hue,        "hue",        0, 360)
  Color::check_constraint(saturation, "saturation", 0, 1)
  Color::check_constraint(brightness, "brightness", 0, 1)

  @hue        = hue
  @saturation = saturation
  @brightness = brightness
end

Instance Attribute Details

#brightnessObject

Returns the value of attribute brightness.



10
11
12
# File 'lib/color/hsb.rb', line 10

def brightness
  @brightness
end

#hueObject

Returns the value of attribute hue.



10
11
12
# File 'lib/color/hsb.rb', line 10

def hue
  @hue
end

#saturationObject

Returns the value of attribute saturation.



10
11
12
# File 'lib/color/hsb.rb', line 10

def saturation
  @saturation
end

Instance Method Details

#<=>(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/color/hsb.rb', line 45

def <=>(other)
  cmp = hue <=> other.hue
  if cmp == 0
    cmp = saturation <=> other.saturation
    if cmp == 0
      cmp = brightness <=> other.brightness
    end
  end
  
  cmp
end

#==(other) ⇒ Object



61
62
63
64
# File 'lib/color/hsb.rb', line 61

def ==(other)
  threshold = 0.0001
  floats_equal?(hue, other.hue, threshold) && floats_equal?(saturation, other.saturation, threshold) && floats_equal?(brightness, other.brightness, threshold)
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/color/hsb.rb', line 30

def equal?(other)
  $stderr.puts "*******"

  hue.equal?(other.hue) && 
  if cmp == 0
    cmp = saturation <=> other.saturation
    if cmp == 0
      cmp = brightness <=> other.brightness
    end
  end
  
  puts "returning: #{cmp}"
  cmp
end

#floats_equal?(a, b, threshold) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/color/hsb.rb', line 57

def floats_equal?(a, b, threshold)
  (a - b).abs < 0.0001
end

#inspectObject



26
27
28
# File 'lib/color/hsb.rb', line 26

def inspect
  to_s
end

#to_rgbObject

Converts HSB (HSV) to RGB. Hue is expected as 0-360; saturation and brightness (value) as 0.0-1.0. Returns an RGB object.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/color/hsb.rb', line 68

def to_rgb
  red = green = blue = nil
  
  if saturation == 0
    red = green = blue = brightness * 255
  else
    hi = (hue / 60.0).to_i
    f  = (hue / 60.0) - hi
    p = brightness * (1 - saturation)
    q = brightness * (1 - saturation * f)
    t = brightness * (1 - saturation * (1 - f))
    
    red, green, blue = case hi
                       when 0
                         [ brightness, t, p ]
                       when 1
                         [ q, brightness, p ]
                       when 2
                         [ p, brightness, t ]
                       when 3
                         [ p, q, brightness ]
                       when 4
                         [ t, p, brightness ]
                       when 5
                         [ brightness, p, q ]
                       else
                         raise "error: hue: #{hue}; hi: #{hi}"
                       end
    
    red   *= 255
    green *= 255
    blue  *= 255
  end
  
  RGB.new red, green, blue
end

#to_sObject



22
23
24
# File 'lib/color/hsb.rb', line 22

def to_s
  "hue: #{hue}; saturation: #{saturation}; brightness: #{brightness}"
end