image_paradise 0.1.12

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of image_paradise might be problematic. Click here for more details.

Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +38 -0
  3. data/README.md +179 -0
  4. data/USAGE.md +10 -0
  5. data/bin/black_white +10 -0
  6. data/bin/display_text_from_this_image +7 -0
  7. data/bin/image_paradise +7 -0
  8. data/bin/image_paradise_shell +7 -0
  9. data/bin/image_to_ascii +54 -0
  10. data/bin/image_to_pdf +7 -0
  11. data/bin/make_this_image_transparent +7 -0
  12. data/bin/rotate_left +7 -0
  13. data/bin/rotate_right +7 -0
  14. data/doc/README.gen +162 -0
  15. data/doc/TODO_FOR_THE_GTK_GUI.md +1 -0
  16. data/image_paradise.gemspec +50 -0
  17. data/lib/image_paradise.rb +5 -0
  18. data/lib/image_paradise/base/base.rb +91 -0
  19. data/lib/image_paradise/black_white.rb +89 -0
  20. data/lib/image_paradise/confree_generator/class.rb +614 -0
  21. data/lib/image_paradise/confree_generator/constants.rb +61 -0
  22. data/lib/image_paradise/confree_generator/gui/gtk_confree_generator.rb +752 -0
  23. data/lib/image_paradise/confree_generator/gui/insert_button.rb +78 -0
  24. data/lib/image_paradise/confree_generator/reset.rb +61 -0
  25. data/lib/image_paradise/confree_generator/shared/shared.rb +100 -0
  26. data/lib/image_paradise/constants.rb +43 -0
  27. data/lib/image_paradise/constants/image_file_types.rb +21 -0
  28. data/lib/image_paradise/create_animated_gif.rb +47 -0
  29. data/lib/image_paradise/crop/crop.rb +302 -0
  30. data/lib/image_paradise/gm_support.rb +34 -0
  31. data/lib/image_paradise/graphs.rb +36 -0
  32. data/lib/image_paradise/graphs/accumulator_bar.rb +29 -0
  33. data/lib/image_paradise/graphs/area.rb +64 -0
  34. data/lib/image_paradise/graphs/bar.rb +117 -0
  35. data/lib/image_paradise/graphs/bar_conversion.rb +53 -0
  36. data/lib/image_paradise/graphs/base.rb +1392 -0
  37. data/lib/image_paradise/graphs/bezier.rb +45 -0
  38. data/lib/image_paradise/graphs/bullet.rb +115 -0
  39. data/lib/image_paradise/graphs/deprecated.rb +42 -0
  40. data/lib/image_paradise/graphs/dot.rb +129 -0
  41. data/lib/image_paradise/graphs/line.rb +328 -0
  42. data/lib/image_paradise/graphs/mini/bar.rb +42 -0
  43. data/lib/image_paradise/graphs/mini/legend.rb +109 -0
  44. data/lib/image_paradise/graphs/mini/pie.rb +42 -0
  45. data/lib/image_paradise/graphs/mini/side_bar.rb +41 -0
  46. data/lib/image_paradise/graphs/net.rb +133 -0
  47. data/lib/image_paradise/graphs/photo_bar.rb +106 -0
  48. data/lib/image_paradise/graphs/pie.rb +139 -0
  49. data/lib/image_paradise/graphs/scatter.rb +264 -0
  50. data/lib/image_paradise/graphs/scene.rb +216 -0
  51. data/lib/image_paradise/graphs/side_bar.rb +144 -0
  52. data/lib/image_paradise/graphs/side_stacked_bar.rb +116 -0
  53. data/lib/image_paradise/graphs/spider.rb +163 -0
  54. data/lib/image_paradise/graphs/stacked_area.rb +73 -0
  55. data/lib/image_paradise/graphs/stacked_bar.rb +68 -0
  56. data/lib/image_paradise/graphs/stacked_mixin.rb +30 -0
  57. data/lib/image_paradise/graphs/themes.rb +117 -0
  58. data/lib/image_paradise/graphviz/README.md +2 -0
  59. data/lib/image_paradise/graphviz/generate_graphviz_image.rb +274 -0
  60. data/lib/image_paradise/gui/gtk/control_panel.rb +126 -0
  61. data/lib/image_paradise/identify.rb +145 -0
  62. data/lib/image_paradise/image_border.rb +231 -0
  63. data/lib/image_paradise/image_manipulations.rb +320 -0
  64. data/lib/image_paradise/image_paradise.rb +150 -0
  65. data/lib/image_paradise/image_to_ascii/image_to_ascii.rb +187 -0
  66. data/lib/image_paradise/image_to_pdf/image_to_pdf.rb +99 -0
  67. data/lib/image_paradise/label/README.md +2 -0
  68. data/lib/image_paradise/label/simple_label.rb +206 -0
  69. data/lib/image_paradise/optimizer.rb +483 -0
  70. data/lib/image_paradise/project/project.rb +29 -0
  71. data/lib/image_paradise/random_text_to_image.rb +363 -0
  72. data/lib/image_paradise/requires/common_base_requires.rb +17 -0
  73. data/lib/image_paradise/requires/require_colours.rb +9 -0
  74. data/lib/image_paradise/requires/require_gtk_components.rb +8 -0
  75. data/lib/image_paradise/requires/require_image_to_ascii.rb +7 -0
  76. data/lib/image_paradise/requires/require_the_image_paradise_project.rb +24 -0
  77. data/lib/image_paradise/requires/require_toplevel_methods.rb +21 -0
  78. data/lib/image_paradise/rotate/README.md +2 -0
  79. data/lib/image_paradise/rotate/rotate.rb +98 -0
  80. data/lib/image_paradise/shell/interactive.rb +156 -0
  81. data/lib/image_paradise/svg/README.md +5 -0
  82. data/lib/image_paradise/svg/circle.rb +106 -0
  83. data/lib/image_paradise/svg/feature.rb +48 -0
  84. data/lib/image_paradise/svg/rectangle.rb +154 -0
  85. data/lib/image_paradise/svg/svg.rb +102 -0
  86. data/lib/image_paradise/to_gif.rb +91 -0
  87. data/lib/image_paradise/to_jpg.rb +90 -0
  88. data/lib/image_paradise/toplevel_methods/add_black_border_to_this_image.rb +56 -0
  89. data/lib/image_paradise/toplevel_methods/crop.rb +28 -0
  90. data/lib/image_paradise/toplevel_methods/e.rb +16 -0
  91. data/lib/image_paradise/toplevel_methods/esystem.rb +19 -0
  92. data/lib/image_paradise/toplevel_methods/extract_text_from_this_image.rb +56 -0
  93. data/lib/image_paradise/toplevel_methods/file_related_code.rb +25 -0
  94. data/lib/image_paradise/toplevel_methods/flip_image_left_right.rb +32 -0
  95. data/lib/image_paradise/toplevel_methods/greyscale_this_image.rb +59 -0
  96. data/lib/image_paradise/toplevel_methods/help.rb +30 -0
  97. data/lib/image_paradise/toplevel_methods/make_this_image_transparent.rb +30 -0
  98. data/lib/image_paradise/toplevel_methods/menu.rb +92 -0
  99. data/lib/image_paradise/toplevel_methods/merge_these_images.rb +49 -0
  100. data/lib/image_paradise/toplevel_methods/mirror_image.rb +28 -0
  101. data/lib/image_paradise/toplevel_methods/misc.rb +31 -0
  102. data/lib/image_paradise/toplevel_methods/png_to_svg.rb +34 -0
  103. data/lib/image_paradise/toplevel_methods/roebe.rb +17 -0
  104. data/lib/image_paradise/toplevel_methods/to_png.rb +105 -0
  105. data/lib/image_paradise/toplevel_methods/wallpaper.rb +37 -0
  106. data/lib/image_paradise/toplevel_methods/write_this_text.rb +76 -0
  107. data/lib/image_paradise/version/version.rb +19 -0
  108. data/test/16x16_red_square_image_for_testing.png +0 -0
  109. data/test/testing_confree_generator.rb +8 -0
  110. data/test/testing_crop.rb +19 -0
  111. data/test/testing_image_magick_commands.rb +39 -0
  112. data/test/testing_image_paradise.rb +49 -0
  113. data/test/testing_the_svg_component.html +261 -0
  114. data/test/testing_the_svg_component.rb +106 -0
  115. metadata +217 -0
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'image_paradise/graphs/base.rb'
6
+
7
+ ##
8
+ # Here's how to make a Pie graph:
9
+ #
10
+ # g = Gruff::Pie.new
11
+ # g.title = "Visual Pie Graph Test"
12
+ # g.data 'Fries', 20
13
+ # g.data 'Hamburgers', 50
14
+ # g.write("test/output/pie_keynote.png")
15
+ #
16
+ # To control where the pie chart starts creating slices, use #zero_degree.
17
+
18
+ module ImageParadise
19
+
20
+ class Gruff::Pie < Gruff::Base
21
+
22
+ # === DEFAULT_TEXT_OFFSET_PERCENTAGE
23
+ DEFAULT_TEXT_OFFSET_PERCENTAGE = 0.15
24
+
25
+ # Can be used to make the pie start cutting slices at the top (-90.0)
26
+ # or at another angle. Default is 0.0, which starts at 3 o'clock.
27
+ attr_accessor :zero_degree
28
+ # Do not show labels for slices that are less than this percent. Use 0 to always show all labels.
29
+ # Defaults to 0
30
+ attr_accessor :hide_labels_less_than
31
+ # Affect the distance between the percentages and the pie chart
32
+ # Defaults to 0.15
33
+ attr_accessor :text_offset_percentage
34
+ ## Use values instead of percentages
35
+ attr_accessor :show_values_as_labels
36
+
37
+ # ========================================================================= #
38
+ # === initialize_ivars
39
+ # ========================================================================= #
40
+ def initialize_ivars
41
+ super
42
+ @zero_degree = 0.0
43
+ @hide_labels_less_than = 0.0
44
+ @text_offset_percentage = DEFAULT_TEXT_OFFSET_PERCENTAGE
45
+ @show_values_as_labels = false
46
+ end
47
+
48
+ # ========================================================================= #
49
+ # === draw
50
+ # ========================================================================= #
51
+ def draw
52
+ @hide_line_markers = true
53
+ super
54
+ return unless @has_data
55
+ # diameter = @graph_height # Assigned but unusued
56
+ radius = ([@graph_width, @graph_height].min / 2.0) * 0.8
57
+ center_x = @graph_left + (@graph_width / 2.0)
58
+ center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit
59
+ total_sum = sums_for_pie()
60
+ prev_degrees = @zero_degree
61
+
62
+ # Use full data since we can easily calculate percentages
63
+ data = (@sort ? @data.sort{ |a, b| a[DATA_VALUES_INDEX].first <=> b[DATA_VALUES_INDEX].first } : @data)
64
+ data.each do |data_row|
65
+ if data_row[DATA_VALUES_INDEX].first > 0
66
+ @d = @d.stroke data_row[DATA_COLOR_INDEX]
67
+ @d = @d.fill 'transparent'
68
+ @d.stroke_width(radius) # stroke width should be equal to radius. we'll draw centered on (radius / 2)
69
+
70
+ current_degrees = (data_row[DATA_VALUES_INDEX].first / total_sum) * 360.0
71
+
72
+ # ellipse will draw the the stroke centered on the first two parameters offset by the second two.
73
+ # therefore, in order to draw a circle of the proper diameter we must center the stroke at
74
+ # half the radius for both x and y
75
+ @d = @d.ellipse(center_x, center_y,
76
+ radius / 2.0, radius / 2.0,
77
+ prev_degrees, prev_degrees + current_degrees + 0.5) # <= +0.5 'fudge factor' gets rid of the ugly gaps
78
+
79
+ half_angle = prev_degrees + ((prev_degrees + current_degrees) - prev_degrees) / 2
80
+
81
+ label_val = ((data_row[DATA_VALUES_INDEX].first / total_sum) * 100.0).round
82
+ unless label_val < @hide_labels_less_than
83
+ # RMagick must use sprintf with the string and % has special significance.
84
+ label_string = @show_values_as_labels ? data_row[DATA_VALUES_INDEX].first.to_s : label_val.to_s + '%'
85
+ @d = draw_label(center_x,center_y, half_angle,
86
+ radius + (radius * @text_offset_percentage),
87
+ label_string)
88
+ end
89
+
90
+ prev_degrees += current_degrees
91
+ end
92
+ end
93
+
94
+ # TODO debug a circle where the text is drawn...
95
+
96
+ @d.draw(@base_image)
97
+ end
98
+
99
+ private
100
+
101
+ # ========================================================================= #
102
+ # === draw_label
103
+ #
104
+ # Labels are drawn around a slightly wider ellipse to give room for
105
+ # labels on the left and right.
106
+ # ========================================================================= #
107
+ def draw_label(center_x, center_y, angle, radius, amount)
108
+ # TODO Don't use so many hard-coded numbers
109
+ r_offset = 20.0 # The distance out from the center of the pie to get point
110
+ x_offset = center_x # + 15.0 # The label points need to be tweaked slightly
111
+ y_offset = center_y # This one doesn't though
112
+ radius_offset = (radius + r_offset)
113
+ ellipse_factor = radius_offset * @text_offset_percentage
114
+ x = x_offset + ((radius_offset + ellipse_factor) * Math.cos(deg2rad(angle)))
115
+ y = y_offset + (radius_offset * Math.sin(deg2rad(angle)))
116
+
117
+ # Draw label
118
+ @d.fill = @font_color
119
+ @d.font = @font if @font
120
+ @d.pointsize = scale_fontsize(@marker_font_size)
121
+ @d.stroke = 'transparent'
122
+ @d.font_weight = BoldWeight
123
+ @d.gravity = CenterGravity
124
+ @d.annotate_scaled( @base_image,
125
+ 0, 0,
126
+ x, y,
127
+ amount, @scale)
128
+ end
129
+
130
+ # ========================================================================= #
131
+ # === sums_for_pie
132
+ # ========================================================================= #
133
+ def sums_for_pie
134
+ total_sum = 0.0
135
+ @data.collect {|data_row| total_sum += data_row[DATA_VALUES_INDEX].first }
136
+ total_sum
137
+ end
138
+
139
+ end; end
@@ -0,0 +1,264 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'image_paradise/graphs/base.rb'
6
+
7
+ # Here's how to set up an XY Scatter Chart
8
+ #
9
+ # g = Gruff::Scatter.new(800)
10
+ # g.data(:apples, [1,2,3,4], [4,3,2,1])
11
+ # g.data('oranges', [5,7,8], [4,1,7])
12
+ # g.write('test/output/scatter.png')
13
+ module ImageParadise
14
+
15
+ class Gruff::Scatter < Gruff::Base
16
+
17
+ # Maximum X Value. The value will get overwritten by the max in the
18
+ # datasets.
19
+ attr_accessor :maximum_x_value
20
+
21
+ # Minimum X Value. The value will get overwritten by the min in the
22
+ # datasets.
23
+ attr_accessor :minimum_x_value
24
+
25
+ # The number of vertical lines shown for reference
26
+ attr_accessor :marker_x_count
27
+
28
+ #~ # Draw a dashed horizontal line at the given y value
29
+ #~ attr_accessor :baseline_y_value
30
+
31
+ #~ # Color of the horizontal baseline
32
+ #~ attr_accessor :baseline_y_color
33
+
34
+ #~ # Draw a dashed horizontal line at the given y value
35
+ #~ attr_accessor :baseline_x_value
36
+
37
+ #~ # Color of the horizontal baseline
38
+ #~ attr_accessor :baseline_x_color
39
+
40
+
41
+ # === initialize
42
+ #
43
+ # Gruff::Scatter takes the same parameters as the Gruff::Line graph
44
+ #
45
+ # ==== Example
46
+ #
47
+ # g = Gruff::Scatter.new
48
+ #
49
+ def initialize(*args)
50
+ super(*args)
51
+
52
+ @maximum_x_value = @minimum_x_value = nil
53
+ @baseline_x_color = @baseline_y_color = 'red'
54
+ @baseline_x_value = @baseline_y_value = nil
55
+ @marker_x_count = nil
56
+ end
57
+
58
+ def setup_drawing
59
+ # TODO Need to get x-axis labels working. Current behavior will be to not allow.
60
+ @labels = {}
61
+
62
+ super
63
+
64
+ # Translate our values so that we can use the base methods for drawing
65
+ # the standard chart stuff
66
+ @column_count = @x_spread
67
+ end
68
+ # === draw
69
+ def draw
70
+ super
71
+ return unless @has_data
72
+
73
+ # Check to see if more than one datapoint was given. NaN can result otherwise.
74
+ @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width
75
+
76
+ #~ if (defined?(@norm_y_baseline)) then
77
+ #~ level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
78
+ #~ @d = @d.push
79
+ #~ @d.stroke_color @baseline_color
80
+ #~ @d.fill_opacity 0.0
81
+ #~ @d.stroke_dasharray(10, 20)
82
+ #~ @d.stroke_width 5
83
+ #~ @d.line(@graph_left, level, @graph_left + @graph_width, level)
84
+ #~ @d = @d.pop
85
+ #~ end
86
+
87
+ #~ if (defined?(@norm_x_baseline)) then
88
+
89
+ #~ end
90
+
91
+ @norm_data.each do |data_row|
92
+ data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
93
+ x_value = data_row[DATA_VALUES_X_INDEX][index]
94
+ next if data_point.nil? || x_value.nil?
95
+ new_x = get_x_coord(x_value, @graph_width, @graph_left)
96
+ new_y = @graph_top + (@graph_height - data_point * @graph_height)
97
+ # Reset each time to avoid thin-line errors
98
+ @d = @d.stroke data_row[DATA_COLOR_INDEX]
99
+ @d = @d.fill data_row[DATA_COLOR_INDEX]
100
+ @d = @d.stroke_opacity 1.0
101
+ @d = @d.stroke_width clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 4), 5.0)
102
+ circle_radius = clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 2.5), 5.0)
103
+ @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
104
+ end
105
+ end
106
+
107
+ @d.draw(@base_image)
108
+ end
109
+
110
+ # The first parameter is the name of the dataset. The next two are the
111
+ # x and y axis data points contain in their own array in that respective
112
+ # order. The final parameter is the color.
113
+ #
114
+ # Can be called multiple times with different datasets for a multi-valued
115
+ # graph.
116
+ #
117
+ # If the color argument is nil, the next color from the default theme will
118
+ # be used.
119
+ #
120
+ # NOTE: If you want to use a preset theme, you must set it before calling
121
+ # data().
122
+ #
123
+ # ==== Parameters
124
+ # name:: String or Symbol containing the name of the dataset.
125
+ # x_data_points:: An Array of of x-axis data points.
126
+ # y_data_points:: An Array of of y-axis data points.
127
+ # color:: The hex string for the color of the dataset. Defaults to nil.
128
+ #
129
+ # ==== Exceptions
130
+ # Data points contain nil values::
131
+ # This error will get raised if either the x or y axis data points array
132
+ # contains a <tt>nil</tt> value. The graph will not make an assumption
133
+ # as how to graph <tt>nil</tt>
134
+ # x_data_points is empty::
135
+ # This error is raised when the array for the x-axis points are empty
136
+ # y_data_points is empty::
137
+ # This error is raised when the array for the y-axis points are empty
138
+ # x_data_points.length != y_data_points.length::
139
+ # Error means that the x and y axis point arrays do not match in length
140
+ #
141
+ # ==== Examples
142
+ # g = Gruff::Scatter.new
143
+ # g.data(:apples, [1,2,3], [3,2,1])
144
+ # g.data('oranges', [1,1,1], [2,3,4])
145
+ # g.data('bitter_melon', [3,5,6], [6,7,8], '#000000')
146
+ #
147
+ def data(name, x_data_points=[], y_data_points=[], color=nil)
148
+
149
+ raise ArgumentError, 'Data Points contain nil Value!' if x_data_points.include?(nil) || y_data_points.include?(nil)
150
+ raise ArgumentError, 'x_data_points is empty!' if x_data_points.empty?
151
+ raise ArgumentError, 'y_data_points is empty!' if y_data_points.empty?
152
+ raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length
153
+
154
+ # Call the existing data routine for the y axis data
155
+ super(name, y_data_points, color)
156
+ # append the x data to the last entry that was just added in the @data member
157
+ last_elem = @data.length - 1
158
+ @data[last_elem] << x_data_points
159
+
160
+ if @maximum_x_value.nil? && @minimum_x_value.nil?
161
+ @maximum_x_value = @minimum_x_value = x_data_points.first
162
+ end
163
+
164
+ @maximum_x_value = x_data_points.max > @maximum_x_value ?
165
+ x_data_points.max : @maximum_x_value
166
+ @minimum_x_value = x_data_points.min < @minimum_x_value ?
167
+ x_data_points.min : @minimum_x_value
168
+ end
169
+
170
+ protected
171
+
172
+ def calculate_spread #:nodoc:
173
+ super
174
+ @x_spread = @maximum_x_value.to_f - @minimum_x_value.to_f
175
+ @x_spread = @x_spread > 0 ? @x_spread : 1
176
+ end
177
+
178
+ def normalize(force=@xy_normalize)
179
+ if @norm_data.nil? || force
180
+ @norm_data = []
181
+ return unless @has_data
182
+
183
+ @data.each do |data_row|
184
+ norm_data_points = [data_row[DATA_LABEL_INDEX]]
185
+ norm_data_points << data_row[DATA_VALUES_INDEX].map do |r|
186
+ (r.to_f - @minimum_value.to_f) / @spread
187
+ end
188
+ norm_data_points << data_row[DATA_COLOR_INDEX]
189
+ norm_data_points << data_row[DATA_VALUES_X_INDEX].map do |r|
190
+ (r.to_f - @minimum_x_value.to_f) / @x_spread
191
+ end
192
+ @norm_data << norm_data_points
193
+ end
194
+ end
195
+ #~ @norm_y_baseline = (@baseline_y_value.to_f / @maximum_value.to_f) if @baseline_y_value
196
+ #~ @norm_x_baseline = (@baseline_x_value.to_f / @maximum_x_value.to_f) if @baseline_x_value
197
+ end
198
+
199
+ def draw_line_markers
200
+ # do all of the stuff for the horizontal lines on the y-axis
201
+ super
202
+ return if @hide_line_markers
203
+
204
+ @d = @d.stroke_antialias false
205
+
206
+ if @x_axis_increment.nil?
207
+ # TODO Do the same for larger numbers...100, 75, 50, 25
208
+ if @marker_x_count.nil?
209
+ (3..7).each do |lines|
210
+ if @x_spread % lines == 0.0
211
+ @marker_x_count = lines
212
+ break
213
+ end
214
+ end
215
+ @marker_x_count ||= 4
216
+ end
217
+ @x_increment = (@x_spread > 0) ? significant(@x_spread / @marker_x_count) : 1
218
+ else
219
+ # TODO Make this work for negative values
220
+ @maximum_x_value = [@maximum_value.ceil, @x_axis_increment].max
221
+ @minimum_x_value = @minimum_x_value.floor
222
+ calculate_spread
223
+ normalize(true)
224
+
225
+ @marker_count = (@x_spread / @x_axis_increment).to_i
226
+ @x_increment = @x_axis_increment
227
+ end
228
+ @increment_x_scaled = @graph_width.to_f / (@x_spread / @x_increment)
229
+
230
+ # Draw vertical line markers and annotate with numbers
231
+ (0..@marker_x_count).each do |index|
232
+ # TODO Fix the vertical lines. Not pretty when they don't match up with top y-axis line
233
+ # x = @graph_left + @graph_width - index.to_f * @increment_x_scaled
234
+ # @d = @d.stroke(@marker_color)
235
+ # @d = @d.stroke_width 1
236
+ # @d = @d.line(x, @graph_top, x, @graph_bottom)
237
+
238
+ unless @hide_line_numbers
239
+ marker_label = index * @x_increment + @minimum_x_value.to_f
240
+ y_offset = @graph_bottom + LABEL_MARGIN
241
+ x_offset = get_x_coord(index.to_f, @increment_x_scaled, @graph_left)
242
+
243
+ @d.fill = @font_color
244
+ @d.font = @font if @font
245
+ @d.stroke('transparent')
246
+ @d.pointsize = scale_fontsize(@marker_font_size)
247
+ @d.gravity = NorthGravity
248
+
249
+ @d = @d.annotate_scaled(@base_image,
250
+ 1.0, 1.0,
251
+ x_offset, y_offset,
252
+ label(marker_label, @x_increment), @scale)
253
+ end
254
+ end
255
+ @d = @d.stroke_antialias true
256
+ end
257
+
258
+ private
259
+
260
+ def get_x_coord(x_data_point, width, offset) #:nodoc:
261
+ x_data_point * width + offset
262
+ end
263
+
264
+ end; end # end Gruff::Scatter
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ begin
6
+ require 'observer'
7
+ rescue LoadError; end
8
+ require 'image_paradise/graphs/base.rb'
9
+
10
+ ##
11
+ # A scene is a non-linear graph that assembles layers together to tell a story.
12
+ # Layers are folders with appropriately named files (see below). You can group
13
+ # layers and control them together or just set their values individually.
14
+ #
15
+ # Examples:
16
+ #
17
+ # * A city scene that changes with the time of day and the weather conditions.
18
+ # * A traffic map that shows red lines on streets that are crowded and green on free-flowing ones.
19
+ #
20
+ # Usage:
21
+ #
22
+ # g = Gruff::Scene.new("500x100", "path/to/city_scene_directory")
23
+ #
24
+ # # Define order of layers, back to front
25
+ # g.layers = %w(background haze sky clouds)
26
+ #
27
+ # # Define groups that will be controlled by the same input
28
+ # g.weather_group = %w(clouds)
29
+ # g.time_group = %w(background sky)
30
+ #
31
+ # # Set values for the layers or groups
32
+ # g.weather = "cloudy"
33
+ # g.time = Time.now
34
+ # g.haze = true
35
+ #
36
+ # # Write the final graph to disk
37
+ # g.write "hazy_daytime_city_scene.png"
38
+ #
39
+ #
40
+ # There are several rules that will magically select a layer when possible.
41
+ #
42
+ # * Numbered files will be selected according to the closest value that is less than the input value.
43
+ # * 'true.png' and 'false.png' will be used as booleans.
44
+ # * Other named files will be used if the input matches the filename (without the filetype extension).
45
+ # * If there is a file named 'default.png', it will be used unless other input values are set for the corresponding layer.
46
+
47
+ module ImageParadise
48
+
49
+ class Gruff::Scene < Gruff::Base
50
+
51
+ # An array listing the foldernames that will be rendered, from back to front.
52
+ #
53
+ # g.layers = %w(sky clouds buildings street people)
54
+ #
55
+ attr_reader :layers
56
+ # === initialize
57
+ def initialize(target_width, base_dir)
58
+ @base_dir = base_dir
59
+ @groups = {}
60
+ @layers = []
61
+ super target_width
62
+ end
63
+ # === draw
64
+ def draw
65
+ # Join all the custom paths and filter out the empty ones
66
+ image_paths = @layers.map { |layer| layer.path }.select { |path| !path.empty? }
67
+ images = Magick::ImageList.new(*image_paths)
68
+ @base_image = images.flatten_images
69
+ end
70
+ # === layers=
71
+ def layers=(ordered_list)
72
+ ordered_list.each do |layer_name|
73
+ @layers << Gruff::Layer.new(@base_dir, layer_name)
74
+ end
75
+ end
76
+
77
+ # Group layers to input values
78
+ #
79
+ # g.weather_group = ["sky", "sea", "clouds"]
80
+ #
81
+ # Set input values
82
+ #
83
+ # g.weather = "cloudy"
84
+ #
85
+ def method_missing(method_name, *args)
86
+ case method_name.to_s
87
+ when /^(\w+)_group=$/
88
+ add_group $1, *args
89
+ return
90
+ when /^(\w+)=$/
91
+ set_input $1, args.first
92
+ return
93
+ end
94
+ super
95
+ end
96
+
97
+ private
98
+
99
+ def add_group(input_name, layer_names)
100
+ @groups[input_name] = Gruff::Group.new(input_name, @layers.select { |layer| layer_names.include?(layer.name) })
101
+ end
102
+
103
+ def set_input(input_name, input_value)
104
+ if not @groups[input_name].nil?
105
+ @groups[input_name].send_updates(input_value)
106
+ else
107
+ if chosen_layer = @layers.detect { |layer| layer.name == input_name }
108
+ chosen_layer.update input_value
109
+ end
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+
116
+ class Gruff::Group
117
+
118
+ include Observable
119
+ attr_reader :name
120
+
121
+ def initialize(folder_name, layers)
122
+ @name = folder_name
123
+ layers.each do |layer|
124
+ layer.observe self
125
+ end
126
+ end
127
+
128
+ def send_updates(value)
129
+ changed
130
+ notify_observers value
131
+ end
132
+
133
+ end
134
+
135
+
136
+ class Gruff::Layer
137
+
138
+ attr_reader :name
139
+
140
+ def initialize(base_dir, folder_name)
141
+ @base_dir = base_dir.to_s
142
+ @name = folder_name.to_s
143
+ @filenames = Dir.open(File.join(base_dir, folder_name)).entries.select { |file| file =~ /^[^.]+\.png$/ }.sort
144
+ @selected_filename = select_default
145
+ end
146
+
147
+ # Register this layer so it receives updates from the group
148
+ def observe(obj)
149
+ obj.add_observer self
150
+ end
151
+
152
+ # Choose the appropriate filename for this layer, based on the input
153
+ def update(value)
154
+ @selected_filename = case value.to_s
155
+ when /^(true|false)$/
156
+ select_boolean value
157
+ when /^(\w|\s)+$/
158
+ select_string value
159
+ when /^-?(\d+\.)?\d+$/
160
+ select_numeric value
161
+ when /(\d\d):(\d\d):\d\d/
162
+ select_time "#{$1}#{$2}"
163
+ else
164
+ select_default
165
+ end
166
+ # Finally, try to use 'default' if we're still blank
167
+ @selected_filename ||= select_default
168
+ end
169
+
170
+ # Returns the full path to the selected image, or a blank string
171
+ def path
172
+ unless @selected_filename.nil? || @selected_filename.empty?
173
+ return File.join(@base_dir, @name, @selected_filename)
174
+ end
175
+ ''
176
+ end
177
+
178
+ private
179
+
180
+ # Match "true.png" or "false.png"
181
+ def select_boolean(value)
182
+ file_exists_or_blank value.to_s
183
+ end
184
+
185
+ # Match -5 to _5.png
186
+ def select_numeric(value)
187
+ file_exists_or_blank value.to_s.gsub('-', '_')
188
+ end
189
+
190
+ def select_time(value)
191
+ times = @filenames.map { |filename| filename.gsub('.png', '') }
192
+ times.each_with_index do |time, index|
193
+ if (time > value) && (index > 0)
194
+ return "#{times[index - 1]}.png"
195
+ end
196
+ end
197
+ return "#{times.last}.png"
198
+ end
199
+
200
+ # Match "partly cloudy" to "partly_cloudy.png"
201
+ def select_string(value)
202
+ file_exists_or_blank value.to_s.gsub(' ', '_')
203
+ end
204
+
205
+ def select_default
206
+ @filenames.include?("default.png") ? "default.png" : ''
207
+ end
208
+
209
+ # Returns the string "#{filename}.png", if it exists.
210
+ #
211
+ # Failing that, it returns default.png, or '' if that doesn't exist.
212
+ def file_exists_or_blank(filename)
213
+ @filenames.include?("#{filename}.png") ? "#{filename}.png" : select_default
214
+ end
215
+
216
+ end; end