identiconify 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/identiconify/version.rb +1 -1
- data/lib/identiconify.rb +45 -14
- data/test/test.rb +12 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eee400110cb4bc5725c78c80660d096234dc8b66
|
4
|
+
data.tar.gz: f18b0408a27534063be413e461bc387713ab7cfc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c1250f3827a233091811db3277c5120cab852fed2e9f9618ddec02d92f2938bab4d8d5166db8aac4af435b43894e50f1c4dd0c8c11ee0269b4c9fcc155c355f
|
7
|
+
data.tar.gz: dd5c602bfcb43c70137f577e3f0b6c93d02916fae2b977244eea1ae912fa6704c4010ce93cec07c0b2026db6baab02cb41fc4f71141c2566a016176866613f74
|
data/lib/identiconify/version.rb
CHANGED
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,
|
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
|
-
@
|
18
|
+
@size = options.fetch(:size) { 250 }
|
14
19
|
@row_count = 5
|
15
|
-
@square_size = @
|
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 = @
|
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
|
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
|
-
|
34
|
-
|
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(
|
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 =
|
52
|
-
x1 =
|
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 :
|
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.
|
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
|