-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpen.go
More file actions
64 lines (58 loc) · 2.07 KB
/
pen.go
File metadata and controls
64 lines (58 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package graphics2d
import (
"github.com/jphsd/graphics2d/color"
"image"
)
// Pen describes the color/image, stroke and shape to image transform to
// use when rendering shapes. If Stroke is nil then the shape's paths are
// used as is and forced closed (i.e. this is a fill). If Xfm is nil then
// the identity xfm is assumed.
type Pen struct {
Filler image.Image
Stroke PathProcessor
Xfm Transform
}
// Predefined pens.
var (
BlackPen = NewPen(color.Black, 1)
DarkGrayPen = NewPen(color.DarkGray, 1)
GrayPen = NewPen(color.MidGray, 1)
LightGrayPen = NewPen(color.LightGray, 1)
WhitePen = NewPen(color.White, 1)
RedPen = NewPen(color.Red, 1)
GreenPen = NewPen(color.Green, 1)
BluePen = NewPen(color.Blue, 1)
YellowPen = NewPen(color.Yellow, 1)
MagentaPen = NewPen(color.Magenta, 1)
CyanPen = NewPen(color.Cyan, 1)
OrangePen = NewPen(color.Orange, 1)
BrownPen = NewPen(color.Brown, 1)
)
// NewPen returns a pen that will render a shape with the given pen
// width and color into an image.
// It uses the Stroke path processor to accomplish this with the bevel join and butt cap functions.
func NewPen(color color.Color, width float64) *Pen {
return &Pen{image.NewUniform(color), NewStrokeProc(width), nil}
}
// NewProcessorPen returns a pen that will render a shape with the given pen
// width and color into an image after applying the supplied path processor.
func NewProcessorPen(color color.Color, width float64, proc PathProcessor) *Pen {
return &Pen{image.NewUniform(color), NewCompoundProc(proc, NewStrokeProc(width)), nil}
}
// NewStrokedPen returns a pen that will render a shape with the given pen
// width and color into an image using the supplied join and cap functions.
func NewStrokedPen(color color.Color, width float64,
join func(Part, []float64, Part) []Part,
cap func(Part, []float64, Part) []Part) *Pen {
if width < 0 {
width = -width
}
width /= 2
if join == nil {
join = JoinBevel
}
if cap == nil {
cap = CapButt
}
return &Pen{image.NewUniform(color), NewStrokeProcExt(width, -width, join, 0.5, cap), nil}
}