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/intense_color8.rb
Defined Under Namespace
Classes: Color256, Color8, IntenseColor8, Text
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, code, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/term/ansicolor/attribute.rb', line 73
def initialize(name, code, options = {})
@name = name.to_sym
@code = code.to_s
if html = options[:html]
@rgb = RGBTriple.from_html(html)
elsif !options.empty?
@rgb = RGBTriple.from_hash(options)
else
@rgb = nil end
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
85
86
87
|
# File 'lib/term/ansicolor/attribute.rb', line 85
def name
@name
end
|
#rgb ⇒ Object
Returns the value of attribute rgb.
103
104
105
|
# File 'lib/term/ansicolor/attribute.rb', line 103
def rgb
@rgb
end
|
Class Method Details
.[](name) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/term/ansicolor/attribute.rb', line 33
def self.[](name)
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
|
.attributes(&block) ⇒ Object
17
18
19
|
# File 'lib/term/ansicolor/attribute.rb', line 17
def self.attributes(&block)
@__order__.map { |name| @__store__[name] }
end
|
.get(name) ⇒ Object
45
46
47
|
# File 'lib/term/ansicolor/attribute.rb', line 45
def self.get(name)
@__store__[name.to_sym]
end
|
.named_attributes(&block) ⇒ Object
57
58
59
|
# File 'lib/term/ansicolor/attribute.rb', line 57
def self.named_attributes(&block)
@named_attributes ||= attributes.reject(&:rgb_color?).each(&block)
end
|
.nearest_rgb_color(color, options = {}) ⇒ Object
61
62
63
64
65
|
# File 'lib/term/ansicolor/attribute.rb', line 61
def self.nearest_rgb_color(color, options = {})
rgb = RGBTriple[color]
colors = rgb_colors(options)
colors.reject(&:background?).min_by { |c| c.distance_to(rgb, options) }
end
|
.nearest_rgb_on_color(color, options = {}) ⇒ Object
67
68
69
70
71
|
# File 'lib/term/ansicolor/attribute.rb', line 67
def self.nearest_rgb_on_color(color, options = {})
rgb = RGBTriple[color]
colors = rgb_colors(options)
colors.select(&:background?).min_by { |c| c.distance_to(rgb, options) }
end
|
.rgb_colors(options = {}, &block) ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/term/ansicolor/attribute.rb', line 49
def self.rgb_colors(options = {}, &block)
colors = @rgb_colors ||= attributes.select(&:rgb_color?)
if options.key?(:gray) && !options[:gray]
colors = colors.reject(&:gray?)
end
colors.each(&block)
end
|
.set(name, code, options = {}) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/term/ansicolor/attribute.rb', line 9
def self.set(name, code, options = {})
name = name.to_sym
result = @__store__[name] = new(name, code, options)
@__order__ << name
@rgb_colors = nil
result
end
|
Instance Method Details
#apply(string = nil, &block) ⇒ Object
95
96
97
|
# File 'lib/term/ansicolor/attribute.rb', line 95
def apply(string = nil, &block)
::Term::ANSIColor.color(self, string, &block)
end
|
#background? ⇒ Boolean
99
100
101
|
# File 'lib/term/ansicolor/attribute.rb', line 99
def background?
@name.to_s.start_with?('on_')
end
|
#code ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/term/ansicolor/attribute.rb', line 87
def code
if rgb_color?
background? ? "48;5;#{@code}" : "38;5;#{@code}"
else
@code
end
end
|
#distance_to(other, options = {}) ⇒ Object
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/term/ansicolor/attribute.rb', line 117
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
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/term/ansicolor/attribute.rb', line 128
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
our_rgb.gradient_to(other_rgb, options).map do |rgb_triple|
self.class.nearest_rgb_color(rgb_triple, options)
end
else
[]
end
end
|
#gray? ⇒ Boolean
109
110
111
|
# File 'lib/term/ansicolor/attribute.rb', line 109
def gray?
rgb_color? && to_rgb_triple.gray?
end
|
#rgb_color? ⇒ Boolean
105
106
107
|
# File 'lib/term/ansicolor/attribute.rb', line 105
def rgb_color?
!!@rgb
end
|
#to_rgb_triple ⇒ Object
113
114
115
|
# File 'lib/term/ansicolor/attribute.rb', line 113
def to_rgb_triple
@rgb
end
|