term-ansicolor 1.1.5 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e87520103afc6ad7866c8f53d7f69543f2e34c4
4
- data.tar.gz: 2acd9fcd522f0a461f5ad4198e2800219e7a6d0d
3
+ metadata.gz: af8a2335a9c7525731899d81ed738d42f1476acf
4
+ data.tar.gz: 39ad9bcec2cc2cb6e35e751abe0a9dcb1a874df3
5
5
  SHA512:
6
- metadata.gz: b1780b21998f041f4d5ac2d0d481c670a7a7168f09cbabb974c1317fa49bf0c715f1e13e3c0806ddad30e488db602d1d46caeea67c7599bec8a1c4cbfd7b9ce7
7
- data.tar.gz: 719ab987185dcccf196eba49391f6b785db7c6c43a17d02895d210b80a4e02b670a9fa29ffa3c9b8642c55c062a3be9c182af682f386ae3e30c92005416f3e8e
6
+ metadata.gz: 4cf91d3f8b52f8440741ba9e84f3f436fedfaa27d10ffa52a95782e8e044c103e4fbfa423e349368afa88dedcaee11ca2b4672a448144c2b2f7336f961c9c2b0
7
+ data.tar.gz: dd425666ea88970b3da1ca256892e8c2da08a5c37a1f48328f452d5e41204392ac5f5d0a6611d3cb758601d58cbbefd74e50597152a71822ffce82836ac646b0
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  coverage
5
5
  pkg
6
+ tags
data/CHANGES CHANGED
@@ -1,3 +1,6 @@
1
+ 2013-05-16 - 1.2.0 * Add term_mandel and term_display executables.
2
+ * Implement configurable color metrics.
3
+ * Add gradient functionality for color attributes.
1
4
  2013-04-18 - 1.1.5 * Add colortab executable to rubygem binary config
2
5
  2013-03-26 - 1.1.4 * Easier access to color attributes via color(123) or
3
6
  approximate html colors like color('#336caf').
data/README.rdoc CHANGED
@@ -31,9 +31,18 @@ The homepage of this library is located at
31
31
 
32
32
  == Examples
33
33
 
34
- Additional to the two executables cdiff and decolor, the file
35
- examples/example.rb in the source/gem-distribution shows how this library can
36
- be used.
34
+ The following executables are provided with Term::ANSIColor:
35
+
36
+ * cdiff: colors a diff patch
37
+ * decolor: decolors any text file that was colored with ANSI escape sequences
38
+ * colortab: Displays a table of the 256 terminal colors with their indices and
39
+ nearest html equivalents.
40
+ * term_display: displays a ppm3 or ppm6 image file in the terminal. If the netpbm
41
+ programs are installed it can handle a lot of other image file formats.
42
+ * term_mandel: displays the mandelbrot set in the terminal
43
+
44
+ Additionally the file examples/example.rb in the source/gem-distribution shows
45
+ how this library can be used.
37
46
 
38
47
  == Author
39
48
 
data/Rakefile CHANGED
@@ -13,12 +13,15 @@ GemHadar do
13
13
  description 'This library uses ANSI escape sequences to control the attributes of terminal output'
14
14
  licenses << 'GPL-2'
15
15
  test_dir 'tests'
16
- ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', 'coverage'
16
+ ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', 'coverage', 'tags'
17
17
  readme 'README.rdoc'
18
- executables << 'cdiff' << 'decolor' << 'colortab'
18
+ executables << 'cdiff' << 'decolor' << 'colortab' << 'term_mandel' << 'term_display'
19
19
 
20
+ dependency 'tins', '~>0.8'
20
21
  development_dependency 'simplecov'
21
22
 
23
+ post_install_message File.read(File.join(File.dirname(__FILE__), 'examples/smiley.txt'))
24
+
22
25
  install_library do
23
26
  destdir = "#{ENV['DESTDIR']}"
