-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorspec.go
273 lines (240 loc) · 8.16 KB
/
colorspec.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gist
import (
"fmt"
"image"
"image/color"
"github.com/goki/ki/kit"
"github.com/goki/mat32"
"github.com/srwiley/rasterx"
)
// Color defines a standard color object for GUI use, with RGBA values, and
// all the usual necessary conversion functions to / from names, strings, etc
// ColorSpec fully specifies the color for rendering -- used in FillStyle and
// StrokeStyle
type ColorSpec struct {
Source ColorSources `desc:"source of color (solid, gradient)"`
Color Color `desc:"color for solid color source"`
Gradient *rasterx.Gradient `desc:"gradient parameters for gradient color source"`
}
var KiT_ColorSpec = kit.Types.AddType(&ColorSpec{}, nil)
// see colorparse.go for ColorSpec.SetString() method
// ColorSources determine how the color is generated -- used in FillStyle and StrokeStyle
type ColorSources int32
const (
SolidColor ColorSources = iota
LinearGradient
RadialGradient
ColorSourcesN
)
//go:generate stringer -type=ColorSources
var KiT_ColorSources = kit.Enums.AddEnumAltLower(ColorSourcesN, kit.NotBitFlag, StylePropProps, "")
func (ev ColorSources) MarshalJSON() ([]byte, error) { return kit.EnumMarshalJSON(ev) }
func (ev *ColorSources) UnmarshalJSON(b []byte) error { return kit.EnumUnmarshalJSON(ev, b) }
// GradientPoints defines points within the gradient
type GradientPoints int32
const (
GpX1 GradientPoints = iota
GpY1
GpX2
GpY2
GradientPointsN
)
// IsNil tests for nil solid or gradient colors
func (cs *ColorSpec) IsNil() bool {
if cs.Source == SolidColor {
return cs.Color.IsNil()
}
return cs.Gradient == nil
}
// ColorOrNil returns the solid color if non-nil, or nil otherwise -- for
// consumers that handle nil colors
func (cs *ColorSpec) ColorOrNil() color.Color {
if cs.Color.IsNil() {
return nil
}
return cs.Color
}
// SetColor sets a solid color
func (cs *ColorSpec) SetColor(cl color.Color) {
cs.Color.SetColor(cl)
cs.Source = SolidColor
cs.Gradient = nil
}
// SetName sets a solid color by name
func (cs *ColorSpec) SetName(name string) {
cs.Color.SetName(name)
cs.Source = SolidColor
cs.Gradient = nil
}
// Copy copies a gradient, making new copies of the stops instead of
// re-using pointers
func (cs *ColorSpec) CopyFrom(cp *ColorSpec) {
*cs = *cp
if cp.Gradient != nil {
cs.Gradient = &rasterx.Gradient{}
*cs.Gradient = *cp.Gradient
sn := len(cp.Gradient.Stops)
cs.Gradient.Stops = make([]rasterx.GradStop, sn)
copy(cs.Gradient.Stops, cp.Gradient.Stops)
}
}
// CopyStopsFrom copies gradient stops from other color spec, if both
// have gradient stops
func (cs *ColorSpec) CopyStopsFrom(cp *ColorSpec) {
if cp.Gradient == nil || cs.Gradient == nil {
return
}
sn := len(cp.Gradient.Stops)
if sn == 0 {
return
}
if len(cs.Gradient.Stops) != sn {
cs.Gradient.Stops = make([]rasterx.GradStop, sn)
}
copy(cs.Gradient.Stops, cp.Gradient.Stops)
}
// NewLinearGradient creates a new Linear gradient in spec, sets Source
// to LinearGradient.
func (cs *ColorSpec) NewLinearGradient() {
cs.Source = LinearGradient
cs.Gradient = &rasterx.Gradient{IsRadial: false, Matrix: rasterx.Identity, Spread: rasterx.PadSpread}
cs.Gradient.Bounds.W = 1
cs.Gradient.Bounds.H = 1
}
// NewRadialGradient creates a new Radial gradient in spec, sets Source
// to RadialGradient.
func (cs *ColorSpec) NewRadialGradient() {
cs.Source = RadialGradient
cs.Gradient = &rasterx.Gradient{IsRadial: true, Matrix: rasterx.Identity, Spread: rasterx.PadSpread}
cs.Gradient.Bounds.W = 1
cs.Gradient.Bounds.H = 1
}
// SetGradientPoints sets UserSpaceOnUse points for gradient based on given bounding box
func (cs *ColorSpec) SetGradientPoints(bbox mat32.Box2) {
if cs.Gradient == nil {
return
}
cs.Gradient.Units = rasterx.UserSpaceOnUse
if cs.Gradient.IsRadial {
ctr := bbox.Min.Add(bbox.Max).MulScalar(.5)
rad := 0.5 * mat32.Max(bbox.Max.X-bbox.Min.X, bbox.Max.Y-bbox.Min.Y)
cs.Gradient.Points = [5]float64{float64(ctr.X), float64(ctr.Y), float64(ctr.X), float64(ctr.Y), float64(rad)}
} else {
cs.Gradient.Points = [5]float64{float64(bbox.Min.X), float64(bbox.Min.Y), float64(bbox.Max.X), float64(bbox.Min.Y), 0} // linear R-L
}
}
// SetShadowGradient sets a linear gradient starting at given color and going
// down to transparent based on given color and direction spec (defaults to
// "to down")
func (cs *ColorSpec) SetShadowGradient(cl color.Color, dir string) {
cs.Color.SetColor(cl)
if dir == "" {
dir = "to down"
}
cs.SetString(fmt.Sprintf("linear-gradient(%v, lighter-0, transparent)", dir), nil)
cs.Source = LinearGradient
}
// SetGradientBounds sets bounds of the gradient
func SetGradientBounds(grad *rasterx.Gradient, bounds image.Rectangle) {
grad.Bounds.X = float64(bounds.Min.X)
grad.Bounds.Y = float64(bounds.Min.Y)
sz := bounds.Size()
grad.Bounds.W = float64(sz.X)
grad.Bounds.H = float64(sz.Y)
}
// CopyGradient copies a gradient, making new copies of the stops instead of
// re-using pointers
func CopyGradient(dst, src *rasterx.Gradient) {
*dst = *src
sn := len(src.Stops)
dst.Stops = make([]rasterx.GradStop, sn)
copy(dst.Stops, src.Stops)
}
func MatToRasterx(mat *mat32.Mat2) rasterx.Matrix2D {
return rasterx.Matrix2D{float64(mat.XX), float64(mat.YX), float64(mat.XY), float64(mat.YY), float64(mat.X0), float64(mat.Y0)}
}
func RasterxToMat(mat *rasterx.Matrix2D) mat32.Mat2 {
return mat32.Mat2{float32(mat.A), float32(mat.B), float32(mat.C), float32(mat.D), float32(mat.E), float32(mat.F)}
}
// RenderColor gets the color for rendering, applying opacity and bounds for
// gradients
func (cs *ColorSpec) RenderColor(opacity float32, bounds image.Rectangle, xform mat32.Mat2) interface{} {
if cs.Source == SolidColor || cs.Gradient == nil {
return rasterx.ApplyOpacity(cs.Color, float64(opacity))
} else {
if cs.Source == RadialGradient {
cs.Gradient.IsRadial = true
} else {
cs.Gradient.IsRadial = false
}
SetGradientBounds(cs.Gradient, bounds)
return cs.Gradient.GetColorFunctionUS(float64(opacity), MatToRasterx(&xform))
}
}
// SetIFace sets the color spec from given interface value, e.g., for ki.Props
// key is an optional property key for error -- always logs errors
func (c *ColorSpec) SetIFace(val interface{}, ctxt Context, key string) error {
switch valv := val.(type) {
case string:
c.SetString(valv, ctxt)
case *Color:
c.SetColor(*valv)
case *ColorSpec:
*c = *valv
case color.Color:
c.SetColor(valv)
}
return nil
}
// ApplyXForm transforms the points for a UserSpaceOnUse gradient
func (c *ColorSpec) ApplyXForm(xf mat32.Mat2) {
if c.Gradient == nil {
return
}
if c.Gradient.Units == rasterx.ObjectBoundingBox {
return
}
mat := RasterxToMat(&c.Gradient.Matrix)
rot := xf.ExtractRot()
if c.Gradient.IsRadial || rot != 0 || !mat.IsIdentity() { // radial uses transform instead of points
mat = mat.Mul(xf)
c.Gradient.Matrix = MatToRasterx(&mat)
} else {
p1 := mat32.Vec2{float32(c.Gradient.Points[0]), float32(c.Gradient.Points[1])}
p1 = xf.MulVec2AsPt(p1)
p2 := mat32.Vec2{float32(c.Gradient.Points[2]), float32(c.Gradient.Points[3])}
p2 = xf.MulVec2AsPt(p2)
c.Gradient.Points[0] = float64(p1.X)
c.Gradient.Points[1] = float64(p1.Y)
c.Gradient.Points[2] = float64(p2.X)
c.Gradient.Points[3] = float64(p2.Y)
}
}
// ApplyXFormPt transforms the points for a UserSpaceOnUse gradient
// relative to a given center point
func (c *ColorSpec) ApplyXFormPt(xf mat32.Mat2, pt mat32.Vec2) {
if c.Gradient == nil {
return
}
if c.Gradient.Units == rasterx.ObjectBoundingBox {
return
}
mat := RasterxToMat(&c.Gradient.Matrix)
rot := xf.ExtractRot()
if c.Gradient.IsRadial || rot != 0 || !mat.IsIdentity() { // radial uses transform instead of points
mat = mat.MulCtr(xf, pt)
c.Gradient.Matrix = MatToRasterx(&mat)
} else {
p1 := mat32.Vec2{float32(c.Gradient.Points[0]), float32(c.Gradient.Points[1])}
p1 = xf.MulVec2AsPtCtr(p1, pt)
p2 := mat32.Vec2{float32(c.Gradient.Points[2]), float32(c.Gradient.Points[3])}
p2 = xf.MulVec2AsPtCtr(p2, pt)
c.Gradient.Points[0] = float64(p1.X)
c.Gradient.Points[1] = float64(p1.Y)
c.Gradient.Points[2] = float64(p2.X)
c.Gradient.Points[3] = float64(p2.Y)
}
}