quilt 0.0.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -6
- data/Rakefile +1 -1
- data/lib/quilt.rb +75 -7
- data/quilt.gemspec +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15b56cf2df6e1e03120d2d6dac7dacd578fd0d5f
|
4
|
+
data.tar.gz: bcf7cf0847dfa5e7b77773be2becc9fa039ef813
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/lib/quilt.rb
CHANGED
@@ -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
|
-
|
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 =
|
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
|
-
|
216
|
-
|
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 -
|
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
|
307
|
+
if @transparent
|
240
308
|
if fore == @back_color
|
241
309
|
@image.polygon_clip points
|
242
310
|
else
|
data/quilt.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "quilt"
|
5
|
-
s.version = "0.0
|
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-
|
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
|
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-
|
11
|
+
date: 2014-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|