identiconify 0.0.1 → 0.0.2

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: 0ae4048275190d0183b3c2af76fe7e53b5179c0f
4
- data.tar.gz: 9afeeff01281022b2cac056bb671e3ba15a78f4d
3
+ metadata.gz: 437de306876ca2063bb4da1d21c071a22fc2ab6f
4
+ data.tar.gz: 10db2215c471afbef61efb9cec4c1260fb04dc5e
5
5
  SHA512:
6
- metadata.gz: d6e19538db2b8529837d03cbb47ecffa4f49113eb7dcf4bae4095d2cd959cc5458b0ceec1c5890ee0f120ed0ed264496af046311cb590b582af8822dd2f3f3a6
7
- data.tar.gz: 68dad38abee7c6997c8b80283407ca5f38132fb1765a20b0bffe36aeca691ed08d92788d2b0658eccdfa1a2397bfbe9c0ebbb1ce653d2d83f1275da40f52ada3
6
+ metadata.gz: 36d54596ba82a24cd92ef29a75fa4c1ffa856f30331a42ad93e9162c4324240a3e23df780543bcb8898fd1a358564f62b2bb63cf2f7b0f4c6a5ae580f913c2da
7
+ data.tar.gz: 1edda89f727358415794d332c20b5f9b35cf8d212c4a5aa00eea2165638ea0b1450f2360c2b4a425b519640c5d85358f24b82a9feccc6f06dae2a25efcbaf57f
data/README.md CHANGED
@@ -7,7 +7,7 @@ values such as usernames or ip addresses.
7
7
 
8
8
  Add this line to your application's Gemfile:
9
9
 
10
- gem 'identiconify'
10
+ gem "identiconify"
11
11
 
12
12
  And then execute:
13
13
 
@@ -22,7 +22,7 @@ Or install it yourself as:
22
22
  Require the Identiconify
23
23
 
24
24
  ```ruby
25
- require 'identiconify'
25
+ require "identiconify"
26
26
  ```
27
27
 
28
28
  Now you have access to the Identiconify::Identicon class which can be use as
@@ -32,13 +32,23 @@ such:
32
32
  string = "this is my string"
33
33
  identicon = Identiconify::Identicon.new(string)
34
34
  png_data = identicon.to_png_blob
35
+ ```
36
+
37
+ The png data can then be written to disk or be sent over an http connection.
35
38
 
36
- # The png data can then be written to disk or be sent over an http connection
37
- File.open('image.png', 'w') do |file|
39
+ ```ruby
40
+ File.open("image.png", "w") do |file|
38
41
  file.write(png_data)
39
42
  end
40
43
  ```
41
44
 
45
+ To specify the desired size of the identicon (in pixels), pass the widht option
46
+ to the constructor.
47
+
48
+ ```ruby
49
+ identicon = Identiconify::Identicon.new(string, width: 512)
50
+ ```
51
+
42
52
  ## Contributing
43
53
 
44
54
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module Identiconify
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/identiconify.rb CHANGED
@@ -6,22 +6,23 @@ module Identiconify
6
6
  class Identicon
7
7
  HASH_KEY = "61616f73646a6173646a616f7369646a"
8
8
 
9
- attr_reader :string, :square_size, :row_count
9
+ attr_reader :string, :square_size, :row_count, :width, :inverse_offset
10
10
 
11
- def initialize(string)
11
+ def initialize(string, options={})
12
12
  @string = string
13
- @square_size = 50
13
+ @width = options.fetch(:width) { 250 }
14
14
  @row_count = 5
15
+ @square_size = @width / @row_count
16
+ # Since we can't draw subpixels we need to calculate how much we have to
17
+ # offset the inverted version of the identicon to not create gaps or
18
+ # overlaps in the middle of the image.
19
+ @inverse_offset = @width - @square_size * @row_count
15
20
  end
16
21
 
17
22
  def column_count
18
23
  row_count.even? ? row_count/2 : row_count/2+1
19
24
  end
20
25
 
21
- def width
22
- square_size * row_count
23
- end
24
-
25
26
  def to_png_blob
26
27
  hash = SipHash.digest(HASH_KEY, string)
27
28
 
@@ -37,8 +38,8 @@ module Identiconify
37
38
  hash >>= 24
38
39
 
39
40
  png = ChunkyPNG::Image.new(width, width, bg_color)
40
- 0.upto(row_count).each do |row|
41
- 0.upto(column_count).each do |column|
41
+ 0.upto(row_count-1).each do |row|
42
+ 0.upto(column_count-1).each do |column|
42
43
  if hash & 1 == 1
43
44
  x0 = column*square_size
44
45
  y0 = row*square_size
@@ -47,8 +48,8 @@ module Identiconify
47
48
  png.rect(x0, y0, x1, y1, color, color)
48
49
 
49
50
  # Inverse the x coordinates making the image mirrored vertically
50
- x0 = width-(column+1)*square_size
51
- x1 = width-column*square_size-1
51
+ x0 = width-(column+1)*square_size-inverse_offset
52
+ x1 = width-column*square_size-inverse_offset-1
52
53
  png.rect(x0, y0, x1, y1, color, color)
53
54
  end
54
55
  hash >>= 1
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.1
4
+ version: 0.0.2
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-02 00:00:00.000000000 Z
11
+ date: 2013-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler