Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

Package \`nft\` draws the Settlers, pixel-art gnomes. The art is dedicated to the public domain under CC0 1.0, see Cr...

Readme View source

Settlers NTF

Package that draws the Settlers, small pixel-art gnomes, as an SVG.

Given the same traits, it always draws the same image, so it's safe and cheap for any realm to import and call directly.

Code also lives in jeronimoalbi/gnoland-settlers.

From a seed

1traits := nft.NewTraits("some-seed-string")
2svg := nft.RenderSVG(traits, nft.Options{Size: 512})

The traits (skin, hat, beard, clothes, glasses, background, and so on) are all worked out deterministically from the seed, so the same seed always gives the same gnome.

From packed traits

Traits are usually stored packed into a single uint32:

1packed := nft.Pack(traits)
2traits, err := nft.FromPacked(packed)
3svg := nft.RenderSVG(traits, nft.Options{})

Options also takes NoBackground (a transparent image) and BackgroundColor (a custom one). RenderDataURI gives the same image as a data:image/svg+xml;... URI, ready to use as an <img> source.

License

The art itself is dedicated to the public domain under CC0 1.0 Universal; see Credits.

Overview

Package `nft` draws the Settlers, pixel-art gnomes. The art is dedicated to the public domain under CC0 1.0, see Credits.

Constants 2

const Style, Credits

1const (
2	// Style is the ID of the art style.
3	Style = "settlers-v1"
4
5	// Credits is the license of the art.
6	Credits = "The art of the Settlers is dedicated to the public domain under " +
7		"[CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/)"
8)
source

const CanonicalSize

1const CanonicalSize = 512
source

CanonicalSize is the width and height in pixels of the image used for NFTs.

Functions 9

func BuildGrid

1func BuildGrid(t Traits) []string
source

BuildGrid returns the pixel grid of a gnome, as 32 rows of 24 palette keys ('.' is transparent). Traits must be valid.

func DataURI

1func DataURI(svg string) string
source

DataURI encodes an SVG document as a "data:image/svg+xml;base64," URI. It is slower than RenderDataURI, use it when Base64 is needed.

func Pack

1func Pack(t Traits) uint32
source

Pack packs the traits in 20 bits.

func RenderDataURI

1func RenderDataURI(t Traits, o Options) string
source

RenderDataURI renders the gnome as a "data:image/svg+xml;charset=utf-8," URI, percent-encoded.

Cheapest way to embed a gnome. SVG is written encoded, instead of encoding it afterwards. Traits must be valid.

func RenderSVG

1func RenderSVG(t Traits, o Options) string
source

RenderSVG renders the gnome as an SVG document. Traits must be valid. The same traits and options always give the same bytes.

func RenderSeed

1func RenderSeed(seed string, o Options) string
source

RenderSeed renders the gnome of a seed as an SVG document.

func FromPacked

1func FromPacked(v uint32) (Traits, error)
source

FromPacked unpacks traits and checks them.

func NewTraits

1func NewTraits(seed string) Traits
source

NewTraits derives the traits of the gnome from a seed. A single seed always generate the same traits. Use `Validate()` to check traits before drawing.

func Unpack

1func Unpack(v uint32) Traits
source

Unpack unpacks the traits. It does not check the values, use FromPacked for untrusted input.

Types 3

type Attribute

struct
1type Attribute struct {
2	Type  string
3	Value string
4}
source

Attribute is a named trait of a gnome.

type Options

struct
 1type Options struct {
 2	// Size is the width and the height of the image in pixels, because the image is a square.
 3	// Multiples of 32 keep every art pixel as a whole number of screen pixels.
 4	// Zero omits the width and height, so that the image scales to its container.
 5	Size int
 6
 7	// NoBackground omits the background.
 8	NoBackground bool
 9
10	// BackgroundColor replaces the color of the background, in "#rrggbb" format.
11	BackgroundColor string
12}
source

Options are the options to render a gnome.

type Traits

struct
 1type Traits struct {
 2	Skin       uint8
 3	Hat        uint8
 4	BeardColor uint8
 5	Tunic      uint8
 6	Background uint8
 7	HatShape   uint8 // 0 pointy, 1 bent
 8	Flip       uint8 // 0 or 1, no effect on pointy hats
 9	Glasses    uint8
10	Beard      uint8
11}
source

Traits describe a gnome.

Methods on Traits

func Attributes

method on Traits
1func (t Traits) Attributes() []Attribute
source

Attributes returns the traits of a gnome with their names. It panics when traits are not valid.

func Normalized

method on Traits
1func (t Traits) Normalized() Traits
source

Normalized returns the traits with the flip cleared when it has no effect. Two traits that draw the same gnome have the same normalized traits, so this is the value to compare and to keep when gnomes must be different.

func Validate

method on Traits
1func (t Traits) Validate() error
source

Validate checks that all the traits are in range.

Imports 4

  • crypto/sha256 stdlib
  • encoding/base64 stdlib
  • errors stdlib
  • strconv stdlib

Source Files 7