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 +4 -4
- data/identiconify.gemspec +0 -1
- data/lib/identiconify.rb +84 -58
- data/lib/identiconify/version.rb +1 -1
- data/test/test.rb +1 -1
- metadata +3 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9812922e146b23b22a72f1ef7ba813a833ca3f4c
|
4
|
+
data.tar.gz: e08d764334018ce4a69113c4df489e4ca71bcff8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19939b39766680b9c4cc03f47908c5a409307d24c8e7aa5c1cbcade0958c0beec7f9d3b933e872644f9c4f70253411d1aaf5f96b502418460cbdf2043728e7c6
|
7
|
+
data.tar.gz: 203e43620492f67e7f32aa1c0adb8e74199f2efba85bf2320800c0b6c1903fa32753e04c50b4293cc7e95529ea7dbdda52b7dcc5dec1e3cc7502d6113ec2cb68
|
data/identiconify.gemspec
CHANGED
data/lib/identiconify.rb
CHANGED
@@ -1,54 +1,95 @@
|
|
1
1
|
require "identiconify/version"
|
2
2
|
require "chunky_png"
|
3
|
-
require "
|
3
|
+
require "digest"
|
4
4
|
|
5
5
|
module Identiconify
|
6
6
|
class Identicon
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
@
|
20
|
-
@
|
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
|
-
|
25
|
-
@colors = options.fetch(:colors) { :default }.to_sym
|
70
|
+
size - square_size * row_count
|
26
71
|
end
|
27
72
|
|
28
|
-
def
|
29
|
-
|
73
|
+
def hash
|
74
|
+
hash_provider.call(string)
|
30
75
|
end
|
31
76
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
41
|
-
ChunkyPNG::Color
|
83
|
+
def bg_color
|
84
|
+
ChunkyPNG::Color::TRANSPARENT
|
42
85
|
end
|
43
86
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
63
|
-
|
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
|
-
|
69
|
-
|
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
|
-
|
72
|
-
0.
|
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
|
data/lib/identiconify/version.rb
CHANGED
data/test/test.rb
CHANGED
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.
|
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-
|
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.
|
98
|
+
rubygems_version: 2.0.3
|
119
99
|
signing_key:
|
120
100
|
specification_version: 4
|
121
101
|
summary: Super simple identicons
|