Class: Cairo::Color::HSV

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

Instance Attribute Summary collapse

Attributes inherited from Base

#alpha

Instance Method Summary collapse

Constructor Details

#initialize(h, s, v, a = 1.0) ⇒ HSV

Returns a new instance of HSV.



215
216
217
218
219
220
221
222
# File 'lib/cairo/color.rb', line 215

def initialize(h, s, v, a=1.0)
  super(a)
  assert_in_range(s, "saturation")
  assert_in_range(v, "value")
  @hue = h.modulo(360.0)
  @saturation = s
  @value = v
end

Instance Attribute Details

#hueObject Also known as: h

Returns the value of attribute hue.



206
207
208
# File 'lib/cairo/color.rb', line 206

def hue
  @hue
end

#saturationObject Also known as: s

Returns the value of attribute saturation.



206
207
208
# File 'lib/cairo/color.rb', line 206

def saturation
  @saturation
end

#valueObject Also known as: v

Returns the value of attribute value.



206
207
208
# File 'lib/cairo/color.rb', line 206

def value
  @value
end

Instance Method Details

#to_aObject Also known as: to_ary



224
225
226
# File 'lib/cairo/color.rb', line 224

def to_a
  [@hue, @saturation, @value, @alpha]
end

#to_cmykObject



258
259
260
# File 'lib/cairo/color.rb', line 258

def to_cmyk
  to_rgb.to_cmyk
end

#to_hsvObject



262
263
264
# File 'lib/cairo/color.rb', line 262

def to_hsv
  clone
end

#to_rgbObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cairo/color.rb', line 229

def to_rgb
  if s > 0
    h_60 = @hue / 60.0
    hi = h_60.floor.modulo(6)
    f = h_60 - hi
    p = @value * (1 - @saturation)
    q = @value * (1 - f * @saturation)
    t = @value * (1 - (1 - f) * @saturation)
    case hi
    when 0
      rgb = [@value, t, p]
    when 1
      rgb = [q, @value, p]
    when 2
      rgb = [p, @value, t]
    when 3
      rgb = [p, q, @value]
    when 4
      rgb = [t, p, @value]
    when 5
      rgb = [@value, p, q]
    end
    rgba = rgb + [@alpha]
    RGB.new(*rgba)
  else
    RGB.new(@value, @value, @value, @alpha)
  end
end