-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorparse.go
573 lines (550 loc) · 15.8 KB
/
colorparse.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// 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.
// color parsing is adapted from github.com/srwiley/oksvg:
//
// Copyright 2017 The oksvg Authors. All rights reserved.
//
// created: 2/12/2017 by S.R.Wiley
package gist
import (
"encoding/xml"
"fmt"
"image/color"
"io"
"log"
"strconv"
"strings"
"github.com/goki/ki/kit"
"github.com/goki/mat32"
// "github.com/rcoreilly/rasterx"
"github.com/srwiley/rasterx"
"golang.org/x/net/html/charset"
)
// XMLAttr searches for given attribute in slice of xml attributes -- returns "" if not found
func XMLAttr(name string, attrs []xml.Attr) string {
for _, attr := range attrs {
if attr.Name.Local == name {
return attr.Value
}
}
return ""
}
// ColorSpecCache is a cache of named color specs -- only a few are constantly re-used
// so we save them in the cache instead of constantly recomputing!
var ColorSpecCache map[string]*ColorSpec
// SetString sets the color spec from a standard CSS-formatted string -- see
// https://www.w3schools.com/css/css3_gradients.asp -- see UnmarshalXML for
// XML-based version
func (cs *ColorSpec) SetString(clrstr string, ctxt Context) bool {
// pr := prof.Start("ColorSpec.SetString")
// defer pr.End()
if ColorSpecCache == nil {
ColorSpecCache = make(map[string]*ColorSpec)
}
fullnm := cs.Color.HexString() + clrstr
if ccg, ok := ColorSpecCache[fullnm]; ok {
cs.CopyFrom(ccg)
return true
}
clrstr = strings.TrimSpace(clrstr)
if strings.HasPrefix(clrstr, "url(") {
if ctxt != nil {
cspec := ctxt.ContextColorSpecByURL(clrstr)
if cspec != nil {
*cs = *cspec
return true
}
}
fmt.Printf("gi.Color Warning: Not able to find url: %v\n", clrstr)
cs.Gradient = nil
cs.Source = SolidColor
cs.Color = Black
return false
}
clrstr = strings.ToLower(clrstr)
grad := "-gradient"
if gidx := strings.Index(clrstr, grad); gidx > 0 {
gtyp := clrstr[:gidx]
rmdr := clrstr[gidx+len(grad):]
pidx := strings.IndexByte(rmdr, '(')
if pidx < 0 {
log.Printf("gi.ColorSpec.Parse gradient parameters not found\n")
return false
}
pars := rmdr[pidx+1:]
pars = strings.TrimSuffix(pars, ");")
pars = strings.TrimSuffix(pars, ")")
switch gtyp {
case "repeating-linear":
cs.Gradient = &rasterx.Gradient{Points: [5]float64{0, 0, 0, 1, 0}, IsRadial: false, Matrix: rasterx.Identity, Spread: rasterx.RepeatSpread}
cs.Source = LinearGradient
cs.parseLinearGrad(pars)
case "linear":
cs.Gradient = &rasterx.Gradient{Points: [5]float64{0, 0, 0, 1, 0}, IsRadial: false, Matrix: rasterx.Identity, Spread: rasterx.PadSpread}
cs.Source = LinearGradient
cs.parseLinearGrad(pars)
case "repeating-radial":
cs.Gradient = &rasterx.Gradient{Points: [5]float64{0.5, 0.5, 0.5, 0.5, 0.5}, IsRadial: true, Matrix: rasterx.Identity, Spread: rasterx.RepeatSpread}
cs.Source = RadialGradient
cs.parseRadialGrad(pars)
case "radial":
cs.Gradient = &rasterx.Gradient{Points: [5]float64{0.5, 0.5, 0.5, 0.5, 0.5}, IsRadial: true, Matrix: rasterx.Identity, Spread: rasterx.PadSpread}
cs.Source = RadialGradient
cs.parseRadialGrad(pars)
}
FixGradientStops(cs.Gradient)
svcs := &ColorSpec{} // critical to save a copy..
svcs.CopyFrom(cs)
ColorSpecCache[fullnm] = svcs
} else {
cs.Gradient = nil
cs.Source = SolidColor
cs.Color.SetString(clrstr, nil)
}
return true
}
// GradientDegToSides maps gradient degree notation to side notation
var GradientDegToSides = map[string]string{
"0deg": "top",
"360deg": "top",
"45deg": "top right",
"-315deg": "top right",
"90deg": "right",
"-270deg": "right",
"135deg": "bottom right",
"-225deg": "bottom right",
"180deg": "bottom",
"-180deg": "bottom",
"225deg": "bottom left",
"-135deg": "bottom left",
"270deg": "left",
"-90deg": "left",
"315deg": "top left",
"-45deg": "top left",
}
func (cs *ColorSpec) parseLinearGrad(pars string) bool {
plist := strings.Split(pars, ", ")
var prevColor color.Color
stopIdx := 0
for pidx := 0; pidx < len(plist); pidx++ {
par := strings.TrimRight(strings.TrimSpace(plist[pidx]), ",")
origPar := par
switch {
case strings.Contains(par, "deg"):
// can't use trig, b/c need to be full 1, 0 values -- just use map
var ok bool
par, ok = GradientDegToSides[par]
if !ok {
log.Printf("gi.ColorSpec.Parse invalid gradient angle -- must be at 45 degree increments: %v\n", origPar)
}
par = "to " + par
fallthrough
case strings.HasPrefix(par, "to "):
sides := strings.Split(par[3:], " ")
cs.Gradient.Points = [5]float64{0, 0, 0, 0, 0}
for _, side := range sides {
switch side {
case "bottom":
cs.Gradient.Points[GpY1] = 0
cs.Gradient.Points[GpY2] = 1
case "top":
cs.Gradient.Points[GpY1] = 1
cs.Gradient.Points[GpY2] = 0
case "right":
cs.Gradient.Points[GpX1] = 0
cs.Gradient.Points[GpX2] = 1
case "left":
cs.Gradient.Points[GpX1] = 1
cs.Gradient.Points[GpX2] = 0
}
}
case strings.HasPrefix(par, ")"):
break
default: // must be a color stop
var stop *rasterx.GradStop
if len(cs.Gradient.Stops) > stopIdx {
stop = &(cs.Gradient.Stops[stopIdx])
} else {
stop = &rasterx.GradStop{Opacity: 1.0, StopColor: color.Black}
}
if stopIdx == 0 {
prevColor = cs.Color // base color
// fmt.Printf("starting prev color: %v\n", prevColor)
}
if parseColorStop(stop, prevColor, par) {
if len(cs.Gradient.Stops) <= stopIdx {
cs.Gradient.Stops = append(cs.Gradient.Stops, *stop)
}
if stopIdx == 0 {
cs.Color.SetColor(stop.StopColor) // keep first one
}
prevColor = stop.StopColor
stopIdx++
}
}
}
if len(cs.Gradient.Stops) > stopIdx {
cs.Gradient.Stops = cs.Gradient.Stops[:stopIdx]
}
return true
}
// todo: this is complex:
// https://www.w3.org/TR/css3-images/#radial-gradients
func (cs *ColorSpec) parseRadialGrad(pars string) bool {
plist := strings.Split(pars, ", ")
var prevColor color.Color
stopIdx := 0
for pidx := 0; pidx < len(plist); pidx++ {
par := strings.TrimRight(strings.TrimSpace(plist[pidx]), ",")
// origPar := par
switch {
case strings.Contains(par, "circle"):
cs.Gradient.Points = [5]float64{0.5, 0.5, 0.5, 0.5, 0.5}
case strings.Contains(par, "ellipse"):
cs.Gradient.Points = [5]float64{0.5, 0.5, 0.5, 0.5, 0.5}
case strings.HasPrefix(par, "at "):
sides := strings.Split(par[3:], " ")
cs.Gradient.Points = [5]float64{0, 0, 0, 0, 0}
for _, side := range sides {
switch side {
case "bottom":
cs.Gradient.Points[GpY1] = 0
case "top":
cs.Gradient.Points[GpY1] = 1
case "right":
cs.Gradient.Points[GpX1] = 0
case "left":
cs.Gradient.Points[GpX1] = 1
}
}
case strings.HasPrefix(par, ")"):
break
default: // must be a color stop
var stop *rasterx.GradStop
if len(cs.Gradient.Stops) > stopIdx {
stop = &(cs.Gradient.Stops[stopIdx])
} else {
stop = &rasterx.GradStop{Opacity: 1.0, StopColor: color.Black}
}
if stopIdx == 0 {
prevColor = cs.Color // base color
// fmt.Printf("starting prev color: %v\n", prevColor)
}
if parseColorStop(stop, prevColor, par) {
if len(cs.Gradient.Stops) <= stopIdx {
cs.Gradient.Stops = append(cs.Gradient.Stops, *stop)
}
if stopIdx == 0 {
cs.Color.SetColor(stop.StopColor) // keep first one
}
prevColor = stop.StopColor
stopIdx++
}
}
}
if len(cs.Gradient.Stops) > stopIdx {
cs.Gradient.Stops = cs.Gradient.Stops[:stopIdx]
}
return true
}
func parseColorStop(stop *rasterx.GradStop, prevColor color.Color, par string) bool {
cnm := par
if spcidx := strings.Index(par, " "); spcidx > 0 {
cnm = par[:spcidx]
offs := strings.TrimSpace(par[spcidx+1:])
off, err := readFraction(offs)
if err != nil {
log.Printf("gi.ColorSpec.Parse invalid offset: %v\n", err)
return false
}
stop.Offset = off
}
// color blending doesn't work well in pre-multiplied alpha RGB space!
if prevColor != nil && strings.HasPrefix(cnm, "clearer-") {
pcts := strings.TrimPrefix(cnm, "clearer-")
pct, _ := kit.ToFloat(pcts)
stop.Opacity = (100.0 - pct) / 100.0
stop.StopColor = prevColor
} else if prevColor != nil && cnm == "transparent" {
stop.Opacity = 0
stop.StopColor = prevColor
} else {
clr, err := ColorFromString(cnm, prevColor)
if err != nil {
log.Printf("gi.ColorSpec.Parse invalid color string: %v\n", err)
return false
}
stop.StopColor = clr
}
// fmt.Printf("color: %v from: %v\n", stop.StopColor, par)
return true
}
// ReadXML reads XML-formatted ColorSpec from io.Reader
func (cs *ColorSpec) ReadXML(reader io.Reader) error {
decoder := xml.NewDecoder(reader)
decoder.CharsetReader = charset.NewReaderLabel
for {
t, err := decoder.Token()
if err != nil {
if err == io.EOF {
break
}
log.Printf("gi.ColorSpec parsing error: %v\n", err)
return err
}
switch se := t.(type) {
case xml.StartElement:
return cs.UnmarshalXML(decoder, se)
// todo: ignore rest?
}
}
return nil
}
// UnmarshalXML parses the given XML-formatted string to set the color
// specification
func (cs *ColorSpec) UnmarshalXML(decoder *xml.Decoder, se xml.StartElement) error {
start := &se
for {
var t xml.Token
var err error
if start != nil {
t = *start
start = nil
} else {
t, err = decoder.Token()
}
if err != nil {
if err == io.EOF {
break
}
log.Printf("gi.ColorSpec.UnmarshalXML color parsing error: %v\n", err)
return err
}
switch se := t.(type) {
case xml.StartElement:
switch se.Name.Local {
case "linearGradient":
if cs.Gradient == nil {
cs.Gradient = &rasterx.Gradient{Points: [5]float64{0, 0, 1, 0, 0},
IsRadial: false, Matrix: rasterx.Identity}
} else {
cs.Gradient.IsRadial = false
}
cs.Source = LinearGradient
// fmt.Printf("lingrad %v\n", cs.Gradient)
for _, attr := range se.Attr {
// fmt.Printf("attr: %v val: %v\n", attr.Name.Local, attr.Value)
switch attr.Name.Local {
// note: id not processed here - must be done externally
case "x1":
cs.Gradient.Points[0], err = readFraction(attr.Value)
case "y1":
cs.Gradient.Points[1], err = readFraction(attr.Value)
case "x2":
cs.Gradient.Points[2], err = readFraction(attr.Value)
case "y2":
cs.Gradient.Points[3], err = readFraction(attr.Value)
default:
err = cs.ReadGradAttr(attr)
}
if err != nil {
log.Printf("gi.ColorSpec.UnmarshalXML linear gradient parsing error: %v\n", err)
return err
}
}
case "radialGradient":
if cs.Gradient == nil {
cs.Gradient = &rasterx.Gradient{Points: [5]float64{0.5, 0.5, 0.5, 0.5, 0.5},
IsRadial: true, Matrix: rasterx.Identity}
} else {
cs.Gradient.IsRadial = true
}
cs.Source = RadialGradient
var setFx, setFy bool
for _, attr := range se.Attr {
// fmt.Printf("stop attr: %v val: %v\n", attr.Name.Local, attr.Value)
switch attr.Name.Local {
// note: id not processed here - must be done externally
case "r":
cs.Gradient.Points[4], err = readFraction(attr.Value)
case "cx":
cs.Gradient.Points[0], err = readFraction(attr.Value)
case "cy":
cs.Gradient.Points[1], err = readFraction(attr.Value)
case "fx":
setFx = true
cs.Gradient.Points[2], err = readFraction(attr.Value)
case "fy":
setFy = true
cs.Gradient.Points[3], err = readFraction(attr.Value)
default:
err = cs.ReadGradAttr(attr)
}
if err != nil {
log.Printf("gi.ColorSpec.UnmarshalXML radial gradient parsing error: %v\n", err)
return err
}
}
if setFx == false { // set fx to cx by default
cs.Gradient.Points[2] = cs.Gradient.Points[0]
}
if setFy == false { // set fy to cy by default
cs.Gradient.Points[3] = cs.Gradient.Points[1]
}
case "stop":
stop := rasterx.GradStop{Opacity: 1.0, StopColor: color.Black}
ats := se.Attr
sty := XMLAttr("style", ats)
if sty != "" {
spl := strings.Split(sty, ";")
for _, s := range spl {
s := strings.TrimSpace(s)
ci := strings.IndexByte(s, ':')
if ci < 0 {
continue
}
a := xml.Attr{}
a.Name.Local = s[:ci]
a.Value = s[ci+1:]
ats = append(ats, a)
}
}
for _, attr := range ats {
switch attr.Name.Local {
case "offset":
stop.Offset, err = readFraction(attr.Value)
case "stop-color":
clr, err := ColorFromString(attr.Value, nil)
if err != nil {
log.Printf("gi.ColorSpec.UnmarshalXML invalid color string: %v\n", err)
return err
}
stop.StopColor = clr
case "stop-opacity":
stop.Opacity, err = strconv.ParseFloat(attr.Value, 64)
// fmt.Printf("got opacity: %v\n", stop.Opacity)
}
if err != nil {
log.Printf("gi.ColorSpec.UnmarshalXML color stop parsing error: %v\n", err)
return err
}
}
if cs.Gradient == nil {
fmt.Printf("no gradient but stops in: %v stop: %v\n", cs, stop)
} else {
cs.Gradient.Stops = append(cs.Gradient.Stops, stop)
}
default:
errStr := "gi.ColorSpec Cannot process svg element " + se.Name.Local
log.Println(errStr)
}
case xml.EndElement:
if se.Name.Local == "linearGradient" || se.Name.Local == "radialGradient" {
// fmt.Printf("gi.ColorSpec got gradient end element: %v\n", se.Name.Local)
return nil
}
if se.Name.Local != "stop" {
fmt.Printf("gi.ColorSpec got unexpected end element: %v\n", se.Name.Local)
}
case xml.CharData:
}
}
return nil
}
func readFraction(v string) (f float64, err error) {
v = strings.TrimSpace(v)
d := 1.0
if strings.HasSuffix(v, "%") {
d = 100
v = strings.TrimSuffix(v, "%")
}
f, err = strconv.ParseFloat(v, 64)
f /= d
// if f > 1 {
// f = 1
// } else
if f < 0 {
f = 0
}
return
}
// func ReadGradUrl(v string) (grad *rasterx.Gradient, err error) {
// if strings.HasPrefix(v, "url(") && strings.HasSuffix(v, ")") {
// urlStr := strings.TrimSpace(v[4 : len(v)-1])
// if strings.HasPrefix(urlStr, "#") {
// switch grad := c.icon.Ids[urlStr[1:]].(type) {
// case *rasterx.Gradient:
// return grad, nil
// default:
// return nil, nil //missingIdError
// }
// }
// }
// return nil, nil // not a gradient url, and not an error
// }
func (cs *ColorSpec) ReadGradAttr(attr xml.Attr) (err error) {
switch attr.Name.Local {
case "gradientTransform":
tx := mat32.Identity2D()
tx.SetString(attr.Value)
cs.Gradient.Matrix = MatToRasterx(&tx)
case "gradientUnits":
switch strings.TrimSpace(attr.Value) {
case "userSpaceOnUse":
cs.Gradient.Units = rasterx.UserSpaceOnUse
case "objectBoundingBox":
cs.Gradient.Units = rasterx.ObjectBoundingBox
}
case "spreadMethod":
switch strings.TrimSpace(attr.Value) {
case "pad":
cs.Gradient.Spread = rasterx.PadSpread
case "reflect":
cs.Gradient.Spread = rasterx.ReflectSpread
case "repeat":
cs.Gradient.Spread = rasterx.RepeatSpread
}
}
return nil
}
// FixGradientStops applies the CSS rules to regularize the gradient stops: https://www.w3.org/TR/css3-images/#color-stop-syntax
func FixGradientStops(grad *rasterx.Gradient) {
sz := len(grad.Stops)
if sz == 0 {
return
}
splitSt := -1
last := 0.0
for i := 0; i < sz; i++ {
st := &(grad.Stops[i])
if i == sz-1 && st.Offset == 0 {
if last < 1.0 {
st.Offset = 1.0
} else {
st.Offset = last
}
}
if i > 0 && st.Offset == 0 && splitSt < 0 {
splitSt = i
st.Offset = last
continue
}
if splitSt > 0 {
start := grad.Stops[splitSt].Offset
end := st.Offset
per := (end - start) / float64(1+(i-splitSt))
cur := start + per
for j := splitSt; j < i; j++ {
grad.Stops[j].Offset = cur
cur += per
}
}
last = st.Offset
}
// fmt.Printf("grad stops:\n")
// for i := 0; i < sz; i++ {
// st := grad.Stops[i]
// fmt.Printf("%v\t%v opacity: %v\n", i, st.Offset, st.Opacity)
// }
}