avatarro 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f21bca192dbc522b1a059df19dec16799003b24788cb5b8142d7ca041ee09ee5
4
+ data.tar.gz: 9beacfa4c1b188177e8ae0701bab1c8398b48a3462129980f20883ba074f15cd
5
+ SHA512:
6
+ metadata.gz: 46d3ed500dd01eb7765c751ce9cdb1ae756d6b0cd6356dc982511004eae31202983c427ab3244c689dfe0fd8b64ae30c93f9c9ae29bd652771d98a0758eebf16
7
+ data.tar.gz: e477c29bbe3a49b39f0dc6d045e16c8be7c7e7316640c71fefb63fa40f06a3c337bfa6c888366229ec1df148e671eaed903585c83915f2ccc55b95b09c516e62
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Igor Kasyanchuk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # Avatarro
2
+
3
+ Google-style avatar generator for your app.
4
+
5
+ This gem generates SVG which can be used inside your HTML or img(src) to display google-style avatar with initials.
6
+
7
+ Sample:
8
+
9
+ [<img src="https://github.com/igorkasyanchuk/avatarro/blob/main/docs/avataro.png?raw=true"
10
+ />](https://github.com/igorkasyanchuk/avatarro/blob/main/docs/avataro.png?raw=true)
11
+
12
+ ## Usage
13
+
14
+ You can generate avatar using 2 methods, depending how you need to use it. Using `Avatarro.svg` method to embed svg inside HTML or `Avatarro.image` method to generate Base64 image use with image_tag.
15
+
16
+ ```erb
17
+ <%= image_tag Avatarro.image(current_user.full_name), size: '24x24' %>
18
+ <%= image_tag Avatarro.image("Igor Kasyanchuk", solid: true), size: '24x24' %>
19
+ <%= image_tag Avatarro.image("IK"), size: '32x32' %>
20
+ <%= raw Avatarro.svg("IK") %>
21
+ ```
22
+
23
+ It you need rounded avatars just add CSS to the img.
24
+
25
+ ```erb
26
+ <%= image_tag Avatarro.image("IK"), size: '32x32', style: 'border-radius: 50%' %>
27
+ ```
28
+
29
+ More samples (shown on screenshot above):
30
+
31
+ ```erb
32
+ <div class="card">
33
+ <h3>Avatar Generator for the<br>APP</h3>
34
+ <div class="people round">
35
+ <%= image_tag Avatarro.image("OI"), size: '16x16' %>
36
+ <%= image_tag Avatarro.image("JS"), size: '16x16' %>
37
+ <%= image_tag Avatarro.image("MK"), size: '16x16' %>
38
+ <%= image_tag Avatarro.image("ZP"), size: '16x16' %>
39
+ </div>
40
+ </div>
41
+
42
+ <div class="card">
43
+ <h3>Generates SVG for the html</h3>
44
+ <div class="people round">
45
+ <%= image_tag Avatarro.image("OI", solid: true), size: '24x24' %>
46
+ <%= image_tag Avatarro.image("JS", solid: true), size: '24x24' %>
47
+ <%= image_tag Avatarro.image("MK", solid: true), size: '24x24' %>
48
+ <%= image_tag Avatarro.image("ZP", solid: true), size: '24x24' %>
49
+ <%= image_tag Avatarro.image("😁", solid: true), size: '24x24' %>
50
+ <%= image_tag Avatarro.image("😂", solid: true), size: '24x24' %>
51
+ </div>
52
+ </div>
53
+
54
+ <div class="card">
55
+ <h3>Or can be used as<br> img src="..."</h3>
56
+ <div class="people">
57
+ <%= image_tag Avatarro.image("ZO", random: true), size: '32x32' %>
58
+ <%= image_tag Avatarro.image("AB", random: true), size: '32x32' %>
59
+ <%= image_tag Avatarro.image("ФЯ", random: true), size: '32x32' %>
60
+ <%= image_tag Avatarro.image("DA", random: true), size: '32x32' %>
61
+ </div>
62
+ </div>
63
+
64
+ <div class="clear"></div>
65
+
66
+ <div class="card">
67
+ <%= raw Avatarro.svg("IK") %>
68
+ <%= raw Avatarro.svg("WS") %>
69
+ <%= raw Avatarro.svg("AP") %>
70
+ <hr>
71
+ <%= image_tag Avatarro.image("WW") %>
72
+ <%= image_tag Avatarro.image("OI", random: true) %>
73
+ <%= image_tag Avatarro.image("ZA"), style: 'border-radius: 50%', size: '32x32' %>
74
+ <%= image_tag Avatarro.image("OZ", random: true), style: 'border-radius: 50%', size: '16x16' %>
75
+ </div>
76
+
77
+
78
+ <div class="card">
79
+ <%= image_tag Avatarro.image("WA", random: true, width: 300, height: 300) %>
80
+ </div>
81
+
82
+ <div class="card">
83
+ <%= image_tag Avatarro.image("⚡", random: true, width: 300, height: 300) %>
84
+ </div>
85
+ ```
86
+
87
+ ## Configuration
88
+
89
+ Example of the possible options (see args hash):
90
+
91
+ ```ruby
92
+ def Avatarro.template(initials, args = {})
93
+ data = args[:random] ? GRADIENTS.sample : GRADIENTS[index(initials) % GRADIENTS.size]
94
+ start = args[:start].presence || data[0]
95
+ finish = args[:solid] ? data[0] : (args[:finish].presence || data[1])
96
+ degree = args[:degree].presence || data[2].presence || 90
97
+ color = args[:color].presence || data[3].presence || '#FFF'
98
+ width = args[:width].presence || 64
99
+ height = args[:height].presence || 64
100
+ ```
101
+
102
+ For example:
103
+
104
+ ```erb
105
+ <%= image_tag Avatarro.image("WA", random: true, width: 300, height: 300, color: 'red') %>
106
+ ```
107
+
108
+ ## Installation
109
+ Add this line to your application's Gemfile:
110
+
111
+ ```ruby
112
+ gem 'avatarro'
113
+ ```
114
+
115
+ And then execute:
116
+ ```bash
117
+ $ bundle
118
+ ```
119
+
120
+ Or install it yourself as:
121
+ ```bash
122
+ $ gem install avatarro
123
+ ```
124
+
125
+ ## Contributing
126
+ Contribution directions go here.
127
+
128
+ ## TODO
129
+
130
+ - CI
131
+ - better graphics and more gradients
132
+ - radial gradient
133
+
134
+ ## Credits
135
+
136
+ The idea of this gem came after reading an article: https://kukicola.io/posts/creating-google-like-letter-avatars-using-erb-generated-svgs/. We actually did something similar with HTML/CSS on our project too, but with SVG solution looks just better.
137
+
138
+ ## License
139
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ task default: :test
data/lib/avatarro.rb ADDED
@@ -0,0 +1,94 @@
1
+ require "avatarro/version"
2
+ require "avatarro/railtie"
3
+ require "base64"
4
+
5
+ module Avatarro
6
+ GRADIENTS = [
7
+ ["#ff9a9e", "#fad0c4", 45, '#444'],
8
+ ["#a18cd1", "#fbc2eb", 90],
9
+ ["#fad0c4", "#ffd1ff", 90],
10
+ ["#ffecd2", "#fcb69f", 90],
11
+ ["#ff8177", "#b12a5b", 90],
12
+ ["#fbc2eb", "#a6c1ee", 90],
13
+ ["#fdcbf1", "#e6dee9", 90, '#444'],
14
+ ["#a1c4fd", "#c2e9fb", 120],
15
+ ["#d4fc79", "#96e6a1", 120],
16
+ ["#cfd9df", "#e2ebf0", 90, '#444'],
17
+ ["#a6c0fe", "#f68084", 120],
18
+ ["#fccb90", "#d57eeb", 120],
19
+ ["#e0c3fc", "#8ec5fc", 120],
20
+ ["#f093fb", "#f5576c", 120],
21
+ ["#43e97b", "#38f9d7", 120],
22
+ ["#30cfd0", "#330867", 90],
23
+ ["#d299c2", "#fef9d7", 120],
24
+ ["#16d9e3", "#46aef7", 120],
25
+ ["#b8cbb8", "#cf6cc9", 90],
26
+ ["#fff1eb", "#ace0f9", 90, '#444'],
27
+ ["#0ba360", "#3cba92", 90],
28
+ ["#9795f0", "#fbc8d4", 90],
29
+ ["#93a5cf", "#e4efe9", 90],
30
+ ["#ffafbd", "#ffc3a0", 45],
31
+ ["#cc2b5e", "#753a88", 180],
32
+ ["#ee9ca7", "#ffdde1", 180],
33
+ ["#42275a", "#734b6d", 90],
34
+ ["#bdc3c7", "#2c3e50", 90],
35
+ ["#06beb6", "#48b1bf", 120],
36
+ ["#eb3349", "#f45c43", 120],
37
+ ["#56ab2f", "#a8e063", 90],
38
+ ["#614385", "#516395", 120],
39
+ ["#d66d75", "#e29587", 290],
40
+ ["#000428", "#004e92", 90],
41
+ ["#ddd6f3", "#faaca8", 90],
42
+ ["#7b4397", "#dc2430", 190],
43
+ ["#4568dc", "#b06ab3", 90],
44
+ ["#4568dc", "#b06ab3", 90],
45
+ ]
46
+
47
+
48
+ def Avatarro.image(initials, args = {})
49
+ "data:image/svg+xml;base64,#{Base64.encode64(svg(initials, args))}".html_safe
50
+ end
51
+
52
+ def Avatarro.svg(initials, args = {})
53
+ template(initials, args)
54
+ end
55
+
56
+ private
57
+
58
+ def Avatarro.index(initials)
59
+ initials.chars.inject(0) {|sum, e| sum+= e.ord; sum}
60
+ end
61
+
62
+ def Avatarro.template(initials, args = {})
63
+ data = args[:random] ? GRADIENTS.sample : GRADIENTS[index(initials) % GRADIENTS.size]
64
+ start = args[:start].presence || data[0]
65
+ finish = args[:solid] ? data[0] : (args[:finish].presence || data[1])
66
+ degree = args[:degree].presence || data[2].presence || 90
67
+ color = args[:color].presence || data[3].presence || '#FFF'
68
+ width = args[:width].presence || 64
69
+ height = args[:height].presence || 64
70
+
71
+ gradient_id = "gradient_#{Time.now.to_i}_#{rand(1_000_000)}"
72
+ %{<?xml version="1.0" encoding="UTF-8"?>
73
+ <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="#{width}" height="#{height}" viewBox="0 0 #{width} #{height}">
74
+ <linearGradient id="#{gradient_id}" gradientTransform="rotate(#{degree})">
75
+ <stop stop-color="#{start}" offset="0%" />
76
+ <stop stop-color="#{finish}" offset="100%" />
77
+ </linearGradient>
78
+ <rect width="100%" height="100%" fill="url(##{gradient_id})" />
79
+ <text fill="#{color}" font-family="Open Sans,Helvetica,Arial,sans-serif" font-size="#{1 + height / 2}" font-weight="500" x="50%" y="55%" dominant-baseline="middle" text-anchor="middle">
80
+ #{Avatarro.format_initials initials}
81
+ </text>
82
+ </svg>
83
+ }
84
+ end
85
+
86
+ def Avatarro.format_initials(initials)
87
+ if initials.size > 2
88
+ initials.scan(/([[:alpha:]])[[:alpha:]]*/).flatten.join.upcase
89
+ else
90
+ initials
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,4 @@
1
+ module Avatarro
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Avatarro
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :avatarro do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avatarro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Generate avatars (gmail-like) in your apps
42
+ email:
43
+ - igorkasyanchuk@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/avatarro.rb
52
+ - lib/avatarro/railtie.rb
53
+ - lib/avatarro/version.rb
54
+ - lib/tasks/avatarro_tasks.rake
55
+ homepage: https://github.com/igorkasyanchuk/avatarro
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ homepage_uri: https://github.com/igorkasyanchuk/avatarro
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.2.3
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Generate avatars (gmail-like) in your apps
79
+ test_files: []