asciiart 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ spec/reports
17
17
  test/tmp
18
18
  test/version_tmp
19
19
  tmp
20
+ .DS_Store
data/README.md CHANGED
@@ -131,6 +131,16 @@ _or make it thinner_
131
131
  |oooooooooooooooooooooooooooooooooooooooooooooooooo|
132
132
  +--------------------------------------------------+
133
133
 
134
+ Add color with the "color" option
135
+
136
+ require 'asciiart'
137
+ a = AsciiArt.new("http://www.evangogh.org/images/paintings/self-portrait.jpg")
138
+ => #<AsciiArt:0x007fa889cbacf8 @data="...">
139
+
140
+ puts a.to_ascii_art(color: true) =>
141
+
142
+ [Booyah!](http://farm9.staticflickr.com/8080/8424360420_8011af48fe_b.jpg)
143
+
134
144
  ### In The Command Line
135
145
 
136
146
  Local Files
@@ -147,6 +157,8 @@ Get Help
147
157
 
148
158
  Usage: asciiart [options] <path_or_url>
149
159
  -w, --width WIDTH Width of the finished Ascii Art (Default: 100)
160
+ -c, --color Switch to use colored terminal output (Default: false)
161
+ -i, --invert-chars Invert the character map. Depending on your terminal and image this can make the image clearer (or a lot worse)
150
162
  -v, --version Show AsciiArt version
151
163
  -h, --help Show this message
152
164
 
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency('rmagick')
21
+ gem.add_dependency('rainbow')
21
22
 
22
23
  gem.add_development_dependency('pry')
23
24
  end
@@ -3,7 +3,9 @@
3
3
  require 'asciiart'
4
4
  require 'optparse'
5
5
 
6
- options = {}
6
+ options = {}
7
+ invert_chars = false
8
+
7
9
  opt_parser = OptionParser.new do |opts|
8
10
  opts.banner = 'Usage: asciiart [options] <path_or_url>'
9
11
 
@@ -11,6 +13,14 @@ opt_parser = OptionParser.new do |opts|
11
13
  options[:width] = width.to_i
12
14
  end
13
15
 
16
+ opts.on('-c', '--color', 'Switch to use colored terminal output (Default: false)') do
17
+ options[:color] = true
18
+ end
19
+
20
+ opts.on('-i', '--invert-chars', 'Invert the character map. Depending on your terminal and image this can make the image clearer (or a lot worse)') do
21
+ invert_chars = true
22
+ end
23
+
14
24
  opts.on_tail('-v', '--version', 'Show AsciiArt version') do
15
25
  puts 'AsciiArt version ' + AsciiArt::VERSION
16
26
  exit
@@ -20,6 +30,7 @@ opt_parser = OptionParser.new do |opts|
20
30
  puts opts
21
31
  exit
22
32
  end
33
+
23
34
  end
24
35
 
25
36
  begin
@@ -34,5 +45,9 @@ if ARGV.length == 0
34
45
  exit
35
46
  end
36
47
 
37
- puts AsciiArt.new(ARGV[0]).to_ascii_art(options)
48
+ art = AsciiArt.new(ARGV[0])
49
+
50
+ art.image_chars = art.image_chars.reverse if invert_chars
51
+
52
+ puts art.to_ascii_art(options)
38
53
 
@@ -2,6 +2,7 @@ require "asciiart/version"
2
2
  require 'RMagick'
3
3
  require 'uri'
4
4
  require 'open-uri'
5
+ require 'rainbow'
5
6
 
6
7
  class AsciiArt
7
8
 
@@ -14,20 +15,18 @@ class AsciiArt
14
15
  end
15
16
 
16
17
  def to_ascii_art(options = {})
17
- options = { width: 100 }.merge(options)
18
+ options = { width: 100, color: false }.merge(options)
18
19
 
19
20
  img = Magick::Image.from_blob(@data).first
20
21
 
21
- width = options[:width]
22
- scale = (width.to_f / img.columns)
23
- height = ((img.rows * scale) / 2).to_i
22
+ width = options[:width]
23
+ scale = (width.to_f / img.columns)
24
+ height = ((img.rows * scale) / 2).to_i
24
25
 
25
26
  img.resize!(width, height)
26
-
27
- img = img.quantize(image_chars.length, Magick::GRAYColorspace)
28
- img = img.normalize
29
-
30
- quantum_calc = Magick::QuantumRange / Magick::QuantumPixel.to_i
27
+ color_image = img.dup if options[:color]
28
+ img = img.quantize(image_chars.length, Magick::GRAYColorspace).normalize
29
+ quantum_calc = Magick::QuantumRange / Magick::QuantumPixel.to_i
31
30
 
32
31
  border = "+#{'-' * width}+\n"
33
32
  output = border.dup
@@ -35,7 +34,17 @@ class AsciiArt
35
34
  img.view(0, 0, width, height) do |view|
36
35
  height.times do |i|
37
36
  output << '|'
38
- width.times { |j| output << image_chars[view[i][j].red/quantum_calc] }
37
+ width.times do |j|
38
+
39
+ character = image_chars[view[i][j].red/quantum_calc]
40
+
41
+ if options[:color]
42
+ pix = color_image.pixel_color(j,i)
43
+ character = character.color(unified_rgb_value(pix.red), unified_rgb_value(pix.green), unified_rgb_value(pix.blue))
44
+ end
45
+
46
+ output << character
47
+ end
39
48
  output << "|\n"
40
49
  end
41
50
  end
@@ -50,5 +59,12 @@ class AsciiArt
50
59
  def inspect
51
60
  "#<#{self.class.to_s}>"
52
61
  end
62
+
63
+ private
64
+
65
+ def unified_rgb_value(number)
66
+ (Magick::QuantumDepth == 16) ? (number / 256) : number
67
+ end
68
+
53
69
  end
54
70
 
@@ -1,3 +1,3 @@
1
1
  class AsciiArt
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciiart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-25 00:00:00.000000000 Z
13
+ date: 2013-01-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rmagick
@@ -28,6 +28,22 @@ dependencies:
28
28
  - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
30
  version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rainbow
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
31
47
  - !ruby/object:Gem::Dependency
32
48
  name: pry
33
49
  requirement: !ruby/object:Gem::Requirement