# File lib/highline/style.rb, line 85 def self.ansi_rgb_to_hex(ansi_number) raise "Invalid ANSI rgb code #{ansi_number}" unless (16..231).include?(ansi_number) parts = (ansi_number-16).to_s(6).rjust(3,'0').scan(/./).map{|d| (d.to_i*255.0/6.0).ceil} rgb_hex(*parts) end
# File lib/highline/style.rb, line 95 def self.code_index @@code_index ||= {} end
# File lib/highline/style.rb, line 45 def self.index(style) if style.name @@styles ||= {} @@styles[style.name] = style end if !style.list @@code_index ||= {} @@code_index[style.code] ||= [] @@code_index[style.code].reject!{|indexed_style| indexed_style.name == style.name} @@code_index[style.code] << style end style end
# File lib/highline/style.rb, line 91 def self.list @@styles ||= {} end
Single color/styles have :name, :code, :rgb (possibly), :builtin Compound styles have :name, :list, :builtin
# File lib/highline/style.rb, line 108 def initialize(defn = {}) @definition = defn @name = defn[:name] @code = defn[:code] @rgb = defn[:rgb] @list = defn[:list] @builtin = defn[:builtin] if @rgb hex = self.class.rgb_hex(@rgb) rgb = self.class.rgb_parts(hex) @name ||= 'rgb_' + hex elsif @list @name ||= @list end self.class.index self unless defn[:no_index] end
# File lib/highline/style.rb, line 69 def self.rgb(*colors) hex = rgb_hex(*colors) name = ('rgb_' + hex).to_sym if style = list[name] style else parts = rgb_parts(hex) new(:name=>name, :code=>"\e[38;5;#{rgb_number(parts)}m", :rgb=>parts) end end
# File lib/highline/style.rb, line 59 def self.rgb_hex(*colors) colors.map do |color| color.is_a?(Numeric) ? '%02x'%color : color.to_s end.join end
# File lib/highline/style.rb, line 80 def self.rgb_number(*parts) parts = parts.flatten 16 + parts.inject(0) {|kode, part| kode*6 + (part/256.0*6.0).floor} end
# File lib/highline/style.rb, line 173 def bright raise "Cannot create a bright variant of a style list (#{inspect})" if @list new_name = ('bright_'+@name.to_s).to_sym if style = self.class.list[new_name] style else new_rgb = @rgb == [0,0,0] ? [128, 128, 128] : @rgb.map {|color| color==0 ? 0 : [color+128,255].min } variant(new_name, :increment=>60, :rgb=>new_rgb) end end
# File lib/highline/style.rb, line 133 def color(string) code + string + HighLine::CLEAR end
# File lib/highline/style.rb, line 125 def dup self.class.new(@definition) end
# File lib/highline/style.rb, line 168 def on new_name = ('on_'+@name.to_s).to_sym self.class.list[new_name] ||= variant(new_name, :increment=>10) end
# File lib/highline/style.rb, line 157 def variant(new_name, options={}) raise "Cannot create a variant of a style list (#{inspect})" if @list new_code = options[:code] || code if options[:increment] raise "Unexpected code in #{inspect}" unless new_code =~ /^(.*?)(\d+)(.*)/ new_code = $1 + ($2.to_i + options[:increment]).to_s + $3 end new_rgb = options[:rgb] || @rgb new_style = self.class.new(self.to_hash.merge(:name=>new_name, :code=>new_code, :rgb=>new_rgb)) end
Generated with the Darkfish Rdoc Generator 2.