Class: Term::ANSIColor::Attribute
- Inherits:
-
Object
- Object
- Term::ANSIColor::Attribute
show all
- Defined in:
- lib/term/ansicolor/attribute.rb,
lib/term/ansicolor/attribute/text.rb,
lib/term/ansicolor/attribute/color8.rb,
lib/term/ansicolor/attribute/color256.rb,
lib/term/ansicolor/attribute/underline.rb,
lib/term/ansicolor/attribute/intense_color8.rb
Defined Under Namespace
Modules: Underline
Classes: Color256, Color8, IntenseColor8, Text
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.[](name, true_coloring: false) ⇒ Object
-
.attributes(&block) ⇒ Object
-
.get(name) ⇒ Object
-
.named_attributes(&block) ⇒ Object
-
.nearest_rgb_color(color, options = {}) ⇒ Object
-
.nearest_rgb_on_color(color, options = {}) ⇒ Object
-
.on_true_color(color, options = {}) ⇒ Object
-
.rgb_background_colors(options = {}, &block) ⇒ Object
-
.rgb_colors(options = {}, &block) ⇒ Object
-
.rgb_foreground_colors(options = {}, &block) ⇒ Object
-
.set(name, code, **options) ⇒ Object
-
.true_color(color, options = {}) ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(name, code, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/term/ansicolor/attribute.rb', line 94
def initialize(name, code, options = {})
@name = name.to_sym
@background = !!options[:background]
@code = code.to_s
@direct = false
@true_color = false
if rgb = options[:true_color]
@true_color = true
@rgb = rgb
elsif rgb = options[:direct]
@direct = true
@rgb = RGBTriple.from_html(rgb)
elsif html = options[:html]
@rgb = RGBTriple.from_html(html)
elsif options.slice(:red, :green, :blue).size == 3
@rgb = RGBTriple.from_hash(options)
else
@rgb = nil end
end
|
Instance Attribute Details
#background=(value) ⇒ Object
Sets the attribute background
141
142
143
|
# File 'lib/term/ansicolor/attribute.rb', line 141
def background=(value)
@background = value
end
|
#name ⇒ Object
Returns the value of attribute name.
115
116
117
|
# File 'lib/term/ansicolor/attribute.rb', line 115
def name
@name
end
|
#rgb ⇒ Object
Returns the value of attribute rgb.
143
144
145
|
# File 'lib/term/ansicolor/attribute.rb', line 143
def rgb
@rgb
end
|
Class Method Details
.[](name, true_coloring: false) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/term/ansicolor/attribute.rb', line 23
def self.[](name, true_coloring: false)
true_coloring ||= Term::ANSIColor.true_coloring?
if true_coloring
case
when self === name then name
when Array === name then true_color name
when name.respond_to?(:to_rgb_triple) then true_color(name.to_rgb_triple.to_a)
when name.to_s =~ /\A(on_)?(\d+)\z/ then get "#$1color#$2"
when name.to_s =~ /\A#([0-9a-f]{3}){1,2}\z/i then true_color name
when name.to_s =~ /\Aon_#([0-9a-f]{3}){1,2}\z/i then on_true_color name
else get name
end
else
case
when self === name then name
when Array === name then nearest_rgb_color name
when name.respond_to?(:to_rgb_triple) then nearest_rgb_color(name.to_rgb_triple.to_a)
when name.to_s =~ /\A(on_)?(\d+)\z/ then get "#$1color#$2"
when name.to_s =~ /\A#([0-9a-f]{3}){1,2}\z/i then nearest_rgb_color name
when name.to_s =~ /\Aon_#([0-9a-f]{3}){1,2}\z/i then nearest_rgb_on_color name
else get name
end
end
end
|
.attributes(&block) ⇒ Object
19
20
21
|
# File 'lib/term/ansicolor/attribute.rb', line 19
def self.attributes(&block)
@__store__.each_value(&block)
end
|
.get(name) ⇒ Object
48
49
50
|
# File 'lib/term/ansicolor/attribute.rb', line 48
def self.get(name)
@__store__[name.to_sym]
end
|
.named_attributes(&block) ⇒ Object
70
71
72
|
# File 'lib/term/ansicolor/attribute.rb', line 70
def self.named_attributes(&block)
@named_attributes ||= attributes.reject(&:rgb_color?).each(&block)
end
|
.nearest_rgb_color(color, options = {}) ⇒ Object
74
75
76
77
|
# File 'lib/term/ansicolor/attribute.rb', line 74
def self.nearest_rgb_color(color, options = {})
rgb = RGBTriple[color]
rgb_foreground_colors(options).min_by { |c| c.distance_to(rgb, options) }
end
|
.nearest_rgb_on_color(color, options = {}) ⇒ Object
79
80
81
82
|
# File 'lib/term/ansicolor/attribute.rb', line 79
def self.nearest_rgb_on_color(color, options = {})
rgb = RGBTriple[color]
rgb_background_colors(options).min_by { |c| c.distance_to(rgb, options) }
end
|
.on_true_color(color, options = {}) ⇒ Object
89
90
91
92
|
# File 'lib/term/ansicolor/attribute.rb', line 89
def self.on_true_color(color, options = {})
rgb = RGBTriple[color]
new(:on_true, "", { true_color: rgb, background: true })
end
|
.rgb_background_colors(options = {}, &block) ⇒ Object
65
66
67
|
# File 'lib/term/ansicolor/attribute.rb', line 65
def rgb_background_colors(options = {}, &block)
rgb_colors(options).select(&:background?).each(&block)
end
|
.rgb_colors(options = {}, &block) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/term/ansicolor/attribute.rb', line 53
def rgb_colors(options = {}, &block)
colors = attributes.select(&:rgb_color?)
if options.key?(:gray) && !options[:gray]
colors = colors.reject(&:gray?)
end
colors.each(&block)
end
|
.rgb_foreground_colors(options = {}, &block) ⇒ Object
61
62
63
|
# File 'lib/term/ansicolor/attribute.rb', line 61
def rgb_foreground_colors(options = {}, &block)
rgb_colors(options).reject(&:background?).each(&block)
end
|
.set(name, code, **options) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/term/ansicolor/attribute.rb', line 6
def self.set(name, code, **options)
name = name.to_sym
result = @__store__[name] = new(name, code, options)
unless options[:skip_definition]
::Term::ANSIColor.class_eval do
define_method(name) do |string = nil, &block|
apply_attribute(name, string, &block)
end
end
end
result
end
|
.true_color(color, options = {}) ⇒ Object
84
85
86
87
|
# File 'lib/term/ansicolor/attribute.rb', line 84
def self.true_color(color, options = {})
rgb = RGBTriple[color]
new(:true, "", { true_color: rgb, background: false })
end
|
Instance Method Details
#apply(string = nil, &block) ⇒ Object
129
130
131
|
# File 'lib/term/ansicolor/attribute.rb', line 129
def apply(string = nil, &block)
::Term::ANSIColor.apply_attribute(self, string, &block)
end
|
#background? ⇒ Boolean
133
134
135
|
# File 'lib/term/ansicolor/attribute.rb', line 133
def background?
!!@background
end
|
#code ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/term/ansicolor/attribute.rb', line 117
def code
if true_color?
background? ? "48;2;#{@rgb.to_a * ?;}" : "38;2;#{@rgb.to_a * ?;}"
elsif rgb_color?
background? ? "48;5;#{@code}" : "38;5;#{@code}"
elsif direct?
background? ? (@code.to_i + 10).to_s : @code
else
@code
end
end
|
#direct? ⇒ Boolean
137
138
139
|
# File 'lib/term/ansicolor/attribute.rb', line 137
def direct?
!!@direct
end
|
#distance_to(other, options = {}) ⇒ Object
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/term/ansicolor/attribute.rb', line 161
def distance_to(other, options = {})
if our_rgb = to_rgb_triple and
other.respond_to?(:to_rgb_triple) and
other_rgb = other.to_rgb_triple
then
our_rgb.distance_to(other_rgb, options)
else
1 / 0.0
end
end
|
#gradient_to(other, options = {}) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/term/ansicolor/attribute.rb', line 172
def gradient_to(other, options = {})
if our_rgb = to_rgb_triple and
other.respond_to?(:to_rgb_triple) and
other_rgb = other.to_rgb_triple
then
true_coloring = options[:true_coloring] || Term::ANSIColor.true_coloring?
our_rgb.gradient_to(other_rgb, options).map do |rgb_triple|
if true_coloring
self.class.true_color(rgb_triple, options)
else
self.class.nearest_rgb_color(rgb_triple, options)
end
end
else
[]
end
end
|
#gray? ⇒ Boolean
153
154
155
|
# File 'lib/term/ansicolor/attribute.rb', line 153
def gray?
rgb_color? && to_rgb_triple.gray?
end
|
#rgb_color? ⇒ Boolean
145
146
147
|
# File 'lib/term/ansicolor/attribute.rb', line 145
def rgb_color?
!!@rgb && !@true_color && !@direct
end
|
#to_rgb_triple ⇒ Object
157
158
159
|
# File 'lib/term/ansicolor/attribute.rb', line 157
def to_rgb_triple
@rgb
end
|
#true_color? ⇒ Boolean
149
150
151
|
# File 'lib/term/ansicolor/attribute.rb', line 149
def true_color?
!!(@rgb && @true_color)
end
|