hash_avatar 0.0.1

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.
Files changed (3) hide show
  1. data/lib/hash_avatar.rb +71 -0
  2. data/lib/hash_avatar/icon.rb +345 -0
  3. metadata +46 -0
@@ -0,0 +1,71 @@
1
+ #encoding: utf-8
2
+
3
+ require 'RMagick'
4
+ require 'digest/sha1'
5
+
6
+ class HashAvatar
7
+
8
+ def self.hi
9
+ puts "HashAvatar"
10
+ end
11
+
12
+ include Magick
13
+
14
+ BackgroundColor = "white";
15
+ FrontColor = "black";
16
+
17
+ ICON_ROWS = 64
18
+ ICON_COLS = 64
19
+
20
+ QUARTER_ROWS = ICON_ROWS << 1
21
+ QUARTER_COLS = ICON_COLS << 1
22
+
23
+ AVATAR_ROWS = ICON_ROWS << 2
24
+ AVATAR_COLS = ICON_COLS << 2
25
+
26
+ def self.get_icons
27
+ @@icon_method = Icon.singleton_methods
28
+ @@icon_method_size = @@icon_method.size
29
+ end
30
+
31
+ def self.gen(string='paolongtao')
32
+
33
+ hash = Digest::SHA1.hexdigest(string)
34
+ self.get_icons
35
+ @@avatar = Image.new(AVATAR_COLS, AVATAR_ROWS)
36
+ quarter = Image.new(QUARTER_COLS, QUARTER_ROWS)
37
+
38
+ hash_slice = []
39
+ for i in 0..5 do
40
+ hash_slice << hash.slice(i*4, 4)
41
+ end
42
+
43
+ icon_method_index = []
44
+ for i in 0..4 do
45
+ icon_method_index << Integer("0x#{hash_slice[i]}") % @@icon_method_size
46
+ end
47
+ icon_method_index << Integer("0x#{hash_slice[i]}") % Magick.colors.size
48
+
49
+ #Icon.singleton_methods
50
+ #Icon.send :method
51
+ gc = Magick::Draw.new
52
+ gc.composite(0, 0, 0, 0, Icon.send(@@icon_method[icon_method_index[0]]))
53
+ gc.composite(ICON_COLS, 0, 0, 0, Icon.send(@@icon_method[icon_method_index[1]]))
54
+ gc.composite(0, ICON_ROWS, 0, 0, Icon.send(@@icon_method[icon_method_index[2]]))
55
+ gc.composite(ICON_COLS, ICON_ROWS, 0, 0, Icon.send(@@icon_method[icon_method_index[3]]))
56
+ gc.draw(quarter)
57
+
58
+ color = Magick.colors[icon_method_index[4]].name
59
+ quarter = quarter.opaque_channel(FrontColor, color, false, 10000, Magick::AllChannels)
60
+
61
+ gc.composite(0, 0, 0, 0, quarter)
62
+ gc.composite(QUARTER_COLS, 0, 0, 0, quarter.flop)
63
+ gc.composite(0, QUARTER_ROWS, 0, 0, quarter.flip)
64
+ gc.composite(QUARTER_COLS, QUARTER_ROWS, 0, 0, quarter.flop.flip)
65
+ gc.draw(@@avatar)
66
+
67
+ return @@avatar
68
+ end
69
+ end
70
+
71
+ require 'hash_avatar/icon'
@@ -0,0 +1,345 @@
1
+ #encoding: utf-8
2
+
3
+ require 'rvg/rvg'
4
+
5
+ class HashAvatar::Icon
6
+
7
+ include Magick
8
+
9
+ def self.draw_full_rect
10
+ rvg = RVG.new(64, 64) do |canvas|
11
+ canvas.background_fill = 'white'
12
+ canvas.viewbox(0, 0, 320, 320)
13
+ canvas.rect(320, 320).styles(:fill => 'black', :stroke_width => 0)
14
+ end
15
+ rvg.draw
16
+ end
17
+
18
+ def self.draw_half_rect_1
19
+ rvg = RVG.new(64, 64) do |canvas|
20
+ canvas.background_fill = 'white'
21
+ canvas.viewbox(0, 0, 320, 320)
22
+ canvas.rect(160, 320, 0, 0).styles(:fill => 'black', :stroke_width => 0)
23
+ end
24
+ rvg.draw
25
+ end
26
+
27
+ def self.draw_half_rect_2
28
+ rvg = RVG.new(64, 64) do |canvas|
29
+ canvas.background_fill = 'white'
30
+ canvas.viewbox(0, 0, 320, 320)
31
+ canvas.rect(320, 160, 0, 0).styles(:fill => 'black', :stroke_width => 0)
32
+ end
33
+ rvg.draw
34
+ end
35
+
36
+ def self.draw_half_rect_3
37
+ rvg = RVG.new(64, 64) do |canvas|
38
+ canvas.background_fill = 'white'
39
+ canvas.viewbox(0, 0, 320, 320)
40
+ canvas.rect(320, 160, 0, 160).styles(:fill => 'black', :stroke_width => 0)
41
+ end
42
+ rvg.draw
43
+ end
44
+
45
+ def self.draw_half_rect_4
46
+ rvg = RVG.new(64, 64) do |canvas|
47
+ canvas.background_fill = 'white'
48
+ canvas.viewbox(0, 0, 320, 320)
49
+ canvas.rect(160, 320, 160, 0).styles(:fill => 'black', :stroke_width => 0)
50
+ end
51
+ rvg.draw
52
+ end
53
+
54
+ def self.draw_half_rect_5
55
+ rvg = RVG.new(64, 64) do |canvas|
56
+ canvas.background_fill = 'white'
57
+ canvas.viewbox(0, 0, 320, 320)
58
+ canvas.rect(160, 160).styles(:fill => 'black', :stroke_width => 0)
59
+ canvas.rect(160, 160, 160, 160).styles(:fill => 'black', :stroke_width => 0)
60
+ end
61
+ rvg.draw
62
+ end
63
+
64
+ def self.draw_half_rect_6
65
+ rvg = RVG.new(64, 64) do |canvas|
66
+ canvas.background_fill = 'white'
67
+ canvas.viewbox(0, 0, 320, 320)
68
+ canvas.rect(160, 160, 0, 160).styles(:fill => 'black', :stroke_width => 0)
69
+ canvas.rect(160, 160, 160, 0).styles(:fill => 'black', :stroke_width => 0)
70
+ end
71
+ rvg.draw
72
+ end
73
+
74
+ def self.draw_quarter_rect_1
75
+ rvg = RVG.new(64, 64) do |canvas|
76
+ canvas.background_fill = 'white'
77
+ canvas.viewbox(0, 0, 320, 320)
78
+ canvas.rect(160, 160).styles(:fill => 'black', :stroke_width => 0)
79
+ end
80
+ rvg.draw
81
+ end
82
+
83
+ def self.draw_quarter_rect_2
84
+ rvg = RVG.new(64, 64) do |canvas|
85
+ canvas.background_fill = 'white'
86
+ canvas.viewbox(0, 0, 320, 320)
87
+ canvas.rect(160, 160, 160, 160).styles(:fill => 'black', :stroke_width => 0)
88
+ end
89
+ rvg.draw
90
+ end
91
+
92
+ def self.draw_quarter_rect_3
93
+ rvg = RVG.new(64, 64) do |canvas|
94
+ canvas.background_fill = 'white'
95
+ canvas.viewbox(0, 0, 320, 320)
96
+ canvas.rect(160, 160, 0, 160).styles(:fill => 'black', :stroke_width => 0)
97
+ end
98
+ rvg.draw
99
+ end
100
+
101
+ def self.draw_quarter_rect_4
102
+ rvg = RVG.new(64, 64) do |canvas|
103
+ canvas.background_fill = 'white'
104
+ canvas.viewbox(0, 0, 320, 320)
105
+ canvas.rect(160, 160, 160, 0).styles(:fill => 'black', :stroke_width => 0)
106
+ end
107
+ rvg.draw
108
+ end
109
+
110
+ def self.draw_half_triangle_1
111
+ rvg = RVG.new(64, 64) do |canvas|
112
+ canvas.background_fill = 'white'
113
+ canvas.viewbox(0, 0, 320, 320)
114
+ canvas.path("M 0,0 L 0,320 L 320,320 z").styles(:fill => 'black', :stroke_width => 0)
115
+ end
116
+ rvg.draw
117
+ end
118
+
119
+ def self.draw_half_triangle_2
120
+ rvg = RVG.new(64, 64) do |canvas|
121
+ canvas.background_fill = 'white'
122
+ canvas.viewbox(0, 0, 320, 320)
123
+ canvas.path("M 0,0 L 320,0 L 320,320 z").styles(:fill => 'black', :stroke_width => 0)
124
+ end
125
+ rvg.draw
126
+ end
127
+
128
+ def self.draw_half_triangle_3
129
+ rvg = RVG.new(64, 64) do |canvas|
130
+ canvas.background_fill = 'white'
131
+ canvas.viewbox(0, 0, 320, 320)
132
+ canvas.path("M 0,0 L 0,320 L 320,0 z").styles(:fill => 'black', :stroke_width => 0)
133
+ end
134
+ rvg.draw
135
+ end
136
+
137
+ def self.draw_half_triangle_4
138
+ rvg = RVG.new(64, 64) do |canvas|
139
+ canvas.background_fill = 'white'
140
+ canvas.viewbox(0, 0, 320, 320)
141
+ canvas.path("M 320,0 L 0,320 L 320,320 z").styles(:fill => 'black', :stroke_width => 0)
142
+ end
143
+ rvg.draw
144
+ end
145
+
146
+ def self.draw_half_triangle_5
147
+ rvg = RVG.new(64, 64) do |canvas|
148
+ canvas.background_fill = 'white'
149
+ canvas.viewbox(0, 0, 320, 320)
150
+ canvas.path("M 160,0 L 0,320 L 320,320 z").styles(:fill => 'black', :stroke_width => 0)
151
+ end
152
+ rvg.draw
153
+ end
154
+
155
+ def self.draw_half_triangle_6
156
+ rvg = RVG.new(64, 64) do |canvas|
157
+ canvas.background_fill = 'white'
158
+ canvas.viewbox(0, 0, 320, 320)
159
+ canvas.path("M 160,320 L 0,0 L 320,0 z").styles(:fill => 'black', :stroke_width => 0)
160
+ end
161
+ rvg.draw
162
+ end
163
+
164
+ def self.draw_half_triangle_7
165
+ rvg = RVG.new(64, 64) do |canvas|
166
+ canvas.background_fill = 'white'
167
+ canvas.viewbox(0, 0, 320, 320)
168
+ canvas.path("M 0,160 L 320,0 L 320,320 z").styles(:fill => 'black', :stroke_width => 0)
169
+ end
170
+ rvg.draw
171
+ end
172
+
173
+ def self.draw_half_triangle_8
174
+ rvg = RVG.new(64, 64) do |canvas|
175
+ canvas.background_fill = 'white'
176
+ canvas.viewbox(0, 0, 320, 320)
177
+ canvas.path("M 320,160 L 0,0 L 0,320 z").styles(:fill => 'black', :stroke_width => 0)
178
+ end
179
+ rvg.draw
180
+ end
181
+
182
+ def self.draw_quarter_triangle_1
183
+ rvg = RVG.new(64, 64) do |canvas|
184
+ canvas.background_fill = 'white'
185
+ canvas.viewbox(0, 0, 320, 320)
186
+ canvas.path("M 0,0 L 0,160 L 320,160 z").styles(:fill => 'black', :stroke_width => 0)
187
+ end
188
+ rvg.draw
189
+ end
190
+
191
+ def self.draw_quarter_triangle_2
192
+ rvg = RVG.new(64, 64) do |canvas|
193
+ canvas.background_fill = 'white'
194
+ canvas.viewbox(0, 0, 320, 320)
195
+ canvas.path("M 0,0 L 320,0 L 320,160 z").styles(:fill => 'black', :stroke_width => 0)
196
+ end
197
+ rvg.draw
198
+ end
199
+
200
+ def self.draw_quarter_triangle_3
201
+ rvg = RVG.new(64, 64) do |canvas|
202
+ canvas.background_fill = 'white'
203
+ canvas.viewbox(0, 0, 320, 320)
204
+ canvas.path("M 0,0 L 160,0 L 160,320 z").styles(:fill => 'black', :stroke_width => 0)
205
+ end
206
+ rvg.draw
207
+ end
208
+
209
+ def self.draw_quarter_triangle_4
210
+ rvg = RVG.new(64, 64) do |canvas|
211
+ canvas.background_fill = 'white'
212
+ canvas.viewbox(0, 0, 320, 320)
213
+ canvas.path("M 0,0 L 0,320 L 160,320 z").styles(:fill => 'black', :stroke_width => 0)
214
+ end
215
+ rvg.draw
216
+ end
217
+
218
+ def self.draw_quarter_triangle_5
219
+ rvg = RVG.new(64, 64) do |canvas|
220
+ canvas.background_fill = 'white'
221
+ canvas.viewbox(0, 0, 320, 320)
222
+ canvas.path("M 320,320 L 320,0 L 160,0 z").styles(:fill => 'black', :stroke_width => 0)
223
+ end
224
+ rvg.draw
225
+ end
226
+
227
+ def self.draw_quarter_triangle_6
228
+ rvg = RVG.new(64, 64) do |canvas|
229
+ canvas.background_fill = 'white'
230
+ canvas.viewbox(0, 0, 320, 320)
231
+ canvas.path("M 320,320 L 160,320 L 160,0 z").styles(:fill => 'black', :stroke_width => 0)
232
+ end
233
+ rvg.draw
234
+ end
235
+
236
+ def self.draw_quarter_triangle_7
237
+ rvg = RVG.new(64, 64) do |canvas|
238
+ canvas.background_fill = 'white'
239
+ canvas.viewbox(0, 0, 320, 320)
240
+ canvas.path("M 320,320 L 0,320 L 0,160 z").styles(:fill => 'black', :stroke_width => 0)
241
+ end
242
+ rvg.draw
243
+ end
244
+
245
+ def self.draw_quarter_triangle_8
246
+ rvg = RVG.new(64, 64) do |canvas|
247
+ canvas.background_fill = 'white'
248
+ canvas.viewbox(0, 0, 320, 320)
249
+ canvas.path("M 320,320 L 320,160 L 0,160 z").styles(:fill => 'black', :stroke_width => 0)
250
+ end
251
+ rvg.draw
252
+ end
253
+
254
+ def self.draw_full_circle
255
+ rvg = RVG.new(64, 64) do |canvas|
256
+ canvas.background_fill = 'white'
257
+ canvas.viewbox(0, 0, 320, 320)
258
+ canvas.circle(150, 160, 160).styles(:fill => 'black', :stroke_width => 0)
259
+ end
260
+ rvg.draw
261
+ end
262
+
263
+ def self.draw_quarter_circle_1
264
+ rvg = RVG.new(64, 64) do |canvas|
265
+ canvas.background_fill = 'white'
266
+ canvas.viewbox(0, 0, 320, 320)
267
+ canvas.circle(70, 80, 80).styles(:fill => 'black', :stroke_width => 0)
268
+ end
269
+ rvg.draw
270
+ end
271
+
272
+ def self.draw_quarter_circle_2
273
+ rvg = RVG.new(64, 64) do |canvas|
274
+ canvas.background_fill = 'white'
275
+ canvas.viewbox(0, 0, 320, 320)
276
+ canvas.circle(70, 80, 240).styles(:fill => 'black', :stroke_width => 0)
277
+ end
278
+ rvg.draw
279
+ end
280
+
281
+ def self.draw_quarter_circle_3
282
+ rvg = RVG.new(64, 64) do |canvas|
283
+ canvas.background_fill = 'white'
284
+ canvas.viewbox(0, 0, 320, 320)
285
+ canvas.circle(70, 240, 80).styles(:fill => 'black', :stroke_width => 0)
286
+ end
287
+ rvg.draw
288
+ end
289
+
290
+ def self.draw_quarter_circle_4
291
+ rvg = RVG.new(64, 64) do |canvas|
292
+ canvas.background_fill = 'white'
293
+ canvas.viewbox(0, 0, 320, 320)
294
+ canvas.circle(70, 240, 240).styles(:fill => 'black', :stroke_width => 0)
295
+ end
296
+ rvg.draw
297
+ end
298
+
299
+ def self.draw_quarter_circle_5
300
+ rvg = RVG.new(64, 64) do |canvas|
301
+ canvas.background_fill = 'white'
302
+ canvas.viewbox(0, 0, 320, 320)
303
+ canvas.circle(70, 80, 160).styles(:fill => 'black', :stroke_width => 0)
304
+ end
305
+ rvg.draw
306
+ end
307
+
308
+ def self.draw_quarter_circle_6
309
+ rvg = RVG.new(64, 64) do |canvas|
310
+ canvas.background_fill = 'white'
311
+ canvas.viewbox(0, 0, 320, 320)
312
+ canvas.circle(70, 240, 160).styles(:fill => 'black', :stroke_width => 0)
313
+ end
314
+ rvg.draw
315
+ end
316
+
317
+ def self.draw_quarter_circle_7
318
+ rvg = RVG.new(64, 64) do |canvas|
319
+ canvas.background_fill = 'white'
320
+ canvas.viewbox(0, 0, 320, 320)
321
+ canvas.circle(70, 160, 80).styles(:fill => 'black', :stroke_width => 0)
322
+ end
323
+ rvg.draw
324
+ end
325
+
326
+ def self.draw_quarter_circle_8
327
+ rvg = RVG.new(64, 64) do |canvas|
328
+ canvas.background_fill = 'white'
329
+ canvas.viewbox(0, 0, 320, 320)
330
+ canvas.circle(70, 160, 240).styles(:fill => 'black', :stroke_width => 0)
331
+ end
332
+ rvg.draw
333
+ end
334
+
335
+ def self.draw_quarter_circle_9
336
+ rvg = RVG.new(64, 64) do |canvas|
337
+ canvas.background_fill = 'white'
338
+ canvas.viewbox(0, 0, 320, 320)
339
+ canvas.circle(70, 160, 160).styles(:fill => 'black', :stroke_width => 0)
340
+ end
341
+ rvg.draw
342
+ end
343
+
344
+
345
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_avatar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - paolongtao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: avatar hashed from a string
15
+ email: zhuanyepaolongtao@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/hash_avatar.rb
21
+ - lib/hash_avatar/icon.rb
22
+ homepage: https://github.com/paolongtao/hash_avatar
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: avatar hashed from a string
46
+ test_files: []