identicon 0.0.3 → 0.0.4

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 (6) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +48 -22
  4. data/identicon.gemspec +12 -12
  5. data/lib/identicon.rb +174 -155
  6. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 089111120e3ecd928f381fb37796dc024b33a89e
4
- data.tar.gz: 6e9af318764806f81014b5c03b2e287d07f3da0f
3
+ metadata.gz: ad3357652583debadda9d607a8ec8d8920cb8948
4
+ data.tar.gz: b86a208eb83c3460d16339a785a8c9bb6085cd7e
5
5
  SHA512:
6
- metadata.gz: f3db94a2ec35df83d71ec3b8d62db038b63930d543a7b09cc098c6c0bf1ee1330cff26476c3ed81f217c9cc5024aa60513581f8cabf3b1b2540a4d43812bd465
7
- data.tar.gz: 3c7885d97bcc5dbd30286564efff9d8d41149acfae6b4bdab33df44a4d2723fea37d65347c3b1559d36dc51efc002385cb1e9ed182302a7b5c0446a1cf5c64b7
6
+ metadata.gz: e9293359cfbbb9c259fa386b912fc1d5ef88f797f18a824058c6517f252e781f4c68695ba368e9e0aac02b4466dfd667681b9f4c088ec536e8e47ccd1d9e45fd
7
+ data.tar.gz: 03b9f0597cec35e02b7dbde8d83d60a88cb0774de9d70a31c4ae1345ade8d6834eff8d44d294fa457b5269933a9789c61324729d3d17192c2f15c077d161b590
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2013 Victor Gama
3
+ Copyright (c) 2014-2015 Victor Gama
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
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
- gem `identicon`
16
-
16
+ ```ruby
17
+ gem 'identicon'
18
+ ```
17
19
  and run
18
20
 
19
- $ bundle
21
+ $ bundle
20
22
 
21
23
  or manually install it typing
22
24
 
23
- $ gem install identicon
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
- require 'identicon'
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
- Identicon.data_url_for "Whatever you want!"
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
- Identicon.data_url_for "Whatever you want!", 128, [255, 255, 255]
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
- *But Vito, I want to save it as a file.*
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
- Identicon.file_for "User's email? Username? Telephone?", "/path/to/the/image"
67
+ Pull requests are welcome!
59
68
 
60
- And, as you may guess, it also takes that optional parameters.
69
+ ----------
61
70
 