24
27
  libdir = CONFIG["sitelibdir"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.5
1
+ 1.2.0
data/bin/term_display ADDED
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tempfile'
4
+ require 'term/ansicolor'
5
+ require 'tins/xt'
6
+ include Tins::GO
7
+
8
+ def provide_ppm_file(filename, opts)
9
+ pnmtopnm = `which pnmtopnm`
10
+ if pnmtopnm.empty?
11
+ STDERR.puts "You might consider installing netpbm to enable image conversion/scaling!"
12
+ File.new(filename, 'rb')
13
+ else
14
+ cmd = [ pnmtopnm.chomp! ]
15
+ ext = File.extname(filename)
16
+ scale = `which pamscale`.chomp!
17
+ console_scale = "#{scale} -#{opts['s']} #{opts['C']} #{opts['R']}"
18
+ cmd.unshift console_scale
19
+ aspect_scaling = "#{scale} -xscale #{opts['a']}"
20
+ cmd.unshift aspect_scaling
21
+ convert_to_pnm =
22
+ case ext
23
+ when /\A(\.ppm\d*|\.pnm|\z)/
24
+ ''
25
+ when /\A.(jpe?g|exif|jfif)\z/i
26
+ 'jpegtopnm'
27
+ when /\A.tiff?\z/i
28
+ 'tifftopnm'
29
+ else
30
+ "#{ext[1..-1].downcase}topnm"
31
+ end
32
+ unless convert_to_pnm.empty?
33
+ convert_to_pnm = `which #{convert_to_pnm}`.chomp!
34
+ convert_to_pnm.empty? and fail "unknown pnm converter #{convert_to_pnm}"
35
+ cmd.unshift convert_to_pnm
36
+ end
37
+ temp = Tempfile.open(File.basename($0))
38
+ cmd *= '|'
39
+ STDERR.puts "Executing #{cmd.inspect}"
40
+ IO.popen(cmd, 'r+') do |converter|
41
+ File.open(filename, 'rb') do |input|
42
+ until input.eof?
43
+ converter.write input.read(8192)
44
+ end
45
+ converter.close_write
46
+ end
47
+ until converter.eof?
48
+ temp.write(converter.read)
49
+ end
50
+ end
51
+ temp.tap(&:rewind)
52
+ end
53
+ end
54
+
55
+ def usage(rc = 0)
56
+ puts to <<-end
57
+ Usage: #$0 [OPTIONS] FILENAME"
58
+
59
+ Options are
60
+
61
+ -m METRIC for distance (METRIC = #{Term::ANSIColor::RGBColorMetrics.metrics * '|'})
62
+ -g yes|no use/don't use gray values, defaults to yes
63
+ -s xyfit|xyfill scaling strategy, defaults to xyfit
64
+ -a ASPECT x:y aspect, defaults to 2.2
65
+ -C COLS number of columns for rendering with aspect, defaults to max
66
+ -R ROWS number of rows for rendering with aspect, defaults to max - 1
67
+ -h this help
68
+
69
+ end
70
+ exit rc
71
+ end
72
+
73
+ opts = go 'hm:g:s:a:C:R:'
74
+ opts['h'] and usage
75
+ filename = ARGV.shift or usage 1
76
+ metric = Term::ANSIColor::RGBColorMetrics.metric(opts['m'] || 'CIELab')
77
+ gray = (opts['g'] || 'yes') == 'yes'
78
+ opts['s'] ||= 'xyfit'
79
+ opts['a'] ||= '2.2'
80
+ opts['C'] ||= Tins::Terminal.cols
81
+ opts['R'] ||= [ Tins::Terminal.rows - 1, 0 ].max
82
+
83
+ file = provide_ppm_file(filename, opts)
84
+ ppm = Term::ANSIColor::PPMReader.new(
85
+ file,
86
+ :metric => metric,
87
+ :gray => gray
88
+ )
89
+
90
+ puts ppm
data/bin/term_mandel ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'term/ansicolor'
4
+ require 'tins/xt'
5
+ require "complex"
6
+ include Tins::GO
7
+
8
+ @width, @height = Tins::Terminal.cols, Tins::Terminal.lines
9
+
10
+ def color_random
11
+ (1..3).map { rand(255) }
12
+ end
13
+
14
+ def draw_set(rx, ry)
15
+ sx = (rx.end - rx.begin).abs / @width
16
+ sy = (ry.end - ry.begin).abs / @height
17
+
18
+ ac = Term::ANSIColor
19
+ color =
20
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
21
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
22
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
23
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
24
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
25
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
26
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
27
+ ac::Attribute[ color_random ].gradient_to(ac::Attribute['#000'], :steps => 16)
28
+ iters = color.size - 2
29
+
30
+ text = ''
31
+ for j in 0...@height
32
+ for i in 0...@width
33
+ n, z_n = 0, Complex(0, 0)
34
+ c = Complex(sx * i + rx.begin, sy * j + ry.begin)
35
+ while n <= iters
36
+ break if z_n.abs > 2
37
+ z_n = z_n ** 2 + c
38
+ n += 1
39
+ end
40
+ text << ac.on_color(color[n]) << ' '
41
+ end
42
+ text << ac.reset << "\n"
43
+ end
44
+ puts text
45
+ end
46
+
47
+ opts = go 'x:y:'
48
+
49
+ rx = opts['x'].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-2.0..1.0)
50
+ ry = opts['y'].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-1.0..1.0)
51
+
52
+ draw_set rx, ry
Binary file
Binary file
Binary file
@@ -0,0 +1,135 @@
1
+ P3
2
+ 44 22
3
+ 255
4
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
5
+ 249 242 242 236 209 209 230 188 188 229 182 182 229 182 182 230 188 187 235 207 207 247 238 238
6
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
7
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
8
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
9
+ 255 255 255 255 255 255 255 255 255 255 255 255
10
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 246 234 234 215 137 136
11
+ 203 34 30 206 30 24 206 29 24 206 28 24 207 29 24 206 28 24 206 28 24 203 30 25
12
+ 213 126 125 244 229 229 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
13
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
14
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
15
+ 255 255 255 255 255 255 255 255 255 255 255 255
16
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 247 237 238 202 67 64 206 28 24
17
+ 205 29 24 202 37 32 207 68 65 207 96 95 208 91 89 204 52 49 203 28 23 206 28 24
18
+ 206 28 24 202 51 47 234 207 207 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
19
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
20
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
21
+ 255 255 255 255 255 255 255 255 255 255 255 255
22
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 227 184 184 205 29 24 212 124 123
23
+ 241 221 221 253 250 250 255 255 255 255 255 255 255 255 255 255 254 254 249 239 239 230 194 194
24
+ 206 82 80 206 28 24 202 36 32 234 207 207 255 255 255 255 255 255 255 255 255 255 255 255
25
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
26
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
27
+ 255 255 255 255 255 255 255 255 255 255 255 255
28
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 243 230 230 234 204 203 250 245 245
29
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
30
+ 250 244 244 209 118 117 206 29 24 202 47 43 242 223 223 255 255 255 255 255 255 255 255 255
31
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
32
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
33
+ 255 255 255 255 255 255 255 255 255 255 255 255
34
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
35
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
36
+ 255 255 255 250 245 245 206 98 97 206 28 24 202 74 72 247 237 238 255 255 255 255 255 255
37
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
38
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
39
+ 255 255 255 255 255 255 255 255 255 255 255 255
40
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
41
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
42
+ 255 255 255 255 255 255 246 234 234 201 41 37 206 28 24 206 105 104 252 247 247 255 255 255
43
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
44
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
45
+ 255 255 255 255 255 255 255 255 255 255 255 255
46
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
47
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
48
+ 255 255 255 254 251 252 215 146 146 204 28 24 206 28 24 206 29 24 213 137 136 255 253 253
49
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
50
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
51
+ 255 255 255 255 255 255 255 255 255 255 255 255
52
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
53
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
54
+ 252 248 249 212 133 132 205 28 24 206 28 24 206 29 24 206 29 24 204 29 24 221 169 168
55
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
56
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
57
+ 255 255 255 255 255 255 255 255 255 255 255 255
58
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
59
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 250 245 245
60
+ 209 118 117 205 28 24 206 29 24 206 28 24 206 28 24 206 28 24 206 28 24 202 29 25
61
+ 231 198 198 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
62
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
63
+ 255 255 255 255 255 255 255 255 255 255 255 255
64
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
65
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 249 240 240 207 104 103
66
+ 206 29 24 206 28 24 206 29 24 206 28 24 206 28 24 206 28 24 205 32 27 206 29 24
67
+ 202 44 40 240 219 219 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
68
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
69
+ 255 255 255 255 255 255 255 255 255 255 255 255
70
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
71
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 246 236 236 206 90 89 206 28 24
72
+ 206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 105 103 243 228 229 204 92 91
73
+ 206 28 24 202 71 68 246 235 235 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
74
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
75
+ 255 255 255 255 255 255 255 255 255 255 255 255
76
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
77
+ 255 255 255 255 255 255 255 255 255 255 255 255 244 229 229 203 77 75 206 28 24 206 28 24
78
+ 206 28 24 206 29 24 206 28 24 205 30 24 210 120 118 250 245 245 255 255 255 245 233 233
79
+ 202 64 61 206 28 24 206 101 100 251 246 247 255 255 255 255 255 255 255 255 255 255 255 255
80
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
81
+ 255 255 255 255 255 255 255 255 255 255 255 255
82
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
83
+ 255 255 255 255 255 255 255 255 255 241 222 221 203 64 62 206 28 24 206 28 24 206 28 24
84
+ 206 28 24 206 29 24 205 29 24 213 135 134 252 249 249 255 255 255 255 255 255 255 255 255
85
+ 239 216 215 202 38 34 206 30 24 212 132 130 255 253 253 255 255 255 255 255 255 255 255 255
86
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
87
+ 255 255 255 255 255 255 255 255 255 255 255 255
88
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
89
+ 255 255 255 255 255 255 237 212 212 202 53 49 206 28 24 206 28 24 206 28 24 206 28 24
90
+ 206 28 24 204 29 24 217 152 151 254 252 252 255 255 255 255 255 255 255 255 255 255 255 255
91
+ 255 255 255 229 192 192 202 29 25 204 29 24 220 164 164 255 255 255 255 255 255 255 255 255
92
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
93
+ 255 255 255 255 255 255 255 255 255 255 255 255
94
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
95
+ 255 255 255 233 203 203 203 41 36 206 29 24 205 28 24 206 28 24 206 28 24 206 28 24
96
+ 204 29 25 221 167 167 255 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
97
+ 255 255 255 255 255 255 219 162 162 205 29 24 202 30 25 230 194 194 255 255 255 255 255 255
98
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
99
+ 255 255 255 255 255 255 255 255 255 255 255 255
100
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
101
+ 229 191 191 202 32 28 206 29 24 206 28 24 206 29 24 206 28 24 206 29 24 203 31 26
102
+ 227 182 182 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
103
+ 255 255 255 255 255 255 254 252 253 212 131 130 206 30 24 203 41 35 236 211 211 255 255 255
104
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 252 250 250
105
+ 249 243 243 251 247 247 255 255 255 255 255 255
106
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 224 177 177
107
+ 203 29 25 206 28 24 206 28 24 206 28 24 206 28 24 206 29 24 203 38 34 232 197 197
108
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
109
+ 255 255 255 255 255 255 255 255 255 251 246 246 205 100 98 206 29 24 203 42 38 229 192 191
110
+ 255 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 252 252 214 141 140
111
+ 203 26 22 206 130 130 255 255 255 255 255 255
112
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 254 220 163 163 204 29 24
113
+ 206 28 24 206 28 24 206 28 24 206 28 24 206 29 24 203 49 46 236 209 209 255 255 255
114
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
115
+ 255 255 255 255 255 255 255 255 255 255 255 255 247 235 235 202 69 67 206 29 24 205 29 24
116
+ 209 96 94 224 166 165 232 190 190 233 203 203 233 195 194 225 170 170 209 95 93 205 29 24
117
+ 206 28 24 213 147 147 255 255 255 255 255 255
118
+ 255 255 255 255 255 255 255 255 255 255 255 255 254 252 252 216 149 148 205 28 24 206 29 24
119
+ 206 28 24 206 28 24 206 28 24 206 28 24 202 62 59 239 219 219 255 255 255 255 255 255
120
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
121
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 243 226 226 203 72 69 206 29 24
122
+ 206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 29 24
123
+ 204 79 76 246 233 234 255 255 255 255 255 255
124
+ 255 255 255 255 255 255 255 255 255 252 250 250 209 137 136 202 33 29 203 33 29 203 33 28
125
+ 203 33 28 203 33 28 203 34 28 203 78 76 243 228 228 255 255 255 255 255 255 255 255 255
126
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
127
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 249 241 241 223 164 163
128
+ 205 75 73 203 29 24 205 28 24 206 28 24 206 28 24 203 30 25 208 90 88 224 174 173
129
+ 250 244 244 255 255 255 255 255 255 255 255 255
130
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
131
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
132
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
133
+ 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
134
+ 255 255 255 248 241 241 243 228 228 242 227 226 243 228 228 249 244 244 255 255 255 255 255 255
135
+ 255 255 255 255 255 255 255 255 255 255 255 255
Binary file
@@ -0,0 +1,4 @@
1
+ P6
2
+ 44 22
3
+ 255
4
+ ������������������������������漼嶶嶶漻���������������������������������������������������������������������������������������������������������������׉��"��������~}����������������������������������������������������������������������������������������������������C@���% �DA�`_�[Y�41����3/���������������������������������������������������������������������������������������������㸸��|{�������������������������RP��$ �������������������������������������������������������������������������������������������������������������������������������vu��/+�������������������������������������������������������������������������������������������������������������������������������ba��JH�������������������������������������������������������������������������������������������������������������������������������)%��ih������������������������������������������������������������������������������������������������������������������������ג����Չ�������������������������������������������������������������������������������������������������������������������ԅ������ݩ��������������������������������������������������������������������������������������������������������������vu�����������������������������������������������������������������������������������������������������������������hg������� ��,(����������������������������������������������������������������������������������������������������ZY�������ig����\[��GD����������������������������������������������������������������������������������������������MK�������xv����������@=��ed����������������������������������������������������������������������������������������@>������Շ�����������������&"�Ԅ�����������������������������������������������������������������������������������51������٘������������������������ܤ�����������������������������������������������������������������������������)$������ݧ�������������������������ۢ������������������������������������������������������������������������忿� ������㶶������������������������������ԃ���)#���������������������������������������������������������������౱�������&"����������������������������������������db��*&������������������������֍��΂�������������������������ܣ��������1.����������������������������������������������EC���`^থ达������᪪�_]��Փ����������������������ؕ��������>;����������������������������������������������������HE����������OL���������������������щ��!�!�!�!�!�"�NL���������������������������������������������������������ߤ��KI������ZX஭������������������������������������������������������������������������������������������������������������������������������������������������
Binary file
Binary file
@@ -0,0 +1,36 @@
1
+             
2
+                              
3
+                                        
4
+                                               
5
+                                                    
6
+                                                          
7
+                                                              
8
+                                                                
9
+                                                                 
10
+                                                           
11
+                                                     
12
+                                                    
13
+                                                   
14
+                                                      
15
+                                                         
16
+                                                              
17
+                                                        
18
+                                                        
19
+                                                     
20
+                                                   
21
+                                                       
22
+                                                     
23
+                                                    
24
+                                                   
25
+                                       
26
+                                         
27
+                                              
28
+                                                      
29
+                                                          
30
+                                                    
31
+                                          
32
+                                      
33
+                               
34
+                          
35
+                          
36
+            
data/examples/wool.jpg ADDED
Binary file
@@ -6,6 +6,7 @@ module Term
6
6
  require 'term/ansicolor/version'
