27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/caleidenticon.rb', line 27
def self.create_blob(input, options)
options = DEFAULT_OPTIONS.merge(options)
full_salt = "#{BCRYPT_SALT_HEAD}#{options[:salt]}."
options[:complexity] = [options[:complexity], 2].max
options[:scale] = [options[:scale], 1].max
options[:density] = [[options[:density], 2].max, 10].min
options[:spikiness] = [options[:spikiness], 1].max
raise ArgumentError.new('input cannot be empty') if input.nil? || input == ''
raise ArgumentError.new('salt must be a string of 21 ASCII chars') unless BCrypt::Engine.valid_salt?(full_salt)
raise ArgumentError.new('invalid colors') unless options[:colors].map{|array| array.size} == [3,3,3,3] &&
options[:colors].flatten.all?{|val| (0..255).include?(val)}
@debug = options[:debug]
debug("creating blob for input '#{input}'")
hash = BCrypt::Engine.hash_secret(input, full_salt)
hash.slice!(0, BCRYPT_SALT_HEAD.length) hash = (hash * options[:complexity]).bytes.inject {|a, b| (a << 8) + b}
debug("hash bits: #{hash.to_s(2).length}")
tint_rgb = Array.new(3) {hash >>= 8; hash >> 8 & 0xff}
debug("tint color: #{tint_rgb.inspect}")
colors = options[:colors].map do |option_rgb|
[ChunkyPNG::Color.rgba(
(option_rgb[0] + tint_rgb[0]) / 2, (option_rgb[1] + tint_rgb[1]) / 2, (option_rgb[2] + tint_rgb[2]) / 2, 0xff), ChunkyPNG::Color.rgba(
(option_rgb[0] * 2 + tint_rgb[0] * 5) / 9, (option_rgb[1] * 2 + tint_rgb[1] * 5) / 9, (option_rgb[2] * 2 + tint_rgb[2] * 5) / 9, 0xff)] end.flatten
color_shifts = Array.new(10) {hash >>= 3; hash >> 3 & 7} size_multiplicators = Array.new(10) {hash >>= 2; [(hash >> 2 & 3), 1].max}
debug("shifts: #{color_shifts.inspect} mults: #{size_multiplicators.inspect}")
grid_size = options[:complexity] * 2 + 1
unused_cells = [((options[:complexity] * options[:spikiness])/4), (grid_size - 2)].min
circle_radius = options[:scale]/2
margin = circle_radius * 3 image_size = (grid_size * options[:scale] + margin) * 2
image = ChunkyPNG::Image.new(image_size, image_size, ChunkyPNG::Color::TRANSPARENT)
sprinkle_inset = options[:complexity]/2 + 1
usable_space = [unused_cells - sprinkle_inset, 0].max
space_to_use = [usable_space, options[:corner_sprinkle]].min
sprinkle_range = space_to_use > 0 ? sprinkle_inset..(sprinkle_inset + space_to_use) : []
debug("grid size: #{grid_size} non-main: #{unused_cells} sprinkled: #{space_to_use}")
element_scarcity = 10 - options[:density]
rectangle_amount = (hash >> 1 & 1) + 1
rect_matcher = rectangle_amount == 1 ? 1 : 3
column, row = 0, 0
rectangles_per_row = {}
(grid_size ** 2).times do |i|
if column > grid_size row += 1
column = 0
end
in_sprinkle_range = sprinkle_range.include?(row) && sprinkle_range.include?(column)
in_primary_cells = row >= unused_cells
unless in_primary_cells || in_sprinkle_range
column += 1
next
end
hash >>= element_scarcity
if (hash >> element_scarcity & element_scarcity) == element_scarcity
x = (column + 1) * circle_radius * 2 + margin
y = (row + 1) * circle_radius * 2 + margin
vectors = [[x, y], [y, x], [image_size-x, y], [image_size-y, x], [x, image_size-y], [y, image_size-x], [image_size-x, image_size-y], [image_size-y, image_size-x]]
color = colors[color_shifts[i%10]]
element_size = circle_radius * size_multiplicators[i%10]
rectangles_per_row[row] = 0 unless rectangles_per_row.has_key?(row)
hash >>= rectangle_amount
if ((hash >> rectangle_amount & rect_matcher) != rect_matcher || rectangles_per_row[row] == 2)
rectangles_per_row[row] += 1
rect_width, rect_length = element_size, element_size/4
rect_width, rect_length = rect_length, rect_width if hash >> 1 & 1 == 1 hash >>= 1
vectors.each_with_index do |v, idx|
x_size, y_size = rect_width, rect_length
x_size, y_size = y_size, x_size if idx % 2 != 0
image.rect(v[0]-x_size, v[1]-y_size, v[0]+x_size, v[1]+y_size, color, color)
end
type = 'rect'
else
vectors.each {|v| image.circle(v[0], v[1], element_size, color, color)}
type = 'circle'
end
debug("cell #{i} color: #{color}, size: #{size_multiplicators[i%10]}, type: #{type}")
end
column += 1
end
image.to_blob color_mode: ChunkyPNG::COLOR_INDEXED
end
|