letteravatar

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: Apache-2.0, MIT Imports: 12 Imported by: 0

README

letteravatar

GoDoc GitHub Workflow Status (with branch)

Letter avatar generation for Go.

Usage

Generate a 100x100px 'A'-letter avatar:

img, err := letteravatar.Draw(100, 'A', nil)

The third parameter options *Options can be used for customization:

type Options struct {
	Font        *truetype.Font
	Palette     []color.Color
	LetterColor color.Color
	PaletteKey  string
	FontSize    int
}

Using a custom palette:

img, err := letteravatar.Draw(100, []rune{'A'}, &letteravatar.Options{
	Palette: []color.Color{
		color.RGBA{255, 0, 0, 255},
		color.RGBA{0, 255, 0, 255},
		color.RGBA{0, 0, 255, 255},
	},
})

Documentation

letteravatar-docs

Examples

package main

import (
	"image/png"
	"log"
	"os"
	"unicode/utf8"

	"github.com/Gikabu/letteravatar"
)

var names = []string{
	"Alice",
	"Bob",
	"Carol",
	"Dave",
	"Eve",
	"Frank",
	"Gloria",
	"Henry",
	"Isabella",
	"James",
	"Жозефина",
	"Ярослав",
}

func main() {
	for _, name := range names {
		firstLetter, _ := utf8.DecodeRuneInString(name)

		img, err := letteravatar.Draw(75, []rune{firstLetter}, nil)
		if err != nil {
			log.Fatal(err)
		}

		file, err := os.Create(name + ".png")
		if err != nil {
			log.Fatal(err)
		}

		err = png.Encode(file, img)
		if err != nil {
			log.Fatal(err)
		}
	}
}

License

The package "letteravatar" is distributed under the terms of the MIT license.

The Roboto-Medium font is distributed under the terms of the Apache License v2.0.

See LICENSE.

Documentation

Overview

Package letteravatar generates letter-avatars.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Draw

func Draw(size int, letters []rune, options *Options) (image.Image, error)

Draw generates a new letter-avatar image of the given size using the given letter with the given options. Default parameters are used if a nil *Options is passed.

Types

type Options

type Options struct {
	Font        *truetype.Font
	Palette     []color.Color
	LetterColor color.Color
	FontSize    int

	// PaletteKey is used to pick the background color from the Palette.
	// Using the same PaletteKey leads to the same background color being picked.
	// If PaletteKey is empty (default) the background color is picked randomly.
	PaletteKey string
}

Options are letter-avatar parameters.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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