Class: YAMG::Icon
- Inherits:
-
Object
- Object
- YAMG::Icon
- Defined in:
- lib/yamg/icon.rb
Overview
ICONS
Instance Attribute Summary collapse
-
#dpi ⇒ Object
Returns the value of attribute dpi.
-
#icons ⇒ Object
Returns the value of attribute icons.
-
#img ⇒ Object
Returns the value of attribute img.
-
#radius ⇒ Object
Returns the value of attribute radius.
-
#rounded ⇒ Object
(also: #rounded?)
Returns the value of attribute rounded.
-
#size ⇒ Object
Returns the value of attribute size.
-
#src ⇒ Object
Returns the value of attribute src.
Instance Method Summary collapse
- #apply_background ⇒ Object
- #dimensions ⇒ Object
- #find_closest_gte_icon ⇒ Object
-
#ico!(out) ⇒ Object
ICO!.
- #image(out = nil) ⇒ Object
-
#initialize(src, size, bg = nil, rounded = false, radius = 9) ⇒ Icon
constructor
Icon.
-
#round ⇒ Object
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.
-
#svg! ⇒ Object
Just copy the svg, never resize.
-
#write_out(path = nil) ⇒ Object
Writes image to disk.
Constructor Details
#initialize(src, size, bg = nil, rounded = false, radius = 9) ⇒ Icon
Icon
Icon.new(src, size, rounded).image Image class Icon.new(src, size, rounded).image(‘.path.ext’) Export image
ICO: 16/32/48
19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 |
Instance Attribute Details
#dpi ⇒ Object
Returns the value of attribute dpi.
7 8 9 |
# File 'lib/yamg/icon.rb', line 7 def dpi @dpi end |
#icons ⇒ Object
Returns the value of attribute icons.
7 8 9 |
# File 'lib/yamg/icon.rb', line 7 def icons @icons end |
#img ⇒ Object
Returns the value of attribute img.
7 8 9 |
# File 'lib/yamg/icon.rb', line 7 def img @img end |
#radius ⇒ Object
Returns the value of attribute radius.
7 8 9 |
# File 'lib/yamg/icon.rb', line 7 def radius @radius end |
#rounded ⇒ Object Also known as: rounded?
Returns the value of attribute rounded.
7 8 9 |
# File 'lib/yamg/icon.rb', line 7 def rounded @rounded end |
#size ⇒ Object
Returns the value of attribute size.
7 8 9 |
# File 'lib/yamg/icon.rb', line 7 def size @size end |
#src ⇒ Object
Returns the value of attribute src.
7 8 9 |
# File 'lib/yamg/icon.rb', line 7 def src @src end |
Instance Method Details
#apply_background ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/yamg/icon.rb', line 89 def apply_background clone = ::MiniMagick::Image.open img.path clone. do |o| o.draw 'color 0,0 reset' o.fill @bg end clone.composite(img) { |i| i.compose 'Over' } end |
#dimensions ⇒ Object
46 47 48 |
# File 'lib/yamg/icon.rb', line 46 def dimensions img.dimensions.join(',') end |
#find_closest_gte_icon ⇒ Object
33 34 35 36 37 38 39 40 |
# 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) ⇒ Object
ICO!
104 105 106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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 ⇒ Object
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/yamg/icon.rb', line 59 def round mask = MiniMagick::Image.open(img.path) mask.format 'png' mask. do |m| m.alpha 'transparent' m.background 'none' m.draw "roundrectangle 0,0,#{dimensions},#{radius}" end = ::MiniMagick::Image.open img.path .format 'png' . 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(, 'png') do |i| i.compose 'Over' end masked end |
#svg! ⇒ Object
Just copy the svg, never resize
99 100 101 |
# File 'lib/yamg/icon.rb', line 99 def svg! raise unless @icons.find { |i| File.extname(i) == 'svg' } end |
#write_out(path = nil) ⇒ Object
Writes image to disk
138 139 140 141 142 143 144 145 |
# 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 |