identicon 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +48 -22
- data/identicon.gemspec +12 -12
- data/lib/identicon.rb +174 -155
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad3357652583debadda9d607a8ec8d8920cb8948
|
4
|
+
data.tar.gz: b86a208eb83c3460d16339a785a8c9bb6085cd7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9293359cfbbb9c259fa386b912fc1d5ef88f797f18a824058c6517f252e781f4c68695ba368e9e0aac02b4466dfd667681b9f4c088ec536e8e47ccd1d9e45fd
|
7
|
+
data.tar.gz: 03b9f0597cec35e02b7dbde8d83d60a88cb0774de9d70a31c4ae1345ade8d6834eff8d44d294fa457b5269933a9789c61324729d3d17192c2f15c077d161b590
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
Identicon
|
2
2
|
=========
|
3
|
+
[![Dependency Status](https://gemnasium.com/victorgama/identicon.svg)](https://gemnasium.com/victorgama/identicon)
|
4
|
+
|
3
5
|
![](https://dl.dropboxusercontent.com/u/262919/Identicons/1.png)
|
4
6
|
![](https://dl.dropboxusercontent.com/u/262919/Identicons/2.png)
|
5
7
|
![](https://dl.dropboxusercontent.com/u/262919/Identicons/3.png)
|
@@ -11,16 +13,16 @@ A Ruby library that generates GitHub-like [identicons](https://github.com/blog/1
|
|
11
13
|
That's easy! Use [RubyGems](http://rubygems.org)!
|
12
14
|
|
13
15
|
Insert `Identicon` on your application's Gemfile:
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
```ruby
|
17
|
+
gem 'identicon'
|
18
|
+
```
|
17
19
|
and run
|
18
20
|
|
19
|
-
|
21
|
+
$ bundle
|
20
22
|
|
21
23
|
or manually install it typing
|
22
24
|
|
23
|
-
|
25
|
+
$ gem install identicon
|
24
26
|
|
25
27
|
and hitting `return` with your favourite finger.
|
26
28
|
|
@@ -30,37 +32,61 @@ and hitting `return` with your favourite finger.
|
|
30
32
|
## Using
|
31
33
|
|
32
34
|
Require it as always
|
33
|
-
|
34
|
-
|
35
|
-
|
35
|
+
```ruby
|
36
|
+
require 'identicon'
|
37
|
+
```
|
36
38
|
Now you can use it through two methods: `data_url_for` or `file_for`. Simple like that.
|
37
39
|
|
38
40
|
## `data_url_for`
|
39
41
|
This generates a data-url, so you can use it immediately, check it out:
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
```ruby
|
43
|
+
Identicon.data_url_for "Whatever you want!"
|
44
|
+
```
|
43
45
|
You can also specify a image size and a background color:
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
```ruby
|
47
|
+
Identicon.data_url_for "Whatever you want!", 128, [255, 255, 255]
|
48
|
+
```
|
47
49
|
This creates a 128x128px image, with a nice white background.
|
48
50
|
|
49
51
|
---------
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
No probs!
|
53
|
+
– But Vito, I want to save it as a file.
|
54
|
+
– No problemo! fil_for is the way to go.
|
54
55
|
|
55
56
|
# `file_for`
|
56
57
|
This generates and writes the result to a file. It's as easy as just making a data-url. Check it out:
|
58
|
+
```ruby
|
59
|
+
Identicon.file_for "User's email? Username? Telephone?", "/path/to/the/image"
|
60
|
+
```
|
61
|
+
And, as you may guess, it also takes those optional parameters.
|
62
|
+
```ruby
|
63
|
+
Identicon.file_for "User's email? Username? Telephone?", "/path/to/the/image", 128, [255, 255, 255]
|
64
|
+
```
|
65
|
+
----------
|
57
66
|
|
58
|
-
|
67
|
+
Pull requests are welcome!
|
59
68
|
|
60
|
-
|
69
|
+
----------
|
61
70
|
|
62
|
-
|
71
|
+
```
|
72
|
+
The MIT License (MIT)
|
63
73
|
|
64
|
-
|
74
|
+
Copyright (c) 2014-2015 Victor Gama
|
75
|
+
|
76
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
77
|
+
this software and associated documentation files (the "Software"), to deal in
|
78
|
+
the Software without restriction, including without limitation the rights to
|
79
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
80
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
81
|
+
subject to the following conditions:
|
82
|
+
|
83
|
+
The above copyright notice and this permission notice shall be included in all
|
84
|
+
copies or substantial portions of the Software.
|
65
85
|
|
66
|
-
|
86
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
87
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
88
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
89
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
90
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
91
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
92
|
+
```
|
data/identicon.gemspec
CHANGED
@@ -2,19 +2,19 @@ lib = File.expand_path('../lib', __FILE__)
|
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
s.name = "identicon"
|
6
|
+
s.version = "0.0.4"
|
7
|
+
s.authors = ["Victor Gama"]
|
8
|
+
s.email = ["hey@vito.io"]
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
s.description = "A simple github-like identicons generator."
|
11
|
+
s.summary = "A simple github-like identicons generator."
|
12
|
+
s.homepage = "http://github.com/victorgama/identicon"
|
13
|
+
s.license = "MIT"
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
s.files = `git ls-files`.split($/)
|
16
|
+
s.executables = s.files.grep(%r{^bin/}) { |file| File.basename(file) }
|
17
|
+
s.require_paths = ["lib"]
|
18
18
|
|
19
|
-
|
19
|
+
s.add_dependency "chunky_png"
|
20
20
|
end
|
data/lib/identicon.rb
CHANGED
@@ -3,164 +3,183 @@ require 'chunky_png'
|
|
3
3
|
|
4
4
|
# Generates GitHub-like identicons using MD5 hashes
|
5
5
|
class Identicon
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
# background - (Optional) The background color to be used in a rgb array.
|
6
|
+
attr_accessor :size, :data, :background
|
7
|
+
|
8
|
+
CHARS_REGEX = /(\w)(\w)/
|
9
|
+
|
10
|
+
COLORS = {
|
11
|
+
a: [196, 108, 168],
|
12
|
+
b: [127, 219, 107],
|
13
|
+
c: [83, 89, 215],
|
14
|
+
d: [111, 198, 148],
|
15
|
+
e: [186, 130, 82],
|
16
|
+
f: [113, 66, 197],
|
17
|
+
"0": [201, 186, 84],
|
18
|
+
"1": [114, 153, 205],
|
19
|
+
"2": [141, 108, 204],
|
20
|
+
"3": [75, 183, 114],
|
21
|
+
"4": [192, 86, 82],
|
22
|
+
"5": [199, 115, 209],
|
23
|
+
"6": [206, 77, 94],
|
24
|
+
"7": [211, 135, 162],
|
25
|
+
"8": [71, 194, 197],
|
26
|
+
"9": [188, 110, 93]
|
27
|
+
}
|
28
|
+
|
29
|
+
class << self
|
30
|
+
# Generates a identicon using the data:image protocol.
|
31
|
+
# Useful for web applications.
|
32
|
+
#
|
33
|
+
# === Parameters
|
34
|
+
# data - The data that will be converted to a identicon
|
35
|
+
# size - (Optional) The image size. Defaults to 420px
|
36
|
+
# background - (Optional) The background color to be used in a rgb array.
|
38
37
|
# Ex: [255, 255, 255]
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
# size - (Optional) The image size. Defaults to 420px
|
54
|
-
# background - (Optional) The background color to be used in a rgb array.
|
38
|
+
def data_url_for(data, size = 420, background = nil)
|
39
|
+
img = Identicon.new(data, size)
|
40
|
+
img.background = background unless background.nil?
|
41
|
+
img.generate_image
|
42
|
+
img.to_data_url
|
43
|
+
end
|
44
|
+
|
45
|
+
# Generates a identicon and stores it in a file
|
46
|
+
#
|
47
|
+
# === Parameters
|
48
|
+
# data - The data that will be converted to a identicon
|
49
|
+
# path - The path where the file will be stored
|
50
|
+
# size - (Optional) The image size. Defaults to 420px
|
51
|
+
# background - (Optional) The background color to be used in a rgb array.
|
55
52
|
# Ex: [255, 255, 255]
|
56
53
|
#
|
57
54
|
# Returns nothing
|
58
|
-
|
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
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
55
|
+
def file_for(data, path, size = 420, background = nil)
|
56
|
+
img = Identicon.new(data, size)
|
57
|
+
img.background = background unless background.nil?
|
58
|
+
img.generate_image
|
59
|
+
img.save(path)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Generates a identicon and returns the binary data
|
63
|
+
#
|
64
|
+
# === Parameters
|
65
|
+
# data - The data that will be converted to a identicon
|
66
|
+
# size - (Optional) The image size. Defaults to 420px
|
67
|
+
# background - (Optional) The background color to be used in a rgb array.
|
68
|
+
# Ex: [255, 255, 255]
|
69
|
+
#
|
70
|
+
# Returns binary_data
|
71
|
+
def blob_for(data, size = 420, background = nil)
|
72
|
+
img = Identicon.new(data, size)
|
73
|
+
img.background = background unless background.nil?
|
74
|
+
img.generate_image
|
75
|
+
img.to_blob
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Constructs a new Identicon instance
|
80
|
+
#
|
81
|
+
# === Parameters
|
82
|
+
# data - The data that will be converted to a identicon
|
83
|
+
# size - The image size
|
84
|
+
def initialize(data, size)
|
85
|
+
@hash = Digest::MD5.hexdigest(data.to_s)
|
86
|
+
@chars = @hash.scan(CHARS_REGEX)
|
87
|
+
@color = [0, 0, 0]
|
88
|
+
@size = size
|
89
|
+
@pixel_ratio = (size / 6).round
|
90
|
+
@image_size = @size - (@pixel_ratio / 1.5).round
|
91
|
+
@square_array = Hash.new { Hash.new(false) }
|
92
|
+
@background = [240, 240, 240]
|
93
|
+
convert_hash
|
94
|
+
end
|
95
|
+
|
96
|
+
def size=(value)
|
97
|
+
@size = value
|
98
|
+
@pixel_ratio = (value / 5).round
|
99
|
+
value
|
100
|
+
end
|
101
|
+
|
102
|
+
def data=(value)
|
103
|
+
@hash = Digest::MD5.hexdigest(data.to_s)
|
104
|
+
@chars = @hash.scan(CHARS_REGEX)
|
105
|
+
convert_hash
|
106
|
+
value
|
107
|
+
end
|
108
|
+
|
109
|
+
# Process the specified data and generates the result
|
110
|
+
def generate_image
|
111
|
+
png_image = ChunkyPNG::Image.new(@image_size,
|
112
|
+
@image_size,
|
113
|
+
ChunkyPNG::Color.rgb(@background[0],
|
114
|
+
@background[1],
|
115
|
+
@background[2])
|
116
|
+
)
|
117
|
+
@image = ChunkyPNG::Image.new(@size,
|
118
|
+
@size,
|
119
|
+
ChunkyPNG::Color.rgb(@background[0],
|
120
|
+
@background[1],
|
121
|
+
@background[2])
|
122
|
+
)
|
123
|
+
@square_array.each_key do |line_key|
|
124
|
+
line_value = @square_array[line_key]
|
125
|
+
line_value.each_index do |col_key|
|
126
|
+
col_value = line_value[col_key]
|
127
|
+
if col_value
|
128
|
+
png_image.rect(
|
129
|
+
col_key * @pixel_ratio,
|
130
|
+
line_key * @pixel_ratio,
|
131
|
+
(col_key + 1) * @pixel_ratio,
|
132
|
+
(line_key + 1) * @pixel_ratio,
|
133
|
+
ChunkyPNG::Color::TRANSPARENT,
|
115
134
|
ChunkyPNG::Color.rgb(@color[0], @color[1], @color[2])
|
116
135
|
)
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
@image.compose!(png_image, @pixel_ratio / 2, @pixel_ratio / 2)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Exports the result to a data:image format.
|
143
|
+
def to_data_url
|
144
|
+
@image.to_data_url
|
145
|
+
end
|
146
|
+
|
147
|
+
# Exports the result to a file
|
148
|
+
# === Parameters
|
149
|
+
# path - Path where the image will be stored
|
150
|
+
def save(path)
|
151
|
+
@image.save(path)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Exports the result in binary format
|
155
|
+
def to_blob
|
156
|
+
@image.to_blob
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
def convert_hash
|
162
|
+
chars = @chars.map { |p| p[1] }
|
163
|
+
chars.each_index do |i|
|
164
|
+
char = chars[i]
|
165
|
+
sq_index = (i / 3).round
|
166
|
+
@square_array[i / 3] = [] unless @square_array.member? sq_index
|
167
|
+
if i % 3 == 0
|
168
|
+
@square_array[i / 3][0] = h_to_b char
|
169
|
+
@square_array[i / 3][4] = h_to_b char
|
170
|
+
elsif i % 3 == 1
|
171
|
+
@square_array[i / 3][1] = h_to_b char
|
172
|
+
@square_array[i / 3][3] = h_to_b char
|
173
|
+
else
|
174
|
+
@square_array[i / 3][2] = h_to_b char
|
175
|
+
end
|
176
|
+
end
|
177
|
+
color = :a
|
178
|
+
0.upto(5) { color = chars.shift.to_sym }
|
179
|
+
@color = COLORS[color]
|
180
|
+
end
|
181
|
+
|
182
|
+
def h_to_b(value)
|
183
|
+
value.bytes.first.even?
|
184
|
+
end
|
185
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: identicon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Gama
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chunky_png
|
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
56
|
version: '0'
|
57
57
|
requirements: []
|
58
58
|
rubyforge_project:
|
59
|
-
rubygems_version: 2.
|
59
|
+
rubygems_version: 2.4.6
|
60
60
|
signing_key:
|
61
61
|
specification_version: 4
|
62
62
|
summary: A simple github-like identicons generator.
|