Class: Ruby2D::Color::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby2d/color.rb

Overview

An array of colors

Instance Method Summary collapse

Constructor Details

#initialize(colors) ⇒ Set

Create a color set from an array of colors

Raises:



11
12
13
14
15
# File 'lib/ruby2d/color.rb', line 11

def initialize(colors)
  raise Error, 'a Color::Set requires at least one color, got an empty array' if colors.empty?

  @colors = colors.map { |c| Color.new(c) }
end

Instance Method Details

#[](index) ⇒ Object

Get a color by index



18
19
20
# File 'lib/ruby2d/color.rb', line 18

def [](index)
  @colors[index]
end

#each(&block) ⇒ Object

Iterate over each color



28
29
30
# File 'lib/ruby2d/color.rb', line 28

def each(&block)
  @colors.each(&block)
end

#first(*args) ⇒ Object

Get the first color, or the first n colors if a count is given



33
34
35
# File 'lib/ruby2d/color.rb', line 33

def first(*args)
  @colors.first(*args)
end

#last(*args) ⇒ Object

Get the last color, or the last n colors if a count is given



38
39
40
# File 'lib/ruby2d/color.rb', line 38

def last(*args)
  @colors.last(*args)
end

#lengthObject

Get the number of colors in the set



23
24
25
# File 'lib/ruby2d/color.rb', line 23

def length
  @colors.length
end

#opacityObject

Get the opacity of the first color



43
44
45
# File 'lib/ruby2d/color.rb', line 43

def opacity
  @colors.first.opacity
end

#opacity=(opacity) ⇒ Object

Set the opacity for all colors



48
49
50
51
52
53
54
55
56
# File 'lib/ruby2d/color.rb', line 48

def opacity=(opacity)
  unless opacity.is_a?(Numeric)
    raise ArgumentError, "opacity must be a number between 0.0 and 1.0, got #{opacity.inspect}"
  end

  @colors.each do |color|
    color.opacity = opacity
  end
end

#vertex(i) ⇒ Object

The color for the i-th vertex. Paired with Color#vertex so per-vertex rendering code can uniformly call color.vertex(i) whether it holds a single Color (every vertex is the same) or a Color::Set.



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

def vertex(i)
  @colors[i]
end