-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgradient.go
65 lines (57 loc) · 1.91 KB
/
gradient.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
// Copyright 2020 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The API doc comments are based on the MDN Web Docs for the [Canvas API]
// by Mozilla Contributors and are licensed under [CC-BY-SA 2.5].
//
// [Canvas API]: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
// [CC-BY-SA 2.5]: https://creativecommons.org/licenses/by-sa/2.5/
package canvas
import "image/color"
// Gradient represents a gradient. It is returned by the methods
// Context.CreateLinearGradient and Context.CreateRadialGradient.
// It can be used with the Context.SetFillStyleGradient and
// Context.SetStrokeStyleGradient methods.
//
// The gradient should be released with the Release method when it is no longer
// needed.
type Gradient struct {
id uint32
ctx *Context
released bool
}
// AddColorStop adds a new stop, defined by an offset and a color, to the
// gradient.
func (g *Gradient) AddColorStop(offset float64, c color.Color) {
g.checkUseAfterRelease()
g.ctx.buf.addByte(bGradientAddColorStop)
g.ctx.buf.addUint32(g.id)
g.ctx.buf.addFloat64(offset)
g.ctx.buf.addColor(c)
}
// AddColorStopString adds a new stop, defined by an offset and a color, to
// the gradient.
//
// The color is parsed as a CSS color value like "#a100cb", "#ccc",
// "darkgreen", "rgba(0.5, 0.2, 0.7, 1.0)", etc.
func (g *Gradient) AddColorStopString(offset float64, color string) {
g.checkUseAfterRelease()
g.ctx.buf.addByte(bGradientAddColorStopString)
g.ctx.buf.addUint32(g.id)
g.ctx.buf.addFloat64(offset)
g.ctx.buf.addString(color)
}
// Release releases the gradient on the client side.
func (g *Gradient) Release() {
if g.released {
return
}
g.ctx.buf.addByte(bReleaseGradient)
g.ctx.buf.addUint32(g.id)
g.released = true
}
func (g *Gradient) checkUseAfterRelease() {
if g.released {
panic("Gradient: use after release")
}
}