7
7
  require 'term/ansicolor/attribute'
8
8
  require 'term/ansicolor/rgb_triple'
9
+ require 'term/ansicolor/ppm_reader'
9
10
 
10
11
  Attribute.set :clear , 0 # String#clear is already used to empty string in Ruby 1.9
11
12
  Attribute.set :reset , 0 # synonym for :clear
@@ -202,7 +203,8 @@ module Term
202
203
  end
203
204
 
204
205
  def on_color(name, string = nil, &block)
205
- color("on_#{name}", string, &block)
206
+ attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}"
207
+ color("on_#{attribute.name}", string, &block)
206
208
  end
207
209
 
208
210
  class << self
@@ -3,16 +3,37 @@ module Term
3
3
  class Attribute
4
4
  @__store__ = {}
5
5
 
6
- def self.set(name, code, options = {})
7
- name = name.to_sym
8
- result = @__store__[name] = new(name, code, options)
9
- @rgb_colors = nil
10
- result
6
+ if RUBY_VERSION < '1.9'
7
+ @__order__ = []
8
+
9
+ def self.set(name, code, options = {})
10
+ name = name.to_sym
11
+ result = @__store__[name] = new(name, code, options)
12
+ @__order__ << name
13
+ @rgb_colors = nil
14
+ result
15
+ end
16
+
17
+ def self.attributes(&block)
18
+ @__order__.map { |name| @__store__[name] }
19
+ end
20
+ else
21
+ def self.set(name, code, options = {})
22
+ name = name.to_sym
23
+ result = @__store__[name] = new(name, code, options)
24
+ @rgb_colors = nil
25
+ result
26
+ end
27
+
28
+ def self.attributes(&block)
29
+ @__store__.each_value(&block)
30
+ end
11
31
  end
