avatar

package module
v0.0.0-...-8e927e5 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: Apache-2.0 Imports: 25 Imported by: 0

README

A V A T A R

Create avatars based on names. The colors are chosen based on the first character. You can save to disk ToDisk or send back over HTTP ToHTTP.

This fork is based off github.com/argylelabcoat/avatar, which is forked from andrew's https://github.com/ae0000/avatar.

All this fork cares about is ditching the logging and os.Exit bits, instead returning error values. This makes it safer to use as a library.

Example

package main

import (
	"github.com/argylelabcoat/avatar"
)

func main() {
	avatar.ToDisk("AE", "../ae.png")

    // Which is the same as
    avatar.ToDisk("Andrew Edwards", "../ae.png")
}


Example

You can pass in a single character as well

// Single initial as well..
avatar.ToDisk("Jet", "../j.png")

Example

HTTP example

Using go-chi (highly recommended HTTP router)

package main

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/argylelabcoat/avatar"
	"github.com/go-chi/chi"
)

func main() {
	r := chi.NewRouter()

	// Get the png based on the initials, You would use it like this:
	//    <img src="http://localhost:3000/avatar/ae/png" width="150">
	r.Get("/avatar/{initials}.png", func(w http.ResponseWriter, r *http.Request) {
		initials := chi.URLParam(r, "initials")

		avatar.ToHTTP(initials, w)
	})

	http.ListenAndServe(":3000", r)
}

TODO

  • HTTP example
  • Caching
  • Custom colors
  • Add unique colors that are missing (T-Z,0-9)

Documentation

Index

Constants

View Source
const (
	LettersCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Threshold  = 130000
)

Variables

View Source
var (
	White = image.Uniform{color.RGBA{255, 255, 255, 255}}
	Black = image.Uniform{color.RGBA{0, 0, 0, 255}}
)

Colors for background

Functions

func DropsBG

func DropsBG(bg image.Uniform) *image.RGBA

func FastBG

func FastBG(bg image.Uniform) *image.RGBA

func GetBG

func GetBG(bg image.Uniform, method BGMethod) *image.RGBA

func GetDefaultBG

func GetDefaultBG(bg image.Uniform) *image.RGBA

func HexToColor

func HexToColor(hexColor string) (color.Color, error)

func LinenLikeBG

func LinenLikeBG(bg image.Uniform) *image.RGBA

func LoadGPL

func LoadGPL(fs http.FileSystem, fname string) (color.Palette, error)

func LoadHex

func LoadHex(fs fs.FS, fname string) (color.Palette, error)

func SetDefaultBGType

func SetDefaultBGType(bgtype BGMethod)

func ToDisk

func ToDisk(initials, path string) error

ToDisk saves the image to disk

func ToDiskCustom

func ToDiskCustom(initials, path, bgColor, fontColor string) error

ToDiskCustom saves the image to disk

func ToHTTP

func ToHTTP(initials string, w http.ResponseWriter) error

ToHTTP sends the image to a http.ResponseWriter (as a PNG)

func ToHTTPCustom

func ToHTTPCustom(initials, bgColor, fontColor string, w http.ResponseWriter) error

ToHTTPCustom sends the image to a http.ResponseWriter (as a PNG)

func ToSlice

func ToSlice(initials string, colorSalt int) ([]byte, error)

ToSlice simply buffers the image and returns the byte slice (as a PNG)

func ToSliceCustomColors

func ToSliceCustomColors(initials string, bgColor, fontColor string) ([]byte, error)

ToSlice simply buffers the image and returns the byte slice (as a PNG)

Types

type BGMethod

type BGMethod int
const (
	Fast BGMethod = iota
	LinenLike
	Drops
)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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