Class: Colorable::HSB

Inherits:
ColorSpace show all
Defined in:
lib/colorable/color_space.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ColorSpace

#-, #<=>, #coerce, #move_to_top, #to_s

Methods included from Converter

#hex2rgb, #hsb2rgb, #name2rgb, #rgb2hex, #rgb2hsb, #rgb2hsl, #rgb2name

Constructor Details

#initialize(h = 0, s = 0, b = 0) ⇒ HSB

Returns a new instance of HSB.



135
136
137
# File 'lib/colorable/color_space.rb', line 135

def initialize(h=0,s=0,b=0)
  @h, @s, @b = @hsb = validate_hsb([h, s, b])
end

Instance Attribute Details

#bObject Also known as: bright

Returns the value of attribute b.



134
135
136
# File 'lib/colorable/color_space.rb', line 134

def b
  @b
end

#hObject Also known as: hue

Returns the value of attribute h.



134
135
136
# File 'lib/colorable/color_space.rb', line 134

def h
  @h
end

#hsbObject Also known as: to_a

Returns the value of attribute hsb.



134
135
136
# File 'lib/colorable/color_space.rb', line 134

def hsb
  @hsb
end

#sObject Also known as: sat

Returns the value of attribute s.



134
135
136
# File 'lib/colorable/color_space.rb', line 134

def s
  @s
end

Instance Method Details

#+(arg) ⇒ Object

Pass Array of [h, s, b] or a Fixnum. Returns new HSB object with added HSB.



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/colorable/color_space.rb', line 146

def +(arg)
  arg =
    case arg
    when Array
      raise ArgumentError, "Must be three numbers contained" unless arg.size==3
      arg
    else
      raise ArgumentError, "Accept only Array of three numbers"
    end
  new_hsb = self.hsb.zip(arg, [360, 101, 101]).map { |x, y, div| (x + y) % div }
  self.class.new *new_hsb
end

#to_hexObject



167
168
169
# File 'lib/colorable/color_space.rb', line 167

def to_hex
  rgb2hex(self.to_rgb)
end

#to_nameObject



159
160
161
# File 'lib/colorable/color_space.rb', line 159

def to_name
  rgb2name(self.to_rgb)
end

#to_rgbObject



163
164
165
# File 'lib/colorable/color_space.rb', line 163

def to_rgb
  hsb2rgb(self.to_a)
end