12
32
 
13
33
  def self.[](name)
14
34
  case
15
35
  when self === name then name
36
+ when Array === name then nearest_rgb_color name
16
37
  when name.to_s =~ /\A(on_)?(\d+)\z/ then get "#$1color#$2"
17
38
  when name.to_s =~ /\A#([0-9a-f]{3}){1,2}\z/i then nearest_rgb_color name
18
39
  when name.to_s =~ /\Aon_#([0-9a-f]{3}){1,2}\z/i then nearest_rgb_on_color name
@@ -24,10 +45,6 @@ module Term
24
45
  @__store__[name.to_sym]
25
46
  end
26
47
 
27
- def self.attributes(&block)
28
- @__store__.each_value(&block)
29
- end
30
-
31
48
  def self.rgb_colors(&block)
32
49
  @rgb_colors ||= attributes.select(&:rgb_color?).each(&block)
33
50
  end
@@ -36,14 +53,22 @@ module Term
36
53
  @named_attributes ||= attributes.reject(&:rgb_color?).each(&block)
37
54
  end
38
55
 
39
- def self.nearest_rgb_color(color)
56
+ def self.nearest_rgb_color(color, options = {})
40
57
  rgb = RGBTriple[color]
41
- rgb_colors.reject(&:background?).min_by { |c| c.distance_to(rgb) }
58
+ colors = rgb_colors
59
+ if options.key?(:gray) && !options[:gray]
60
+ colors = colors.reject(&:gray?)
61
+ end
62
+ colors.reject(&:background?).min_by { |c| c.distance_to(rgb, options) }
42
63
  end
