| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
require 'term/ansicolor' |
| 4 |
|
| 5 |
class String |
| 6 |
include Term::ANSIColor |
| 7 |
end |
| 8 |
|
| 9 |
def print_color(c) |
| 10 |
color = Term::ANSIColor::Attribute[c] |
| 11 |
text = [ |
| 12 |
Term::ANSIColor::Attribute.nearest_rgb_color('#000'), |
| 13 |
Term::ANSIColor::Attribute.nearest_rgb_color('#fff'), |
| 14 |
].max_by { |t| t.distance_to(color) } |
| 15 |
print ("%3u #{color.rgb.html} " % c).on_color(color.name).color(text.name) |
| 16 |
end |
| 17 |
|
| 18 |
if Term::ANSIColor.true_coloring = ENV['COLORTERM'] =~ /\A(truecolor|24bit)\z/ |
| 19 |
|
| 20 |
puts "True colors".bold, "" |
| 21 |
step = 36 |
| 22 |
(0..255).step(step) do |g| |
| 23 |
(0..255).step(step) do |r| |
| 24 |
(0..255).step(step) do |b| |
| 25 |
print Term::ANSIColor.on_color(Term::ANSIColor::RGBTriple.new(r, g, b)) { ' ' } |
| 26 |
end |
| 27 |
print ' ' |
| 28 |
end |
| 29 |
puts |
| 30 |
end |
| 31 |
|
| 32 |
puts |
| 33 |
(0..255).step(4) do |g| |
| 34 |
print Term::ANSIColor.on_color(Term::ANSIColor::RGBTriple.new(g, g, g)) { ' ' } |
| 35 |
end |
| 36 |
puts |
| 37 |
puts |
| 38 |
|
| 39 |
end |
| 40 |
|
| 41 |
Term::ANSIColor.true_coloring = false |
| 42 |
|
| 43 |
puts "256 colors".bold, "" |
| 44 |
for c in 0..3 |
| 45 |
print_color c |
| 46 |
end |
| 47 |
puts |
| 48 |
|
| 49 |
for c in 4..7 |
| 50 |
print_color c |
| 51 |
end |
| 52 |
puts |
| 53 |
puts |
| 54 |
|
| 55 |
for c in 8..11 |
| 56 |
print_color c |
| 57 |
end |
| 58 |
puts |
| 59 |
|
| 60 |
for c in 12..15 |
| 61 |
print_color c |
| 62 |
end |
| 63 |
|
| 64 |
for c in 16..231 |
| 65 |
(c - 16) % 6 == 0 and puts |
| 66 |
(c - 16) % 36 == 0 and puts |
| 67 |
print_color c |
| 68 |
end |
| 69 |
puts |
| 70 |
|
| 71 |
for c in 232..255 |
| 72 |
(c - 16) % 6 == 0 and puts |
| 73 |
print_color c |
| 74 |
end |
| 75 |
puts |