monogram 0.9.1

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: 41c278c31721307b66470cc91a5e2ccfd583d1501fea154a9b4af29aa8ce76b0
4
+ data.tar.gz: d8b1ae681f2ceb390cd37f6ea818fab329a1832e4fffa24eb15d23cd4868088f
5
+ SHA512:
6
+ metadata.gz: 8e643536245563599a7daff62350bb9416365ba328a29ec34ab4b825cac99e91f71735112a458a7123eb9cdfc621757d32bbc9b3da03cc561390b51eb3c1422e
7
+ data.tar.gz: dae67e25f1d1cc310fe4c1cf1ad8bc2646509a55d23ccaf79263aaa6171329616f57a1c5737deddab70fe0f750f5e8c4ea737e9eb8516d625fd2f9c6ff107aef
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+ require 'base64'
5
+
6
+ class Monogram
7
+ class Avatar
8
+ SHAPES = [
9
+ SQUARE = 'square',
10
+ CIRCLE = 'circle',
11
+ ROUNDED = 'rounded'
12
+ ].freeze
13
+
14
+ def initialize(initials:, background:, shape:)
15
+ @initials = initials
16
+ @background = background
17
+ @shape = shape
18
+ end
19
+
20
+ def data_url
21
+ "data:image/svg+xml;base64,#{Base64.encode64(svg)}"
22
+ end
23
+
24
+ def svg
25
+ bind_template('avatar')
26
+ end
27
+
28
+ def shape
29
+ bind_template("_#{@shape}")
30
+ end
31
+
32
+ # Scale the text size proportinaly based on the number of initials
33
+ # to ensure they all fit, with a max size of 26
34
+ def text_size
35
+ [(51.0 / @initials.chars.count).ceil, 26].min
36
+ end
37
+
38
+ private
39
+
40
+ def bind_template(file)
41
+ ERB.new(template(file)).result(binding)
42
+ end
43
+
44
+ def template(file)
45
+ File.read(template_path(file))
46
+ end
47
+
48
+ def template_path(file)
49
+ File.expand_path("../templates/#{file}.svg.erb", File.dirname(__FILE__))
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Monogram
4
+ class Config
5
+ COLOURS = %w[#B91C1C #B45309 #047857 #1D4ED8 #6D28D9].freeze
6
+ UPPERCASE = true
7
+ SHAPE = Avatar::SQUARE
8
+
9
+ attr_accessor :colours, :uppercase, :shape
10
+ alias colors= colours=
11
+
12
+ def initialize
13
+ reset!
14
+ end
15
+
16
+ def reset!
17
+ @colours = COLOURS
18
+ @uppercase = UPPERCASE
19
+ @shape = SHAPE
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Monogram
4
+ VERSION = '0.9.1'
5
+ end
data/lib/monogram.rb ADDED
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'monogram/avatar'
4
+ require_relative 'monogram/config'
5
+
6
+ class Monogram
7
+ def initialize(initials: nil, name: nil, background: nil, shape: nil)
8
+ @initials = initials
9
+ @name = name
10
+ @background = background
11
+ @shape = shape
12
+
13
+ validate_arguments
14
+ end
15
+
16
+ def to_s
17
+ avatar.svg
18
+ end
19
+
20
+ def data_url
21
+ avatar.data_url
22
+ end
23
+
24
+ def self.svg(**options)
25
+ new(**options).to_s
26
+ end
27
+
28
+ def self.data_url(**options)
29
+ new(**options).data_url
30
+ end
31
+
32
+ def self.config
33
+ @config ||= Config.new
34
+ end
35
+
36
+ def self.configure
37
+ yield config
38
+ end
39
+
40
+ private
41
+
42
+ attr_accessor :name
43
+
44
+ def avatar
45
+ @avatar ||= Avatar.new(initials: initials, background: background, shape: shape)
46
+ end
47
+
48
+ def validate_arguments
49
+ raise ArgumentError, 'either `initials:` or `name:` must be specified' unless @name || @initials
50
+
51
+ return if Avatar::SHAPES.include?(shape)
52
+
53
+ raise ArgumentError, "`shape:` '#{shape}' is invalid, must be one of #{Avatar::SHAPES}"
54
+ end
55
+
56
+ def config
57
+ self.class.config
58
+ end
59
+
60
+ def initials
61
+ @initials ||= initials_from_name
62
+
63
+ return @initials.upcase if config.uppercase
64
+
65
+ @initials
66
+ end
67
+
68
+ def initials_from_name
69
+ name.split.map { |n| n[0] }.join
70
+ end
71
+
72
+ def background
73
+ @background ||= background_for_initials
74
+ end
75
+
76
+ def background_for_initials
77
+ Random.srand(initials_seed)
78
+ index = (rand * config.colours.length).floor
79
+ config.colours[index]
80
+ end
81
+
82
+ def initials_seed
83
+ initials.downcase.chars.map(&:ord).sum
84
+ end
85
+
86
+ def shape
87
+ @shape ||= config.shape
88
+ end
89
+ end
90
+
91
+ def Object.const_missing(name)
92
+ if name == :Supertramp
93
+ warn '[DEPRECIATION WARNING] The Supertramp constant is deprecated and will be removed in version 1.0. '\
94
+ 'Please use Monogram instead.'
95
+
96
+ Monogram
97
+ else
98
+ super
99
+ end
100
+ end
@@ -0,0 +1 @@
1
+ <circle fill="<%= @background %>" r="50%" cx="25" cy="25"></circle>
@@ -0,0 +1 @@
1
+ <rect width="100%" height="100%" fill="<%= @background %>" rx="7" ry="7"></rect>
@@ -0,0 +1 @@
1
+ <rect width="100%" height="100%" fill="<%= @background %>"/>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
3
+ <%= shape %>
4
+ <text fill="#fff" font-family="Helvetica,Arial,sans-serif" font-size="<%= text_size %>" font-weight="500" x="50%" y="55%" dominant-baseline="middle" text-anchor="middle">
5
+ <%= @initials %>
6
+ </text>
7
+ </svg>
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monogram
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Bearman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - matt@mattbearman.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/monogram.rb
21
+ - lib/monogram/avatar.rb
22
+ - lib/monogram/config.rb
23
+ - lib/monogram/version.rb
24
+ - lib/templates/_circle.svg.erb
25
+ - lib/templates/_rounded.svg.erb
26
+ - lib/templates/_square.svg.erb
27
+ - lib/templates/avatar.svg.erb
28
+ homepage: https://github.com/mattbearman/monogram
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/mattbearman/monogram
33
+ source_code_uri: https://github.com/mattbearman/monogram
34
+ changelog_uri: https://github.com/mattbearman/monogram/CHANGELOG.md
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.2.25
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Creates SVG letter avatars on the fly with consistent colours.
54
+ test_files: []