quilt 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -6
  3. data/Rakefile +1 -1
  4. data/lib/quilt.rb +75 -7
  5. data/quilt.gemspec +2 -2
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49c7f7200f1db6b3653b1365badc140a6ee76bf4
4
- data.tar.gz: 277abcc41fa1cafa27c9a0f99489ac12d538ee0f
3
+ metadata.gz: 15b56cf2df6e1e03120d2d6dac7dacd578fd0d5f
4
+ data.tar.gz: bcf7cf0847dfa5e7b77773be2becc9fa039ef813
5
5
  SHA512:
6
- metadata.gz: 74e214842d20758249bea661edc432ca8c172cb1237a02ce67a9709d5292c53df18dfacab3d9567d68239f54b98085414865edbbdfd8486c3b632f48c83bb573
7
- data.tar.gz: 49b75db2774830fccdffa1dd2941d7df2312895d714e82092e2f947aaafebc7c6f0013f142634a4a6c58a0b2a860e46eb73ce71c1e2feace7d70ed725b8e3fee
6
+ metadata.gz: 0f55ca7e7a3af932fc0b571b365beba802df70fba9196a9a241312b528ffcde0208ebde6178a98bd015a243502d87ec08a58463dd2aa488cbe86dd3ff09d5618
7
+ data.tar.gz: eac2b59b691c8c37efcaee68ba32581d221254234fdb21e9d00c7b30cd8472cf761074c86d9f39222bc0cd99a210b48668ae551a70d6551b77873a68851b04cc
data/README.md CHANGED
@@ -10,6 +10,7 @@ Identicon: http://en.wikipedia.org/wiki/Identicon
10
10
 
11
11
  ## Updates
12
12
 
13
+ 2014-11-09T00:44:08+09:00 Add SVG support
13
14
  2014-11-03T02:05:32+09:00 Add transparent background option
14
15
 
15
16
  ## Installation
@@ -41,18 +42,25 @@ Required rmagick or ruby-gd. (default rmagick)
41
42
  identicon = Quilt::Identicon.new 'sample'
42
43
  print identicon.to_blob
43
44
 
44
- # change image library to Rmagick to GD
45
- Quilt::Identicon.image_lib = Quilt::ImageGD
46
- identicon = Quilt::Identicon.new 'sample'
47
- identicon.write 'sample15_15_gd.png'
48
-
49
45
  # output: 150 * 150 png tranparent background
50
46
  identicon = Quilt::Identicon.new 'sample', :scale => 10, :transparent => true
51
47
  identicon.write 'sample_t_150_150.png'
52
48
 
49
+ # output: 150 * 150 svg
50
+ identicon = Quilt::Identicon.new 'sample', :scale => 10, :format => 'svg'
51
+ identicon.write 'sample_150_150.svg'
52
+
53
+ # output: 150 * 150 svg tranparent background
54
+ identicon = Quilt::Identicon.new 'sample', :scale => 10, :transparent => true, :format => 'svg'
55
+ identicon.write 'sample_150_150_t.svg'
56
+
57
+ # change image library to Rmagick to GD
58
+ Quilt::Identicon.image_lib = Quilt::ImageGD
59
+ identicon = Quilt::Identicon.new 'sample'
60
+ identicon.write 'sample15_15_gd.png'
53
61
 
54
62
  ## Information
55
63
 
56
64
  Copyright (c) 2008 swdyh
57
65
  The MIT License
58
- https://github.com/swdyh/quilt
66
+ https://github.com/swdyh/quilt
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'bundler/gem_tasks'
4
4
 
5
5
  spec = Gem::Specification.new do |s|
6
6
  s.name = "quilt"
7
- s.version = "0.0.9"
7
+ s.version = "0.1.0"
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.has_rdoc = false
10
10
  s.summary = "A Ruby library for generating identicon."
@@ -3,6 +3,64 @@ require 'rubygems'
3
3
  require 'digest/sha1'
4
4
 
5
5
  module Quilt
