Class: ColorLegend::BaseLegend

Inherits:
Object
  • Object
show all
Defined in:
lib/graphics/base_legend.rb

Overview

This class provides basic methods for the other color legends to work with. The children need to define the method #create_output_string_for which should return the desired output string. If the child class does not implement this method BaseLegend raises a NotImplementedError.

Direct Known Subclasses

ColorData, ColorDelta

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_value, max_value) ⇒ BaseLegend

initialization

Parameters:

  • min_value (Float)

    minimum value

  • max_value (Float)

    maximum value



26
27
28
29
# File 'lib/graphics/base_legend.rb', line 26

def initialize(min_value, max_value)
  @min_value = min_value
  @max_value = max_value
end

Instance Attribute Details

#deltaFloat (readonly)

Returns size of intervals.

Returns:

  • (Float)

    size of intervals



21
22
23
# File 'lib/graphics/base_legend.rb', line 21

def delta
  @delta
end

#max_valueFloat (readonly)

Returns maximum boundary.

Returns:

  • (Float)

    maximum boundary



19
20
21
# File 'lib/graphics/base_legend.rb', line 19

def max_value
  @max_value
end

#min_valueFloat (readonly)

Returns minimum boundary.

Returns:

  • (Float)

    minimum boundary



17
18
19
# File 'lib/graphics/base_legend.rb', line 17

def min_value
  @min_value
end

#value_legendArray[Float] (readonly)

Returns Array with interval values.

Returns:

  • (Array[Float])

    Array with interval values



15
16
17
# File 'lib/graphics/base_legend.rb', line 15

def value_legend
  @value_legend
end

Instance Method Details

#create_color_legend(length) ⇒ Object (private)

creates color legend #min_value + i * #delta => value at index i + 1

Parameters:

  • length (Integer)

    the number of colors of the legend



71
72
73
74
75
76
77
78
79
80
# File 'lib/graphics/base_legend.rb', line 71

def create_color_legend(length)
  @delta = (@max_value.to_f - @min_value.to_f).abs / length

  @value_legend = Array.new(length)

  @value_legend.each_index { |index|
    @value_legend[index] = (@min_value +  (index + 1) *
                @delta).round(3)
  }
end

#create_output_string_for(value, out_str) ⇒ Object

This method is abstract.

subclasses need to implement this method

Raises:

  • (NotImplementedError)

    if the subclass does not have this method



60
61
62
63
64
# File 'lib/graphics/base_legend.rb', line 60

def create_output_string_for(value, out_str)
  fail NotImplementedError, " Error: the subclass #{self.class} " \
    "needs to implement the method: create_output_string_for " \
    "from its base class".red
end

prints color legend with given colors

Parameters:

  • with_legend (boolean)

    boolean which determines if the extended legend options should be printed



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphics/base_legend.rb', line 34

def print_color_legend(with_legend)
  puts "Legend: %.3f; %.3f; delta = %.3f" % [@min_value, @max_value, delta]
  @value_legend.each { |value|
    2.times {print create_output_string_for(value, '  ') }
  }

  if (with_legend)
    print_interval_values
  end

  puts
end

prints the intervall boundaries of the color legend if the parameter -l is set



49
50
51
52
53
54
55
56
# File 'lib/graphics/base_legend.rb', line 49

def print_interval_values
  @value_legend.each_index { |index|
    puts if (index % 5 == 0)
    value = @value_legend[index]
    print create_output_string_for(value, " <= #{value};").
          black.exchange_grounds
  }
end