Documentation ¶
Overview ¶
Package plavatar implements the core avatar generation functionality.
It contains the Generator struct, which implements the library's main method: Generator.GenerateAvatar.
Generator.GenerateAvatar is called in combination with a GeneratorFunction like Generator.Smiley (see the matching avatar_XXX.go file for details on the GeneratorFunction's implementations)
Index ¶
- Constants
- func DrawCanvasBackground(canvas *svg.SVG, options *Options)
- func RasterizeSVGToPNG(svg io.Reader, imageSize int) (*bytes.Buffer, error)
- type Format
- type Generator
- func (generator *Generator) GenerateAvatar(...) (*bytes.Buffer, string, error)
- func (generator *Generator) Gradient(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
- func (generator *Generator) Happy(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
- func (generator *Generator) Laughing(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
- func (generator *Generator) Marble(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
- func (generator *Generator) Pixels(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
- func (generator *Generator) Smiley(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
- func (generator *Generator) Solid(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
- type Options
- type Shape
Constants ¶
const CanvasSize = 512
Variables ¶
This section is empty.
Functions ¶
func DrawCanvasBackground ¶
DrawCanvasBackground fills the canvas with the fitting background. Important: when using this method, the canvas must already contain definitions for the bg color gradient.
Example definition in your custom generatorFunc:
func MyCustomGenerator(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options) { backgroundColor := utils.RandomColorHex(rng) canvas.Def() gradientColors := []svg.Offcolor{{0, backgroundColor, 1}} canvas.LinearGradient("bg", 0, 0, 100, 100, gradientColors) canvas.DefEnd() DrawCanvasBackground(canvas, options) ... }
See avatar_solid.go's source code for a full example on how to define the bg color.
func RasterizeSVGToPNG ¶
RasterizeSVGToPNG rasterizes the SVG file to a PNG image of the given imageSize in the form of a bytes.Buffer.
Types ¶
type Generator ¶
type Generator struct{}
A Generator is used to generate avatars using its Generator.GenerateAvatar method.
func (*Generator) GenerateAvatar ¶
func (generator *Generator) GenerateAvatar(generatorFunc func(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options), generatorOptions *Options) (*bytes.Buffer, string, error)
GenerateAvatar generates an avatar by setting-up the image canvas and then calling the passed generatorFunc. It uses the passed generatorOptions to instruct the avatar generation.
The passed generatorFunc can either be a built-in one like Generator.Smiley, Generator.Solid, or a custom one written by you.
A successful generation, returns err == nil, a string with the used rng seed, and a buffer filled with the image data.
Usage Example:
func generateMyAvatar() (*bytes.Buffer, string) { avatarGenerator := plavatar.Generator{} options := &plavatar.Options{ Name: "exampleSeed", OutputSize: 256, OutputFormat: plavatar.FormatSVG, OutputShape: plavatar.ShapeSquare, } avatar, rngSeed, err := avatarGenerator.GenerateAvatar(avatarGenerator.Smiley, options) if err != nil { panic(err) } return avatar, rngSeed }
func (*Generator) Pixels ¶
func (generator *Generator) Pixels(canvas *svg.SVG, rng *rand.Rand, rngSeed int64, options *Options)
Pixels TODO: Currently this always outputs a ShapeCircle in PNG output mode. oksvg does not support clippaths at the moment, so the whole square gets rasterized see: https://github.com/srwiley/oksvg/issues/10