ansi256 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -0
- data/ansi256.gemspec +1 -0
- data/lib/ansi256.rb +25 -0
- data/lib/ansi256/version.rb +1 -1
- data/test/test_ansi256.rb +55 -0
- metadata +18 -8
data/README.md
CHANGED
@@ -51,6 +51,16 @@ puts [ s.white, s.white.bold, s.white.bold.on_white ].join ' '
|
|
51
51
|
|
52
52
|
![colorize-me-16](https://github.com/junegunn/ansi256/raw/master/colorize-me-16.png)
|
53
53
|
|
54
|
+
### RGB color approximated to 256-color ANSI code
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
puts "RGB Color (RRGGBB)".fg('ff9930').bg('203366')
|
58
|
+
|
59
|
+
puts "RGB Color (R-G-B-)".fg('f90').bg('036')
|
60
|
+
|
61
|
+
puts "RGB Color (Monochrome)".fg('ef').bg('3f')
|
62
|
+
```
|
63
|
+
|
54
64
|
Nesting
|
55
65
|
-------
|
56
66
|
|
data/ansi256.gemspec
CHANGED
data/lib/ansi256.rb
CHANGED
@@ -12,6 +12,8 @@ module Ansi256
|
|
12
12
|
"\e[#{CODE[code]}m"
|
13
13
|
elsif code.is_a?(Fixnum) && (0..255).include?(code)
|
14
14
|
"\e[38;5;#{code}m"
|
15
|
+
elsif ansirgb = ansicode_for_rgb(code)
|
16
|
+
"\e[38;5;#{ansirgb}m"
|
15
17
|
else
|
16
18
|
raise ArgumentError, "Invalid color code: #{code}"
|
17
19
|
end
|
@@ -24,6 +26,8 @@ module Ansi256
|
|
24
26
|
"\e[#{CODE[code] + 10}m"
|
25
27
|
elsif code.is_a?(Fixnum) && (0..255).include?(code)
|
26
28
|
"\e[48;5;#{code}m"
|
29
|
+
elsif ansirgb = ansicode_for_rgb(code)
|
30
|
+
"\e[48;5;#{ansirgb}m"
|
27
31
|
else
|
28
32
|
raise ArgumentError, "Invalid color code: #{code}"
|
29
33
|
end
|
@@ -56,6 +60,27 @@ module Ansi256
|
|
56
60
|
"\e[#{nums.compact.join ';'}m"
|
57
61
|
end
|
58
62
|
|
63
|
+
def ansicode_for_rgb rgb
|
64
|
+
return unless rgb.is_a?(String) &&
|
65
|
+
rgb =~ /^#?([0-9a-f]+)$/i
|
66
|
+
rgb = $1
|
67
|
+
|
68
|
+
case rgb.length
|
69
|
+
when 2
|
70
|
+
m = (256 - 231) * rgb.to_i(16) / 256
|
71
|
+
return m == 0 ? 16 : (231 + m)
|
72
|
+
when 3
|
73
|
+
r, g, b = rgb.each_char.map { |e| (e * 2).to_i(16) }
|
74
|
+
when 6
|
75
|
+
r, g, b = rgb.each_char.each_slice(2).map { |e| e.join.to_i(16) }
|
76
|
+
else
|
77
|
+
return
|
78
|
+
end
|
79
|
+
|
80
|
+
r, g, b = [r, g, b].map { |e| 6 * e / 256 }
|
81
|
+
r * 36 + g * 6 + b + 16
|
82
|
+
end
|
83
|
+
|
59
84
|
def wrap str, color
|
60
85
|
current = [nil, nil, Set.new]
|
61
86
|
|
data/lib/ansi256/version.rb
CHANGED
data/test/test_ansi256.rb
CHANGED
@@ -115,6 +115,8 @@ class TestAnsi256 < MiniTest::Unit::TestCase
|
|
115
115
|
assert_raises(ArgumentError) { Ansi256.fg(300) }
|
116
116
|
assert_raises(ArgumentError) { Ansi256.bg(-1) }
|
117
117
|
assert_raises(ArgumentError) { Ansi256.bg(300) }
|
118
|
+
assert_raises(ArgumentError) { Ansi256.bg('ffaa') }
|
119
|
+
assert_raises(ArgumentError) { Ansi256.bg('ffeeffaa') }
|
118
120
|
end
|
119
121
|
|
120
122
|
def test_fg_bg_underline
|
@@ -139,4 +141,57 @@ class TestAnsi256 < MiniTest::Unit::TestCase
|
|
139
141
|
assert_equal "\e[32;48;5;200;1mWow \e[38;5;100;44;1;4mhello\e[0m\e[32;48;5;200;1m world\e[0m",
|
140
142
|
"Wow #{'hello'.fg(100).underline.on_blue} world".green.bold.bg(200)
|
141
143
|
end
|
144
|
+
|
145
|
+
def test_rgb
|
146
|
+
{
|
147
|
+
'00' => 16,
|
148
|
+
'000000' => 16,
|
149
|
+
'111' => 16,
|
150
|
+
'11' => 232,
|
151
|
+
'101010' => 16,
|
152
|
+
'1a' => 233,
|
153
|
+
'1a1a1a' => 16,
|
154
|
+
'20' => 234,
|
155
|
+
'22' => 234,
|
156
|
+
'202020' => 16,
|
157
|
+
'222222' => 16,
|
158
|
+
'222222' => 16,
|
159
|
+
'ff' => 255,
|
160
|
+
'ffffff' => 231,
|
161
|
+
'FFFFFF' => 231,
|
162
|
+
'ff0' => 226,
|
163
|
+
'ffff00' => 226,
|
164
|
+
'ff0000' => 196,
|
165
|
+
'00ff00' => 46,
|
166
|
+
'0000ff' => 21,
|
167
|
+
'ff9900' => 214,
|
168
|
+
'00ffff' => 51,
|
169
|
+
'0ff' => 51,
|
170
|
+
}.each do |rgb, ansi|
|
171
|
+
assert_equal ansi, Ansi256.fg(rgb).scan(/[0-9]+/).last.to_i
|
172
|
+
assert_equal ansi, Ansi256.bg(rgb).scan(/[0-9]+/).last.to_i
|
173
|
+
end
|
174
|
+
[:bg, :fg].each do |m|
|
175
|
+
(0..255).each do |r|
|
176
|
+
color = r.to_s(16).rjust(2, '0') * 3
|
177
|
+
print Ansi256.send(m, color, color + ' ')
|
178
|
+
end
|
179
|
+
(0..255).each do |r|
|
180
|
+
color = r.to_s(16).rjust(2, '0')
|
181
|
+
print Ansi256.send(m, color, color + ' ')
|
182
|
+
end
|
183
|
+
end
|
184
|
+
0.step(255, 20) do |r|
|
185
|
+
0.step(255, 20) do |g|
|
186
|
+
0.step(255, 20) do |b|
|
187
|
+
color = [r, g, b].map { |c| c.to_s(16).rjust(2, '0') }.join
|
188
|
+
print Ansi256.bg(color, color + ' ')
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
puts "RGB Color".fg('ff9900').bg('003366')
|
193
|
+
puts "RGB Color".fg('f90').bg('#036')
|
194
|
+
puts "RGB Color".fg('#ff9900').bg('#003366')
|
195
|
+
puts "RGB Color (Monochrome)".fg('ef').bg('3a')
|
196
|
+
end
|
142
197
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ansi256
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
description: Colorize text using 256-color ANSI codes
|
47
63
|
email:
|
48
64
|
- junegunn.c@gmail.com
|
@@ -76,18 +92,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
92
|
- - ! '>='
|
77
93
|
- !ruby/object:Gem::Version
|
78
94
|
version: '0'
|
79
|
-
segments:
|
80
|
-
- 0
|
81
|
-
hash: -3827977892707263520
|
82
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
96
|
none: false
|
84
97
|
requirements:
|
85
98
|
- - ! '>='
|
86
99
|
- !ruby/object:Gem::Version
|
87
100
|
version: '0'
|
88
|
-
segments:
|
89
|
-
- 0
|
90
|
-
hash: -3827977892707263520
|
91
101
|
requirements: []
|
92
102
|
rubyforge_project:
|
93
103
|
rubygems_version: 1.8.25
|