graphical_editor 0.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 322a5163b59c4b734ee7d4ba2e4b0787699c1891
4
+ data.tar.gz: 3de3049c8ac7bc1dbaa4e38f0bfb15d6466675eb
5
+ SHA512:
6
+ metadata.gz: 5dca1d5affdadce49de6f16df3d726b7eaea1745295647991cf9e509ceaf405c32044c670a373dd7ce76e706c129c9ac7e500e89343e1d03baab417d01cdbf6c
7
+ data.tar.gz: e44c892326da6ec1a764fac4b2881b5b64c9d7e5782f9c95fb3c65792f9449591537dd751719b787df8affc26379052a017b8314b64d3e38b8f82198da5b8c55
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'readline'
4
+ require_relative '../lib/graphical_editor/editor.rb'
5
+
6
+ graphical_editor = GraphicalEditor::Editor.new(STDOUT)
7
+
8
+ graphical_editor.start
@@ -0,0 +1,58 @@
1
+ require_relative 'image'
2
+
3
+ module GraphicalEditor
4
+ class Editor
5
+ REGEX_1_250_RANGE = '[1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|250'
6
+
7
+
8
+ def initialize(output)
9
+ @output = output
10
+ end
11
+
12
+ def start
13
+ @output.puts "*"*100
14
+ @output.puts 'Welcome to Graphical Image Editor Session.'
15
+ @output.puts "*"*100
16
+ listen
17
+ end
18
+
19
+ def listen
20
+ loop do
21
+ cmd = Readline::readline 'Graphical Editor:> ', true
22
+ execute(cmd)
23
+ end
24
+ end
25
+
26
+ def execute(cmd)
27
+ case cmd
28
+
29
+ when /^I (#{REGEX_1_250_RANGE}) (#{REGEX_1_250_RANGE})$/
30
+ @image = Image.new($1.to_i, $2.to_i)
31
+
32
+ when /^C$/
33
+ @image.clear
34
+
35
+ when /^L (#{REGEX_1_250_RANGE}) (#{REGEX_1_250_RANGE}) ([A-Z])$/
36
+ @image.colourize($1.to_i, $2.to_i, $3)
37
+
38
+ when /^V (#{REGEX_1_250_RANGE}) (#{REGEX_1_250_RANGE}) (#{REGEX_1_250_RANGE}) ([A-Z])$/
39
+ @image.draw_vertical($1.to_i, $2.to_i, $3.to_i, $4)
40
+
41
+ when /^H (#{REGEX_1_250_RANGE}) (#{REGEX_1_250_RANGE}) (#{REGEX_1_250_RANGE}) ([A-Z])$/
42
+ @image.draw_horizontal($1.to_i, $2.to_i, $3.to_i, $4)
43
+
44
+ when /^F (#{REGEX_1_250_RANGE}) (#{REGEX_1_250_RANGE}) ([A-Z])$/
45
+ @image.fill_region($1.to_i, $2.to_i, $3)
46
+
47
+ when /^S$/
48
+ puts @image
49
+
50
+ when /^X$/
51
+ exit
52
+
53
+ else
54
+ puts 'Unknown command'
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,61 @@
1
+
2
+ module GraphicalEditor
3
+ class Image
4
+ DEFAULT_COLOR = 'O'
5
+
6
+ def initialize(m, n)
7
+ @pixels = default_color_pixels_for(m, n)
8
+ end
9
+
10
+ def clear
11
+ @pixels.map! do |pixel|
12
+ pixel.map! {DEFAULT_COLOR}
13
+ end
14
+ end
15
+
16
+ def colourize(x, y, colour)
17
+ @pixels[y.pred][x.pred] = colour
18
+ end
19
+
20
+ def draw_vertical(x, y1, y2, colour)
21
+ (y1..y2).each do |y|
22
+ colourize(x, y, colour)
23
+ end
24
+ end
25
+
26
+ def draw_horizontal(x1, x2, y, colour)
27
+ (x1..x2).each do |x|
28
+ colourize(x, y, colour)
29
+ end
30
+ end
31
+
32
+ def fill_region(x, y, colour)
33
+ target_colour = @pixels[y.pred][x.pred]
34
+ flood_fill(x, y, target_colour, colour)
35
+ end
36
+
37
+ def to_s
38
+ @pixels.map {|pixel| pixel.join(' ')}.join("\n")
39
+ end
40
+
41
+ private
42
+ def default_color_pixels_for(m, n)
43
+ [].tap {|pixels| n.times {pixels << Array.new(m, DEFAULT_COLOR)}}
44
+ end
45
+
46
+ def flood_fill(x, y, target_colour, replacement_colour)
47
+ if within_bounds(x, y) && @pixels[y.pred][x.pred] == target_colour
48
+ @pixels[y.pred][x.pred] = replacement_colour
49
+ flood_fill(x, y.pred, target_colour, replacement_colour)
50
+ flood_fill(x, y.next, target_colour, replacement_colour)
51
+ flood_fill(x.pred, y, target_colour, replacement_colour)
52
+ flood_fill(x.next, y, target_colour, replacement_colour)
53
+ end
54
+ end
55
+
56
+ def within_bounds(x, y)
57
+ x >= 1 && x <= @pixels.first.size &&
58
+ y >= 1 && y <= @pixels.size
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphical_editor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Satheesh Kumar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Graphical editors allow users to edit images in the same way text editors let us modify documents.
15
+ Images are represented as an M x N array of pixels with each pixel given colour.
16
+ email: satheeshcsedct@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/graphical_editor.rb
22
+ - lib/graphical_editor/editor.rb
23
+ - lib/graphical_editor/image.rb
24
+ homepage: http://rubygems.org/gems/graphical_editor
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.5.1
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: simple interactive graphical editor.
48
+ test_files: []