43
64
 
44
- def self.nearest_rgb_on_color(color)
65
+ def self.nearest_rgb_on_color(color, options = {})
45
66
  rgb = RGBTriple[color]
46
- rgb_colors.select(&:background?).min_by { |c| c.distance_to(rgb) }
67
+ colors = rgb_colors
68
+ if options.key?(:gray) && !options[:gray]
69
+ colors = colors.reject(&:gray?)
70
+ end
71
+ colors.select(&:background?).min_by { |c| c.distance_to(rgb, options) }
47
72
  end
48
73
 
49
74
  def initialize(name, code, options = {})
@@ -66,6 +91,10 @@ module Term
66
91
  end
67
92
  end
68
93
 
94
+ def apply(string = nil, &block)
95
+ ::Term::ANSIColor.color(self, string, &block)
96
+ end
97
+
69
98
  def background?
70
99
  @name.to_s.start_with?('on_')
71
100
  end
@@ -76,20 +105,37 @@ module Term
76
105
  !!@rgb
77
106
  end
78
107
 
108
+ def gray?
109
+ rgb_color? && to_rgb_triple.gray?
110
+ end
111
+
79
112
  def to_rgb_triple
80
113
  @rgb
81
114
  end
82
115
 
83
- def distance_to(other)
116
+ def distance_to(other, options = {})
84
117
  if our_rgb = to_rgb_triple and
85
118
  other.respond_to?(:to_rgb_triple) and
86
119
  other_rgb = other.to_rgb_triple
87
120
  then
88
- our_rgb.distance_to other_rgb
121
+ our_rgb.distance_to(other_rgb, options)
89
122
  else
90
123
  1 / 0.0
91
124
  end
92
125
  end
126
+
127
+ def gradient_to(other, options = {})
128
+ if our_rgb = to_rgb_triple and
129
+ other.respond_to?(:to_rgb_triple) and
130
+ other_rgb = other.to_rgb_triple
131
+ then
132
+ our_rgb.gradient_to(other_rgb, options).map do |rgb_triple|
133
+ self.class.nearest_rgb_color(rgb_triple, options)
134
+ end
135
+ else
136
+ []
137
+ end
138
+ end
93
139
  end
94
140
  end
95
141
  end