npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fn-ui-avatars

v1.0.5

Published

Automatically generate consistent, colorful avatars using UI-Avatars API

Downloads

853

Readme

npm version npm bundle size License: MIT

Auto-generate consistent, colorful initials avatars using the UI-Avatars API.

Colors are deterministic — the same name always produces the same background color, across sessions and devices. It also features Smart Contrast, automatically switching the text color between black and white to ensure maximum legibility based on the generated background.


Installation

npm install fn-ui-avatars

Usage

In the browser (vanilla JS)

Add data-name to any <img> element and call loadAvatars() after the DOM is ready:

<img class="fn-ui-avatars" data-name="Maria Silva" alt="Avatar">
<img class="fn-ui-avatars" data-name="João Ferreira" alt="Avatar">

<script type="module">
  import { loadAvatars } from 'fn-ui-avatars';

  document.addEventListener('DOMContentLoaded', () => {
    loadAvatars(); // targets 'img.fn-ui-avatars' by default
  });
</script>

Via CDN (No bundler required)

If you are not using a build tool, you can import it directly from unpkg:

<script type="module">
  import { loadAvatars } from 'https://unpkg.com/fn-ui-avatars/dist/index.js';

  document.addEventListener('DOMContentLoaded', () => {
    loadAvatars();
  });
</script>

Generate a single URL

import { getAvatarUrl } from 'fn-ui-avatars';

const url = getAvatarUrl('Maria Silva');
// → https://eu.ui-avatars.com/api/?size=192&...&name=Maria+Silva&...

document.querySelector('#my-avatar').src = url;

With React (or any component framework)

import { getAvatarUrl } from 'fn-ui-avatars';

function Avatar({ name }) {
  return <img src={getAvatarUrl(name)} alt={name} />;
}

CommonJS (Node / older bundlers)

const { getAvatarUrl, loadAvatars } = require('fn-ui-avatars');

API

getAvatarUrl(name, options?)

Returns a UI-Avatars URL string for the given name.

| Parameter | Type | Description | |---|---|---| | name | string | Full name (e.g. "Maria Silva"). Required. | | options | object | Optional configuration (see below). |

Options:

| Option | Type | Default | Description | |---|---|---|---| | size | number | 192 | Image size in pixels. | | fontSize | number | 0.33 | Font size ratio (0.11). | | length | number | 2 | Number of initials to display. | | rounded | boolean | true | Circular avatar. | | color | string | 'fff' | Text color (hex without #). Automatically outputs 'fff' or '000' for best contrast, unless overridden. | | format | string | 'svg' | Image format: 'svg' or 'png'. | | baseUrl | string | 'https://eu.ui-avatars.com/api/' | Custom API base URL. |


loadAvatars(selector?, options?)

Scans the DOM and injects src attributes into matching elements that have data-name set and no existing src.

| Parameter | Type | Default | Description | |---|---|---|---| | selector | string | 'img.fn-ui-avatars' | CSS selector for target elements. | | options | object | {} | Same options as getAvatarUrl. |

Returns the number of avatars injected.

⚠️ This function requires a browser environment (document must be defined).


hashCode(str) / intToRGB(i)

Low-level utility functions, exported for advanced use cases (e.g. generating matching border colors, custom backgrounds).

import { hashCode, intToRGB } from 'fn-ui-avatars';

const hex = intToRGB(hashCode('Maria Silva')); // → e.g. "4A7BCC"

How colors work

Each name is hashed with a simple djb2-style algorithm, and the resulting integer is masked to produce a 6-digit hex color. This ensures:

  • The same name always gets the same color.
  • Different names get visually distinct colors.
  • The text color automatically adapts to be legible on light or dark backgrounds.
  • No configuration or storage required.

Running tests

npm test

No external test runner needed — tests use only Node's built-in assert.


License

MIT