class YAMG::Icon

ICONS

Attributes

dpi[RW]
icons[RW]
img[RW]
radius[RW]
rounded[RW]
rounded?[RW]
size[RW]
src[RW]

Public Class Methods

new(src, size, bg = nil, rounded = false, radius = 9) click to toggle source

Icon

Icon.new(src, size, rounded).image Image class Icon.new(src, size, rounded).image('.path.ext') Export image

ICO: 16/32/48

# File lib/yamg/icon.rb, line 19
def initialize(src, size, bg = nil, rounded = false, radius = 9)
  raise 'No source' if src.nil? || src.empty?
  @src = src
  @size = size
  @rounded = rounded
  @icons = YAMG.load_images(src)
  YAMG.puts_and_exit("No sources in '#{src}'") if icons.empty?
  @choosen = File.join(src, find_closest_gte_icon)
  @radius = radius || 9
  # @dpi = 90
  @bg = bg
end

Public Instance Methods

apply_background() click to toggle source
# File lib/yamg/icon.rb, line 89
def apply_background
  clone = ::MiniMagick::Image.open img.path
  clone.combine_options do |o|
    o.draw 'color 0,0 reset'
    o.fill @bg
  end
  clone.composite(img) { |i| i.compose 'Over' }
end
dimensions() click to toggle source
# File lib/yamg/icon.rb, line 46
def dimensions
  img.dimensions.join(',')
end
find_closest_gte_icon() click to toggle source
# File lib/yamg/icon.rb, line 33
def find_closest_gte_icon
  proc = ->(x) { x.tr('^0-9', '').to_i }
  return icons.max_by(&proc) if icons.map(&proc).max < size
  icons.min_by do |f|
    n = proc.call(f)
    size > n ? Float::INFINITY : n
  end
end
ico!(out) click to toggle source

ICO!

# File lib/yamg/icon.rb, line 104
def ico!(out)
  temp = ->(s) { "/tmp/#{s}-#{Thread.current.object_id}.png" }
  MiniMagick::Tool::Convert.new do |o|
    o << Icon.new(@src, 16).image(temp.call(16))
    o << Icon.new(@src, 32).image(temp.call(32))
    o << Icon.new(@src, 48).image(temp.call(48))
    o.colors 256
    o << out
  end
end
image(out = nil) click to toggle source
# File lib/yamg/icon.rb, line 115
def image(out = nil)
  return svg! if out =~ /svg$/
  return ico!(out) if out =~ /ico$/
  temp = out || "/tmp/#{@choosen.object_id}.png"
  if File.extname(@choosen) =~ /svg/
    pixels = dpi ? "-d #{dpi} -p #{dpi}" : nil
    args = "#{pixels} -w #{size} -h #{size} -f png"
    YAMG.run_rsvg(@choosen, temp, args)
    @img = MiniMagick::Image.open(temp)
    @img.format File.extname(out) if out
  else
    @img = MiniMagick::Image.open(@choosen)
    @img.format File.extname(out) if out
    @img.resize size # "NxN"
  end
  @img = apply_background if @bg
  @img = round if rounded?
  out ? write_out(out) : img
end
round() click to toggle source

Maybe this can be smaller, terminal equivalent: convert -size 512x512 xc:none -draw “roundrectangle 0,0,512,512,55,55” mask.png convert icon.png -matte mask.png -compose DstIn -composite picture_with_rounded_corners.png gist.github.com/artemave/c20e7450af866f5e7735

# File lib/yamg/icon.rb, line 59
def round
  mask = MiniMagick::Image.open(img.path)
  mask.format 'png'

  mask.combine_options do |m|
    m.alpha 'transparent'
    m.background 'none'
    m.draw "roundrectangle 0,0,#{dimensions},#{radius}"
  end

  overlay = ::MiniMagick::Image.open img.path
  overlay.format 'png'

  overlay.combine_options do |o|
    o.alpha 'transparent'
    o.background 'none'
    o.draw "roundrectangle 0,0,#{dimensions},#{radius}"
  end

  masked = img.composite(mask, 'png') do |i|
    i.alpha 'set'
    i.compose 'DstIn'
  end

  masked.composite(overlay, 'png') do |i|
    i.compose 'Over'
  end
  masked
end
svg!() click to toggle source

Just copy the svg, never resize

# File lib/yamg/icon.rb, line 99
def svg!
  raise unless @icons.find { |i| File.extname(i) == 'svg' }
end
write_out(path = nil) click to toggle source

Writes image to disk

# File lib/yamg/icon.rb, line 138
def write_out(path = nil)
  return img unless path
  FileUtils.mkdir_p File.dirname(path)
  img.write(path)
  path
rescue Errno::ENOENT
  puts_and_exit("Path not found '#{path}'")
end