AnimalAvatarGenerator (Bonfire v0.9.12-social-beta.71)

View Source

Generates avatars that look like cute animal faces.

Based on https://www.npmjs.com/package/animal-avatar-generator

Examples

iex> svg = avatar("test123")

iex> svg = avatar("custom", size: 200, blackout: false, round: false)
iex> String.contains?(svg, "width='200'") and String.contains?(svg, "height='200'")
true

Summary

Functions

Generates an SVG avatar based on the given seed string.

Creates an SVG with the given size and child elements.

Helper function to darken a hexadecimal color value.

Seeds the random number generator based on the provided seed string.

Functions

avatar(seed, opts \\ [])

Generates an SVG avatar based on the given seed string.

The seed is used to generate a different random colour/shape for each seed, and keeps the generated avatar consistent if called again with the same seed.

Options

  • :size - Size of the avatar in pixels (default: 150)
  • :avatar_colors - List of color hex codes for the avatar (default: ["#d7b89c", "#b18272", "#ec8a90", "#a1Ac88", "#99c9bd", "#50c8c6"])
  • :background_colors - List of color hex codes for the background (default: @default_background_colors)
  • :blackout - Whether to add a slight darkening to the right side (default: true)
  • :round - Whether to use round or rectangular background (default: true)
  • :skip_randomizer - Whether to skip re-seeding the random generator (default: true)
  • :background_class - CSS class to apply to the background (default: none)

Examples

iex> svg = avatar("test123", size: 100)
iex> String.contains?(svg, "width='100'")
true

iex> svg = avatar("test123", background_colors: ["#ff0000"])
iex> String.contains?(svg, "#ff0000")
true

iex> svg = avatar("test123", round: false)
iex> String.contains?(svg, "rx='0'")
true

iex> svg = avatar("test123", round: true)
iex> String.contains?(svg, "rx='250'")
true

iex> svg = avatar("test123")
iex> String.starts_with?(svg, "<svg") and String.contains?(svg, "viewBox='0 0 500 500'")
true

avatar_face(seed, avatar_colors \\ ["#d7b89c", "#b18272", "#ec8a90", "#a1Ac88", "#99c9bd", "#50c8c6"], skip_randomizer? \\ false)

Generates an animal avatar face SVG based on the given seed string.

This function is used internally by avatar/2 but can be used separately to generate just the face without the background.

Params

  • :avatar_colors - List of color hex codes for the avatar (default: ["#d7b89c", "#b18272", "#ec8a90", "#a1Ac88", "#99c9bd", "#50c8c6"])
  • :skip_randomizer - Whether to skip re-seeding the random generator (default: false)

Examples

iex> face = avatar_face("test123")
iex> String.contains?(face, "path")
true

iex> face1 = avatar_face("consistent")
iex> face2 = avatar_face("consistent")
iex> face1 == face2
true

iex> face = avatar_face("test123", ["#ff0000"])
iex> String.contains?(face, "#ff0000")
true

create_svg(size, children)

Creates an SVG with the given size and child elements.

Examples

iex> svg = create_svg(100, ["<rect width='100' height='100' fill='red'/>"])
iex> String.contains?(svg, "width='100'")
true
iex> String.contains?(svg, "height='100'")
true
iex> String.contains?(svg, "<rect width='100' height='100' fill='red'/>")
true

darken(hex, amount)

Helper function to darken a hexadecimal color value.

Parameters

  • hex - The hexadecimal color string (e.g., "#ff0000")
  • amount - The amount to darken (positive values) or lighten (negative values)

Examples

iex> darken("#ff0000", 30)
"#00E100"

iex> darken("#00ff00", -30)
"#1E1EFF"

iex> darken("#ffffff", 100)
"#9B9B9B"

seed_randomiser(seed)

Seeds the random number generator based on the provided seed string.

This function ensures consistent random choices for the same seed.

Examples

iex> :rand.uniform_real()
iex> seed_randomiser("abc")
iex> first = :rand.uniform_real()
iex> seed_randomiser("abc")
iex> second = :rand.uniform_real()
iex> first == second
true

iex> first = seed_randomiser("xyz")
iex> different = :rand.uniform_real()
iex> first == different
false