forked from darmie/nanovgo-gles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paint.go
104 lines (95 loc) · 3.48 KB
/
paint.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package nanovgo
import (
"math"
)
// Paint structure represent paint information including gradient and image painting.
// Context.SetFillPaint() and Context.SetStrokePaint() accept this instance.
type Paint struct {
xform TransformMatrix
extent [2]float32
radius float32
feather float32
innerColor Color
outerColor Color
image int32
}
func (p *Paint) setPaintColor(color Color) {
p.xform = IdentityMatrix()
p.extent[0] = 0.0
p.extent[1] = 0.0
p.radius = 0.0
p.feather = 1.0
p.innerColor = color
p.outerColor = color
p.image = 0
}
// LinearGradient creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
// of the linear gradient, icol specifies the start color and ocol the end color.
// The gradient is transformed by the current transform when it is passed to Context.FillPaint() or Context.StrokePaint().
func LinearGradient(sx, sy, ex, ey float32, iColor, oColor Color) Paint {
var large float32 = 1e5
dx := ex - sx
dy := ey - sy
d := float32(math.Sqrt(float64(dx*dx + dy*dy)))
if d > 0.0001 {
dx /= d
dy /= d
} else {
dx = 0.0
dy = 1.0
}
return Paint{
xform: TransformMatrix{dy, -dx, dx, dy, sx - dx*large, sy - dy*large},
extent: [2]float32{large, large + d*0.5},
radius: 0.0,
feather: maxF(1.0, d),
innerColor: iColor,
outerColor: oColor,
}
}
// RadialGradient creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
// the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
// The gradient is transformed by the current transform when it is passed to Context.FillPaint() or Context.StrokePaint().
func RadialGradient(cx, cy, inR, outR float32, iColor, oColor Color) Paint {
r := (inR + outR) * 0.5
f := outR - inR
return Paint{
xform: TranslateMatrix(cx, cy),
extent: [2]float32{r, r},
radius: 0.0,
feather: maxF(1.0, f),
innerColor: iColor,
outerColor: oColor,
}
}
// BoxGradient creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
// drop shadows or highlights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
// (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
// the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
// The gradient is transformed by the current transform when it is passed to Context.FillPaint() or Context.StrokePaint().
func BoxGradient(x, y, w, h, r, f float32, iColor, oColor Color) Paint {
return Paint{
xform: TranslateMatrix(x+w*0.5, y+h*0.5),
extent: [2]float32{w * 0.5, h * 0.5},
radius: r,
feather: maxF(1.0, f),
innerColor: iColor,
outerColor: oColor,
}
}
// ImagePattern creates and returns an image patter. Parameters (ox,oy) specify the left-top location of the image pattern,
// (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render.
// The gradient is transformed by the current transform when it is passed to Context.FillPaint() or Context.StrokePaint().
func ImagePattern(cx, cy, w, h, angle float32, img int32, alpha float32) Paint {
xform := RotateMatrix(angle)
xform[4] = cx
xform[5] = cy
color := RGBAf(1, 1, 1, alpha)
return Paint{
xform: xform,
extent: [2]float32{w, h},
image: img,
innerColor: color,
outerColor: color,
}
}