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 canvas is a fixed-size, 16-color pixel grid.

Readme View source

canvas

Pure package gno.land/p/g1sw5xklxjjuv0yvuxy5f5s3l3mnj0nqq626a9wr/canvas/v0: a fixed-size grid of palette indexes with lazy per-row allocation, plus the x,y,color;... batch codec.

Used by the realm gno.land/r/g1sw5xklxjjuv0yvuxy5f5s3l3mnj0nqq626a9wr/million/v0, which adds pricing and access control. The package itself holds no chain state and has no chain imports, so it is fully covered by plain unit tests (gno test .).

  • New(width, height): empty canvas, sides between 1 and 10000.
  • Set(Pixel), Get(x, y), Check(Pixel): paint, read, validate.
  • Rows(from, to): rows as one hex digit per pixel, no separators.
  • ParsePixels, Encode, ParseColor: the batch format.
  • Palette: 16 CSS colors, index 0 being "never painted".

Overview

Package canvas is a fixed-size, 16-color pixel grid.

It holds no chain state and knows nothing about payments: the realm gno.land/r/g1sw5xklxjjuv0yvuxy5f5s3l3mnj0nqq626a9wr/million/v0 wraps it with pricing and access control.

Storage layout: one []byte per row, allocated on first paint. A row that was never painted is nil and costs nothing. Each byte holds a palette index; Blank (0) means "never painted".

Constants 1

const Blank, MaxColor, MaxSide

1const (
2	// Blank is the color of a pixel nobody painted. It cannot be painted.
3	Blank byte = 0
4	// MaxColor is the highest valid palette index.
5	MaxColor byte = 15
6	// MaxSide bounds width and height so x*y fits comfortably in an int.
7	MaxSide = 10000
8)
source

Variables 2

var Palette

 1var Palette = [MaxColor + 1]string{
 2	"#ffffff",
 3	"#ffffff",
 4	"#e4e4e4",
 5	"#888888",
 6	"#222222",
 7	"#ffa7d1",
 8	"#e50000",
 9	"#e59500",
10	"#a06a42",
11	"#e5d900",
12	"#94e044",
13	"#02be01",
14	"#00d3dd",
15	"#0083c7",
16	"#cf6ee4",
17	"#820080",
18}
source

Palette maps a color index to its CSS hex value. Index 0 is Blank and is rendered as white by clients.

Functions 4

func Encode

1func Encode(pixels []Pixel) string
source

Encode is the inverse of ParsePixels.

func ParseColor

1func ParseColor(s string) (byte, error)
source

ParseColor converts a decimal color index, rejecting Blank.

func New

1func New(width, height int) *Canvas
source

New returns an empty canvas. It panics on a non-positive or oversized side.

func ParsePixels

1func ParsePixels(s string) ([]Pixel, error)
source

ParsePixels decodes "x,y,color;x,y,color;...". A trailing ';' is allowed. It validates the syntax and the color range but not the coordinates, which depend on the canvas: call Check for that.

Types 2

type Canvas

struct
1type Canvas struct {
2	width, height int
3	rows          [][]byte
4	painted       int
5}
source

Canvas is a width x height grid of palette indexes.

Methods on Canvas

func Check

method on Canvas
1func (c *Canvas) Check(p Pixel) error
source

Check reports whether p can be painted on this canvas.

func Get

method on Canvas
1func (c *Canvas) Get(x, y int) byte
source

Get returns the color at (x, y). Out-of-range coordinates panic.

func Height

method on Canvas
1func (c *Canvas) Height() int
source

func Painted

method on Canvas
1func (c *Canvas) Painted() int
source

Painted is the number of pixels that are not Blank.

func Rows

method on Canvas
1func (c *Canvas) Rows(from, to int) string
source

Rows encodes rows [from, to) as one hex digit per pixel, row after row, with no separator. Clients slice it with the known width.

func Set

method on Canvas
1func (c *Canvas) Set(p Pixel)
source

Set paints p. It panics when Check(p) fails.

func Width

method on Canvas
1func (c *Canvas) Width() int
source

type Pixel

struct
1type Pixel struct {
2	X, Y  int
3	Color byte
4}
source

Pixel is one paint request.

Imports 3

  • errors stdlib
  • strconv stdlib
  • strings stdlib

Source Files 4