6
+ class ImageSVG
7
+ def initialize width, height, opt = {}
8
+ @transparent = !!opt[:transparent]
9
+ @mask = @transparent ? %(mask="clip") : ''
10
+ @masks = ''
11
+ @image = ''
12
+ @width = width
13
+ @height = height
14
+ @head = %(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 #{width} #{height}">\n)
15
+ end
16
+
17
+ def color r, g, b
18
+ "rgb(%d, %d, %d)" % [r, g, b]
19
+ end
20
+
21
+ def fill_rect x, y, _x, _y, color
22
+ @image << %(<rect x="#{x}" y="#{y}" width="#{_x - x}" height="#{_y - y}" fill="#{color}" stroke-width="0" stroke="white" mask="url(#clip)"/>\n)
23
+ end
24
+
25
+ def polygon points, color
26
+ unless points.empty?
27
+ ps = points.map { |i| i.join(',') }.join(' ')
28
+ @image << %(<polygon points="#{ps}" stroke-width="0" fill="#{color}" mask="(#clip)"/>\n)
29
+ end
30
+ end
31
+
32
+ def polygon_clip points
33
+ unless points.empty?
34
+ ps = points.map { |i| i.join(',') }.join(' ')
35
+ @masks << %(<polygon points="#{ps}" stroke-width="0"/>\n)
36
+ end
37
+ end
38
+
39
+ def write path
40
+ open(path, 'wb') {|f| f.write self.to_blob }
41
+ end
42
+
43
+ def to_blob
44
+ if @transparent
45
+ @head + build_mask + @image + "\n</svg>"
46
+ else
47
+ @head + @image + "\n</svg>"
48
+ end
49
+ end
50
+
51
+ private
52
+ def build_mask
53
+ <<-EOS
54
+ <mask id="clip" maskUnits="userSpaceOnUse" x="0" y="0" width="#{@width}" height="#{@height}">
55
+ <g fill="black" fill-rule="evenodd">
56
+ <rect x="0" y="0" width="#{@width}" height="#{@height}" fill="white"/>
57
+ #{@masks}
58
+ </g>
59
+ </mask>
60
+ EOS
61
+ end
62
+ end
63
+
6
64
  class ImageRmagick
7
65
  def initialize width, height, opt = {}
8
66
  begin
@@ -155,9 +213,17 @@ module Quilt
155
213
  @scale = opt[:scale] || 1
156
214
  end
157
215
 
158
- @transparent = !!opt[:transparent]
216
+ if opt[:format].to_s == 'svg'
217
+ @image_lib = ImageSVG
218
+ else
219
+ @image_lib = @@image_lib
220
+ end
221
+
222
+ @transparent = !!opt[:transparent] &&
223
+ [ImageRmagick, ImageSVG].include?(@image_lib)
159
224
  @patch_width = PATCH_SIZE * @scale
160
- @image = @@image_lib.new @patch_width * 3, @patch_width * 3, :transparent => @transparent
225
+ @image = @image_lib.new(@patch_width * 3, @patch_width * 3,
226
+ :transparent => @transparent)
161
227
  @back_color = @image.color 255, 255, 255
162
228
  @fore_color = @image.color @decode[:red], @decode[:green], @decode[:blue]
163
229
  render
@@ -212,16 +278,18 @@ module Quilt
212
278
  fore, back = @fore_color, @back_color
213
279
  end
214
280
 
215
- if @transparent && back == @back_color
216
- back = 'Transparent'
281
+ offset = (@image_lib == Quilt::ImageSVG) ? 0 : 1
282
+
283
+ if !(@transparent && back == @back_color)
284
+ @image.fill_rect(x, y, x + @patch_width - offset,
285
+ y + @patch_width - offset, back)
217
286
  end
218
- @image.fill_rect(x, y, x + @patch_width - 1, y + @patch_width - 1, back)
219
287
 
220
288
  points = []
221
289
  PATCHES[patch].each do |pt|
222
290
  dx = pt % PATCH_SIZE
223
291
  dy = pt / PATCH_SIZE
224
- len = @patch_width - 1
292
+ len = @patch_width - offset
225
293
  px = dx.to_f / (PATCH_SIZE - 1) * len
226
294
  py = dy.to_f / (PATCH_SIZE - 1) * len
227
295
 
@@ -236,7 +304,7 @@ module Quilt
236
304
  points << [x + px, y + py]
237
305
  end
238
306
 
239
- if @transparent && @@image_lib == Quilt::ImageRmagick
307
+ if @transparent
240
308
  if fore == @back_color
241
309
  @image.polygon_clip points
242
310
  else
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "quilt"
5
- s.version = "0.0.9"
5
+ s.version = "0.1.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["swdyh"]
9
- s.date = "2014-11-02"
9
+ s.date = "2014-11-08"
10
10
  s.description = "A Ruby library for generating identicon.\nhttps://github.com/swdyh/quilt"
11
11
  s.email = "youhei@gmail.com"
12
12
  s.files = [".travis.yml", "ChangeLog", "Gemfile", "MIT-LICENSE", "README.md", "Rakefile", "lib/quilt.rb", "quilt.gemspec", "test/quilt_test.rb", "test/test_helper.rb"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quilt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - swdyh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-02 00:00:00.000000000 Z
11
+ date: 2014-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler