identiconify 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eee400110cb4bc5725c78c80660d096234dc8b66
4
- data.tar.gz: f18b0408a27534063be413e461bc387713ab7cfc
3
+ metadata.gz: 9812922e146b23b22a72f1ef7ba813a833ca3f4c
4
+ data.tar.gz: e08d764334018ce4a69113c4df489e4ca71bcff8
5
5
  SHA512:
6
- metadata.gz: 2c1250f3827a233091811db3277c5120cab852fed2e9f9618ddec02d92f2938bab4d8d5166db8aac4af435b43894e50f1c4dd0c8c11ee0269b4c9fcc155c355f
7
- data.tar.gz: dd5c602bfcb43c70137f577e3f0b6c93d02916fae2b977244eea1ae912fa6704c4010ce93cec07c0b2026db6baab02cb41fc4f71141c2566a016176866613f74
6
+ metadata.gz: 19939b39766680b9c4cc03f47908c5a409307d24c8e7aa5c1cbcade0958c0beec7f9d3b933e872644f9c4f70253411d1aaf5f96b502418460cbdf2043728e7c6
7
+ data.tar.gz: 203e43620492f67e7f32aa1c0adb8e74199f2efba85bf2320800c0b6c1903fa32753e04c50b4293cc7e95529ea7dbdda52b7dcc5dec1e3cc7502d6113ec2cb68
data/identiconify.gemspec CHANGED
@@ -23,5 +23,4 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_runtime_dependency 'chunky_png', '~> 1.2', '>= 1.2.9'
26
- spec.add_runtime_dependency 'siphash', '~> 0.0', '>= 0.0.1'
27
26
  end
data/lib/identiconify.rb CHANGED
@@ -1,54 +1,95 @@
1
1
  require "identiconify/version"
2
2
  require "chunky_png"
3
- require "siphash"
3
+ require "digest"
4
4
 
5
5
  module Identiconify
6
6
  class Identicon
7
- HASH_KEY = "61616f73646a6173646a616f7369646a"
8
-
9
- attr_reader :string,
10
- :square_size,
11
- :row_count,
12
- :size,
13
- :inverse_offset,
14
- :colors
7
+ DEFAULT_HASH_PROVIDER = ->(string) {
8
+ Digest::SHA1.hexdigest(string).to_i(16)
9
+ }
15
10
 
16
11
  def initialize(string, options={})
17
12
  @string = string
18
13
  @size = options.fetch(:size) { 250 }
19
- @row_count = 5
20
- @square_size = @size / @row_count
14
+ @colors = options.fetch(:colors) { :default }.to_sym
15
+ @hash_provider = options.fetch(:hash_provider) { DEFAULT_HASH_PROVIDER }
16
+ end
17
+
18
+ def to_png_blob
19
+ render.to_blob :fast_rgba
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :string, :size, :colors, :hash_provider
25
+
26
+ def render
27
+ bits = hash.to_s(2).split("").map(&:to_i)
28
+
29
+ # Drop the first three bytes that are used for the color
30
+ bits.drop(3*8)
31
+
32
+ png = ChunkyPNG::Image.new(size, size, bg_color)
33
+
34
+ coordinates.each do |row, column|
35
+ render_square(row, column, png) if bits.shift == 1
36
+ end
37
+
38
+ png
39
+ end
40
+
41
+ def render_square(row, column, png)
42
+ x0 = column*square_size
43
+ y0 = row*square_size
44
+ x1 = (column+1)*square_size-1
45
+ y1 = (row+1)*square_size-1
46
+ png.rect(x0, y0, x1, y1, color, color)
47
+
48
+ # Inverse the x coordinates making the image mirrored vertically
49
+ x0 = size-(column+1)*square_size-inverse_offset
50
+ x1 = size-column*square_size-inverse_offset-1
51
+ png.rect(x0, y0, x1, y1, color, color)
52
+ end
53
+
54
+ def row_count
55
+ 7
56
+ end
57
+
58
+ def column_count
59
+ row_count.even? ? row_count / 2 : row_count / 2 + 1
60
+ end
61
+
62
+ def square_size
63
+ size / row_count
64
+ end
65
+
66
+ def inverse_offset
21
67
  # Since we can't draw subpixels we need to calculate how much we have to
22
68
  # offset the inverted version of the identicon to not create gaps or
23
69
  # overlaps in the middle of the image.
24
- @inverse_offset = @size - @square_size * @row_count
25
- @colors = options.fetch(:colors) { :default }.to_sym
70
+ size - square_size * row_count
26
71
  end
27
72
 
28
- def column_count
29
- row_count.even? ? row_count/2 : row_count/2+1
73
+ def hash
74
+ hash_provider.call(string)
30
75
  end
31
76
 
32
- def color_for_hash(hash)
33
- # Use the three first bytes of the hash to generate a color
34
- r = hash & 0xff
35
- g = (hash >> 8) & 0xff
36
- b = (hash >> 16) & 0xff
37
- transform_color ChunkyPNG::Color.rgb(r,g,b)
77
+ def coordinates
78
+ rows = 0.upto(row_count-1).to_a
79
+ columns = 0.upto(column_count-1).to_a
80
+ rows.product(columns)
38
81
  end
39
82
 
40
- def greyscale(color)
41
- ChunkyPNG::Color.to_grayscale(color)
83
+ def bg_color
84
+ ChunkyPNG::Color::TRANSPARENT
42
85
  end
43
86
 
44
- def tint(color)
45
- r = ChunkyPNG::Color.r(color)
46
- g = ChunkyPNG::Color.g(color)
47
- b = ChunkyPNG::Color.b(color)
48
- r += (0.3*(255-r)).round
49
- g += (0.3*(255-g)).round
50
- b += (0.3*(255-b)).round
51
- ChunkyPNG::Color.rgb(r,g,b)
87
+ def color
88
+ # Use the three first bytes of the hash to generate a color
89
+ r = hash & 255
90
+ g = (hash >> 8) & 255
91
+ b = (hash >> 16) & 255
92
+ transform_color ChunkyPNG::Color.rgb(r,g,b)
52
93
  end
53
94
 
54
95
  def transform_color(color)
@@ -59,34 +100,19 @@ module Identiconify
59
100
  end
60
101
  end
61
102
 
62
- def to_png_blob
63
- hash = SipHash.digest(HASH_KEY, string)
64
-
65
- color = color_for_hash(hash)
66
- bg_color = ChunkyPNG::Color::TRANSPARENT
103
+ def greyscale(color)
104
+ ChunkyPNG::Color.to_grayscale(color)
105
+ end
67
106
 
68
- # Remove the used three color bytes
69
- hash >>= 24
107
+ def tint(color)
108
+ r = ChunkyPNG::Color.r(color)
109
+ g = ChunkyPNG::Color.g(color)
110
+ b = ChunkyPNG::Color.b(color)
111
+ ChunkyPNG::Color.rgb(tint_component(r),tint_component(g),tint_component(b))
112
+ end
70
113
 
71
- png = ChunkyPNG::Image.new(size, size, bg_color)
72
- 0.upto(row_count-1).each do |row|
73
- 0.upto(column_count-1).each do |column|
74
- if hash & 1 == 1
75
- x0 = column*square_size
76
- y0 = row*square_size
77
- x1 = (column+1)*square_size-1
78
- y1 = (row+1)*square_size-1
79
- png.rect(x0, y0, x1, y1, color, color)
80
-
81
- # Inverse the x coordinates making the image mirrored vertically
82
- x0 = size-(column+1)*square_size-inverse_offset
83
- x1 = size-column*square_size-inverse_offset-1
84
- png.rect(x0, y0, x1, y1, color, color)
85
- end
86
- hash >>= 1
87
- end
88
- end
89
- png.to_blob :fast_rgba
114
+ def tint_component(component)
115
+ component + (0.3 * (255 - component)).round
90
116
  end
91
117
  end
92
- end
118
+ end
@@ -1,3 +1,3 @@
1
1
  module Identiconify
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/test/test.rb CHANGED
@@ -9,4 +9,4 @@ filename = File.join(tmpdir, "image.png")
9
9
  File.open(filename, "w") do |file|
10
10
  file.write(png_data)
11
11
  end
12
- system "open #{filename}"
12
+ system "open #{filename}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: identiconify
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
  - calleerlandsson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-03 00:00:00.000000000 Z
11
+ date: 2013-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,26 +58,6 @@ dependencies:
58
58
  - - '>='
59
59
  - !ruby/object:Gem::Version
60
60
  version: 1.2.9
61
- - !ruby/object:Gem::Dependency
62
- name: siphash
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ~>
66
- - !ruby/object:Gem::Version
67
- version: '0.0'
68
- - - '>='
69
- - !ruby/object:Gem::Version
70
- version: 0.0.1
71
- type: :runtime
72
- prerelease: false
73
- version_requirements: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: '0.0'
78
- - - '>='
79
- - !ruby/object:Gem::Version
80
- version: 0.0.1
81
61
  description: Identiconify makes it super simple to generate identicons representing
82
62
  string values such as usernames or ip addresses.
83
63
  email:
@@ -115,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
95
  version: '0'
116
96
  requirements: []
117
97
  rubyforge_project:
118
- rubygems_version: 2.0.6
98
+ rubygems_version: 2.0.3
119
99
  signing_key:
120
100
  specification_version: 4
121
101
  summary: Super simple identicons