Go Home Page
The Go Programming Language

Package draw

import "exp/draw"

Package draw provides basic graphics and drawing primitives, in the style of the Plan 9 graphics library (see http://plan9.bell-labs.com/magic/man2html/2/draw) and the X Render extension.

Package files

arith.go color.go draw.go event.go

func Border

func Border(dst Image, r Rectangle, w int, src image.Image, sp Point)

Border aligns r.Min in dst with sp in src and then replaces pixels in a w-pixel border around r in dst with the result of the Porter-Duff compositing operation “src over dst.” If w is positive, the border extends w pixels inside r. If w is negative, the border extends w pixels outside r.

func Draw

func Draw(dst Image, r Rectangle, src image.Image, sp Point)

Draw calls DrawMask with a nil mask and an Over op.

func DrawMask

func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Image, mp Point, op Op)

DrawMask aligns r.Min in dst with sp in src and mp in mask and then replaces the rectangle r in dst with the result of a Porter-Duff composition. A nil mask is treated as opaque. The implementation is simple and slow. TODO(nigeltao): Optimize this.

type Color

A Color represents a color with 8-bit R, G, B, and A values, packed into a uint32—0xRRGGBBAA—so that comparison is defined on colors. Color implements image.Color. Color also implements image.Image: it is a 10⁹x10⁹-pixel image of uniform color.

type Color uint32

var (
    Opaque        Color = 0xFFFFFFFF
    Transparent   Color = 0x00000000
    Black         Color = 0x000000FF
    White         Color = 0xFFFFFFFF
    Red           Color = 0xFF0000FF
    Green         Color = 0x00FF00FF
    Blue          Color = 0x0000FFFF
    Cyan          Color = 0x00FFFFFF
    Magenta       Color = 0xFF00FFFF
    Yellow        Color = 0xFFFF00FF
    PaleYellow    Color = 0xFFFFAAFF
    DarkYellow    Color = 0xEEEE9EFF
    DarkGreen     Color = 0x448844FF
    PaleGreen     Color = 0xAAFFAAFF
    MedGreen      Color = 0x88CC88FF
    DarkBlue      Color = 0x000055FF
    PaleBlueGreen Color = 0xAAFFFFFF
    PaleBlue      Color = 0x0000BBFF
    BlueGreen     Color = 0x008888FF
    GreyGreen     Color = 0x55AAAAFF
    PaleGreyGreen Color = 0x9EEEEEFF
    YellowGreen   Color = 0x99994CFF
    MedBlue       Color = 0x000099FF
    GreyBlue      Color = 0x005DBBFF
    PaleGreyBlue  Color = 0x4993DDFF
    PurpleBlue    Color = 0x8888CCFF
)

func (Color) At

func (c Color) At(x, y int) image.Color

func (Color) ColorModel

func (c Color) ColorModel() image.ColorModel

func (Color) Height

func (c Color) Height() int

func (Color) RGBA

func (c Color) RGBA() (r, g, b, a uint32)

func (Color) SetAlpha

func (c Color) SetAlpha(a uint8) Color

SetAlpha returns the color obtained by changing c's alpha value to a and scaling r, g, and b appropriately.

func (Color) Width

func (c Color) Width() int

type Context

A Context represents a single graphics window.

type Context interface {
    // Screen returns an editable Image of window.
    Screen() Image

    // FlushImage flushes changes made to Screen() back to screen.
    FlushImage()

    // KeyboardChan returns a channel carrying keystrokes.
    // An event is sent each time a key is pressed or released.
    // The value k represents key k being pressed.
    // The value -k represents key k being released.
    // The specific set of key values is not specified,
    // but ordinary character represent themselves.
    KeyboardChan() <-chan int

    // MouseChan returns a channel carrying mouse events.
    // A new event is sent each time the mouse moves or a
    // button is pressed or released.
    MouseChan() <-chan Mouse

    // ResizeChan returns a channel carrying resize events.
    // An event is sent each time the window is resized;
    // the client should respond by calling Screen() to obtain
    // the new screen image.
    // The value sent on the channel is always ``true'' and can be ignored.
    ResizeChan() <-chan bool

    // QuitChan returns a channel carrying quit requests.
    // After reading a value from the quit channel, the application
    // should exit.
    QuitChan() <-chan bool
}