62
- Identicon.file_for "User's email? Username? Telephone?", "/path/to/the/image", 128, [255, 255, 255]
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
- Pull requests are welcome!
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
+ ```
@@ -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
- s.name = "identicon"
6
- s.version = "0.0.3"
7
- s.authors = ["Victor Gama"]
8
- s.email = ["hey@vito.io"]
5
+ s.name = "identicon"
6
+ s.version = "0.0.4"
7
+ s.authors = ["Victor Gama"]
8
+ s.email = ["hey@vito.io"]
9
9
 
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"
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
- s.files = `git ls-files`.split($/)
16
- s.executables = s.files.grep(%r{^bin/}) { |file| File.basename(file) }
17
- s.require_paths = ["lib"]
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
- s.add_dependency "chunky_png"
19
+ s.add_dependency "chunky_png"
20
20
  end
@@ -3,164 +3,183 @@ require 'chunky_png'
3
3
 
4
4
  # Generates GitHub-like identicons using MD5 hashes
5
5
  class Identicon
6
-
7
- attr_accessor :size, :data, :background
8
-
9
- CHARS_REGEX = /(\w)(\w)/
10
-
11
- COLORS = {
12
- :a => [196, 108, 168],
13
- :b => [127, 219, 107],
14
- :c => [83, 89, 215],
15
- :d => [111, 198, 148],
16
- :e => [186, 130, 82],
17
- :f => [113, 66, 197],
18
- :"0" => [201, 186, 84],
19
- :"1" => [114, 153, 205],
20
- :"2" => [141, 108, 204],
21
- :"3" => [75, 183, 114],
22
- :"4" => [192, 86, 82],
23
- :"5" => [199, 115, 209],
24
- :"6" => [206, 77, 94],
25
- :"7" => [211, 135, 162],
26
- :"8" => [71, 194, 197],
27
- :"9" => [188, 110, 93]
28
- }
29
-
30
- class << self
31
- # Generates a identicon using the data:image protocol.
32
- # Usefull for web applications.
33
- #
34
- # === Parameters
35
- # data - The data that will be converted to a identicon
36
- # size - (Optional) The image size. Defaults to 420px
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
- def data_url_for(data, size = 420, background = nil)
40
- img = self.new(data, size)
41
- img.background = background unless background.nil?
42
- img.generate_image
43
- img.to_data_url
44
- end
45
-
46
-
47
- # Generates a identicon and stores it in a file
48
- # Useful for web applications.
49
- #
50
- # === Parameters
51
- # data - The data that will be converted to a identicon
52
- # path - The path where the file will be stored
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
- def file_for(data, path, size = 420, background = nil)
59
- img = self.new(data, size)
60
- img.background = background unless background.nil?
61
- img.generate_image
62
- img.save(path)
63
- end
64
- end
65
-
66
-
67
- # Constructs a new Identicon instance
68
- #
69
- # === Parameters
70
- # data - The data that will be converted to a identicon
71
- # size - The image size
72
- def initialize(data, size)
73
- @hash = Digest::MD5.hexdigest(data.to_s)
74
- @chars = @hash.scan(CHARS_REGEX)
75
- @color = [0, 0, 0]
76
- @size = size
77
- @pixel_ratio = (size / 6).round
78
- @image_size = @size - (@pixel_ratio / 1.5).round
79
- @square_array = Hash.new { |h| h = Hash.new(false) }
80
- @background = [240, 240, 240]
81
- convert_hash
82
- end
83
-
84
-
85
- def size=(value)
86
- @size = value
87
- @pixel_ratio = (value / 5).round
88
- value
89
- end
90
-
91
-
92
- def data=(value)
93
- @hash = Digest::MD5.hexdigest(data.to_s)
94
- @chars = @hash.scan(CHARS_REGEX)
95
- convert_hash
96
- value
97
- end
98
-
99
-
100
- # Process the specified data and generates the result
101
- def generate_image
102
- pImage = ChunkyPNG::Image.new(@image_size, @image_size, ChunkyPNG::Color::rgb(@background[0], @background[1], @background[2]))
103
- @image = ChunkyPNG::Image.new(@size, @size, ChunkyPNG::Color::rgb(@background[0], @background[1], @background[2]))
104
- @square_array.each_key { |lineKey|
105
- lineValue = @square_array[lineKey]
106
- lineValue.each_index { |colKey|
107
- colValue = lineValue[colKey]
108
- if(colValue)
109
- pImage.rect(
110
- colKey * @pixel_ratio,
111
- lineKey * @pixel_ratio,
112
- (colKey + 1) * @pixel_ratio,
113
- (lineKey + 1) * @pixel_ratio,
114
- ChunkyPNG::Color::TRANSPARENT,
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
- end
118
- }
119
- }
120
- @image.compose!(pImage, @pixel_ratio / 2, @pixel_ratio / 2)
121
- end
122
-
123
-
124
- # Exports the result to a data:image format.
125
- def to_data_url
126
- @image.to_data_url
127
- end
128
-
129
-
130
- # Exports the result to a file
131
- # === Parameters
132
- # path - Path where the image will be stored
133
- def save(path)
134
- @image.save(path)
135
- end
136
-
137
-
138
- private
139
- def convert_hash
140
- chars = @chars.map { |p|
141
- p[1]
142
- }
143
- chars.each_index { |i|
144
- char = chars[i]
145
- sqIndex = (i / 3).round
146
- @square_array[i/3] = [] unless @square_array.member? sqIndex
147
-
148
- if(i % 3 == 0)
149
- @square_array[i/3][0] = h_to_b char
150
- @square_array[i/3][4] = h_to_b char
151
- elsif(i % 3 == 1)
152
- @square_array[i/3][1] = h_to_b char
153
- @square_array[i/3][3] = h_to_b char
154
- else
155
- @square_array[i/3][2] = h_to_b char
156
- end
157
- }
158
- color = :a
159
- 0.upto(5) { color = chars.shift.to_sym }
160
- @color = COLORS[color]
161
- end
162
-
163
- def h_to_b(value)
164
- value.bytes.first % 2 == 0
165
- end
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.3
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: 2014-05-12 00:00:00.000000000 Z
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.2.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.