identiconify 0.0.2 → 0.0.3

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: 437de306876ca2063bb4da1d21c071a22fc2ab6f
4
- data.tar.gz: 10db2215c471afbef61efb9cec4c1260fb04dc5e
3
+ metadata.gz: eee400110cb4bc5725c78c80660d096234dc8b66
4
+ data.tar.gz: f18b0408a27534063be413e461bc387713ab7cfc
5
5
  SHA512:
6
- metadata.gz: 36d54596ba82a24cd92ef29a75fa4c1ffa856f30331a42ad93e9162c4324240a3e23df780543bcb8898fd1a358564f62b2bb63cf2f7b0f4c6a5ae580f913c2da
7
- data.tar.gz: 1edda89f727358415794d332c20b5f9b35cf8d212c4a5aa00eea2165638ea0b1450f2360c2b4a425b519640c5d85358f24b82a9feccc6f06dae2a25efcbaf57f
6
+ metadata.gz: 2c1250f3827a233091811db3277c5120cab852fed2e9f9618ddec02d92f2938bab4d8d5166db8aac4af435b43894e50f1c4dd0c8c11ee0269b4c9fcc155c355f
7
+ data.tar.gz: dd5c602bfcb43c70137f577e3f0b6c93d02916fae2b977244eea1ae912fa6704c4010ce93cec07c0b2026db6baab02cb41fc4f71141c2566a016176866613f74
@@ -1,3 +1,3 @@
1
1
  module Identiconify
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/identiconify.rb CHANGED
@@ -6,38 +6,69 @@ module Identiconify
6
6
  class Identicon
7
7
  HASH_KEY = "61616f73646a6173646a616f7369646a"
8
8
 
9
- attr_reader :string, :square_size, :row_count, :width, :inverse_offset
9
+ attr_reader :string,
10
+ :square_size,
11
+ :row_count,
12
+ :size,
13
+ :inverse_offset,
14
+ :colors
10
15
 
11
16
  def initialize(string, options={})
12
17
  @string = string
13
- @width = options.fetch(:width) { 250 }
18
+ @size = options.fetch(:size) { 250 }
14
19
  @row_count = 5
15
- @square_size = @width / @row_count
20
+ @square_size = @size / @row_count
16
21
  # Since we can't draw subpixels we need to calculate how much we have to
17
22
  # offset the inverted version of the identicon to not create gaps or
18
23
  # overlaps in the middle of the image.
19
- @inverse_offset = @width - @square_size * @row_count
24
+ @inverse_offset = @size - @square_size * @row_count
25
+ @colors = options.fetch(:colors) { :default }.to_sym
20
26
  end
21
27
 
22
28
  def column_count
23
29
  row_count.even? ? row_count/2 : row_count/2+1
24
30
  end
25
31
 
26
- def to_png_blob
27
- hash = SipHash.digest(HASH_KEY, string)
28
-
32
+ def color_for_hash(hash)
29
33
  # Use the three first bytes of the hash to generate a color
30
34
  r = hash & 0xff
31
35
  g = (hash >> 8) & 0xff
32
36
  b = (hash >> 16) & 0xff
33
- a = 0xff
34
- color = ChunkyPNG::Color.rgba(r,g,b,a)
37
+ transform_color ChunkyPNG::Color.rgb(r,g,b)
38
+ end
39
+
40
+ def greyscale(color)
41
+ ChunkyPNG::Color.to_grayscale(color)
42
+ end
43
+
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)
52
+ end
53
+
54
+ def transform_color(color)
55
+ case colors
56
+ when :bw then greyscale(color)
57
+ when :tinted then tint(color)
58
+ else color
59
+ end
60
+ end
61
+
62
+ def to_png_blob
63
+ hash = SipHash.digest(HASH_KEY, string)
64
+
65
+ color = color_for_hash(hash)
35
66
  bg_color = ChunkyPNG::Color::TRANSPARENT
36
67
 
37
- # Remove the used three bytes
68
+ # Remove the used three color bytes
38
69
  hash >>= 24
39
70
 
40
- png = ChunkyPNG::Image.new(width, width, bg_color)
71
+ png = ChunkyPNG::Image.new(size, size, bg_color)
41
72
  0.upto(row_count-1).each do |row|
42
73
  0.upto(column_count-1).each do |column|
43
74
  if hash & 1 == 1
@@ -48,14 +79,14 @@ module Identiconify
48
79
  png.rect(x0, y0, x1, y1, color, color)
49
80
 
50
81
  # Inverse the x coordinates making the image mirrored vertically
51
- x0 = width-(column+1)*square_size-inverse_offset
52
- x1 = width-column*square_size-inverse_offset-1
82
+ x0 = size-(column+1)*square_size-inverse_offset
83
+ x1 = size-column*square_size-inverse_offset-1
53
84
  png.rect(x0, y0, x1, y1, color, color)
54
85
  end
55
86
  hash >>= 1
56
87
  end
57
88
  end
58
- png.to_blob :color_mode => ChunkyPNG::COLOR_INDEXED
89
+ png.to_blob :fast_rgba
59
90
  end
60
91
  end
61
92
  end
data/test/test.rb ADDED
@@ -0,0 +1,12 @@
1
+ require_relative '../lib/identiconify'
2
+
3
+ string = ARGV[0]
4
+ identicon = Identiconify::Identicon.new(string, size: 512, colors: :tinted)
5
+ png_data = identicon.to_png_blob
6
+ tmpdir = File.join(File.dirname(__FILE__), "tmp")
7
+ Dir.mkdir(tmpdir) unless File.directory?(tmpdir)
8
+ filename = File.join(tmpdir, "image.png")
9
+ File.open(filename, "w") do |file|
10
+ file.write(png_data)
11
+ end
12
+ system "open #{filename}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: identiconify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - calleerlandsson
@@ -94,6 +94,7 @@ files:
94
94
  - identiconify.gemspec
95
95
  - lib/identiconify.rb
96
96
  - lib/identiconify/version.rb
97
+ - test/test.rb
97
98
  homepage: https://github.com/calleerlandsson/identiconify
98
99
  licenses:
99
100
  - MIT
@@ -118,4 +119,5 @@ rubygems_version: 2.0.6
118
119
  signing_key:
119
120
  specification_version: 4
120
121
  summary: Super simple identicons
121
- test_files: []
122
+ test_files:
123
+ - test/test.rb