Class: CTioga::MovingArray

Inherits:
Object
  • Object
show all
Includes:
Tioga
Defined in:
lib/CTioga/movingarrays.rb

Overview

The MovingArray class provides a way to scroll through an array, such as a set of colors, markers, linstyles, and so on. The class provides a way to set the value directly as well, and to forget it.

Instance Method Summary collapse

Constructor Details

#initialize(sets, current = nil, startpoint = 0) ⇒ MovingArray

Returns a new instance of MovingArray.



51
52
53
54
55
56
57
58
59
# File 'lib/CTioga/movingarrays.rb', line 51

def initialize(sets, current = nil, startpoint = 0)
  @sets = sets

  # Pick the first key as a default
  current = @sets.keys.first unless current
  choose_current_set(current) 

  @current_point = startpoint # the current point in the current set
end

Instance Method Details

#available_setsObject

Returns the available sets.



112
113
114
# File 'lib/CTioga/movingarrays.rb', line 112

def available_sets
  return @sets.keys
end

#choose_current_set(name) ⇒ Object

Chooses the current set. If the name refers to an existing set, this one is chosen. If not, and if the name looks like a gradient specification, we interpret it as such. In any other case, the chosen set is the set containing only the argument. This way, to get a set containing a single color, you can use

choose_current_set(Pink)


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
104
105
106
107
108
109
# File 'lib/CTioga/movingarrays.rb', line 76

def choose_current_set(name)
  @current_set_name = name
  if @sets.key?(name)
    @current_set_name = name
    @current_set = @sets[name]
  elsif name =~ /gradient:(.+)--(.+),(\d+)/
    # Special case for color gradients
    s = CTioga.get_tioga_color($1)
    e = CTioga.get_tioga_color($2)
    nb = $3.to_i
    fact = if nb > 1
             1.0/(nb - 1)     # The famous off-by one...
           else
             warn "Incorrect gradient number: #{nb}"
             1.0
           end
    array = []
    nb.times do |i|
      a = []
      f = i * fact
      e.each_index do |c|
        a << s[c] * (1 - f) + e[c] * f
      end
      array << a
    end
    @current_set = array
  else
    if name.is_a? SpecialArray
      @current_set = name
    else
      @current_set = [name]
    end
  end
end

#valid_set?(name) ⇒ Boolean

Tells whether the given name is a valid set. Takes care of the special case of gradients.

TODO: this looks hackish, somehow, but I don’t see a way to make that less so…

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/CTioga/movingarrays.rb', line 121

def valid_set?(name)
  if name =~ /^gradient:/
    return true
  else
    return @sets.key? name
  end
end

#valueObject



61
62
63
64
65
66
67
# File 'lib/CTioga/movingarrays.rb', line 61

def value
  @current_point += 1
  if @current_point >= @current_set.length
    @current_point %= @current_set.length
  end
  return @current_set[@current_point - 1]
end