image_paradise 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

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,21 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'image_paradise/requires/require_toplevel_methods.rb'
6
+ # ImageParadise.require_toplevel_methods
7
+ # =========================================================================== #
8
+ require 'image_paradise/constants.rb'
9
+
10
+ module ImageParadise
11
+
12
+ # ========================================================================= #
13
+ # === ImageParadise.require_toplevel_methods
14
+ # ========================================================================= #
15
+ def self.require_toplevel_methods
16
+ Dir["#{PROJECT_BASE_DIRECTORY}toplevel_methods/*.rb"].each {|this_image|
17
+ require "image_paradise/toplevel_methods/#{File.basename(this_image)}"
18
+ }
19
+ end
20
+
21
+ end
@@ -0,0 +1,2 @@
1
+ Rotate-specific code, that is when images are flipped around, can
2
+ be found in this directory here.
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # For rotate-actions you can look at the following website:
6
+ #
7
+ # https://imagemagick.org/script/command-line-options.php#rotate
8
+ #
9
+ # The specific API would be:
10
+ #
11
+ # -rotate degrees{<}{>}
12
+ #
13
+ # =========================================================================== #
14
+ # require 'image_paradise/rotate/rotate.rb'
15
+ # =========================================================================== #
16
+ module ImageParadise
17
+
18
+ require 'image_paradise/toplevel_methods/esystem.rb'
19
+
20
+ # ========================================================================= #
21
+ # === ImageParadise.rotate_this_image
22
+ #
23
+ # This method will always operate on the first input argument(s) in
24
+ # batch form - hence we convert the first argument given to an Array,
25
+ # inside of the method.
26
+ #
27
+ # The second input-argument specifies the degree for the rotation; a
28
+ # value with a '+' or without any modified token means "clockwise
29
+ # "rotation; a value that is negative, aka has a leading '-', means
30
+ # counter-clockwise rotation. Rotate-left is counter-clockwise;
31
+ # and rotate-right is clockwise.
32
+ #
33
+ # When we use convert, the commandline flags should look like this:
34
+ #
35
+ # convert -resize "600>" image.png Newimage/image.png
36
+ #
37
+ # ========================================================================= #
38
+ def self.rotate_this_image(
39
+ these_images = ARGV,
40
+ by_n_degrees = '-45',
41
+ use_this_background_colour = nil # <- Fill with this background colour.
42
+ )
43
+ case by_n_degrees
44
+ when :rotate_left,
45
+ :left
46
+ by_n_degrees = '-90'
47
+ when :rotate_right,
48
+ :right
49
+ by_n_degrees = '+90'
50
+ end
51
+ these_images = [these_images].flatten.compact
52
+ these_images.each {|this_image|
53
+ output_file = 'new_file_'+File.basename(this_image)
54
+ _ = 'convert -rotate "'+by_n_degrees.to_s+'"'
55
+ _ << ' '+this_image
56
+ if use_this_background_colour
57
+ case use_this_background_colour.to_s
58
+ when 'black'
59
+ use_this_background_colour = 'FFFFFF'
60
+ end
61
+ _ << ' -background "#'+use_this_background_colour.to_s+'"'
62
+ end
63
+ _ << " #{output_file}"
64
+ esystem _
65
+ }
66
+ end; self.instance_eval { alias rotate_these_images rotate_this_image } # === ImageParadise.rotate_these_images
67
+
68
+ # ========================================================================= #
69
+ # === ImageParadise.rotate_left
70
+ # ========================================================================= #
71
+ def self.rotate_left(
72
+ these_images = ARGV,
73
+ by_n_degrees = :rotate_left,
74
+ use_this_background_colour = nil
75
+ )
76
+ ImageParadise.rotate_this_image(
77
+ these_images, by_n_degrees, use_this_background_colour
78
+ )
79
+ end
80
+
81
+ # ========================================================================= #
82
+ # === ImageParadise.rotate_right
83
+ # ========================================================================= #
84
+ def self.rotate_right(
85
+ these_images = ARGV,
86
+ by_n_degrees = :rotate_right,
87
+ use_this_background_colour = nil
88
+ )
89
+ ImageParadise.rotate_this_image(
90
+ these_images, by_n_degrees, use_this_background_colour
91
+ )
92
+ end
93
+
94
+ end
95
+
96
+ if __FILE__ == $PROGRAM_NAME
97
+ ImageParadise.rotate_this_image(ARGV)
98
+ end # rotate_this_image /Users/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/image_paradise/test/16x16_red_square_image_for_testing.png
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === ImageParadise::Interactive
6
+ #
7
+ # Usage example:
8
+ # ImageParadise::Interactive.new
9
+ # =========================================================================== #
10
+ # require 'image_paradise/shell/interactive.rb'
11
+ # =========================================================================== #
12
+ require 'image_paradise/base/base.rb'
13
+ require 'image_paradise/toplevel_methods/write_this_text.rb'
14
+
15
+ module ImageParadise
16
+
17
+ class Interactive < ::ImageParadise::Base # === ImageParadise::Interactive
18
+
19
+ # ========================================================================= #
20
+ # === initialize
21
+ # ========================================================================= #
22
+ def initialize(
23
+ i = nil,
24
+ run_already = true
25
+ )
26
+ register_sigint
27
+ reset
28
+ set_input(i)
29
+ run if run_already
30
+ end
31
+
32
+ # ========================================================================= #
33
+ # === reset (reset tag)
34
+ # ========================================================================= #
35
+ def reset
36
+ end
37
+
38
+ # ========================================================================= #
39
+ # === set_input
40
+ # ========================================================================= #
41
+ def set_input(i = '')
42
+ i = [i] unless i.is_a? Array
43
+ @input = i # Must be an Array.
44
+ end
45
+
46
+ # ========================================================================= #
47
+ # === input?
48
+ # ========================================================================= #
49
+ def input?
50
+ @input
51
+ end
52
+
53
+ # ========================================================================= #
54
+ # === run (run tag)
55
+ # ========================================================================= #
56
+ def run
57
+ try_to_enter_this_directory('/Depot/j')
58
+ loop {
59
+ obtain_user_input
60
+ menu(@user_input)
61
+ }
62
+ end
63
+
64
+ # ========================================================================= #
65
+ # === try_to_enter_this_directory
66
+ # ========================================================================= #
67
+ def try_to_enter_this_directory(i = '/Depot/j')
68
+ if File.directory?(i)
69
+ chdir(i)
70
+ end
71
+ end
72
+
73
+ # ========================================================================= #
74
+ # === obtain_user_input
75
+ # ========================================================================= #
76
+ def obtain_user_input
77
+ @user_input = $stdin.gets.chomp
78
+ end
79
+
80
+ # ========================================================================= #
81
+ # === display_prompt
82
+ # ========================================================================= #
83
+ def display_prompt
84
+ print '> '
85
+ end
86
+
87
+ # ========================================================================= #
88
+ # === menu (menu tag)
89
+ # ========================================================================= #
90
+ def menu(
91
+ i = @input
92
+ )
93
+ if i.is_a? Array
94
+ i.each {|entry| menu(entry) }
95
+ else
96
+ display_prompt
97
+ case i # case tag
98
+ # ===================================================================== #
99
+ # === pwd
100
+ # ===================================================================== #
101
+ when /pwd/
102
+ e sdir(Dir.pwd)
103
+ # ===================================================================== #
104
+ # === bachelor_master
105
+ # ===================================================================== #
106
+ when '1',/bachelor_?master/
107
+ generate_bachelor_and_master_images
108
+ when 'q','exit'
109
+ exit
110
+ end
111
+ end
112
+ end
113
+
114
+ # ========================================================================= #
115
+ # === generate_bachelor_and_master_images (main tag)
116
+ # ========================================================================= #
117
+ def generate_bachelor_and_master_images
118
+ height_value = 28
119
+ width_value = 94
120
+ pointsize_value = 18
121
+ # ======================================================================= #
122
+ # Designate another store-location for our two images there.
123
+ # ======================================================================= #
124
+ ImageParadise.store_image_files_in_this_directory =
125
+ '/home/x/DATA/IMG/STUDIUM/'
126
+ ImageParadise.write_this_text(
127
+ 'Bachelor',
128
+ {colour: 'red', height: height_value, width: width_value, upcase_filename: true, pointsize: pointsize_value}
129
+ )
130
+ ImageParadise.write_this_text(
131
+ 'Master',
132
+ {colour: 'darkblue', height: height_value, width: width_value, upcase_filename: true, pointsize: pointsize_value}
133
+ )
134
+ end
135
+
136
+ # ========================================================================= #
137
+ # === []
138
+ # ========================================================================= #
139
+ def self.[](i = '')
140
+ new(i)
141
+ end
142
+
143
+ end
144
+
145
+ # =========================================================================== #
146
+ # === ImageParadise.interactive_shell
147
+ # =========================================================================== #
148
+ def self.interactive_shell
149
+ ImageParadise::Interactive.new
150
+ end
151
+
152
+ end
153
+
154
+ if __FILE__ == $PROGRAM_NAME
155
+ ImageParadise::Interactive.new(ARGV)
156
+ end # interactive.rb
@@ -0,0 +1,5 @@
1
+ This subdirectory contains code related to SVG.
2
+
3
+ Until August 2020, this used to be a separate project,
4
+ called svg_paradise, before it was integrated into
5
+ this project here.
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'image_paradise/svg/circle.rb'
6
+ # =========================================================================== #
7
+ module ImageParadise
8
+
9
+ module Svg
10
+
11
+ module Circle # === ImageParadise::Svg::Circle
12
+
13
+ # ========================================================================= #
14
+ # === DEFAULT_HEIGHT
15
+ # ========================================================================= #
16
+ DEFAULT_HEIGHT = '100'.freeze
17
+
18
+ # ========================================================================= #
19
+ # === DEFAULT_RADIUS
20
+ # ========================================================================= #
21
+ DEFAULT_RADIUS = '40'.freeze
22
+
23
+ # ========================================================================= #
24
+ # === Svg::Circle.create
25
+ #
26
+ # This will generate a SVG circle.
27
+ #
28
+ # A circle has to be synced though as otherwise it is not a circle.
29
+ # ========================================================================= #
30
+ def self.create(
31
+ height_and_width = DEFAULT_HEIGHT,
32
+ fill_colour = 'red',
33
+ stroke_width = '3',
34
+ radius = DEFAULT_RADIUS
35
+ )
36
+ # ======================================================================= #
37
+ # === Handle Hash given as input to us next
38
+ # ======================================================================= #
39
+ if height_and_width.is_a? Hash
40
+ if height_and_width.has_key? :height
41
+ height_and_width = height_and_width.delete(:height) # This is ok because width and height are the same.
42
+ end
43
+ if height_and_width.has_key? :radius
44
+ radius = height_and_width.delete(:radius)
45
+ case radius
46
+ when :half_default_width
47
+ radius = DEFAULT_RADIUS.to_f / 2
48
+ when :quarter_default_width
49
+ radius = DEFAULT_RADIUS.to_f / 4
50
+ end
51
+ end if height_and_width.is_a? Hash
52
+ if height_and_width.has_key? :stroke_width
53
+ stroke_width = height_and_width.delete(:stroke_width)
54
+ end if height_and_width.is_a? Hash
55
+ if height_and_width.has_key? :colour
56
+ fill_colour = height_and_width.delete(:colour)
57
+ end if height_and_width.is_a? Hash
58
+ if height_and_width.respond_to? :empty?
59
+ height_and_width = DEFAULT_HEIGHT if height_and_width.empty?
60
+ end
61
+ end
62
+ # ======================================================================= #
63
+ # Convert the various arguments passed to this method into a String.
64
+ # ======================================================================= #
65
+ height_and_width = height_and_width.to_s.dup unless height_and_width.is_a? Hash
66
+ fill_colour = fill_colour.to_s.dup
67
+ stroke_width = stroke_width.to_s.dup
68
+ radius = radius.to_i.to_s.dup
69
+ # ======================================================================= #
70
+ # === Handle random fill colour
71
+ # ======================================================================= #
72
+ if fill_colour == 'random'
73
+ fill_colour = Colours::HtmlColours.random.dup
74
+ end
75
+ _ = Svg.strip(
76
+ '<svg height="'+height_and_width+'" width="'+height_and_width+'">
77
+ <circle cx="50" cy="50" r="'+radius+'" stroke="black" stroke-width="'+stroke_width+'" fill="'+fill_colour+'" />'+
78
+ Svg.close_svg
79
+ )+N
80
+ _ = _.to_s.dup
81
+ Svg.add(_)
82
+ return _
83
+ end
84
+
85
+ end
86
+
87
+ # =========================================================================== #
88
+ # === Svg.circle
89
+ #
90
+ # Always sync to the above method.
91
+ #
92
+ # Usage example:
93
+ #
94
+ # Svg.circle(150,:blue,5,20)
95
+ #
96
+ # =========================================================================== #
97
+ def self.circle(
98
+ a = '100',
99
+ b = 'red',
100
+ c = '3',
101
+ r = '40'
102
+ )
103
+ Svg::Circle.create(a,b,c,r)
104
+ end
105
+
106
+ end; end
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'image_paradise/svg/feature.rb'
6
+ # =========================================================================== #
7
+ module ImageParadise
8
+
9
+ module Svg
10
+
11
+ class Feature # === ImageParadise::Svg::Feature
12
+
13
+ attr_accessor :stroke
14
+ attr_accessor :stroke_width
15
+ attr_accessor :fill
16
+ attr_accessor :fill_opacity
17
+ attr_accessor :stroke_opacity
18
+ attr_accessor :style
19
+
20
+ # ========================================================================= #
21
+ # === to_svg
22
+ # ========================================================================= #
23
+ def to_svg
24
+ '<overrideme/>'
25
+ end
26
+
27
+ # ========================================================================= #
28
+ # === attr_to_s
29
+ # ========================================================================= #
30
+ def attr_to_s(
31
+ attr_name, attr_val
32
+ )
33
+ return '' if attr_val.nil?
34
+ " #{attr_name}=\"#{attr_val}\"" unless attr_val.to_s.empty?
35
+ end
36
+
37
+ # ========================================================================= #
38
+ # === point_to_s
39
+ # ========================================================================= #
40
+ def point_to_s(
41
+ x_attr_name, y_attr_name, point
42
+ )
43
+ return '' if point.nil?
44
+ return_val = " #{x_attr_name}=\"#{point.x}\"" unless point.x.to_s.empty?
45
+ return_val + " #{y_attr_name}=\"#{point.y}\"" unless point.y.to_s.empty?
46
+ end
47
+
48
+ end; end; end
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'image_paradise/svg/rectangle.rb'
6
+ # =========================================================================== #
7
+ module ImageParadise
8
+
9
+ module Svg
10
+
11
+ module Rectangle # === ImageParadise::Svg::Rectangle
12
+
13
+ # ========================================================================= #
14
+ # === DEFAULT_WIDTH
15
+ # ========================================================================= #
16
+ DEFAULT_WIDTH = '400'
17
+
18
+ # ========================================================================= #
19
+ # === DEFAULT_HEIGHT
20
+ # ========================================================================= #
21
+ DEFAULT_HEIGHT = '180'
22
+
23
+ # ========================================================================= #
24
+ # === DEFAULT_FILL_COLOUR
25
+ # ========================================================================= #
26
+ DEFAULT_FILL_COLOUR = 'red'
27
+
28
+ # ========================================================================= #
29
+ # === Svg::Rectangle.create
30
+ #
31
+ # This will generate a SVG rectangle.
32
+ #
33
+ # A proper rectangle has a width and a height setting.
34
+ #
35
+ # You can optionally handle opacity if you pass in a block.
36
+ # ========================================================================= #
37
+ def self.create(
38
+ width = DEFAULT_WIDTH,
39
+ height = DEFAULT_HEIGHT,
40
+ fill_colour = DEFAULT_FILL_COLOUR,
41
+ stroke_width = '3'
42
+ )
43
+
44
+ extra_elements = '' # Extra values for the SVG attribute.
45
+ opacity = ''.dup # This variable keeps track over whether we require opacity.
46
+
47
+ # ======================================================================= #
48
+ # === Handle block arguments next
49
+ # ======================================================================= #
50
+ if block_given?
51
+ yielded = yield
52
+ # ===================================================================== #
53
+ # Next convert a Proc to its "real" value.
54
+ # ===================================================================== #
55
+ if yielded.is_a? Proc
56
+ yielded = yielded.call
57
+ end
58
+ if yielded.is_a?(String)
59
+ if yielded.include? 'opacity'
60
+ # ================================================================= #
61
+ # === Handle opacity
62
+ #
63
+ # Popular options are:
64
+ #
65
+ # 'fill-opacity:0.9;stroke-opacity:0.9'
66
+ #
67
+ # ================================================================= #
68
+ opacity << yielded
69
+ opacity << ';' unless opacity.end_with? ';'
70
+ end
71
+ if yielded.include? 'rounded'
72
+ # ================================================================= #
73
+ # === Handle rounded edges for a rectangle
74
+ #
75
+ # The rx and the ry attributes rounds the corners of the rectangle.
76
+ # ================================================================= #
77
+ extra_elements = ' x="50" y="20" rx="20" ry="20" '
78
+ end
79
+ end
80
+ end
81
+
82
+ # ======================================================================= #
83
+ # === Handle Hash given as input to us next
84
+ # ======================================================================= #
85
+ if width.is_a? Hash
86
+ if width.has_key? :fill_colour
87
+ fill_colour = width.delete(:fill_colour)
88
+ end
89
+ if width.has_key? :width
90
+ width = width.delete(:width)
91
+ end if width.is_a? Hash
92
+ if width.has_key? :height
93
+ height = width.delete(:height) # This is ok because width and height are the same.
94
+ end if width.is_a? Hash
95
+ if width.has_key? :stroke_width
96
+ stroke_width = width.delete(:stroke_width)
97
+ end if width.is_a? Hash
98
+ if width.has_key? :colour
99
+ fill_colour = width.delete(:colour)
100
+ end if width.is_a? Hash
101
+ unless width.is_a? Numeric
102
+ width = DEFAULT_WIDTH if width.empty?
103
+ end
104
+ end
105
+ # ======================================================================= #
106
+ # Convert the various arguments passed to this method into a String.
107
+ # ======================================================================= #
108
+ width = width.to_s.dup unless width.is_a? Hash
109
+ height = height.to_s
110
+ fill_colour = fill_colour.to_s
111
+ stroke_width = stroke_width.to_s
112
+ # ======================================================================= #
113
+ # === Handle random fill colour
114
+ # ======================================================================= #
115
+ if fill_colour == 'random'
116
+ fill_colour = Colours::HtmlColours.random
117
+ end
118
+ # ======================================================================= #
119
+ # We will put the inner-rectangle at 80%.
120
+ # ======================================================================= #
121
+ _ = Svg.strip(
122
+ '<svg width="'+width+'" height="'+height+'">
123
+
124
+ <rect '+extra_elements+'width="'+(width.to_f * 80 / 100).to_i.to_s+'" height="'+(height.to_f * 80 / 100).to_i.to_s+'" style="'+opacity+'fill:'+fill_colour+';stroke-width:'+stroke_width+';stroke:rgb(0,0,0)">
125
+ '+Svg.close_svg
126
+ )+N
127
+ _ = _.to_s.dup
128
+ Svg.add(_)
129
+ return _
130
+ end
131
+
132
+ end
133
+
134
+ # =========================================================================== #
135
+ # === ImageParadise::Svg.rectangle
136
+ #
137
+ # Always sync to the above method.
138
+ #
139
+ # Usage examples:
140
+ #
141
+ # Svg.rectangle(150,:blue,5,20)
142
+ #
143
+ # =========================================================================== #
144
+ def self.rectangle(
145
+ a = Svg::Rectangle::DEFAULT_WIDTH,
146
+ b = Svg::Rectangle::DEFAULT_HEIGHT,
147
+ c = Svg::Rectangle::DEFAULT_FILL_COLOUR,
148
+ d = '3',
149
+ &block
150
+ )
151
+ Svg::Rectangle.create(a,b,c,d) { block }
152
+ end
153
+
154
+ end; end