🔤 Supertramp
But please, tell me who I am
- Supertramp, The Logical Song
Warning
This gem has been renamed to Monogram. As such Supertramp will no longer be supported. Please switch to Monogram as soon as possible.
Generate default avatars for your users, containing their initials, with a consistent background colour. Avatars are created as SVGs on the fly, with the background colour chosen based on the initials, meaning the background will stay the same through reloads.
The avatars are returned as a string containing the SVG markup, that can be embedded directly into your HTML. This means no images need to be created or stored, eliminating the need for an image manipulation library such as ImageMagick.
Extracted from PresentDay, inspired by https://kukicola.io/posts/creating-google-like-letter-avatars-using-erb-generated-svgs/
Installation
gem install supertramp
Usage
String output
# Create an instance of Supertramp, and then read it as a string
avatar = Supertramp.new(name: 'Matt Bearman')
avatar.to_s
# => <?xml ... /svg>
"#{avatar}"
# => <?xml ... /svg>
# .svg convenience method
Supertramp.svg(name: 'Matt Bearman')
# => <?xml ... /svg>
# When using in a Rails template, add raw to stop the SVG XML being escaped
<%= raw avatar %>
Data URLs
# To generate a data URL that can be used in image tags
# Create an instance of Supertramp, and then call its data_url method
avatar = Supertramp.new(name: 'Matt Bearman')
avatar.data_url
# => data:image/svg+xml;base64,PD94bWwgdmVyc2lv ... wv\ndGV4dD4KPC9zdmc+Cg==
# .data_url convenience method
Supertramp.data_url(name: 'Matt Bearman')
# => data:image/svg+xml;base64,PD94bWwgdmVyc2lv ... wv\ndGV4dD4KPC9zdmc+Cg==
Arguments
name:
(string)
Required unless initials:
is specified
A name from which the initials will be extracted. Eg: name: 'Super Tramp'
will create an avatar with the initials ST
.
initials:
(string)
Required unless name:
is specified
The initials to be displayed in the avatar. Eg: initials: 'ST'
will create an avatar with the initials ST
.
background:
(string)
Optional
Tha background colour for the avatar. If a background is specified it will take priority over the default background colour selection.
The background
argument can be any valid SVG colour string (eg: 'red', '#ff0000', 'rgba(127, 0, 0, 0.5)'), and does not have to be one of the colours in the configured colours
array.
shape:
(string)
Optional, default = "square"
The shape of the avatar, can be one of "square", "circle", or "rounded" (a rounded rectangle). To avoid typos you should specify this using the constants defined in the Supertramp::Avatar
class:
Supertramp::Avatar::SQUARE
Supertramp::Avatar::CIRCLE
Supertramp::Avatar::ROUNDED
You can also specify this globally using the shape
config attribute. This defaults to "square".
Examples
# Extracting initials from a name, default shape and colour choice
Supertramp.new(name: 'Super Tramp').to_s
# Specifying initials and shape, default colour choice
Supertramp.new(initials: 'mb', shape: 'circle').to_s
# Single initial
Supertramp.new(initials: 'Z').to_s
# Three initials
Supertramp.new(name: 'Maynard James Keenan').to_s
# Extracting initials, specifying custom colour, specifying shape as a constant
Supertramp.new(name: 'custom colour', color: 'rgba(127, 0, 0, 0.8)', shape: Supertramp::Avatar::ROUNDED).to_s
How it works
Outputs an SVG of a square with initials in the center. Initials can either be passed in as initials
, or extracted from the name
argument.
Background colour is chosen using a seeded random based on the initials, so the colour won't change on reload unless the initials also change.
Text size will be dynamically adjusted to ensure they fit within the shape. There's no limit to how many initials an avatar can have, although three is probably the best maximum.
Configuration
Use Supertramp.configure
to set global config:
Supertramp.configure do |config|
# Array of background colours that can be chosen
# (Sorry, I'm English, but for my trans-Atlantic neighbours you can also use config.colors 😊)
# Default: %w[#B91C1C #B45309 #047857 #1D4ED8 #6D28D9]
config.colours = %w[red green blue]
# Set to true to transform initials to uppercase, false will leave them as they are provided
# Note: This also affects initials that have been extracted from the name parameter
# Default: true
config.uppercase = false
# The shape of the avatars, see the `shape:` argument for more details
config.shape = Supertramp::Avatar::CIRCLE
end
Roadmap
Things I'd like to add next, somewhat prioritised:
- Alternative shapes (circle and rounded rectangle)
- Support for three or more initials (dynamic text sizing)
- base64 output for use with
data:
URLs - Custom random seed (eg user_id)
- Dark text when a light background is specified
- Caching
- Full font customisation
- Custom SVG templates
Contributing
- Read how to properly contribute to open source projects on GitHub.
- Fork the project.
- Use a topic/feature branch to easily amend a pull request later, if necessary.
- Write good commit messages.
- Use the same coding conventions as the rest of the project.
- Commit and push until you are happy with your contribution.
- Add tests for your code and make sure they're passing, and that you have 100% test coverage (as reported when running
bundle exec rspec
) - If your change has a corresponding open GitHub issue, prefix the commit message with [Fix #github-issue-number].
- Make sure the test suite is passing and the code you wrote doesn't produce RuboCop offences (
bundle exec rspec
andbundle exec rubocop
). - Squash related commits together.
- Open a pull request that relates to only one subject with a clear title and description in grammatically correct, complete sentences.