Skip to content

Commit

Permalink
Implement color metric, gradient, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
flori committed May 16, 2013
1 parent 561f6b3 commit c6f8a9d
Show file tree
Hide file tree
Showing 29 changed files with 947 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Gemfile.lock
coverage
pkg
tags
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2013-05-16 - 1.2.0 * Add term_mandel and term_display executables.
* Implement configurable color metrics.
* Add gradient functionality for color attributes.
2013-04-18 - 1.1.5 * Add colortab executable to rubygem binary config
2013-03-26 - 1.1.4 * Easier access to color attributes via color(123) or
approximate html colors like color('#336caf').
Expand Down
15 changes: 12 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ The homepage of this library is located at

== Examples

Additional to the two executables cdiff and decolor, the file
examples/example.rb in the source/gem-distribution shows how this library can
be used.
The following executables are provided with Term::ANSIColor:

* cdiff: colors a diff patch
* decolor: decolors any text file that was colored with ANSI escape sequences
* colortab: Displays a table of the 256 terminal colors with their indices and
nearest html equivalents.
* term_display: displays a ppm3 or ppm6 image file in the terminal. If the netpbm
programs are installed it can handle a lot of other image file formats.
* term_mandel: displays the mandelbrot set in the terminal

Additionally the file examples/example.rb in the source/gem-distribution shows
how this library can be used.

== Author

Expand Down
7 changes: 5 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ GemHadar do
description 'This library uses ANSI escape sequences to control the attributes of terminal output'
licenses << 'GPL-2'
test_dir 'tests'
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', 'coverage'
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', 'coverage', 'tags'
readme 'README.rdoc'
executables << 'cdiff' << 'decolor' << 'colortab'
executables << 'cdiff' << 'decolor' << 'colortab' << 'term_mandel' << 'term_display'

dependency 'tins', '~>0.8'
development_dependency 'simplecov'

post_install_message File.read(File.join(File.dirname(__FILE__), 'examples/smiley.txt'))

install_library do
destdir = "#{ENV['DESTDIR']}"
libdir = CONFIG["sitelibdir"]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.5
1.2.0
90 changes: 90 additions & 0 deletions bin/term_display
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env ruby

require 'tempfile'
require 'term/ansicolor'
require 'tins/xt'
include Tins::GO

def provide_ppm_file(filename, opts)
pnmtopnm = `which pnmtopnm`
if pnmtopnm.empty?
STDERR.puts "You might consider installing netpbm to enable image conversion/scaling!"
File.new(filename, 'rb')
else
cmd = [ pnmtopnm.chomp! ]
ext = File.extname(filename)
scale = `which pamscale`.chomp!
console_scale = "#{scale} -#{opts['s']} #{opts['C']} #{opts['R']}"
cmd.unshift console_scale
aspect_scaling = "#{scale} -xscale #{opts['a']}"
cmd.unshift aspect_scaling
convert_to_pnm =
case ext
when /\A(\.ppm\d*|\.pnm|\z)/
''
when /\A.(jpe?g|exif|jfif)\z/i
'jpegtopnm'
when /\A.tiff?\z/i
'tifftopnm'
else
"#{ext[1..-1].downcase}topnm"
end
unless convert_to_pnm.empty?
convert_to_pnm = `which #{convert_to_pnm}`.chomp!
convert_to_pnm.empty? and fail "unknown pnm converter #{convert_to_pnm}"
cmd.unshift convert_to_pnm
end
temp = Tempfile.open(File.basename($0))
cmd *= '|'
STDERR.puts "Executing #{cmd.inspect}"
IO.popen(cmd, 'r+') do |converter|
File.open(filename, 'rb') do |input|
until input.eof?
converter.write input.read(8192)
end
converter.close_write
end
until converter.eof?
temp.write(converter.read)
end
end
temp.tap(&:rewind)
end
end

def usage(rc = 0)
puts to <<-end
Usage: #$0 [OPTIONS] FILENAME"
Options are
-m METRIC for distance (METRIC = #{Term::ANSIColor::RGBColorMetrics.metrics * '|'})
-g yes|no use/don't use gray values, defaults to yes
-s xyfit|xyfill scaling strategy, defaults to xyfit
-a ASPECT x:y aspect, defaults to 2.2
-C COLS number of columns for rendering with aspect, defaults to max
-R ROWS number of rows for rendering with aspect, defaults to max - 1
-h this help
end
exit rc
end

opts = go 'hm:g:s:a:C:R:'
opts['h'] and usage
filename = ARGV.shift or usage 1
metric = Term::ANSIColor::RGBColorMetrics.metric(opts['m'] || 'CIELab')
gray = (opts['g'] || 'yes') == 'yes'
opts['s'] ||= 'xyfit'
opts['a'] ||= '2.2'
opts['C'] ||= Tins::Terminal.cols
opts['R'] ||= [ Tins::Terminal.rows - 1, 0 ].max

file = provide_ppm_file(filename, opts)
ppm = Term::ANSIColor::PPMReader.new(
file,
:metric => metric,
:gray => gray
)

puts ppm
52 changes: 52 additions & 0 deletions bin/term_mandel
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env ruby

require 'term/ansicolor'
require 'tins/xt'
require "complex"
include Tins::GO

@width, @height = Tins::Terminal.cols, Tins::Terminal.lines

def color_random
(1..3).map { rand(255) }
end

def draw_set(rx, ry)
sx = (rx.end - rx.begin).abs / @width
sy = (ry.end - ry.begin).abs / @height

ac = Term::ANSIColor
color =
ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
ac::Attribute[ color_random ].gradient_to(ac::Attribute['#000'], :steps => 16)
iters = color.size - 2

text = ''
for j in 0...@height
for i in 0...@width
n, z_n = 0, Complex(0, 0)
c = Complex(sx * i + rx.begin, sy * j + ry.begin)
while n <= iters
break if z_n.abs > 2
z_n = z_n ** 2 + c
n += 1
end
text << ac.on_color(color[n]) << ' '
end
text << ac.reset << "\n"
end
puts text
end

opts = go 'x:y:'

rx = opts['x'].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-2.0..1.0)
ry = opts['y'].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-1.0..1.0)

