Class: Termin::ANSIColor::Attribute
- Inherits:
-
Object
- Object
- Termin::ANSIColor::Attribute
- Defined in:
- lib/termin/ansicolor/attribute.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rgb ⇒ Object
readonly
Returns the value of attribute rgb.
Class Method Summary collapse
- .[](name) ⇒ Object
- .attributes(&block) ⇒ Object
- .get(name) ⇒ Object
- .named_attributes(&block) ⇒ Object
- .nearest_rgb_color(color, options = {}) ⇒ Object
- .nearest_rgb_on_color(color, options = {}) ⇒ Object
- .rgb_colors(&block) ⇒ Object
- .set(name, code, options = {}) ⇒ Object
Instance Method Summary collapse
- #apply(string = nil, &block) ⇒ Object
- #background? ⇒ Boolean
- #code ⇒ Object
- #distance_to(other, options = {}) ⇒ Object
- #gradient_to(other, options = {}) ⇒ Object
- #gray? ⇒ Boolean
-
#initialize(name, code, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #rgb_color? ⇒ Boolean
- #to_rgb_triple ⇒ Object
Constructor Details
#initialize(name, code, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/termin/ansicolor/attribute.rb', line 74 def initialize(name, code, = {}) @name = name.to_sym @code = code.to_s if html = [:html] @rgb = RGBTriple.from_html(html) elsif !.empty? @rgb = RGBTriple.from_hash() else @rgb = nil # prevent instance variable not initialized warnings end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
86 87 88 |
# File 'lib/termin/ansicolor/attribute.rb', line 86 def name @name end |
#rgb ⇒ Object (readonly)
Returns the value of attribute rgb.
104 105 106 |
# File 'lib/termin/ansicolor/attribute.rb', line 104 def rgb @rgb end |
Class Method Details
.[](name) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/termin/ansicolor/attribute.rb', line 33 def self.[](name) case when self === name then name when Array === name then nearest_rgb_color name 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/termin/ansicolor/attribute.rb', line 17 def self.attributes(&block) @__order__.map { |name| @__store__[name] } end |
.get(name) ⇒ Object
44 45 46 |
# File 'lib/termin/ansicolor/attribute.rb', line 44 def self.get(name) @__store__[name.to_sym] end |
.named_attributes(&block) ⇒ Object
52 53 54 |
# File 'lib/termin/ansicolor/attribute.rb', line 52 def self.named_attributes(&block) @named_attributes ||= attributes.reject(&:rgb_color?).each(&block) end |
.nearest_rgb_color(color, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/termin/ansicolor/attribute.rb', line 56 def self.nearest_rgb_color(color, = {}) rgb = RGBTriple[color] colors = rgb_colors if .key?(:gray) && ![:gray] colors = colors.reject(&:gray?) end colors.reject(&:background?).min_by { |c| c.distance_to(rgb, ) } end |
.nearest_rgb_on_color(color, options = {}) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/termin/ansicolor/attribute.rb', line 65 def self.nearest_rgb_on_color(color, = {}) rgb = RGBTriple[color] colors = rgb_colors if .key?(:gray) && ![:gray] colors = colors.reject(&:gray?) end colors.select(&:background?).min_by { |c| c.distance_to(rgb, ) } end |
.rgb_colors(&block) ⇒ Object
48 49 50 |
# File 'lib/termin/ansicolor/attribute.rb', line 48 def self.rgb_colors(&block) @rgb_colors ||= attributes.select(&:rgb_color?).each(&block) end |
.set(name, code, options = {}) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/termin/ansicolor/attribute.rb', line 9 def self.set(name, code, = {}) name = name.to_sym result = @__store__[name] = new(name, code, ) @__order__ << name @rgb_colors = nil result end |
Instance Method Details
#apply(string = nil, &block) ⇒ Object
96 97 98 |
# File 'lib/termin/ansicolor/attribute.rb', line 96 def apply(string = nil, &block) ::Termin::ANSIColor.color(self, string, &block) end |
#background? ⇒ Boolean
100 101 102 |
# File 'lib/termin/ansicolor/attribute.rb', line 100 def background? @name.to_s.start_with?('on_') end |
#code ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/termin/ansicolor/attribute.rb', line 88 def code if rgb_color? background? ? "48;5;#{@code}" : "38;5;#{@code}" else @code end end |
#distance_to(other, options = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/termin/ansicolor/attribute.rb', line 118 def distance_to(other, = {}) 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, ) else 1 / 0.0 end end |
#gradient_to(other, options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/termin/ansicolor/attribute.rb', line 129 def gradient_to(other, = {}) 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, ).map do |rgb_triple| self.class.nearest_rgb_color(rgb_triple, ) end else [] end end |
#gray? ⇒ Boolean
110 111 112 |
# File 'lib/termin/ansicolor/attribute.rb', line 110 def gray? rgb_color? && to_rgb_triple.gray? end |
#rgb_color? ⇒ Boolean
106 107 108 |
# File 'lib/termin/ansicolor/attribute.rb', line 106 def rgb_color? !!@rgb end |
#to_rgb_triple ⇒ Object
114 115 116 |
# File 'lib/termin/ansicolor/attribute.rb', line 114 def to_rgb_triple @rgb end |