penticon 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/LICENSE +8 -0
- data/README.md +0 -0
- data/lib/penticon.rb +26 -0
- data/lib/penticon/penticons.rb +88 -0
- data/lib/penticon/svgrb.rb +46 -0
- data/lib/penticon/utils.rb +27 -0
- data/penticon.gemspec +12 -0
- metadata +54 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright © 2015 Penticons Authors
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
5
|
+
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
7
|
+
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
File without changes
|
data/lib/penticon.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
require "penticon/utils"
|
4
|
+
require "penticon/svgrb"
|
5
|
+
require "penticon/penticons"
|
6
|
+
|
7
|
+
class Penticon
|
8
|
+
|
9
|
+
def self.uri_image(string='nkman')
|
10
|
+
@penticons = Penticons.new(Utils.new, Svg.new)
|
11
|
+
base64_str = base64_string(string)
|
12
|
+
return "url(data:image/svg+xml;base64,#{base64_str.gsub! "\n", ''});"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.base64_string(string)
|
16
|
+
svg_str = generate(string)
|
17
|
+
base64_str = Base64.encode64(svg_str)
|
18
|
+
return base64_str
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.generate(string)
|
22
|
+
@penticons.create(string)
|
23
|
+
return @penticons.svg_string()
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
class Penticons
|
2
|
+
|
3
|
+
def initialize(util, svg)
|
4
|
+
@hash = nil
|
5
|
+
@svg = nil
|
6
|
+
@tile = []
|
7
|
+
@tile_color = []
|
8
|
+
@util = util
|
9
|
+
@svg = svg
|
10
|
+
end
|
11
|
+
|
12
|
+
def create(string)
|
13
|
+
@hash = @util.hash(string)
|
14
|
+
end
|
15
|
+
|
16
|
+
def svg_string
|
17
|
+
generate_background()
|
18
|
+
generate_foreground()
|
19
|
+
|
20
|
+
return @svg.get_string()
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate_background
|
24
|
+
size = @util.instance_variable_get(:@penticon_size)
|
25
|
+
|
26
|
+
@svg.set_width(size)
|
27
|
+
@svg.set_height(size)
|
28
|
+
|
29
|
+
args = {:fill => @util.instance_variable_get(:@bg)}
|
30
|
+
@svg.rect(0, 0, size, size, args)
|
31
|
+
end
|
32
|
+
|
33
|
+
def generate_foreground
|
34
|
+
mark_tiles()
|
35
|
+
fill_tiles()
|
36
|
+
reflect_tiles()
|
37
|
+
|
38
|
+
place_tiles()
|
39
|
+
end
|
40
|
+
|
41
|
+
def mark_tiles
|
42
|
+
i = 0
|
43
|
+
|
44
|
+
while i < 15 do
|
45
|
+
@tile[i] = @hash[i].ord >> Integer(8-((i%8)+1)) & 1
|
46
|
+
i = i + 1;
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def fill_tiles
|
51
|
+
|
52
|
+
i = 0
|
53
|
+
|
54
|
+
while i < 15 do
|
55
|
+
if @tile[i] == 1
|
56
|
+
dec = Integer(@hash[i], 16)
|
57
|
+
index = @util.mapp(dec)
|
58
|
+
@tile_color[i] = @util.instance_variable_get(:@colors)[index]
|
59
|
+
else
|
60
|
+
@tile_color[i] = @util.instance_variable_get(:@colors)[0]
|
61
|
+
end
|
62
|
+
i = i + 1
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def reflect_tiles
|
67
|
+
i = 15
|
68
|
+
|
69
|
+
while i < 25 do
|
70
|
+
@tile_color[i] = @tile_color[i-(10*(i/10))]
|
71
|
+
i = i + 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def place_tiles
|
76
|
+
padding = @util.instance_variable_get(:@padding)
|
77
|
+
size = @util.instance_variable_get(:@tile_size)
|
78
|
+
|
79
|
+
i = 0
|
80
|
+
while i < 25 do
|
81
|
+
|
82
|
+
args = {:fill => @tile_color[i]}
|
83
|
+
@svg.rect(padding+((size+padding)*(i/5)), padding+((size+padding)*(i%5)), size, size, args)
|
84
|
+
i = i + 1
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class Svg
|
2
|
+
|
3
|
+
def initialize
|
4
|
+
@svg_string = ""
|
5
|
+
@height = nil
|
6
|
+
@width = nil
|
7
|
+
end
|
8
|
+
|
9
|
+
def set_width(w)
|
10
|
+
@width = Integer(w)
|
11
|
+
end
|
12
|
+
|
13
|
+
def set_height(h)
|
14
|
+
@height = Integer(h)
|
15
|
+
end
|
16
|
+
|
17
|
+
def header()
|
18
|
+
return "<svg xmlns='http://www.w3.org/2000/svg' width='#{@width}' height='#{@height}'>"
|
19
|
+
end
|
20
|
+
|
21
|
+
def footer()
|
22
|
+
return "</svg>"
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_string()
|
26
|
+
return header() + @svg_string + footer()
|
27
|
+
end
|
28
|
+
|
29
|
+
def rect(x, y, w, h, args)
|
30
|
+
write_arg = write_args(args)
|
31
|
+
rect_str = "<rect x='#{x}' y='#{y}' width='#{w}' height='#{h}' #{write_arg} />"
|
32
|
+
@svg_string += rect_str
|
33
|
+
end
|
34
|
+
|
35
|
+
def write_args(args)
|
36
|
+
#args = {:fill=>\"this\"}
|
37
|
+
args = args.to_s
|
38
|
+
args.gsub! '{', ''
|
39
|
+
args.gsub! '}', ''
|
40
|
+
args.gsub! '>', ''
|
41
|
+
args.gsub! ':', ''
|
42
|
+
|
43
|
+
return args
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
|
3
|
+
class Utils
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
|
7
|
+
@bg ='#fefefe'
|
8
|
+
@colors = []
|
9
|
+
@colors[0] = "#eeeeee"
|
10
|
+
@colors[1] = "#d6e685"
|
11
|
+
@colors[2] = "#8cc665"
|
12
|
+
@colors[3] = "#44a340"
|
13
|
+
@colors[4] = "#1e6823"
|
14
|
+
|
15
|
+
@padding = 5
|
16
|
+
@tile_size = 30
|
17
|
+
@penticon_size = (5 * @tile_size) + 30
|
18
|
+
end
|
19
|
+
|
20
|
+
def hash(string)
|
21
|
+
return Digest::SHA1.hexdigest string
|
22
|
+
end
|
23
|
+
|
24
|
+
def mapp(a)
|
25
|
+
return Integer((4 * Integer(a)) / 15)
|
26
|
+
end
|
27
|
+
end
|
data/penticon.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'penticon'
|
3
|
+
s.version = '1.0.0'
|
4
|
+
s.date = '2015-05-15'
|
5
|
+
s.summary = "An implementation of Penticons in ruby programming language"
|
6
|
+
s.description = "Identicons similar to GitHub's contribution activity calendar"
|
7
|
+
s.authors = ["Nairitya Khilari", "Pravendra Singh Rathore"]
|
8
|
+
s.email = 'nairityakhilari@gmail.com'
|
9
|
+
s.files = `git ls-files`.split("\n") | Dir.glob('lib/penticon/*')
|
10
|
+
s.homepage = ''
|
11
|
+
s.license = 'MIT'
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: penticon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nairitya Khilari
|
9
|
+
- Pravendra Singh Rathore
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2015-05-15 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: Identicons similar to GitHub's contribution activity calendar
|
16
|
+
email: nairityakhilari@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- lib/penticon.rb
|
25
|
+
- lib/penticon/penticons.rb
|
26
|
+
- lib/penticon/svgrb.rb
|
27
|
+
- lib/penticon/utils.rb
|
28
|
+
- penticon.gemspec
|
29
|
+
homepage: ''
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.23
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: An implementation of Penticons in ruby programming language
|
54
|
+
test_files: []
|