identicon 0.0.2

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 (7) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/LICENSE +20 -0
  4. data/README.md +66 -0
  5. data/identicon.gemspec +20 -0
  6. data/lib/identicon.rb +147 -0
  7. metadata +63 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc364d61b90b314a193e1970d488c6dc4531364c
4
+ data.tar.gz: 4ada24b1a13a39a1bd182167fbd51d516cc2a197
5
+ SHA512:
6
+ metadata.gz: 92ffa1913b4049cac62664a1319a1971058fbadbbbe4742602970f8d03b09b770c0785cafe178937ecf16266cb6204ff359051b18f12276db3ba5af4cd060e28
7
+ data.tar.gz: ba5b800412da3caa320438ed9f32139ecd4df7b5bf4d6d366957b2ab789c835032f2867aa0a6d9323204e8a52e452048a00e269caaa3e60885602f4cf5f10c68
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Victor Gama
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ Identicon
2
+ =========
3
+ ![](https://dl.dropboxusercontent.com/u/262919/Identicons/1.png)
4
+ ![](https://dl.dropboxusercontent.com/u/262919/Identicons/2.png)
5
+ ![](https://dl.dropboxusercontent.com/u/262919/Identicons/3.png)
6
+
7
+ A Ruby library that generates GitHub-like [identicons](https://github.com/blog/1586-identicons)
8
+
9
+ ## Installing
10
+
11
+ That's easy! Use [RubyGems](http://rubygems.org)!
12
+
13
+ Insert `Identicon` on your application's Gemfile:
14
+
15
+ gem `identicon`
16
+
17
+ and run
18
+
19
+ $ bundle
20
+
21
+ or manually install it typing
22
+
23
+ $ gem install identicon
24
+
25
+ and hitting `return` with your favourite finger.
26
+
27
+
28
+
29
+
30
+ ## Using
31
+
32
+ Require it as always
33
+
34
+ require 'identicon'
35
+
36
+ Now you can use it through two methods: `data_url_for` or `file_for`. Simple like that.
37
+
38
+ ## `data_url_for`
39
+ This generates a data-url, so you can use it immediately, check it out:
40
+
41
+ Identicon.data_url_for "Whatever you want!"
42
+
43
+ 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
+
47
+ This creates a 128x128px image, with a nice white background.
48
+
49
+ ---------
50
+
51
+ *But Vito, I want to save it as a file.*
52
+
53
+ No probs!
54
+
55
+ # `file_for`
56
+ This generates and writes the result to a file. It's as easy as just making a data-url. Check it out:
57
+
58
+ Identicon.file_for "User's email? Username? Telephone?", "/path/to/the/image"
59
+
60
+ And, as you may guess, it also takes that optional parameters.
61
+
62
+ Identicon.file_for "User's email? Username? Telephone?", "/path/to/the/image", 128, [255, 255, 255]
63
+
64
+ ----------
65
+
66
+ Pull requests are welcome!
data/identicon.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "identicon"
6
+ s.version = "0.0.2"
7
+ s.authors = ["Victor Gama"]
8
+ s.email = ["victor@insitelabs.com.br"]
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"
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"]
18
+
19
+ s.add_dependency "chunky_png", "~> 1.2.8"
20
+ end
data/lib/identicon.rb ADDED
@@ -0,0 +1,147 @@
1
+ require 'digest'
2
+ require 'chunky_png'
3
+
4
+ # Generates GitHub-like identicons using MD5 hashes
5
+ class Identicon
6
+ class << self
7
+ # Generates a identicon using the data:image protocol.
8
+ # Usefull for web applications.
9
+ #
10
+ # === Parameters
11
+ # * data = The data that will be converted to a identicon
12
+ # * size = (Optional) The image size. Defaults to 420px
13
+ # * background = (Optional) The background color to be used in a rgb array. ([255, 255, 255])
14
+
15
+ def data_url_for(data, size = 420, background = nil)
16
+ img = self.new data, size
17
+ img.background = background unless background.nil?
18
+ img.generate_image
19
+ img.to_data_url
20
+ end
21
+
22
+ # Generates a identicon and stores it in a file
23
+ # Usefull for web applications.
24
+ #
25
+ # === Parameters
26
+ # * data = The data that will be converted to a identicon
27
+ # * path = The path where the file will be stored
28
+ # * size = (Optional) The image size. Defaults to 420px
29
+ # * background = (Optional) The background color to be used in a rgb array. ([255, 255, 255])
30
+ def file_for(data, path, size = 420, background = nil)
31
+ img = self.new data, size
32
+ img.background = background unless background.nil?
33
+ img.generate_image
34
+ img.save(path)
35
+ end
36
+ end
37
+ attr_accessor :size, :data, :background
38
+ CHARS_REGEX = /(\w)(\w)/
39
+
40
+ COLORS = {
41
+ :a => [196, 108, 168],
42
+ :b => [127, 219, 107],
43
+ :c => [83, 89, 215],
44
+ :d => [111, 198, 148],
45
+ :e => [186, 130, 82],
46
+ :f => [113, 66, 197],
47
+ :"0" => [201, 186, 84],
48
+ :"1" => [114, 153, 205],
49
+ :"2" => [141, 108, 204],
50
+ :"3" => [75, 183, 114],
51
+ :"4" => [192, 86, 82],
52
+ :"5" => [199, 115, 209],
53
+ :"6" => [206, 77, 94],
54
+ :"7" => [211, 135, 162],
55
+ :"8" => [71, 194, 197],
56
+ :"9" => [188, 110, 93]
57
+ }
58
+
59
+ # The +new+ method initializes a new Identicon instance
60
+ #
61
+ # === Parameters
62
+ # * data = The data that will be converted to a identicon
63
+ # * size = The image size
64
+ def initialize(data, size)
65
+ @hash = Digest::MD5.hexdigest(data.to_s)
66
+ @chars = @hash.scan(CHARS_REGEX)
67
+ @color = [0, 0, 0]
68
+ @size = size
69
+ @pixel_ratio = (size / 6).round
70
+ @image_size = @size - (@pixel_ratio / 1.5).round
71
+ @square_array = Hash.new { |h| h = Hash.new(false) }
72
+ @background = [240, 240, 240]
73
+ convert_hash
74
+ end
75
+
76
+
77
+ def size=(value)
78
+ @size = value
79
+ @pixel_ratio = (value / 5).round
80
+ value
81
+ end
82
+
83
+ def data=(value)
84
+ @hash = Digest::MD5.hexdigest(data.to_s)
85
+ @chars = @hash.scan(CHARS_REGEX)
86
+ convert_hash
87
+ value
88
+ end
89
+
90
+ private
91
+ def convert_hash
92
+ chars = @chars.map { |p|
93
+ p[1]
94
+ }
95
+ chars.each_index { |i|
96
+ char = chars[i]
97
+ sqIndex = (i / 3).round
98
+ @square_array[i/3] = [] unless @square_array.member? sqIndex
99
+
100
+ if(i % 3 == 0)
101
+ @square_array[i/3][0] = h_to_b char
102
+ @square_array[i/3][4] = h_to_b char
103
+ elsif(i % 3 == 1)
104
+ @square_array[i/3][1] = h_to_b char
105
+ @square_array[i/3][3] = h_to_b char
106
+ else
107
+ @square_array[i/3][2] = h_to_b char
108
+ end
109
+ }
110
+ color = :a
111
+ 0.upto(5) { color = chars.shift.to_sym }
112
+ @color = COLORS[color]
113
+ end
114
+
115
+ def h_to_b(value)
116
+ value.bytes.first % 2 == 0
117
+ end
118
+
119
+ public
120
+ # Process the specified data and generates the result
121
+ def generate_image
122
+ pImage = ChunkyPNG::Image.new(@image_size, @image_size, ChunkyPNG::Color::rgb(@background[0], @background[1], @background[2]))
123
+ @image = ChunkyPNG::Image.new(@size, @size, ChunkyPNG::Color::rgb(@background[0], @background[1], @background[2]))
124
+ @square_array.each_key { |lineKey|
125
+ lineValue = @square_array[lineKey]
126
+ lineValue.each_index { |colKey|
127
+ colValue = lineValue[colKey]
128
+ if(colValue)
129
+ pImage.rect(colKey * @pixel_ratio, lineKey * @pixel_ratio, (colKey + 1) * @pixel_ratio, (lineKey + 1) * @pixel_ratio, ChunkyPNG::Color::TRANSPARENT, ChunkyPNG::Color.rgb(@color[0], @color[1], @color[2]))
130
+ end
131
+ }
132
+ }
133
+ @image.compose!(pImage, @pixel_ratio / 2, @pixel_ratio / 2)
134
+ end
135
+
136
+ # Exports the result to a data:image format.
137
+ def to_data_url
138
+ @image.to_data_url
139
+ end
140
+
141
+ # Exports the result to a file
142
+ # === Parameters
143
+ # * path = Path where the image will be stored
144
+ def save(path)
145
+ @image.save(path)
146
+ end
147
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: identicon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Victor Gama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chunky_png
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.8
27
+ description: A simple github-like identicons generator.
28
+ email:
29
+ - victor@insitelabs.com.br
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - LICENSE
36
+ - README.md
37
+ - identicon.gemspec
38
+ - lib/identicon.rb
39
+ homepage: http://github.com/victorgama/identicon
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.7
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: A simple github-like identicons generator.
63
+ test_files: []