graphed 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d79c6a45d516a094af75eb48fbed98d09b61e97
4
+ data.tar.gz: 0756e7d0abe40799a9da22a9c46cf04663129915
5
+ SHA512:
6
+ metadata.gz: 03e5a66f880681a4fc97a467120746b22f3473676041d9ec2ef772b2eb2a48fa07d9fbe8fc939034269c3c94ba5d5afafb97cdcba5a43621e65df91084da1dc9
7
+ data.tar.gz: a27d26609d9b6b12e9d4c53a009b6bee0fcb1409dc46c80da633ebc500666d8c6fdada33fca33970f5ea35f7a1413e7ee4e1d0e8fc1c0f5fff913190dfebed0f
data/.gitignore ADDED
@@ -0,0 +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
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in graphed.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +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.
data/README.md ADDED
@@ -0,0 +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
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/graphed ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'graphed'
3
+
4
+ Graphed.Launch
data/graphed.gemspec ADDED
@@ -0,0 +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
@@ -0,0 +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
100
+ end
@@ -0,0 +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
65
+ end
@@ -0,0 +1,3 @@
1
+ module Graphed
2
+ VERSION = "0.0.1"
3
+ end
data/lib/graphed.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'graphed/version'
2
+ require 'graphed/console'
3
+ require 'graphed/imageeditor'
4
+
5
+ module Graphed
6
+ class Launch
7
+ Graphed::Looper.main
8
+ end
9
+ end
@@ -0,0 +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
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zoltan Debre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rb-readline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple image editor simulator in a command line.
56
+ email:
57
+ - ''
58
+ executables:
59
+ - graphed
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .rspec
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/graphed
70
+ - graphed.gemspec
71
+ - lib/graphed.rb
72
+ - lib/graphed/console.rb
73
+ - lib/graphed/imageeditor.rb
74
+ - lib/graphed/version.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/szines/graphed
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Simple image editor.
100
+ test_files:
101
+ - spec/spec_helper.rb