type Image

A draw.Image is an image.Image with a Set method to change a single pixel.

type Image interface {
    image.Image
    Set(x, y int, c image.Color)
}

type Mouse

A Mouse represents the state of the mouse.

type Mouse struct {
    Buttons int   // bit mask of buttons: 1<<0 is left, 1<<1 middle, 1<<2 right
    Point         // location of cursor
    Nsec    int64 // time stamp
}

type Op

A Porter-Duff compositing operator.

type Op int

const (
    // Over specifies ``(src in mask) over dst''.
    Over Op = iota
    // Src specifies ``src in mask''.
    Src
)

type Point

A Point is an X, Y coordinate pair.

type Point struct {
    X, Y int
}

ZP is the zero Point.

var ZP Point

func Pt

func Pt(X, Y int) Point

Pt is shorthand for Point{X, Y}.

func (Point) Add

func (p Point) Add(q Point) Point

Add returns the sum of p and q: Pt(p.X+q.X, p.Y+q.Y).

func (Point) Div

func (p Point) Div(k int) Point

Div returns p divided by k: Pt(p.X/k, p.Y/k).

func (Point) Eq

func (p Point) Eq(q Point) bool

Eq returns true if p and q are equal.

func (Point) Mul

func (p Point) Mul(k int) Point

Mul returns p scaled by k: Pt(p.X*k p.Y*k).

func (Point) Sub

func (p Point) Sub(q Point) Point

Sub returns the difference of p and q: Pt(p.X-q.X, p.Y-q.Y).

type Rectangle

A Rectangle contains the Points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y.

type Rectangle struct {
    Min, Max Point
}

ZR is the zero Rectangle.

var ZR Rectangle

func Rect

func Rect(x0, y0, x1, y1 int) Rectangle

Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}.

func Rpt

func Rpt(min, max Point) Rectangle

Rpt is shorthand for Rectangle{min, max}.

func (Rectangle) Add

func (r Rectangle) Add(p Point) Rectangle

Add returns the rectangle r translated by p: Rpt(r.Min.Add(p), r.Max.Add(p)).

func (Rectangle) Canon

func (r Rectangle) Canon() Rectangle

Canon returns a canonical version of r: the returned rectangle has Min.X <= Max.X and Min.Y <= Max.Y.

func (Rectangle) Clip

func (r Rectangle) Clip(r1 Rectangle) Rectangle

Clip returns the largest rectangle containing only points shared by r and r1.

func (Rectangle) Combine

func (r Rectangle) Combine(r1 Rectangle) Rectangle

Combine returns the smallest rectangle containing all points from r and from r1.

func (Rectangle) Dx

func (r Rectangle) Dx() int

Dx returns the width of the rectangle r: r.Max.X - r.Min.X.

func (Rectangle) Dy

func (r Rectangle) Dy() int

Dy returns the width of the rectangle r: r.Max.Y - r.Min.Y.

func (Rectangle) Empty

func (r Rectangle) Empty() bool

Empty retruns true if r contains no points.

func (Rectangle) In

func (r Rectangle) In(r1 Rectangle) bool

InRect returns true if all the points in r are also in r1.

func (Rectangle) Inset

func (r Rectangle) Inset(n int) Rectangle

Inset returns the rectangle r inset by n: Rect(r.Min.X+n, r.Min.Y+n, r.Max.X-n, r.Max.Y-n).

func (Rectangle) Overlaps

func (r Rectangle) Overlaps(r1 Rectangle) bool

Overlaps returns true if r and r1 cross; that is, it returns true if they share any point.

func (Rectangle) Sub

func (r Rectangle) Sub(p Point) Rectangle

Sub returns the rectangle r translated by -p: Rpt(r.Min.Sub(p), r.Max.Sub(p)).

Bugs

This is a toy library and not ready for production use.

Subdirectories

Name   Synopsis
..
x11 This package implements an X11 backend for the exp/draw package.