Class: ColorLegend::BaseLegend
- Inherits:
-
Object
- Object
- ColorLegend::BaseLegend
- 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
Instance Attribute Summary collapse
-
#delta ⇒ Float
readonly
Size of intervals.
-
#max_value ⇒ Float
readonly
Maximum boundary.
-
#min_value ⇒ Float
readonly
Minimum boundary.
-
#value_legend ⇒ Array[Float]
readonly
Array with interval values.
Instance Method Summary collapse
-
#create_color_legend(length) ⇒ Object
private
creates color legend #min_value + i * #delta => value at index i + 1.
- #create_output_string_for(value, out_str) ⇒ Object abstract
-
#initialize(min_value, max_value) ⇒ BaseLegend
constructor
initialization.
-
#print_color_legend(with_legend) ⇒ Object
prints color legend with given colors.
-
#print_interval_values ⇒ Object
prints the intervall boundaries of the color legend if the parameter -l is set.
Constructor Details
#initialize(min_value, max_value) ⇒ BaseLegend
initialization
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
#delta ⇒ Float (readonly)
Returns size of intervals.
21 22 23 |
# File 'lib/graphics/base_legend.rb', line 21 def delta @delta end |
#max_value ⇒ Float (readonly)
Returns maximum boundary.
19 20 21 |
# File 'lib/graphics/base_legend.rb', line 19 def max_value @max_value end |
#min_value ⇒ Float (readonly)
Returns minimum boundary.
17 18 19 |
# File 'lib/graphics/base_legend.rb', line 17 def min_value @min_value end |
#value_legend ⇒ Array[Float] (readonly)
Returns 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
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
subclasses need to implement 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 |
#print_color_legend(with_legend) ⇒ Object
prints color legend with given colors
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 |
#print_interval_values ⇒ Object
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 |