# File lib/highline/style.rb, line 85 85: def self.ansi_rgb_to_hex(ansi_number) 86: raise "Invalid ANSI rgb code #{ansi_number}" unless (16..231).include?(ansi_number) 87: parts = (ansi_number-16).to_s(6).rjust(3,'0').scan(/./).map{|d| (d.to_i*255.0/6.0).ceil} 88: rgb_hex(*parts) 89: end
# File lib/highline/style.rb, line 95 95: def self.code_index 96: @@code_index ||= {} 97: end
# File lib/highline/style.rb, line 45 45: def self.index(style) 46: if style.name 47: @@styles ||= {} 48: @@styles[style.name] = style 49: end 50: if !style.list 51: @@code_index ||= {} 52: @@code_index[style.code] ||= [] 53: @@code_index[style.code].reject!{|indexed_style| indexed_style.name == style.name} 54: @@code_index[style.code] << style 55: end 56: style 57: end
# File lib/highline/style.rb, line 91 91: def self.list 92: @@styles ||= {} 93: end
Single color/styles have :name, :code, :rgb (possibly), :builtin Compound styles have :name, :list, :builtin
# File lib/highline/style.rb, line 108 108: def initialize(defn = {}) 109: @definition = defn 110: @name = defn[:name] 111: @code = defn[:code] 112: @rgb = defn[:rgb] 113: @list = defn[:list] 114: @builtin = defn[:builtin] 115: if @rgb 116: hex = self.class.rgb_hex(@rgb) 117: @name ||= 'rgb_' + hex 118: elsif @list 119: @name ||= @list 120: end 121: self.class.index self unless defn[:no_index] 122: end
# File lib/highline/style.rb, line 69 69: def self.rgb(*colors) 70: hex = rgb_hex(*colors) 71: name = ('rgb_' + hex).to_sym 72: if style = list[name] 73: style 74: else 75: parts = rgb_parts(hex) 76: new(:name=>name, :code=>"\e[38;5;#{rgb_number(parts)}m", :rgb=>parts) 77: end 78: end
# File lib/highline/style.rb, line 59 59: def self.rgb_hex(*colors) 60: colors.map do |color| 61: color.is_a?(Numeric) ? '%02x'%color : color.to_s 62: end.join 63: end
# File lib/highline/style.rb, line 80 80: def self.rgb_number(*parts) 81: parts = parts.flatten 82: 16 + parts.inject(0) {|kode, part| kode*6 + (part/256.0*6.0).floor} 83: end
# File lib/highline/style.rb, line 152 152: def blue 153: @rgb && @rgb[2] 154: end
# File lib/highline/style.rb, line 172 172: def bright 173: raise "Cannot create a bright variant of a style list (#{inspect})" if @list 174: new_name = ('bright_'+@name.to_s).to_sym 175: if style = self.class.list[new_name] 176: style 177: else 178: new_rgb = @rgb == [0,0,0] ? [128, 128, 128] : @rgb.map {|color| color==0 ? 0 : [color+128,255].min } 179: variant(new_name, :increment=>60, :rgb=>new_rgb) 180: end 181: end
# File lib/highline/style.rb, line 136 136: def code 137: if @list 138: @list.map{|element| HighLine.Style(element).code}.join 139: else 140: @code 141: end 142: end
# File lib/highline/style.rb, line 132 132: def color(string) 133: code + string + HighLine::CLEAR 134: end
# File lib/highline/style.rb, line 124 124: def dup 125: self.class.new(@definition) 126: end
# File lib/highline/style.rb, line 148 148: def green 149: @rgb && @rgb[1] 150: end
# File lib/highline/style.rb, line 167 167: def on 168: new_name = ('on_'+@name.to_s).to_sym 169: self.class.list[new_name] ||= variant(new_name, :increment=>10) 170: end
# File lib/highline/style.rb, line 144 144: def red 145: @rgb && @rgb[0] 146: end
# File lib/highline/style.rb, line 128 128: def to_hash 129: @definition 130: end
# File lib/highline/style.rb, line 156 156: def variant(new_name, options={}) 157: raise "Cannot create a variant of a style list (#{inspect})" if @list 158: new_code = options[:code] || code 159: if options[:increment] 160: raise "Unexpected code in #{inspect}" unless new_code =~ /^(.*?)(\d+)(.*)/ 161: new_code = $1 + ($2.to_i + options[:increment]).to_s + $3 162: end 163: new_rgb = options[:rgb] || @rgb 164: self.class.new(self.to_hash.merge(:name=>new_name, :code=>new_code, :rgb=>new_rgb)) 165: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.