term-ansicolor 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +8 -0
- data/Rakefile +4 -1
- data/VERSION +1 -1
- data/bin/colortab +50 -0
- data/lib/term/ansicolor.rb +147 -83
- data/lib/term/ansicolor/attribute.rb +83 -0
- data/lib/term/ansicolor/rgb_triple.rb +91 -0
- data/lib/term/ansicolor/version.rb +2 -2
- data/term-ansicolor.gemspec +14 -10
- data/tests/ansicolor_test.rb +32 -6
- data/tests/rgb_triple_test.rb +29 -0
- data/tests/test_helper.rb +8 -0
- metadata +32 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c9a92f957cb6c294806caa9b03a2c04445d76a9
|
4
|
+
data.tar.gz: 5eac0662983ec23f174bd5d0af4687120fd49cb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e633cda3e9d3b749c7c3a5282012e0f9f0bda78c1674c8de081461b4df16719e7916191c4d7bb57eefa0817c13c7bafc6557a303fdcab8c635c7c786f8598663
|
7
|
+
data.tar.gz: ec810aa236b5ff82b942de368b30f262b82189423afb3abcbc1ade2f6f3673794b29564799008fbf4b2d753f6fe02c20534dff3b530155240f9e52f7b9e55263
|
data/CHANGES
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
2013-03-25 - 1.1.2 * Change the API: color0 - color255 to color(:color0) -
|
2
|
+
color(:color255), and on_color0 to on_color(:color0) -
|
3
|
+
on_color(:color255); the previous way caused some
|
4
|
+
failures, sorry. On the plus side you can now do
|
5
|
+
color('#4a6ab4') and get the nearest terminal color to
|
6
|
+
this html color.
|
7
|
+
* Add colortab executable to display the 255 colors and
|
8
|
+
related HTML colors.
|
1
9
|
2013-03-08 - 1.1.1 * Avoid possible conflicts with other people's attributes
|
2
10
|
methods.
|
3
11
|
* Cache attributes globally, also fix caching for frozen
|
data/Rakefile
CHANGED
@@ -10,12 +10,15 @@ GemHadar do
|
|
10
10
|
email 'flori@ping.de'
|
11
11
|
homepage "http://flori.github.com/#{name}"
|
12
12
|
summary 'Ruby library that colors strings using ANSI escape sequences'
|
13
|
-
description ''
|
13
|
+
description 'This library uses ANSI escape sequences to control the attributes of terminal output'
|
14
|
+
licenses << 'GPL-2'
|
14
15
|
test_dir 'tests'
|
15
16
|
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', 'coverage'
|
16
17
|
readme 'README.rdoc'
|
17
18
|
executables << 'cdiff' << 'decolor'
|
18
19
|
|
20
|
+
development_dependency 'simplecov'
|
21
|
+
|
19
22
|
install_library do
|
20
23
|
destdir = "#{ENV['DESTDIR']}"
|
21
24
|
libdir = CONFIG["sitelibdir"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.2
|
data/bin/colortab
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'term/ansicolor'
|
4
|
+
|
5
|
+
class String
|
6
|
+
include Term::ANSIColor
|
7
|
+
end
|
8
|
+
|
9
|
+
$colors = Term::ANSIColor::Attribute.rgb_colors.to_a
|
10
|
+
|
11
|
+
def print_color(c)
|
12
|
+
color = $colors[c]
|
13
|
+
text = [
|
14
|
+
Term::ANSIColor::Attribute.nearest_rgb_color('#000'),
|
15
|
+
Term::ANSIColor::Attribute.nearest_rgb_color('#fff'),
|
16
|
+
].max_by { |t| t.distance_to(color) }
|
17
|
+
print ("%3u #{color.rgb.html} " % c).on_color(color.name).color(text.name)
|
18
|
+
end
|
19
|
+
|
20
|
+
for c in 0..3
|
21
|
+
print_color c
|
22
|
+
end
|
23
|
+
puts
|
24
|
+
|
25
|
+
for c in 4..7
|
26
|
+
print_color c
|
27
|
+
end
|
28
|
+
puts
|
29
|
+
|
30
|
+
for c in 8..11
|
31
|
+
print_color c
|
32
|
+
end
|
33
|
+
puts
|
34
|
+
|
35
|
+
for c in 12..15
|
36
|
+
print_color c
|
37
|
+
end
|
38
|
+
puts
|
39
|
+
|
40
|
+
for c in 16..231
|
41
|
+
(c - 16) % 6 == 0 and puts
|
42
|
+
print_color c
|
43
|
+
end
|
44
|
+
puts
|
45
|
+
|
46
|
+
for c in 232..255
|
47
|
+
(c - 16) % 6 == 0 and puts
|
48
|
+
print_color c
|
49
|
+
end
|
50
|
+
puts
|
data/lib/term/ansicolor.rb
CHANGED
@@ -4,56 +4,122 @@ module Term
|
|
4
4
|
# classes.
|
5
5
|
module ANSIColor
|
6
6
|
require 'term/ansicolor/version'
|
7
|
+
require 'term/ansicolor/attribute'
|
8
|
+
require 'term/ansicolor/rgb_triple'
|
9
|
+
|
10
|
+
Attribute.set :clear , 0 # String#clear is already used to empty string in Ruby 1.9
|
11
|
+
Attribute.set :reset , 0 # synonym for :clear
|
12
|
+
Attribute.set :bold , 1
|
13
|
+
Attribute.set :dark , 2
|
14
|
+
Attribute.set :italic , 3 # not widely implemented
|
15
|
+
Attribute.set :underline , 4
|
16
|
+
Attribute.set :underscore , 4 # synonym for :underline
|
17
|
+
Attribute.set :blink , 5
|
18
|
+
Attribute.set :rapid_blink , 6 # not widely implemented
|
19
|
+
Attribute.set :negative , 7 # no reverse because of String#reverse
|
20
|
+
Attribute.set :concealed , 8
|
21
|
+
Attribute.set :strikethrough , 9 # not widely implemented
|
22
|
+
|
23
|
+
Attribute.set :black , 30
|
24
|
+
Attribute.set :red , 31
|
25
|
+
Attribute.set :green , 32
|
26
|
+
Attribute.set :yellow , 33
|
27
|
+
Attribute.set :blue , 34
|
28
|
+
Attribute.set :magenta , 35
|
29
|
+
Attribute.set :cyan , 36
|
30
|
+
Attribute.set :white , 37
|
31
|
+
|
32
|
+
Attribute.set :on_black , 40
|
33
|
+
Attribute.set :on_red , 41
|
34
|
+
Attribute.set :on_green , 42
|
35
|
+
Attribute.set :on_yellow , 43
|
36
|
+
Attribute.set :on_blue , 44
|
37
|
+
Attribute.set :on_magenta , 45
|
38
|
+
Attribute.set :on_cyan , 46
|
39
|
+
Attribute.set :on_white , 47
|
40
|
+
|
41
|
+
Attribute.set :intense_black , 90 # High intensity, aixterm (works in OS X)
|
42
|
+
Attribute.set :intense_red , 91
|
43
|
+
Attribute.set :intense_green , 92
|
44
|
+
Attribute.set :intense_yellow , 93
|
45
|
+
Attribute.set :intense_blue , 94
|
46
|
+
Attribute.set :intense_magenta , 95
|
47
|
+
Attribute.set :intense_cyan , 96
|
48
|
+
Attribute.set :intense_white , 97
|
49
|
+
|
50
|
+
Attribute.set :on_intense_black , 100 # High intensity background, aixterm (works in OS X)
|
51
|
+
Attribute.set :on_intense_red , 101
|
52
|
+
Attribute.set :on_intense_green , 102
|
53
|
+
Attribute.set :on_intense_yellow , 103
|
54
|
+
Attribute.set :on_intense_blue , 104
|
55
|
+
Attribute.set :on_intense_magenta, 105
|
56
|
+
Attribute.set :on_intense_cyan , 106
|
57
|
+
Attribute.set :on_intense_white , 107
|
58
|
+
|
59
|
+
Attribute.set :color0, 0, :html => '#000000'
|
60
|
+
Attribute.set :color1, 1, :html => '#800000'
|
61
|
+
Attribute.set :color2, 2, :html => '#808000'
|
62
|
+
Attribute.set :color3, 3, :html => '#808000'
|
63
|
+
Attribute.set :color4, 4, :html => '#000080'
|
64
|
+
Attribute.set :color5, 5, :html => '#800080'
|
65
|
+
Attribute.set :color6, 6, :html => '#008080'
|
66
|
+
Attribute.set :color7, 7, :html => '#c0c0c0'
|
67
|
+
|
68
|
+
Attribute.set :color8, 8, :html => '#808080'
|
69
|
+
Attribute.set :color9, 9, :html => '#ff0000'
|
70
|
+
Attribute.set :color10, 10, :html => '#00ff00'
|
71
|
+
Attribute.set :color11, 11, :html => '#ffff00'
|
72
|
+
Attribute.set :color12, 12, :html => '#0000ff'
|
73
|
+
Attribute.set :color13, 13, :html => '#ff00ff'
|
74
|
+
Attribute.set :color14, 14, :html => '#00ffff'
|
75
|
+
Attribute.set :color15, 15, :html => '#ffffff'
|
76
|
+
|
77
|
+
steps = [ 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff ]
|
78
|
+
|
79
|
+
for i in 16..231
|
80
|
+
red, green, blue = (i - 16).to_s(6).rjust(3, '0').each_char.map { |c| steps[c.to_i] }
|
81
|
+
Attribute.set "color#{i}", i, :red => red, :green => green, :blue => blue
|
82
|
+
end
|
83
|
+
|
84
|
+
grey = 8
|
85
|
+
for i in 232..255
|
86
|
+
Attribute.set "color#{i}", i, :red => grey, :green => grey, :blue => grey
|
87
|
+
grey += 10
|
88
|
+
end
|
89
|
+
|
90
|
+
Attribute.set :on_color0, 0, :html => '#000000'
|
91
|
+
Attribute.set :on_color1, 1, :html => '#800000'
|
92
|
+
Attribute.set :on_color2, 2, :html => '#808000'
|
93
|
+
Attribute.set :on_color3, 3, :html => '#808000'
|
94
|
+
Attribute.set :on_color4, 4, :html => '#000080'
|
95
|
+
Attribute.set :on_color5, 5, :html => '#800080'
|
96
|
+
Attribute.set :on_color6, 6, :html => '#008080'
|
97
|
+
Attribute.set :on_color7, 7, :html => '#c0c0c0'
|
98
|
+
|
99
|
+
Attribute.set :on_color8, 8, :html => '#808080'
|
100
|
+
Attribute.set :on_color9, 9, :html => '#ff0000'
|
101
|
+
Attribute.set :on_color10, 10, :html => '#00ff00'
|
102
|
+
Attribute.set :on_color11, 11, :html => '#ffff00'
|
103
|
+
Attribute.set :on_color12, 12, :html => '#0000ff'
|
104
|
+
Attribute.set :on_color13, 13, :html => '#ff00ff'
|
105
|
+
Attribute.set :on_color14, 14, :html => '#00ffff'
|
106
|
+
Attribute.set :on_color15, 15, :html => '#ffffff'
|
107
|
+
|
108
|
+
steps = [ 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff ]
|
109
|
+
|
110
|
+
for i in 16..231
|
111
|
+
red, green, blue = (i - 16).to_s(6).rjust(3, '0').each_char.map { |c| steps[c.to_i] }
|
112
|
+
Attribute.set "on_color#{i}", i, :red => red, :green => green, :blue => blue
|
113
|
+
end
|
114
|
+
|
115
|
+
grey = 8
|
116
|
+
for i in 232..255
|
117
|
+
Attribute.set "on_color#{i}", i, :red => grey, :green => grey, :blue => grey
|
118
|
+
grey += 10
|
119
|
+
end
|
7
120
|
|
8
121
|
# :stopdoc:
|
9
|
-
|
10
|
-
[ :clear , 0 ], # String#clear is already used to empty string in Ruby 1.9
|
11
|
-
[ :reset , 0 ], # synonym for :clear
|
12
|
-
[ :bold , 1 ],
|
13
|
-
[ :dark , 2 ],
|
14
|
-
[ :italic , 3 ], # not widely implemented
|
15
|
-
[ :underline , 4 ],
|
16
|
-
[ :underscore , 4 ], # synonym for :underline
|
17
|
-
[ :blink , 5 ],
|
18
|
-
[ :rapid_blink , 6 ], # not widely implemented
|
19
|
-
[ :negative , 7 ], # no reverse because of String#reverse
|
20
|
-
[ :concealed , 8 ],
|
21
|
-
[ :strikethrough , 9 ], # not widely implemented
|
22
|
-
[ :black , 30 ],
|
23
|
-
[ :red , 31 ],
|
24
|
-
[ :green , 32 ],
|
25
|
-
[ :yellow , 33 ],
|
26
|
-
[ :blue , 34 ],
|
27
|
-
[ :magenta , 35 ],
|
28
|
-
[ :cyan , 36 ],
|
29
|
-
[ :white , 37 ],
|
30
|
-
[ :on_black , 40 ],
|
31
|
-
[ :on_red , 41 ],
|
32
|
-
[ :on_green , 42 ],
|
33
|
-
[ :on_yellow , 43 ],
|
34
|
-
[ :on_blue , 44 ],
|
35
|
-
[ :on_magenta , 45 ],
|
36
|
-
[ :on_cyan , 46 ],
|
37
|
-
[ :on_white , 47 ],
|
38
|
-
[ :intense_black , 90 ], # High intensity, aixterm (works in OS X)
|
39
|
-
[ :intense_red , 91 ],
|
40
|
-
[ :intense_green , 92 ],
|
41
|
-
[ :intense_yellow , 93 ],
|
42
|
-
[ :intense_blue , 94 ],
|
43
|
-
[ :intense_magenta , 95 ],
|
44
|
-
[ :intense_cyan , 96 ],
|
45
|
-
[ :intense_white , 97 ],
|
46
|
-
[ :on_intense_black , 100 ], # High intensity background, aixterm (works in OS X)
|
47
|
-
[ :on_intense_red , 101 ],
|
48
|
-
[ :on_intense_green , 102 ],
|
49
|
-
[ :on_intense_yellow , 103 ],
|
50
|
-
[ :on_intense_blue , 104 ],
|
51
|
-
[ :on_intense_magenta , 105 ],
|
52
|
-
[ :on_intense_cyan , 106 ],
|
53
|
-
[ :on_intense_white , 107 ]
|
54
|
-
]
|
55
|
-
|
56
|
-
ATTRIBUTE_NAMES = ATTRIBUTES.transpose.first
|
122
|
+
ATTRIBUTE_NAMES = Attribute.named_attributes.map(&:name)
|
57
123
|
# :startdoc:
|
58
124
|
|
59
125
|
# Returns true if Term::ANSIColor supports the +feature+.
|
@@ -84,41 +150,15 @@ module Term
|
|
84
150
|
|
85
151
|
def self.create_color_method(color_name, color_value)
|
86
152
|
module_eval <<-EOT
|
87
|
-
def #{color_name}(string = nil)
|
88
|
-
|
89
|
-
result << "\e[#{color_value}m" if Term::ANSIColor.coloring?
|
90
|
-
if block_given?
|
91
|
-
result << yield
|
92
|
-
elsif string.respond_to?(:to_str)
|
93
|
-
result << string.to_str
|
94
|
-
elsif respond_to?(:to_str)
|
95
|
-
result << to_str
|
96
|
-
else
|
97
|
-
return result #only switch on
|
98
|
-
end
|
99
|
-
result << "\e[0m" if Term::ANSIColor.coloring?
|
100
|
-
result
|
153
|
+
def #{color_name}(string = nil, &block)
|
154
|
+
color(:#{color_name}, string, &block)
|
101
155
|
end
|
102
156
|
EOT
|
103
157
|
self
|
104
158
|
end
|
105
159
|
|
106
|
-
|
107
|
-
|
108
|
-
if color_name
|
109
|
-
::Term::ANSIColor.create_color_method(name, color_value)
|
110
|
-
return __send__(color_name, *args, &block)
|
111
|
-
end
|
112
|
-
color_name = name.to_s
|
113
|
-
if color_name =~ /\A(?:(on_)?color)(\d+)\z/
|
114
|
-
code = $1 ? 48 : 38
|
115
|
-
index = $2.to_i
|
116
|
-
if (0..255).include?(index)
|
117
|
-
::Term::ANSIColor.create_color_method($&, "#{code};5;#{index}")
|
118
|
-
return __send__(color_name, *args, &block)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
super
|
160
|
+
for attribute in Attribute.named_attributes
|
161
|
+
create_color_method(attribute.name, attribute.code)
|
122
162
|
end
|
123
163
|
|
124
164
|
module RespondTo
|
@@ -129,12 +169,12 @@ module Term
|
|
129
169
|
include RespondTo
|
130
170
|
extend RespondTo
|
131
171
|
|
132
|
-
# Regular expression that is used to scan for ANSI-
|
172
|
+
# Regular expression that is used to scan for ANSI-Attributes while
|
133
173
|
# uncoloring strings.
|
134
174
|
COLORED_REGEXP = /\e\[(?:(?:[349]|10)[0-7]|[0-9]|[34]8;5;\d{1,3})?m/
|
135
175
|
|
136
176
|
# Returns an uncolored version of the string, that is all
|
137
|
-
# ANSI-
|
177
|
+
# ANSI-Attributes are stripped from the string.
|
138
178
|
def uncolor(string = nil) # :yields:
|
139
179
|
if block_given?
|
140
180
|
yield.to_str.gsub(COLORED_REGEXP, '')
|
@@ -149,17 +189,41 @@ module Term
|
|
149
189
|
|
150
190
|
alias uncolored uncolor
|
151
191
|
|
192
|
+
# Return +string+ or the result string of the given +block+ colored with
|
193
|
+
# color +name+. If string isn't a string only the escape sequence to switch
|
194
|
+
# on the color +name+ is returned.
|
195
|
+
def color(name, string = nil, &block)
|
196
|
+
attribute = Attribute.get(name) || Attribute.nearest_rgb(name) or
|
197
|
+
raise ArgumentError, "unknown attribute #{name.inspect}"
|
198
|
+
result = ''
|
199
|
+
result << "\e[#{attribute.code}m" if Term::ANSIColor.coloring?
|
200
|
+
if block_given?
|
201
|
+
result << yield
|
202
|
+
elsif string.respond_to?(:to_str)
|
203
|
+
result << string.to_str
|
204
|
+
elsif respond_to?(:to_str)
|
205
|
+
result << to_str
|
206
|
+
else
|
207
|
+
return result #only switch on
|
208
|
+
end
|
209
|
+
result << "\e[0m" if Term::ANSIColor.coloring?
|
210
|
+
result
|
211
|
+
end
|
212
|
+
|
213
|
+
def on_color(name, string = nil, &block)
|
214
|
+
color("on_#{name}", string, &block)
|
215
|
+
end
|
216
|
+
|
152
217
|
class << self
|
153
218
|
# Returns an array of all Term::ANSIColor attributes as symbols.
|
154
219
|
def term_ansicolor_attributes
|
155
|
-
@term_ansicolor_attributes ||= Term::ANSIColor::ATTRIBUTE_NAMES
|
156
|
-
(0..255).map { |index| "color#{index}".to_sym } +
|
157
|
-
(0..255).map { |index| "on_color#{index}".to_sym }
|
220
|
+
@term_ansicolor_attributes ||= Term::ANSIColor::ATTRIBUTE_NAMES
|
158
221
|
end
|
159
222
|
|
160
223
|
alias attributes term_ansicolor_attributes
|
161
224
|
end
|
162
225
|
|
226
|
+
# Returns an array of all Term::ANSIColor attributes as symbols.
|
163
227
|
def term_ansicolor_attributes
|
164
228
|
::Term::ANSIColor.term_ansicolor_attributes
|
165
229
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Term
|
2
|
+
module ANSIColor
|
3
|
+
class Attribute
|
4
|
+
@__store__ = {}
|
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
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.get(name)
|
14
|
+
@__store__[name.to_sym]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.attributes(&block)
|
18
|
+
@__store__.each_value(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.rgb_colors(&block)
|
22
|
+
@rgb_colors ||= attributes.select(&:rgb_color?).each(&block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.named_attributes(&block)
|
26
|
+
@named_attributes ||= attributes.reject(&:rgb_color?).each(&block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.nearest_rgb_color(color)
|
30
|
+
rgb = RGBTriple[color]
|
31
|
+
rgb_colors.reject(&:background?).min_by { |c| c.distance_to(rgb) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.nearest_rgb_on_color(color)
|
35
|
+
rgb = RGBTriple[color]
|
36
|
+
rgb_colors.select(&:background?).min_by { |c| c.distance_to(rgb) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(name, code, options = {})
|
40
|
+
@name = name.to_sym
|
41
|
+
@code = code.to_s
|
42
|
+
if html = options[:html]
|
43
|
+
@rgb = RGBTriple.from_html(html)
|
44
|
+
elsif !options.empty?
|
45
|
+
@rgb = RGBTriple.from_hash(options)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
attr_reader :name
|
50
|
+
|
51
|
+
def code
|
52
|
+
if rgb_color?
|
53
|
+
background? ? "48;5;#{@code}" : "38;5;#{@code}"
|
54
|
+
else
|
55
|
+
@code
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def background?
|
60
|
+
@name.to_s.start_with?('on_')
|
61
|
+
end
|
62
|
+
|
63
|
+
attr_reader :rgb
|
64
|
+
|
65
|
+
def rgb_color?
|
66
|
+
!!@rgb
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_rgb_triple
|
70
|
+
@rgb
|
71
|
+
end
|
72
|
+
|
73
|
+
def distance_to(other)
|
74
|
+
if our_rgb = to_rgb_triple and
|
75
|
+
other.respond_to?(:to_rgb_triple) and
|
76
|
+
other_rgb = other.to_rgb_triple
|
77
|
+
then
|
78
|
+
our_rgb.distance_to other_rgb
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module Term
|
2
|
+
module ANSIColor
|
3
|
+
class RGBTriple
|
4
|
+
def self.convert_value(color)
|
5
|
+
color.nil? and raise ArgumentError, "missing color value"
|
6
|
+
color = Integer(color)
|
7
|
+
(0..0xff) === color or raise ArgumentError,
|
8
|
+
"color value #{color.inspect} not between 0 and 255"
|
9
|
+
color
|
10
|
+
end
|
11
|
+
|
12
|
+
private_class_method :convert_value
|
13
|
+
|
14
|
+
def self.from_html(html)
|
15
|
+
case html
|
16
|
+
when /\A#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\z/i
|
17
|
+
new(*$~.captures.map { |c| convert_value(c.to_i(16)) })
|
18
|
+
when /\A#([0-9a-f])([0-9a-f])([0-9a-f])\z/i
|
19
|
+
new(*$~.captures.map { |c| convert_value(c.to_i(16) << 4) })
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.from_hash(options)
|
24
|
+
new(
|
25
|
+
convert_value(options[:red]),
|
26
|
+
convert_value(options[:green]),
|
27
|
+
convert_value(options[:blue])
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.from_array(array)
|
32
|
+
new(*array)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.[](thing)
|
36
|
+
case
|
37
|
+
when thing.respond_to?(:to_rgb_triple) then thing
|
38
|
+
when thing.respond_to?(:to_ary) then RGBTriple.from_array(thing.to_ary)
|
39
|
+
when thing.respond_to?(:to_str) then RGBTriple.from_html(thing.to_str)
|
40
|
+
when thing.respond_to?(:to_hash) then RGBTriple.from_hash(thing.to_hash)
|
41
|
+
else raise ArgumentError, "cannot convert #{thing.inspect} into #{self}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialize(red, green, blue)
|
46
|
+
@values = [ red, green, blue ]
|
47
|
+
end
|
48
|
+
|
49
|
+
def red
|
50
|
+
@values[0]
|
51
|
+
end
|
52
|
+
|
53
|
+
def green
|
54
|
+
@values[1]
|
55
|
+
end
|
56
|
+
|
57
|
+
def blue
|
58
|
+
@values[2]
|
59
|
+
end
|
60
|
+
|
61
|
+
def html
|
62
|
+
s = '#'
|
63
|
+
@values.each { |c| s << '%02x' % c }
|
64
|
+
s
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_rgb_triple
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
attr_reader :values
|
72
|
+
protected :values
|
73
|
+
|
74
|
+
def to_a
|
75
|
+
@values.dup
|
76
|
+
end
|
77
|
+
|
78
|
+
def ==(other)
|
79
|
+
@values == other.values
|
80
|
+
end
|
81
|
+
|
82
|
+
def distance_to(other)
|
83
|
+
Math.sqrt(
|
84
|
+
((red - other.red) * 0.299) ** 2 +
|
85
|
+
((green - other.green) * 0.587) ** 2 +
|
86
|
+
((blue - other.blue) * 0.114) ** 2
|
87
|
+
)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Term::ANSIColor
|
2
2
|
# Term::ANSIColor version
|
3
|
-
VERSION = '1.1.
|
4
|
-
VERSION_ARRAY = VERSION.split(
|
3
|
+
VERSION = '1.1.2'
|
4
|
+
VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
|
5
5
|
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
|
6
6
|
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
|
7
7
|
VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
|
data/term-ansicolor.gemspec
CHANGED
@@ -2,32 +2,36 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "term-ansicolor"
|
5
|
-
s.version = "1.1.
|
5
|
+
s.version = "1.1.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Florian Frank"]
|
9
|
-
s.date = "2013-03-
|
10
|
-
s.description = ""
|
9
|
+
s.date = "2013-03-26"
|
10
|
+
s.description = "This library uses ANSI escape sequences to control the attributes of terminal output"
|
11
11
|
s.email = "flori@ping.de"
|
12
12
|
s.executables = ["cdiff", "decolor"]
|
13
|
-
s.extra_rdoc_files = ["README.rdoc", "lib/term/ansicolor.rb", "lib/term/ansicolor/version.rb"]
|
14
|
-
s.files = [".gitignore", ".travis.yml", "CHANGES", "COPYING", "Gemfile", "README.rdoc", "Rakefile", "VERSION", "bin/cdiff", "bin/decolor", "examples/example.rb", "lib/term/ansicolor.rb", "lib/term/ansicolor/.keep", "lib/term/ansicolor/version.rb", "term-ansicolor.gemspec", "tests/ansicolor_test.rb"]
|
13
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/term/ansicolor.rb", "lib/term/ansicolor/attribute.rb", "lib/term/ansicolor/rgb_triple.rb", "lib/term/ansicolor/version.rb"]
|
14
|
+
s.files = [".gitignore", ".travis.yml", "CHANGES", "COPYING", "Gemfile", "README.rdoc", "Rakefile", "VERSION", "bin/cdiff", "bin/colortab", "bin/decolor", "examples/example.rb", "lib/term/ansicolor.rb", "lib/term/ansicolor/.keep", "lib/term/ansicolor/attribute.rb", "lib/term/ansicolor/rgb_triple.rb", "lib/term/ansicolor/version.rb", "term-ansicolor.gemspec", "tests/ansicolor_test.rb", "tests/rgb_triple_test.rb", "tests/test_helper.rb"]
|
15
15
|
s.homepage = "http://flori.github.com/term-ansicolor"
|
16
|
+
s.licenses = ["GPL-2"]
|
16
17
|
s.rdoc_options = ["--title", "Term-ansicolor - Ruby library that colors strings using ANSI escape sequences", "--main", "README.rdoc"]
|
17
18
|
s.require_paths = ["lib"]
|
18
|
-
s.rubygems_version = "2.0.
|
19
|
+
s.rubygems_version = "2.0.3"
|
19
20
|
s.summary = "Ruby library that colors strings using ANSI escape sequences"
|
20
|
-
s.test_files = ["tests/ansicolor_test.rb"]
|
21
|
+
s.test_files = ["tests/ansicolor_test.rb", "tests/rgb_triple_test.rb", "tests/test_helper.rb"]
|
21
22
|
|
22
23
|
if s.respond_to? :specification_version then
|
23
24
|
s.specification_version = 4
|
24
25
|
|
25
26
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
-
s.add_development_dependency(%q<gem_hadar>, ["~> 0.
|
27
|
+
s.add_development_dependency(%q<gem_hadar>, ["~> 0.3.0"])
|
28
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
27
29
|
else
|
28
|
-
s.add_dependency(%q<gem_hadar>, ["~> 0.
|
30
|
+
s.add_dependency(%q<gem_hadar>, ["~> 0.3.0"])
|
31
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
29
32
|
end
|
30
33
|
else
|
31
|
-
s.add_dependency(%q<gem_hadar>, ["~> 0.
|
34
|
+
s.add_dependency(%q<gem_hadar>, ["~> 0.3.0"])
|
35
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
32
36
|
end
|
33
37
|
end
|
data/tests/ansicolor_test.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'test/unit'
|
4
|
-
require 'term/ansicolor'
|
1
|
+
require File.expand_path('test_helper', File.dirname(__FILE__))
|
5
2
|
|
6
3
|
class String
|
7
4
|
include Term::ANSIColor
|
@@ -51,6 +48,25 @@ class ANSIColorTest < Test::Unit::TestCase
|
|
51
48
|
assert_equal string_red_on_green, on_green { red { string } }
|
52
49
|
end
|
53
50
|
|
51
|
+
def test_color
|
52
|
+
assert_equal "\e[38;5;128mfoo\e[0m", Color.color(:color128, "foo")
|
53
|
+
assert_equal "\e[38;5;128mfoo\e[0m", "foo".color(:color128)
|
54
|
+
assert_equal "\e[38;5;128mfoo\e[0m", color(:color128, "foo")
|
55
|
+
assert_equal "\e[38;5;128mfoo\e[0m", Color.color(:color128) { "foo" }
|
56
|
+
assert_equal "\e[38;5;128mfoo\e[0m", "foo".color(:color128) { "foo" }
|
57
|
+
assert_equal "\e[38;5;128mfoo\e[0m", color(:color128) { "foo" }
|
58
|
+
assert_equal "\e[38;5;128mfoo\e[0m", color(:color128) + "foo" + color(:reset)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_on_color
|
62
|
+
assert_equal "\e[48;5;128mfoo\e[0m", Color.on_color(:color128, "foo")
|
63
|
+
assert_equal "\e[48;5;128mfoo\e[0m", "foo".on_color(:color128)
|
64
|
+
assert_equal "\e[48;5;128mfoo\e[0m", on_color(:color128, "foo")
|
65
|
+
assert_equal "\e[48;5;128mfoo\e[0m", Color.on_color(:color128) { "foo" }
|
66
|
+
assert_equal "\e[48;5;128mfoo\e[0m", "foo".on_color(:color128) { "foo" }
|
67
|
+
assert_equal "\e[48;5;128mfoo\e[0m", on_color(:color128) { "foo" }
|
68
|
+
assert_equal "\e[48;5;128mfoo\e[0m", on_color(:color128) + "foo" + color(:reset)
|
69
|
+
end
|
54
70
|
|
55
71
|
def test_uncolor
|
56
72
|
assert_equal string, string_red.uncolor
|
@@ -64,8 +80,8 @@ class ANSIColorTest < Test::Unit::TestCase
|
|
64
80
|
assert_equal string, uncolor { string_like_red }
|
65
81
|
assert_equal "", uncolor(Object.new)
|
66
82
|
for index in 0..255
|
67
|
-
assert_equal "foo", Color.uncolor(Color.
|
68
|
-
assert_equal "foo", Color.uncolor(Color.
|
83
|
+
assert_equal "foo", Color.uncolor(Color.color("color#{index}", "foo"))
|
84
|
+
assert_equal "foo", Color.uncolor(Color.on_color("color#{index}", "foo"))
|
69
85
|
end
|
70
86
|
end
|
71
87
|
|
@@ -99,4 +115,14 @@ class ANSIColorTest < Test::Unit::TestCase
|
|
99
115
|
assert string.frozen?
|
100
116
|
assert_equal red, string.red
|
101
117
|
end
|
118
|
+
|
119
|
+
def test_nearest_rgb_color
|
120
|
+
assert_equal Term::ANSIColor::Attribute.get(:color0).rgb, Term::ANSIColor::Attribute.nearest_rgb_color('#000').rgb
|
121
|
+
assert_equal Term::ANSIColor::Attribute.get(:color15).rgb, Term::ANSIColor::Attribute.nearest_rgb_color('#ffffff').rgb
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_nearest_rgb_color
|
125
|
+
assert_equal Term::ANSIColor::Attribute.get(:on_color0).rgb, Term::ANSIColor::Attribute.nearest_rgb_on_color('#000').rgb
|
126
|
+
assert_equal Term::ANSIColor::Attribute.get(:on_color15).rgb, Term::ANSIColor::Attribute.nearest_rgb_on_color('#ffffff').rgb
|
127
|
+
end
|
102
128
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path('test_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class RgbTripleTest < Test::Unit::TestCase
|
4
|
+
include Term::ANSIColor
|
5
|
+
|
6
|
+
def test_rgb_cast
|
7
|
+
rgb = RGBTriple.new(128, 0, 255)
|
8
|
+
assert_equal '#8000ff', RGBTriple[ rgb ].html
|
9
|
+
assert_equal '#8000ff', RGBTriple[ [ 128, 0, 255 ] ].html
|
10
|
+
assert_equal '#8000ff', RGBTriple[ :red => 128, :green => 0, :blue => 255 ].html
|
11
|
+
assert_raise ArgumentError do
|
12
|
+
RGBTriple[ nil ]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_rgb_to_a
|
17
|
+
rgb = RGBTriple.new(128, 0, 255)
|
18
|
+
assert_equal [ 128, 0, 255 ], rgb.to_a
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_rgb_distace
|
22
|
+
rgb1 = RGBTriple.new(128, 0, 255)
|
23
|
+
rgb2 = RGBTriple.new(128, 200, 64)
|
24
|
+
assert_in_delta 0.0, rgb1.distance_to(rgb1), 1e-3
|
25
|
+
assert_in_delta 170.481, RGBTriple.new(0, 0, 0).distance_to(RGBTriple.new(255, 255, 255)), 1e-3
|
26
|
+
assert_in_delta 119.402, rgb1.distance_to(rgb2), 1e-3
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: term-ansicolor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Frank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gem_hadar
|
@@ -16,15 +16,30 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.3.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
27
|
-
|
26
|
+
version: 0.3.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: simplecov
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: This library uses ANSI escape sequences to control the attributes of
|
42
|
+
terminal output
|
28
43
|
email: flori@ping.de
|
29
44
|
executables:
|
30
45
|
- cdiff
|
@@ -33,6 +48,8 @@ extensions: []
|
|
33
48
|
extra_rdoc_files:
|
34
49
|
- README.rdoc
|
35
50
|
- lib/term/ansicolor.rb
|
51
|
+
- lib/term/ansicolor/attribute.rb
|
52
|
+
- lib/term/ansicolor/rgb_triple.rb
|
36
53
|
- lib/term/ansicolor/version.rb
|
37
54
|
files:
|
38
55
|
- .gitignore
|
@@ -44,15 +61,21 @@ files:
|
|
44
61
|
- Rakefile
|
45
62
|
- VERSION
|
46
63
|
- bin/cdiff
|
64
|
+
- bin/colortab
|
47
65
|
- bin/decolor
|
48
66
|
- examples/example.rb
|
49
67
|
- lib/term/ansicolor.rb
|
50
68
|
- lib/term/ansicolor/.keep
|
69
|
+
- lib/term/ansicolor/attribute.rb
|
70
|
+
- lib/term/ansicolor/rgb_triple.rb
|
51
71
|
- lib/term/ansicolor/version.rb
|
52
72
|
- term-ansicolor.gemspec
|
53
73
|
- tests/ansicolor_test.rb
|
74
|
+
- tests/rgb_triple_test.rb
|
75
|
+
- tests/test_helper.rb
|
54
76
|
homepage: http://flori.github.com/term-ansicolor
|
55
|
-
licenses:
|
77
|
+
licenses:
|
78
|
+
- GPL-2
|
56
79
|
metadata: {}
|
57
80
|
post_install_message:
|
58
81
|
rdoc_options:
|
@@ -74,9 +97,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
97
|
version: '0'
|
75
98
|
requirements: []
|
76
99
|
rubyforge_project:
|
77
|
-
rubygems_version: 2.0.
|
100
|
+
rubygems_version: 2.0.3
|
78
101
|
signing_key:
|
79
102
|
specification_version: 4
|
80
103
|
summary: Ruby library that colors strings using ANSI escape sequences
|
81
104
|
test_files:
|
82
105
|
- tests/ansicolor_test.rb
|
106
|
+
- tests/rgb_triple_test.rb
|
107
|
+
- tests/test_helper.rb
|