lib/yamg/icon.rb in yamg-0.3.1 vs lib/yamg/icon.rb in yamg-0.3.3
- old
+ new
@@ -2,20 +2,29 @@
#
# ICONS
#
#
class Icon
- attr_accessor :src, :size, :dpi, :rounded, :icons
+ attr_accessor :src, :size, :dpi, :rounded, :radius, :icons, :img
- def initialize(src, size, rounded = false)
+ #
+ # Icon
+ #
+ # Icon.new(src, size, rounded).image
+ # Image class
+ # Icon.new(src, size, rounded).image('.path.ext')
+ # Export image
+ #
+ def initialize(src, size, rounded = false, radius = 9)
fail 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?
- @path = File.join(src, find_closest_gte_icon)
+ @choosen = File.join(src, find_closest_gte_icon)
+ @radius = radius
@dpi = 90
end
alias_method :rounded?, :rounded
def find_closest_gte_icon
@@ -25,55 +34,45 @@
n = f.to_i
size > n ? Float::INFINITY : n
end
end
- def image(out)
- if File.extname(@path) =~ /svg/
- pixels = dpi ? "-d #{dpi} -p #{dpi}" : nil
- args = "#{pixels} -w #{size} -h #{size} -f png"
- YAMG.run_rsvg(@path, out, args)
- img = MiniMagick::Image.open(out)
- else
- img = MiniMagick::Image.open(@path)
- img.resize size # "NxN"
- write_out(img, out)
- end
- write_out(round(img), out) if rounded?
+ def radius
+ Array.new(2, size / @radius).join(',')
end
+ def dimensions
+ img.dimensions.join(',')
+ end
+
#
# 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
# https://gist.github.com/artemave/c20e7450af866f5e7735
- def round(img, r = 14)
- size = img.dimensions.join(',')
- r = img.dimensions.max / r
- radius = [r, r].join(',')
-
+ def round(r = 14)
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,#{size},#{radius}"
+ 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,#{size},#{radius}"
+ o.draw "roundrectangle 0,0,#{dimensions},#{radius}"
end
masked = img.composite(mask, 'png') do |i|
i.alpha 'set'
i.compose 'DstIn'
@@ -83,16 +82,32 @@
i.compose 'Over'
end
masked
end
+ def image(out = nil)
+ 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)
+ else
+ @img = MiniMagick::Image.open(@choosen)
+ @img.resize size # "NxN"
+ end
+ @img = round if rounded?
+ write_out(out)
+ end
+
#
# Writes image to disk
#
- def write_out(img, out)
- FileUtils.mkdir_p File.dirname(out)
- img.write(out)
+ def write_out(path = nil)
+ return img unless path
+ FileUtils.mkdir_p File.dirname(path)
+ img.write(path)
rescue Errno::ENOENT
- puts_and_exit("Path not found '#{out}'")
+ puts_and_exit("Path not found '#{path}'")
end
end
end