tinyicons

package module
v0.0.0-...-4966319 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2025 License: MIT Imports: 9 Imported by: 0

README

tinyicons

tinyicons is a tiny Go package to generate icons from a user string (such as an email address or username). Similar in principal to gravatar but with a focus on a tiny, fast, and simple implementation and tiny png images.

Examples:

Usage

Simply call tinyicons.Get(username).DrawPNG(w) for a one-liner that writes the icon PNG to an output stream.

Obviously in production you'll want something with more features, so we provide a (Icon).String() method to get the serialized value, and a (Icon).Handler(w http.ResponseWriter, r *http.Request) method to inject relevant headers and the PNG content into your web server.

In practice, I have yet to see any PNG output icons using more than 200 bytes.

How it works

Icons are generated from a 32-bit FNV-1a hash of the input value, which bits determines a 7x7 pattern. The most significant 4 bits determine the foreground color from a pre-defined color palette. The 7 rows of the icon are determined from 4-bit sequences starting at the LSB. Bits 0,1,2,3 are used to determine if the foreground color is set in the 7-pixel-wide row using the pattern: [3210123].

When encoded as a PNG image, a 1-pixel background-color border is added, and all pixels are scaled by ScaleFactor, so the 7x7 pattern becomes (9x9)x4 = 36x36 pixels.

The Icon can thus be packed into a 8-character hex string to allow easy referencing without the original string.

License

MIT License

Documentation

Overview

Package tinyicons is a tiny package to generate icons from a user string (such as an email address or username). Similar in principal to gravatar but with a focus on a tiny, fast, and simple implementation and tiny png images.

Icons are generated from a 32-bit FNV-1a hash of the input value, which bits determines a 7x7 pattern. The most significant 4 bits determine the foreground color from a pre-defined color palette. The 7 rows of the icon are determined from 4-bit sequences starting at the LSB. Bits 0,1,2,3 are used to determine if the foreground color is set in the 7-pixel-wide row using the pattern: [3210123].

When encoded as a PNG image, a 1-pixel background-color border is added, and all pixels are scaled by ScaleFactor, so the 7x7 pattern becomes (9x9)x4 = 36x36 pixels.

The Icon can thus be packed into a 8-character hex string to allow easy referencing without the original string.

Index

Constants

This section is empty.

Variables

View Source
var Palette = []color.RGBA{
	{R: 204, G: 102, B: 119, A: 255},
	{R: 51, G: 34, B: 136, A: 255},
	{R: 51, G: 187, B: 238, A: 255},
	{R: 17, G: 119, B: 51, A: 255},
	{R: 136, G: 34, B: 85, A: 255},
	{R: 68, G: 170, B: 153, A: 255},
	{R: 153, G: 153, B: 51, A: 255},
	{R: 170, G: 68, B: 153, A: 255},

	{R: 221, G: 221, B: 221, A: 255},
}
View Source
var (
	// ScaleFactor scales the 9x9 icon image by this factor in PNG output.
	ScaleFactor = 4
)

Functions

This section is empty.

Types

type Icon

type Icon uint32

Icon is a 7x7 icon with horizontal symmetry, packed into 32 bits.

func From

func From(hexValue string) (Icon, error)

From returns an Icon for the provided hex value (e.g. returned by String).

func Get

func Get(value string) Icon

Get returns an Icon for the provided value.

func (Icon) DrawPNG

func (z Icon) DrawPNG(w io.Writer) error

DrawPNG writes a PNG image of the 7x7 Icon, with a 1px borders, scaled by ScaleFactor, to the io.Writer provided.

func (Icon) Handler

func (z Icon) Handler(w http.ResponseWriter, r *http.Request)

Handler returns HTTP response content for the Icon.

func (Icon) String

func (z Icon) String() string

String returns the hex value of the icon content.

Directories

Path Synopsis
cmd
gen command
Command gen with generate a tinyicon PNG file from the first command-line argument.
Command gen with generate a tinyicon PNG file from the first command-line argument.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL