5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/idcon.rb', line 5
def self.svg(string, dimension: 5, size: 10)
hash = Idcon::Hash::MD5.hash(string)
color = []
3.times do
color << (hash & 255)
hash >>= 8
end
squares = []
((dimension + 1) / 2).times do
squares << (0...dimension).map {|a| (hash >> a) & 1}
hash >>= dimension
end
squares += squares[0..-2].reverse
width = dimension * size
svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"#{width}\" height=\"#{width}\">"
attributes = "width=\"#{size}\" height=\"#{size}\" style=\"fill:rgb(#{color.join(',')});\""
squares.each_with_index do |col, xindex|
col.each_with_index do |square, yindex|
if square == 1
svg << "<rect x=\"#{xindex * size}\" y=\"#{yindex * size}\" #{attributes }/>"
end
end
end
svg << "</svg>"
end
|