graphed 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +17 -17
- data/.rspec +2 -2
- data/Gemfile +4 -4
- data/LICENSE.txt +22 -22
- data/README.md +37 -37
- data/Rakefile +1 -1
- data/bin/graphed +5 -4
- data/graphed.gemspec +24 -24
- data/lib/graphed.rb +7 -9
- data/lib/graphed/imageeditor.rb +64 -64
- data/lib/graphed/{console.rb → looper.rb} +99 -99
- data/lib/graphed/version.rb +3 -3
- data/spec/lib/imageeditor_spec.rb +71 -0
- data/spec/lib/looper_spec.rb +6 -0
- data/spec/spec_helper.rb +17 -17
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ec6193b4d4bac0803d40a91b7f6399350a7efbb
|
4
|
+
data.tar.gz: 1778dc2aa4b84608b9a2d1949cda3ccf419a35c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64a00d97dcd0dff24ff454ebef0e60a93739b41c096a3567af7b8f294b05d960fc92fcdc2ac47fa458fdf4f524b2c30bbd73c217aa826955ce9959ad6aa03691
|
7
|
+
data.tar.gz: cbc1e2ff9b6afd233a75513678b23c44f67301593a19b2b9a122de6ce294ba663c37f931c799f68ad9f8dc7aaa4412fd3e54b296b5357df9ced94dc448b70d5f
|
data/.gitignore
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
*.gem
|
2
|
-
*.rbc
|
3
|
-
.bundle
|
4
|
-
.config
|
5
|
-
.yardoc
|
6
|
-
Gemfile.lock
|
7
|
-
InstalledFiles
|
8
|
-
_yardoc
|
9
|
-
coverage
|
10
|
-
doc/
|
11
|
-
lib/bundler/man
|
12
|
-
pkg
|
13
|
-
rdoc
|
14
|
-
spec/reports
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
18
|
.idea
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--color
|
2
|
-
--format progress
|
1
|
+
--color
|
2
|
+
--format progress
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in graphed.gemspec
|
4
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in graphed.gemspec
|
4
|
+
gemspec
|
data/LICENSE.txt
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
Copyright (c) 2013 Zoltan
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2013 Zoltan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
# Graphed
|
2
|
-
|
3
|
-
This is a simple character based image editor with command line interface.
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
1. Install it yourself as:
|
8
|
-
|
9
|
-
$ gem install graphed
|
10
|
-
|
11
|
-
2. Launch it:
|
12
|
-
|
13
|
-
$ graphed
|
14
|
-
|
15
|
-
## Usage
|
16
|
-
|
17
|
-
This program simulates a simple interactive graphical editor.
|
18
|
-
|
19
|
-
Available commands:
|
20
|
-
|
21
|
-
I M N Create a new MxN image with all pixels coloured white(O).
|
22
|
-
C Clears the table, setting all pixels to white (O).
|
23
|
-
L X Y C Colours the pixel (X,Y) with colour C.
|
24
|
-
V X Y1 Y2 C Draw a vertical segment of colour C in column X between rows Y1 and Y2 (inclusive).
|
25
|
-
H X1 X2 Y C Draw a horizontal segment of colour C in row Y between columns X1 and X2 (inclusive).
|
26
|
-
F X Y C Fill the region R with the colour C. R is defined as: Pixel (X,Y) belongs to R. Any other pixel which is the same colour as (X,Y) and shares a common side with any pixel in R also belongs to this region.
|
27
|
-
S Show the contents of the current image
|
28
|
-
X Terminate the session
|
29
|
-
help Show this help
|
30
|
-
|
31
|
-
## Contributing
|
32
|
-
|
33
|
-
1. Fork it
|
34
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
-
5. Create new Pull Request
|
1
|
+
# Graphed
|
2
|
+
|
3
|
+
This is a simple character based image editor with command line interface.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
1. Install it yourself as:
|
8
|
+
|
9
|
+
$ gem install graphed
|
10
|
+
|
11
|
+
2. Launch it:
|
12
|
+
|
13
|
+
$ graphed
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
This program simulates a simple interactive graphical editor.
|
18
|
+
|
19
|
+
Available commands:
|
20
|
+
|
21
|
+
I M N Create a new MxN image with all pixels coloured white(O).
|
22
|
+
C Clears the table, setting all pixels to white (O).
|
23
|
+
L X Y C Colours the pixel (X,Y) with colour C.
|
24
|
+
V X Y1 Y2 C Draw a vertical segment of colour C in column X between rows Y1 and Y2 (inclusive).
|
25
|
+
H X1 X2 Y C Draw a horizontal segment of colour C in row Y between columns X1 and X2 (inclusive).
|
26
|
+
F X Y C Fill the region R with the colour C. R is defined as: Pixel (X,Y) belongs to R. Any other pixel which is the same colour as (X,Y) and shares a common side with any pixel in R also belongs to this region.
|
27
|
+
S Show the contents of the current image
|
28
|
+
X Terminate the session
|
29
|
+
help Show this help
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/graphed
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
require 'graphed'
|
3
|
-
|
4
|
-
Graphed.Launch
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'graphed'
|
3
|
+
|
4
|
+
#Graphed.Launch
|
5
|
+
Graphed::Looper.main
|
data/graphed.gemspec
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'graphed/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "graphed"
|
8
|
-
spec.version = Graphed::VERSION
|
9
|
-
spec.authors = ["Zoltan Debre"]
|
10
|
-
spec.email = [""]
|
11
|
-
spec.description = %q{Simple image editor simulator in a command line.}
|
12
|
-
spec.summary = %q{Simple image editor.}
|
13
|
-
spec.homepage = "https://github.com/szines/graphed"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files`.split($/)
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = ["lib"]
|
20
|
-
|
21
|
-
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
-
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "rb-readline"
|
24
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'graphed/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "graphed"
|
8
|
+
spec.version = Graphed::VERSION
|
9
|
+
spec.authors = ["Zoltan Debre"]
|
10
|
+
spec.email = [""]
|
11
|
+
spec.description = %q{Simple image editor simulator in a command line.}
|
12
|
+
spec.summary = %q{Simple image editor.}
|
13
|
+
spec.homepage = "https://github.com/szines/graphed"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rb-readline"
|
24
|
+
end
|
data/lib/graphed.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
require 'graphed/version'
|
2
|
-
require 'graphed/
|
3
|
-
require 'graphed/imageeditor'
|
4
|
-
|
5
|
-
module Graphed
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
end
|
1
|
+
require 'graphed/version'
|
2
|
+
require 'graphed/looper'
|
3
|
+
require 'graphed/imageeditor'
|
4
|
+
|
5
|
+
module Graphed
|
6
|
+
|
7
|
+
end
|
data/lib/graphed/imageeditor.rb
CHANGED
@@ -1,65 +1,65 @@
|
|
1
|
-
module Graphed
|
2
|
-
class ImageEditor < Array
|
3
|
-
|
4
|
-
attr_reader :cols, :rows
|
5
|
-
|
6
|
-
def initialize(m, n, default_color = 'O')
|
7
|
-
super(n) {Array.new(m){default_color}} #Create an M * N 2 dimensional matrix with 'white'
|
8
|
-
@cols = m
|
9
|
-
@rows = n
|
10
|
-
end
|
11
|
-
|
12
|
-
def valid_on?(x,y)
|
13
|
-
x>0 && y>0 && x<=@cols && y<=@rows ? true : false
|
14
|
-
end
|
15
|
-
|
16
|
-
def set(x, y, color)
|
17
|
-
self[y-1][x-1] = color
|
18
|
-
end
|
19
|
-
|
20
|
-
def get(x, y)
|
21
|
-
self[y-1][x-1]
|
22
|
-
end
|
23
|
-
|
24
|
-
def vertical(x, y1, y2, color)
|
25
|
-
(y1..y2).each{|y| set(x, y, color)}
|
26
|
-
end
|
27
|
-
|
28
|
-
def horizontal(x1, x2, y, color)
|
29
|
-
(x1..x2).each{|x| set(x, y, color)}
|
30
|
-
end
|
31
|
-
|
32
|
-
#This recursive algorithm from Ruby Forum: http://www.ruby-forum.com/topic/184567
|
33
|
-
def flood_fill(x, y, target_color, replacement_color )
|
34
|
-
return unless valid_on?(x,y)
|
35
|
-
#puts "x=#{x}, y=#{y}, target_color=#{target_color}, replacement_color=#{replacement_color}, get=#{get(x,y)}"
|
36
|
-
#show; sleep(0.2)
|
37
|
-
|
38
|
-
return if get(x,y) != target_color
|
39
|
-
return if get(x,y) == replacement_color
|
40
|
-
|
41
|
-
set(x, y, replacement_color)
|
42
|
-
flood_fill(x+1, y, target_color, replacement_color)
|
43
|
-
flood_fill(x-1, y, target_color, replacement_color)
|
44
|
-
flood_fill(x, y+1, target_color, replacement_color)
|
45
|
-
flood_fill(x, y-1, target_color, replacement_color)
|
46
|
-
end
|
47
|
-
|
48
|
-
def fill(x,y, color)
|
49
|
-
flood_fill(x, y, get(x,y), color)
|
50
|
-
end
|
51
|
-
|
52
|
-
def show
|
53
|
-
puts "\n=>"
|
54
|
-
each {|line| puts line.join}
|
55
|
-
puts "\n"
|
56
|
-
end
|
57
|
-
|
58
|
-
alias :L :set
|
59
|
-
alias :V :vertical
|
60
|
-
alias :H :horizontal
|
61
|
-
alias :F :fill
|
62
|
-
alias :S :show
|
63
|
-
|
64
|
-
end
|
1
|
+
module Graphed
|
2
|
+
class ImageEditor < Array
|
3
|
+
|
4
|
+
attr_reader :cols, :rows
|
5
|
+
|
6
|
+
def initialize(m, n, default_color = 'O')
|
7
|
+
super(n) {Array.new(m){default_color}} #Create an M * N 2 dimensional matrix with 'white'
|
8
|
+
@cols = m
|
9
|
+
@rows = n
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid_on?(x,y)
|
13
|
+
x>0 && y>0 && x<=@cols && y<=@rows ? true : false
|
14
|
+
end
|
15
|
+
|
16
|
+
def set(x, y, color)
|
17
|
+
self[y-1][x-1] = color
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(x, y)
|
21
|
+
self[y-1][x-1]
|
22
|
+
end
|
23
|
+
|
24
|
+
def vertical(x, y1, y2, color)
|
25
|
+
(y1..y2).each{|y| set(x, y, color)}
|
26
|
+
end
|
27
|
+
|
28
|
+
def horizontal(x1, x2, y, color)
|
29
|
+
(x1..x2).each{|x| set(x, y, color)}
|
30
|
+
end
|
31
|
+
|
32
|
+
#This recursive algorithm from Ruby Forum: http://www.ruby-forum.com/topic/184567
|
33
|
+
def flood_fill(x, y, target_color, replacement_color )
|
34
|
+
return unless valid_on?(x,y)
|
35
|
+
#puts "x=#{x}, y=#{y}, target_color=#{target_color}, replacement_color=#{replacement_color}, get=#{get(x,y)}"
|
36
|
+
#show; sleep(0.2)
|
37
|
+
|
38
|
+
return if get(x,y) != target_color
|
39
|
+
return if get(x,y) == replacement_color
|
40
|
+
|
41
|
+
set(x, y, replacement_color)
|
42
|
+
flood_fill(x+1, y, target_color, replacement_color)
|
43
|
+
flood_fill(x-1, y, target_color, replacement_color)
|
44
|
+
flood_fill(x, y+1, target_color, replacement_color)
|
45
|
+
flood_fill(x, y-1, target_color, replacement_color)
|
46
|
+
end
|
47
|
+
|
48
|
+
def fill(x,y, color)
|
49
|
+
flood_fill(x, y, get(x,y), color)
|
50
|
+
end
|
51
|
+
|
52
|
+
def show
|
53
|
+
puts "\n=>"
|
54
|
+
each {|line| puts line.join}
|
55
|
+
puts "\n"
|
56
|
+
end
|
57
|
+
|
58
|
+
alias :L :set
|
59
|
+
alias :V :vertical
|
60
|
+
alias :H :horizontal
|
61
|
+
alias :F :fill
|
62
|
+
alias :S :show
|
63
|
+
|
64
|
+
end
|
65
65
|
end
|
@@ -1,100 +1,100 @@
|
|
1
|
-
require 'readline'
|
2
|
-
require 'pp'
|
3
|
-
|
4
|
-
module Graphed
|
5
|
-
class Looper
|
6
|
-
|
7
|
-
#Processing commands with regex
|
8
|
-
def self.execute_command(command, image)
|
9
|
-
case command
|
10
|
-
when /^I (\d+) (\d+)/
|
11
|
-
m = $1.to_i
|
12
|
-
n = $2.to_i
|
13
|
-
image = ImageEditor.new(m, n)
|
14
|
-
when /^help/
|
15
|
-
help
|
16
|
-
else
|
17
|
-
unless image[0].nil? #Following commands valid only if object is created.
|
18
|
-
case command
|
19
|
-
when /^C/
|
20
|
-
image = ImageEditor.new(image.cols, image.rows)
|
21
|
-
when /^L (\d+) (\d+) (.+$)/
|
22
|
-
x = $1.to_i
|
23
|
-
y = $2.to_i
|
24
|
-
c = $3
|
25
|
-
image.valid_on?(x,y) ? image.L(x, y, c) : puts("Sorry, wrong parameters.")
|
26
|
-
when /^V (\d+) (\d+) (\d+) (.+$)/
|
27
|
-
x = $1.to_i
|
28
|
-
y1 = $2.to_i
|
29
|
-
y2 = $3.to_i
|
30
|
-
c = $4
|
31
|
-
(y1<=y2) && image.valid_on?(x, y1) && image.valid_on?(x, y2) ? image.V(x, y1, y2, c) : puts("Sorry, wrong parameters.")
|
32
|
-
when /^H (\d+) (\d+) (\d+) (.+$)/
|
33
|
-
x1 = $1.to_i
|
34
|
-
x2 = $2.to_i
|
35
|
-
y = $3.to_i
|
36
|
-
c = $4
|
37
|
-
(x1<=x2) && image.valid_on?(x1, y) && image.valid_on?(x2,y) ? image.H(x1, x2, y, c) : puts("Sorry, wrong parameters.")
|
38
|
-
when /^F (\d+) (\d+) (.+$)/
|
39
|
-
x = $1.to_i
|
40
|
-
y = $2.to_i
|
41
|
-
c = $3
|
42
|
-
image.valid_on?(x, y) ? image.F(x, y, c) : puts("Sorry, wrong parameters.")
|
43
|
-
when /^S/
|
44
|
-
image.S
|
45
|
-
else
|
46
|
-
puts "Sorry, try again. Type 'help' to see all commands."
|
47
|
-
end
|
48
|
-
else
|
49
|
-
puts "Tip: create an image first with 'I'. Example: I 9 7"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
image
|
53
|
-
rescue
|
54
|
-
puts "Exception: Sorry, something happened, please try again."
|
55
|
-
return image
|
56
|
-
end
|
57
|
-
|
58
|
-
#Help message and instruction
|
59
|
-
def self.help
|
60
|
-
print <<-EOF
|
61
|
-
This program simulates a simple interactive graphical editor.
|
62
|
-
|
63
|
-
Available commands:
|
64
|
-
I M N Create a new MxN image with all pixels coloured white(O).
|
65
|
-
C Clears the table, setting all pixels to white (O).
|
66
|
-
L X Y C Colours the pixel (X,Y) with colour C.
|
67
|
-
V X Y1 Y2 C Draw a vertical segment of colour C in column X between rows Y1 and Y2 (inclusive).
|
68
|
-
H X1 X2 Y C Draw a horizontal segment of colour C in row Y between columns X1 and X2 (inclusive).
|
69
|
-
F X Y C Fill the region R with the colour C. R is defined as: Pixel (X,Y) belongs to R. Any other pixel which is the same colour as (X,Y) and shares a common side with any pixel in R also belongs to this region.
|
70
|
-
S Show the contents of the current image
|
71
|
-
X Terminate the session
|
72
|
-
help Show this help
|
73
|
-
|
74
|
-
EOF
|
75
|
-
end
|
76
|
-
|
77
|
-
#Good for debugging
|
78
|
-
def self.automata(image)
|
79
|
-
image = execute_command('I 15 10', image)
|
80
|
-
image = execute_command('V 3 3 9 x', image)
|
81
|
-
image = execute_command('V 7 3 9 x', image)
|
82
|
-
image = execute_command('H 3 7 3 x', image)
|
83
|
-
image = execute_command('H 3 7 7 x', image)
|
84
|
-
image = execute_command('S', image)
|
85
|
-
end
|
86
|
-
|
87
|
-
#Main entry point
|
88
|
-
def self.main
|
89
|
-
help #Show help message
|
90
|
-
image = []
|
91
|
-
#image = automata(image)
|
92
|
-
loop do
|
93
|
-
line = Readline::readline('GraphEd> ')
|
94
|
-
exit if line.nil? || line == 'X'
|
95
|
-
Readline::HISTORY.push(line)
|
96
|
-
image = execute_command(line, image)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
1
|
+
require 'readline'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
module Graphed
|
5
|
+
class Looper
|
6
|
+
|
7
|
+
#Processing commands with regex
|
8
|
+
def self.execute_command(command, image)
|
9
|
+
case command
|
10
|
+
when /^I (\d+) (\d+)/
|
11
|
+
m = $1.to_i
|
12
|
+
n = $2.to_i
|
13
|
+
image = ImageEditor.new(m, n)
|
14
|
+
when /^help/
|
15
|
+
help
|
16
|
+
else
|
17
|
+
unless image[0].nil? #Following commands valid only if object is created.
|
18
|
+
case command
|
19
|
+
when /^C/
|
20
|
+
image = ImageEditor.new(image.cols, image.rows)
|
21
|
+
when /^L (\d+) (\d+) (.+$)/
|
22
|
+
x = $1.to_i
|
23
|
+
y = $2.to_i
|
24
|
+
c = $3
|
25
|
+
image.valid_on?(x,y) ? image.L(x, y, c) : puts("Sorry, wrong parameters.")
|
26
|
+
when /^V (\d+) (\d+) (\d+) (.+$)/
|
27
|
+
x = $1.to_i
|
28
|
+
y1 = $2.to_i
|
29
|
+
y2 = $3.to_i
|
30
|
+
c = $4
|
31
|
+
(y1<=y2) && image.valid_on?(x, y1) && image.valid_on?(x, y2) ? image.V(x, y1, y2, c) : puts("Sorry, wrong parameters.")
|
32
|
+
when /^H (\d+) (\d+) (\d+) (.+$)/
|
33
|
+
x1 = $1.to_i
|
34
|
+
x2 = $2.to_i
|
35
|
+
y = $3.to_i
|
36
|
+
c = $4
|
37
|
+
(x1<=x2) && image.valid_on?(x1, y) && image.valid_on?(x2,y) ? image.H(x1, x2, y, c) : puts("Sorry, wrong parameters.")
|
38
|
+
when /^F (\d+) (\d+) (.+$)/
|
39
|
+
x = $1.to_i
|
40
|
+
y = $2.to_i
|
41
|
+
c = $3
|
42
|
+
image.valid_on?(x, y) ? image.F(x, y, c) : puts("Sorry, wrong parameters.")
|
43
|
+
when /^S/
|
44
|
+
image.S
|
45
|
+
else
|
46
|
+
puts "Sorry, try again. Type 'help' to see all commands."
|
47
|
+
end
|
48
|
+
else
|
49
|
+
puts "Tip: create an image first with 'I'. Example: I 9 7"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
image
|
53
|
+
rescue
|
54
|
+
puts "Exception: Sorry, something happened, please try again."
|
55
|
+
return image
|
56
|
+
end
|
57
|
+
|
58
|
+
#Help message and instruction
|
59
|
+
def self.help
|
60
|
+
print <<-EOF
|
61
|
+
This program simulates a simple interactive graphical editor.
|
62
|
+
|
63
|
+
Available commands:
|
64
|
+
I M N Create a new MxN image with all pixels coloured white(O).
|
65
|
+
C Clears the table, setting all pixels to white (O).
|
66
|
+
L X Y C Colours the pixel (X,Y) with colour C.
|
67
|
+
V X Y1 Y2 C Draw a vertical segment of colour C in column X between rows Y1 and Y2 (inclusive).
|
68
|
+
H X1 X2 Y C Draw a horizontal segment of colour C in row Y between columns X1 and X2 (inclusive).
|
69
|
+
F X Y C Fill the region R with the colour C. R is defined as: Pixel (X,Y) belongs to R. Any other pixel which is the same colour as (X,Y) and shares a common side with any pixel in R also belongs to this region.
|
70
|
+
S Show the contents of the current image
|
71
|
+
X Terminate the session
|
72
|
+
help Show this help
|
73
|
+
|
74
|
+
EOF
|
75
|
+
end
|
76
|
+
|
77
|
+
#Good for debugging
|
78
|
+
def self.automata(image)
|
79
|
+
image = execute_command('I 15 10', image)
|
80
|
+
image = execute_command('V 3 3 9 x', image)
|
81
|
+
image = execute_command('V 7 3 9 x', image)
|
82
|
+
image = execute_command('H 3 7 3 x', image)
|
83
|
+
image = execute_command('H 3 7 7 x', image)
|
84
|
+
image = execute_command('S', image)
|
85
|
+
end
|
86
|
+
|
87
|
+
#Main entry point
|
88
|
+
def self.main
|
89
|
+
help #Show help message
|
90
|
+
image = []
|
91
|
+
#image = automata(image)
|
92
|
+
loop do
|
93
|
+
line = Readline::readline('GraphEd> ')
|
94
|
+
exit if line.nil? || line == 'X'
|
95
|
+
Readline::HISTORY.push(line)
|
96
|
+
image = execute_command(line, image)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
100
|
end
|
data/lib/graphed/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Graphed
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
1
|
+
module Graphed
|
2
|
+
VERSION = "0.0.3"
|
3
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "graphed"
|
3
|
+
|
4
|
+
module Graphed
|
5
|
+
|
6
|
+
describe ImageEditor do
|
7
|
+
|
8
|
+
let(:testimage) {ImageEditor.new(6, 7)}
|
9
|
+
|
10
|
+
it "should create 6x7 size 2D array with O" do
|
11
|
+
testimage.should == [['O','O','O','O','O','O'],
|
12
|
+
['O','O','O','O','O','O'],
|
13
|
+
['O','O','O','O','O','O'],
|
14
|
+
['O','O','O','O','O','O'],
|
15
|
+
['O','O','O','O','O','O'],
|
16
|
+
['O','O','O','O','O','O'],
|
17
|
+
['O','O','O','O','O','O']]
|
18
|
+
end
|
19
|
+
|
20
|
+
it "valid on 3x6 but invalid on 7x8" do
|
21
|
+
testimage.should be_valid_on(3,6)
|
22
|
+
testimage.should_not be_valid_on(7,8)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should change color" do
|
26
|
+
testimage.set(3,6,'X')
|
27
|
+
testimage.get(3,6).should == 'X'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should draw a vertical line" do
|
31
|
+
testimage.vertical(3,1,5, 'X')
|
32
|
+
testimage.should == [['O','O','X','O','O','O'],
|
33
|
+
['O','O','X','O','O','O'],
|
34
|
+
['O','O','X','O','O','O'],
|
35
|
+
['O','O','X','O','O','O'],
|
36
|
+
['O','O','X','O','O','O'],
|
37
|
+
['O','O','O','O','O','O'],
|
38
|
+
['O','O','O','O','O','O']]
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should draw a horizontal line" do
|
42
|
+
testimage.horizontal(2, 6, 3, 'Z')
|
43
|
+
testimage.should == [['O','O','O','O','O','O'],
|
44
|
+
['O','O','O','O','O','O'],
|
45
|
+
['O','Z','Z','Z','Z','Z'],
|
46
|
+
['O','O','O','O','O','O'],
|
47
|
+
['O','O','O','O','O','O'],
|
48
|
+
['O','O','O','O','O','O'],
|
49
|
+
['O','O','O','O','O','O']]
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should fill an area" do
|
53
|
+
testimage.V(2,2,6,'X')
|
54
|
+
testimage.V(5,2,6,'X')
|
55
|
+
testimage.H(2,5,2,'X')
|
56
|
+
testimage.H(2,5,6,'X')
|
57
|
+
testimage.fill(3,3,'Z')
|
58
|
+
testimage.should ==
|
59
|
+
[["O", "O", "O", "O", "O", "O"],
|
60
|
+
["O", "X", "X", "X", "X", "O"],
|
61
|
+
["O", "X", "Z", "Z", "X", "O"],
|
62
|
+
["O", "X", "Z", "Z", "X", "O"],
|
63
|
+
["O", "X", "Z", "Z", "X", "O"],
|
64
|
+
["O", "X", "X", "X", "X", "O"],
|
65
|
+
["O", "O", "O", "O", "O", "O"]]
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
-
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
-
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
-
# loaded once.
|
5
|
-
#
|
6
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
-
RSpec.configure do |config|
|
8
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
-
config.run_all_when_everything_filtered = true
|
10
|
-
config.filter_run :focus
|
11
|
-
|
12
|
-
# Run specs in random order to surface order dependencies. If you find an
|
13
|
-
# order dependency and want to debug it, you can fix the order by providing
|
14
|
-
# the seed, which is printed after each run.
|
15
|
-
# --seed 1234
|
16
|
-
config.order = 'random'
|
17
|
-
end
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zoltan Debre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,9 +69,11 @@ files:
|
|
69
69
|
- bin/graphed
|
70
70
|
- graphed.gemspec
|
71
71
|
- lib/graphed.rb
|
72
|
-
- lib/graphed/console.rb
|
73
72
|
- lib/graphed/imageeditor.rb
|
73
|
+
- lib/graphed/looper.rb
|
74
74
|
- lib/graphed/version.rb
|
75
|
+
- spec/lib/imageeditor_spec.rb
|
76
|
+
- spec/lib/looper_spec.rb
|
75
77
|
- spec/spec_helper.rb
|
76
78
|
homepage: https://github.com/szines/graphed
|
77
79
|
licenses:
|
@@ -98,4 +100,6 @@ signing_key:
|
|
98
100
|
specification_version: 4
|
99
101
|
summary: Simple image editor.
|
100
102
|
test_files:
|
103
|
+
- spec/lib/imageeditor_spec.rb
|
104
|
+
- spec/lib/looper_spec.rb
|
101
105
|
- spec/spec_helper.rb
|