Avatar
A person's or organisation's picture, falling back to initials on a fill picked from the name (or a seed: such as an id) — the same colour on every page and every server.
Sizes and fallbacks
Five named sizes and a CSS length, with and without an image. A broken image shows the initials through it.
<% portrait = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCI+PHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0IiBmaWxsPSIjZmRlNjhhIi8+PGNpcmNsZSBjeD0iMzIiIGN5PSIyNiIgcj0iMTIiIGZpbGw9IiM5MjQwMGUiLz48cGF0aCBkPSJNMTAgNjRjMi0xNCAxMi0yMiAyMi0yMnMyMCA4IDIyIDIyeiIgZmlsbD0iI2I0NTMwOSIvPjwvc3ZnPg==" %>
<div class="grid gap-4">
<div class="flex flex-wrap items-center gap-4">
<%= avatar "Ada Lovelace", size: :small %>
<%= avatar "Ada Lovelace" %>
<%= avatar "Ada Lovelace", size: :large %>
<%= avatar "Ada Lovelace", size: :xlarge %>
<%= avatar "Ada Lovelace", size: :xxlarge %>
<%= avatar "Ada Lovelace", size: "1.75rem" %>
<%= avatar "Ada Lovelace", size: :large, skeleton: true %>
</div>
<div class="flex flex-wrap items-center gap-4">
<%= avatar "Grace Hopper", size: :large, src: portrait %>
<%= avatar "Grace Hopper", size: :small, src: "/missing-picture.png" %>
<%= avatar "Grace Hopper", size: :large, src: "/missing-picture.png" %>
<%= avatar "Grace Hopper", size: :xxlarge, src: "/missing-picture.png", fill: Unmagic::Components::Avatar::Gradient.new %>
<%= avatar "4K Gardens", kind: :organization, shape: :square, size: :xlarge, fit: :contain, src: "/missing-logo.png" %>
</div>
</div>Tints
The default fill: six tints from the name, so each person keeps their colour. fill: false is neutral.
<div class="flex flex-wrap items-center gap-3">
<% [ "Ada Lovelace", "Grace Hopper", "Katherine Johnson", "Alan Turing", "Margaret Hamilton",
"Dennis Ritchie", "Barbara Liskov", "Ken Thompson" ].each do |name| %>
<%= avatar name %>
<% end %>
<%= avatar "Neutral Person", fill: false %>
</div>Fills
Avatar::Solid and Avatar::Gradient pick from the whole hue wheel, tuned by their arguments. Set one for the app as config.avatar_fill, or pass fill: for one avatar.
<% people = [ "Ada Lovelace", "Grace Hopper", "Katherine Johnson", "Alan Turing", "Margaret Hamilton", "Dennis Ritchie" ] %>
<div class="grid gap-4">
<% { "Solid" => Unmagic::Components::Avatar::Solid.new,
"Gradient" => Unmagic::Components::Avatar::Gradient.new,
"Gradient, wider turn" => Unmagic::Components::Avatar::Gradient.new(hue_shift: 60, angle: 90, lightness: 40) }.each do |label, fill| %>
<div class="flex flex-wrap items-center gap-3">
<span class="w-40 text-xs text-neutral-500"><%= label %></span>
<% people.each do |name| %>
<%= avatar name, size: :large, fill: fill %>
<% end %>
</div>
<% end %>
</div>People and organisations
kind: reads initials as a person or an organisation: particles, mononyms, legal suffixes, and a three-character mark where the avatar has room.
<div class="flex flex-wrap items-end gap-4">
<% [ "Kestrel Pty Ltd", "BrightPath", "4K Gardens", "Guild of Lanterns" ].each do |name| %>
<%= avatar name, kind: :organization, shape: :square, size: :xlarge %>
<% end %>
<%= avatar "4K Gardens", kind: :organization, shape: :square, size: :small %>
<%= avatar "Ludwig van Beethoven", kind: :person, size: :xlarge %>
<%= avatar "Prince", kind: :person, size: :xlarge %>
</div>A group
avatar_group stacks them, collapsing past max: into a counter that names the rest.
<%= avatar_group max: 3 do |group| %>
<% [ "Ada Lovelace", "Grace Hopper", "Katherine Johnson", "Alan Turing", "Margaret Hamilton", "Barbara Liskov" ].each do |name| %>
<% group.avatar name %>
<% end %>
<% end %>