-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.go
79 lines (65 loc) · 1.36 KB
/
background.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
package pdf
import (
"image/color"
"rogchap.com/skia"
)
type background struct {
container
color string
}
func (b *background) messure(available size) sizePlan {
if b.child != nil {
return b.child.messure(available)
}
return sizePlan{}
}
func (b *background) draw(available size) {
c := parseHexColor(b.color)
p := skia.NewPaint(c)
r := skia.NewRect(0, 0, available.width, available.height)
b.skdoc.canvas.DrawRect(&r, p)
if b.child != nil {
b.child.draw(available)
}
}
func parseHexColor(s string) color.RGBA {
white := color.RGBA{0xFF, 0xFF, 0xFF, 0xFF}
if s == "" {
return white
}
var c color.RGBA
if s[0] == '#' {
s = s[1:]
}
hexToByte := func(b byte) byte {
switch {
case b >= '0' && b <= '9':
return b - '0'
case b >= 'a' && b <= 'f':
return b - 'a' + 10
case b >= 'A' && b <= 'F':
return b - 'A' + 10
}
return 0
}
switch len(s) {
case 8:
c.A = hexToByte(s[0])<<4 + hexToByte(s[1])
c.R = hexToByte(s[2])<<4 + hexToByte(s[3])
c.G = hexToByte(s[4])<<4 + hexToByte(s[5])
c.B = hexToByte(s[6])<<4 + hexToByte(s[7])
case 6:
c.A = 0xFF
c.R = hexToByte(s[0])<<4 + hexToByte(s[1])
c.G = hexToByte(s[2])<<4 + hexToByte(s[3])
c.B = hexToByte(s[4])<<4 + hexToByte(s[5])
case 3:
c.A = 0xFF
c.R = hexToByte(s[0]) * 17
c.G = hexToByte(s[1]) * 17
c.B = hexToByte(s[2]) * 17
default:
c = white
}
return c
}