Sha256: d4797894b602159a76c96b4dca3c57d4113a3dd8444d2c3031d6a9336ee750ca

Contents?: true

Size: 1.4 KB

Versions: 15

Compression:

Stored size: 1.4 KB

Contents

module Galakei
  module Spacer # :nodoc:

    GIF_DATA_PREFIX = ['47494638396101000100f70000'].pack('H*')
    GIF_DATA_POSTFIX = ['f'*1530 + '2c000000000100010000080400010404003b'].pack('H*')

    NAMED_COLORS = {
      'maroon' => '800000',
      'red' => 'ff0000',
      'orange' => 'ffA500',
      'yellow' => 'ffff00',
      'olive' => '808000',
      'purple' => '800080',
      'fuchsia' => 'ff00ff',
      'white' => 'ffffff',
      'lime' => '00ff00',
      'green' => '008000',
      'navy' => '000080',
      'blue' => '0000ff',
      'aqua' => '00ffff',
      'teal' => '008080',
      'black' => '000000',
      'silver' => 'c0c0c0',
      'gray' =>'808080',
      'black' => '000000' }

    def self.create_gif(hex_color)
      raise "invalid color '#{hex_color}'" unless /[0-9a-f]{6}/i =~ hex_color
      GIF_DATA_PREFIX + [hex_color].pack('H*') + GIF_DATA_POSTFIX
    end

    def self.img_tag(options = {})
      width = options[:width] || '100%'
      height = options[:height] || 1
      hex_color = color2hex(options[:color])
      "<img src='/galakei/spacer/#{hex_color}' width='#{width}' height='#{height}'>"
    end

    def self.color2hex(color)
      hex_color = NAMED_COLORS[color] || color.gsub('#','').downcase
      hex_color = hex_color.sub(/(.)(.)(.)/, '\1\1\2\2\3\3') if hex_color.size == 3
      raise "invalid color '#{color}'" unless /[0-9a-f]{6}/i =~ hex_color
      hex_color
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
galakei-0.15.2 lib/galakei/spacer.rb
galakei-0.15.1 lib/galakei/spacer.rb
galakei-0.15.0 lib/galakei/spacer.rb
galakei-0.14.4 lib/galakei/spacer.rb
galakei-0.14.3 lib/galakei/spacer.rb
galakei-0.14.2 lib/galakei/spacer.rb
galakei-0.14.1 lib/galakei/spacer.rb
galakei-0.14.0 lib/galakei/spacer.rb
galakei-0.13.7 lib/galakei/spacer.rb
galakei-0.13.6 lib/galakei/spacer.rb
galakei-0.13.5 lib/galakei/spacer.rb
galakei-0.13.4 lib/galakei/spacer.rb
galakei-0.13.3 lib/galakei/spacer.rb
galakei-0.13.2 lib/galakei/spacer.rb
galakei-0.13.1 lib/galakei/spacer.rb