draw_set rx, ry
Binary file added examples/ColorTest.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Mona_Lisa.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Stilleben.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions examples/lambda-red-plain.ppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
P3
44 22
255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
249 242 242 236 209 209 230 188 188 229 182 182 229 182 182 230 188 187 235 207 207 247 238 238
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 246 234 234 215 137 136
203 34 30 206 30 24 206 29 24 206 28 24 207 29 24 206 28 24 206 28 24 203 30 25
213 126 125 244 229 229 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 247 237 238 202 67 64 206 28 24
205 29 24 202 37 32 207 68 65 207 96 95 208 91 89 204 52 49 203 28 23 206 28 24
206 28 24 202 51 47 234 207 207 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 227 184 184 205 29 24 212 124 123
241 221 221 253 250 250 255 255 255 255 255 255 255 255 255 255 254 254 249 239 239 230 194 194
206 82 80 206 28 24 202 36 32 234 207 207 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 243 230 230 234 204 203 250 245 245
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
250 244 244 209 118 117 206 29 24 202 47 43 242 223 223 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 250 245 245 206 98 97 206 28 24 202 74 72 247 237 238 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 246 234 234 201 41 37 206 28 24 206 105 104 252 247 247 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 254 251 252 215 146 146 204 28 24 206 28 24 206 29 24 213 137 136 255 253 253
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
252 248 249 212 133 132 205 28 24 206 28 24 206 29 24 206 29 24 204 29 24 221 169 168
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 250 245 245
209 118 117 205 28 24 206 29 24 206 28 24 206 28 24 206 28 24 206 28 24 202 29 25
231 198 198 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 249 240 240 207 104 103
206 29 24 206 28 24 206 29 24 206 28 24 206 28 24 206 28 24 205 32 27 206 29 24
202 44 40 240 219 219 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 246 236 236 206 90 89 206 28 24
206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 105 103 243 228 229 204 92 91
206 28 24 202 71 68 246 235 235 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 244 229 229 203 77 75 206 28 24 206 28 24
206 28 24 206 29 24 206 28 24 205 30 24 210 120 118 250 245 245 255 255 255 245 233 233
202 64 61 206 28 24 206 101 100 251 246 247 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 241 222 221 203 64 62 206 28 24 206 28 24 206 28 24
206 28 24 206 29 24 205 29 24 213 135 134 252 249 249 255 255 255 255 255 255 255 255 255
239 216 215 202 38 34 206 30 24 212 132 130 255 253 253 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 237 212 212 202 53 49 206 28 24 206 28 24 206 28 24 206 28 24
206 28 24 204 29 24 217 152 151 254 252 252 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 229 192 192 202 29 25 204 29 24 220 164 164 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 233 203 203 203 41 36 206 29 24 205 28 24 206 28 24 206 28 24 206 28 24
204 29 25 221 167 167 255 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 219 162 162 205 29 24 202 30 25 230 194 194 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
229 191 191 202 32 28 206 29 24 206 28 24 206 29 24 206 28 24 206 29 24 203 31 26
227 182 182 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 254 252 253 212 131 130 206 30 24 203 41 35 236 211 211 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 252 250 250
249 243 243 251 247 247 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 224 177 177
203 29 25 206 28 24 206 28 24 206 28 24 206 28 24 206 29 24 203 38 34 232 197 197
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 251 246 246 205 100 98 206 29 24 203 42 38 229 192 191
255 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 252 252 214 141 140
203 26 22 206 130 130 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 254 220 163 163 204 29 24
206 28 24 206 28 24 206 28 24 206 28 24 206 29 24 203 49 46 236 209 209 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 247 235 235 202 69 67 206 29 24 205 29 24
209 96 94 224 166 165 232 190 190 233 203 203 233 195 194 225 170 170 209 95 93 205 29 24
206 28 24 213 147 147 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 254 252 252 216 149 148 205 28 24 206 29 24
206 28 24 206 28 24 206 28 24 206 28 24 202 62 59 239 219 219 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 243 226 226 203 72 69 206 29 24
206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 28 24 206 29 24
204 79 76 246 233 234 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 252 250 250 209 137 136 202 33 29 203 33 29 203 33 28
203 33 28 203 33 28 203 34 28 203 78 76 243 228 228 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 249 241 241 223 164 163
205 75 73 203 29 24 205 28 24 206 28 24 206 28 24 203 30 25 208 90 88 224 174 173
250 244 244 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 248 241 241 243 228 228 242 227 226 243 228 228 249 244 244 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255
Binary file added examples/lambda-red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c6f8a9d

Please sign in to comment.