Documentation ¶
Overview ¶
Package ivg provides rendering of IconVG icons.
IconVG (github.com/google/iconvg) is a compact, binary format for simple vector graphics: icons, logos, glyphs and emoji.
The code in this package does away with rendering the icon to an intermediate bitmap image and instead directly uses a vector Rasterizer interface.
Example ¶
package main import ( "image" "image/draw" "log" "os" "path/filepath" "github.com/reactivego/ivg/decode" "github.com/reactivego/ivg/raster/vec" "github.com/reactivego/ivg/render" ) func main() { ivgData, err := os.ReadFile(filepath.FromSlash("testdata/action-info.lores.ivg")) if err != nil { log.Fatal(err) } const width = 24 dst := image.NewAlpha(image.Rect(0, 0, width, width)) var z render.Renderer z.SetRasterizer(&vec.Rasterizer{Dst: dst, DrawOp: draw.Src}, dst.Bounds()) if err := decode.Decode(&z, ivgData); err != nil { log.Fatal(err) } const asciiArt = ".++8" buf := make([]byte, 0, width*(width+1)) for y := 0; y < width; y++ { for x := 0; x < width; x++ { a := dst.AlphaAt(x, y).A buf = append(buf, asciiArt[a>>6]) } buf = append(buf, '\n') } os.Stdout.Write(buf) }
Output: ........................ ........................ ........++8888++........ ......+8888888888+...... .....+888888888888+..... ....+88888888888888+.... ...+8888888888888888+... ...88888888..88888888... ..+88888888..88888888+.. ..+888888888888888888+.. ..88888888888888888888.. ..888888888..888888888.. ..888888888..888888888.. ..888888888..888888888.. ..+88888888..88888888+.. ..+88888888..88888888+.. ...88888888..88888888... ...+8888888888888888+... ....+88888888888888+.... .....+888888888888+..... ......+8888888888+...... ........++8888++........ ........................ ........................
Index ¶
- Constants
- Variables
- func DecodeGradient(c color.RGBA) (cBase, nBase, shape, spread, nStops uint8)
- func EncodeGradient(cBase, nBase, shape, spread, nStops uint8) color.RGBA
- func Is1(c color.RGBA) bool
- func Is2(c color.RGBA) bool
- func Is3(c color.RGBA) bool
- func ValidAlphaPremulColor(c color.RGBA) bool
- func ValidGradient(c color.RGBA) bool
- type Color
- func (c Color) Encode1() (x byte, ok bool)
- func (c Color) Encode2() (x [2]byte, ok bool)
- func (c Color) Encode3Direct() (x [3]byte, ok bool)
- func (c Color) Encode3Indirect() (x [3]byte, ok bool)
- func (c Color) Encode4() (x [4]byte, ok bool)
- func (c Color) Is1() bool
- func (c Color) Is2() bool
- func (c Color) Is3() bool
- func (c Color) RGBA() (color.RGBA, bool)
- func (c Color) Resolve(palette *[64]color.RGBA, cReg *[64]color.RGBA) color.RGBA
- func (c Color) String() string
- type ColorType
- type Destination
- type DestinationLogger
- func (d *DestinationLogger) AbsArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)
- func (d *DestinationLogger) AbsCubeTo(x1, y1, x2, y2, x, y float32)
- func (d *DestinationLogger) AbsHLineTo(x float32)
- func (d *DestinationLogger) AbsLineTo(x, y float32)
- func (d *DestinationLogger) AbsQuadTo(x1, y1, x, y float32)
- func (d *DestinationLogger) AbsSmoothCubeTo(x2, y2, x, y float32)
- func (d *DestinationLogger) AbsSmoothQuadTo(x, y float32)
- func (d *DestinationLogger) AbsVLineTo(y float32)
- func (d *DestinationLogger) ClosePathAbsMoveTo(x, y float32)
- func (d *DestinationLogger) ClosePathEndPath()
- func (d *DestinationLogger) ClosePathRelMoveTo(x, y float32)
- func (d *DestinationLogger) RelArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)
- func (d *DestinationLogger) RelCubeTo(x1, y1, x2, y2, x, y float32)
- func (d *DestinationLogger) RelHLineTo(x float32)
- func (d *DestinationLogger) RelLineTo(x, y float32)
- func (d *DestinationLogger) RelQuadTo(x1, y1, x, y float32)
- func (d *DestinationLogger) RelSmoothCubeTo(x2, y2, x, y float32)
- func (d *DestinationLogger) RelSmoothQuadTo(x, y float32)
- func (d *DestinationLogger) RelVLineTo(y float32)
- func (d *DestinationLogger) Reset(viewbox ViewBox, palette [64]color.RGBA)
- func (d *DestinationLogger) SetCReg(adj uint8, incr bool, c Color)
- func (d *DestinationLogger) SetCSel(cSel uint8)
- func (d *DestinationLogger) SetLOD(lod0, lod1 float32)
- func (d *DestinationLogger) SetNReg(adj uint8, incr bool, f float32)
- func (d *DestinationLogger) SetNSel(nSel uint8)
- func (d *DestinationLogger) StartPath(adj uint8, x, y float32)
- type Icon
- type Metadata
- type ViewBox
Examples ¶
Constants ¶
const ( MidViewBox = 0 MidSuggestedPalette = 1 )
const ( // Min aligns min of ViewBox with min of rect Min = 0.0 // Mid aligns mid of ViewBox with mid of rect Mid = 0.5 // Max aligns max of ViewBox with max of rect Max = 1.0 )
const Magic = "\x89IVG"
Variables ¶
var DefaultMetadata = Metadata{ ViewBox: DefaultViewBox, Palette: DefaultPalette, }
DefaultMetadata combines the default ViewBox and the default Palette.
var DefaultPalette = [64]color.RGBA{
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
{0x00, 0x00, 0x00, 0xff},
}
DefaultPalette is the default Palette. Its values should not be modified.
var DefaultViewBox = ViewBox{
MinX: -32, MinY: -32,
MaxX: +32, MaxY: +32,
}
DefaultViewBox is the default ViewBox. Its values should not be modified.
var MagicBytes = []byte(Magic)
Functions ¶
func DecodeGradient ¶
DecodeGradient returns the gradient parameters from a non-sensical RGBA color encoding a gradient.
func EncodeGradient ¶
EncodeGradient returns a non-sensical RGBA color encoding gradient parameters.
func ValidAlphaPremulColor ¶
func ValidGradient ¶
ValidGradient returns true if the RGBA color is non-sensical
Types ¶
type Color ¶
type Color struct {
// contains filtered or unexported fields
}
Color is an IconVG color, whose RGBA values can depend on context. Some Colors are direct RGBA values. Other Colors are indirect, referring to an index of the custom palette, a color register of the decoder virtual machine, or a blend of two other Colors.
See the "Colors" section in the package documentation for details.
func BlendColor ¶
BlendColor returns an indirect Color that blends two other Colors. Those two other Colors must both be encodable as a 1 byte color.
To blend a Color that is not encodable as a 1 byte color, first load that Color into a CREG color register, then call CRegColor to produce a Color that is encodable as a 1 byte color. See testdata/favicon.ivg for an example.
See the "Colors" section in the package documentation for details.
func CRegColor ¶
CRegColor returns an indirect Color referring to a color register of the decoder virtual machine.
func DecodeColor1 ¶
func PaletteIndexColor ¶
PaletteIndexColor returns an indirect Color referring to an index of the custom palette.
func (Color) Encode3Direct ¶
func (Color) Encode3Indirect ¶
func (Color) RGBA ¶
RGBA returns the color as a color.RGBA when that is its color type and the color is a valid premultiplied color. If the color is of a different color type or invalid, it will return a opaque black and false.
type ColorType ¶
type ColorType uint8
ColorType distinguishes types of Colors.
const ( // ColorTypeRGBA is a direct RGBA color. ColorTypeRGBA ColorType = iota // ColorTypePaletteIndex is an indirect color, indexing the custom palette. ColorTypePaletteIndex // ColorTypeCReg is an indirect color, indexing the CREG color registers. ColorTypeCReg // ColorTypeBlend is an indirect color, blending two other colors. ColorTypeBlend )
type Destination ¶
type Destination interface { Reset(viewbox ViewBox, palette [64]color.RGBA) CSel() uint8 SetCSel(cSel uint8) NSel() uint8 SetNSel(nSel uint8) SetCReg(adj uint8, incr bool, c Color) SetNReg(adj uint8, incr bool, f float32) SetLOD(lod0, lod1 float32) StartPath(adj uint8, x, y float32) ClosePathEndPath() ClosePathAbsMoveTo(x, y float32) ClosePathRelMoveTo(x, y float32) AbsHLineTo(x float32) RelHLineTo(x float32) AbsVLineTo(y float32) RelVLineTo(y float32) AbsLineTo(x, y float32) RelLineTo(x, y float32) AbsSmoothQuadTo(x, y float32) RelSmoothQuadTo(x, y float32) AbsQuadTo(x1, y1, x, y float32) RelQuadTo(x1, y1, x, y float32) AbsSmoothCubeTo(x2, y2, x, y float32) RelSmoothCubeTo(x2, y2, x, y float32) AbsCubeTo(x1, y1, x2, y2, x, y float32) RelCubeTo(x1, y1, x2, y2, x, y float32) AbsArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32) RelArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32) }
Destination handles the actions decoded from an IconVG graphic's opcodes.
When passed to Decode, the first method called (if any) will be Reset. No methods will be called at all if an error is encountered in the encoded form before the metadata is fully decoded.
type DestinationLogger ¶
type DestinationLogger struct { Destination Alt bool }
func (*DestinationLogger) AbsArcTo ¶
func (d *DestinationLogger) AbsArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)
func (*DestinationLogger) AbsCubeTo ¶
func (d *DestinationLogger) AbsCubeTo(x1, y1, x2, y2, x, y float32)
func (*DestinationLogger) AbsHLineTo ¶
func (d *DestinationLogger) AbsHLineTo(x float32)
func (*DestinationLogger) AbsLineTo ¶
func (d *DestinationLogger) AbsLineTo(x, y float32)
func (*DestinationLogger) AbsQuadTo ¶
func (d *DestinationLogger) AbsQuadTo(x1, y1, x, y float32)
func (*DestinationLogger) AbsSmoothCubeTo ¶
func (d *DestinationLogger) AbsSmoothCubeTo(x2, y2, x, y float32)
func (*DestinationLogger) AbsSmoothQuadTo ¶
func (d *DestinationLogger) AbsSmoothQuadTo(x, y float32)
func (*DestinationLogger) AbsVLineTo ¶
func (d *DestinationLogger) AbsVLineTo(y float32)
func (*DestinationLogger) ClosePathAbsMoveTo ¶
func (d *DestinationLogger) ClosePathAbsMoveTo(x, y float32)
func (*DestinationLogger) ClosePathEndPath ¶
func (d *DestinationLogger) ClosePathEndPath()
func (*DestinationLogger) ClosePathRelMoveTo ¶
func (d *DestinationLogger) ClosePathRelMoveTo(x, y float32)
func (*DestinationLogger) RelArcTo ¶
func (d *DestinationLogger) RelArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)
func (*DestinationLogger) RelCubeTo ¶
func (d *DestinationLogger) RelCubeTo(x1, y1, x2, y2, x, y float32)
func (*DestinationLogger) RelHLineTo ¶
func (d *DestinationLogger) RelHLineTo(x float32)
func (*DestinationLogger) RelLineTo ¶
func (d *DestinationLogger) RelLineTo(x, y float32)
func (*DestinationLogger) RelQuadTo ¶
func (d *DestinationLogger) RelQuadTo(x1, y1, x, y float32)
func (*DestinationLogger) RelSmoothCubeTo ¶
func (d *DestinationLogger) RelSmoothCubeTo(x2, y2, x, y float32)
func (*DestinationLogger) RelSmoothQuadTo ¶
func (d *DestinationLogger) RelSmoothQuadTo(x, y float32)
func (*DestinationLogger) RelVLineTo ¶
func (d *DestinationLogger) RelVLineTo(y float32)
func (*DestinationLogger) Reset ¶
func (d *DestinationLogger) Reset(viewbox ViewBox, palette [64]color.RGBA)
func (*DestinationLogger) SetCReg ¶
func (d *DestinationLogger) SetCReg(adj uint8, incr bool, c Color)
func (*DestinationLogger) SetCSel ¶
func (d *DestinationLogger) SetCSel(cSel uint8)
func (*DestinationLogger) SetLOD ¶
func (d *DestinationLogger) SetLOD(lod0, lod1 float32)
func (*DestinationLogger) SetNReg ¶
func (d *DestinationLogger) SetNReg(adj uint8, incr bool, f float32)
func (*DestinationLogger) SetNSel ¶
func (d *DestinationLogger) SetNSel(nSel uint8)
func (*DestinationLogger) StartPath ¶
func (d *DestinationLogger) StartPath(adj uint8, x, y float32)
type Icon ¶
type Icon interface { // Name is a unique name of the icon inside your program. e.g. "favicon" // It is used to differentiate it from other icons in your program. Name() string // ViewBox is the ViewBox of the icon. ViewBox() ViewBox // RenderOn is called to let the icon render itself on // a Destination with a list of color.Color overrides. RenderOn(dst Destination, col ...color.Color) error }
Icon is an interface to an icon that can be drawn on a Destination
type Metadata ¶
type Metadata struct { ViewBox ViewBox // Palette is a 64 color palette. When encoding, it is the suggested // palette to place within the IconVG graphic. When decoding, it is either // the optional palette passed to Decode, or if no optional palette was // given, the suggested palette within the IconVG graphic. Palette [64]color.RGBA }
Metadata is an IconVG's metadata.
type ViewBox ¶
type ViewBox struct {
MinX, MinY, MaxX, MaxY float32
}
ViewBox is a Rectangle
func (ViewBox) AspectMeet ¶
AspectMeet fits the ViewBox inside a rectangle of size dx,dy maintaining its aspect ratio. The ax, ay argument determine the position of the resized viewbox in the given rectangle. For example ax = Mid, ay = Mid will position the resized viewbox always in the middle of the rectangle.
func (ViewBox) AspectSlice ¶
AspectSlice fills the rectangle of size dx,dy maintaining the ViewBox's aspect ratio. The ax,ay arguments determine the position of the resized viewbox in the given rectangle. For example ax = Mid, ay = Mid will position the resized viewbox always in the middle of the rectangle