-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhelpers.go
72 lines (57 loc) · 1.18 KB
/
helpers.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
package tableimage
import (
"fmt"
"image/color"
"strings"
)
func parseHexColor(x string) (r, g, b, a int) {
x = strings.TrimPrefix(x, "#")
a = 255
if len(x) == 3 {
format := "%1x%1x%1x"
fmt.Sscanf(x, format, &r, &g, &b)
r |= r << 4
g |= g << 4
b |= b << 4
}
if len(x) == 6 {
format := "%02x%02x%02x"
fmt.Sscanf(x, format, &r, &g, &b)
}
if len(x) == 8 {
format := "%02x%02x%02x%02x"
fmt.Sscanf(x, format, &r, &g, &b, &a)
}
return
}
func getColorByHex(hexColor string) color.RGBA {
r, g, b, a := parseHexColor(hexColor)
return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
}
func wrapText(input string) []string {
var wrapped []string
// Split string into array of words
words := strings.Fields(input)
wordsLength := len(words)
if wordsLength == 0 {
return wrapped
}
var lineText string
for i, word := range words {
if len(lineText)+len(word)+1 >= wrapWordsLen {
wrapped = append(wrapped, lineText)
lineText = word
} else {
if lineText == "" {
lineText += word
} else {
lineText += " " + word
}
//if it is the last word
if i == wordsLength-1 {
wrapped = append(wrapped, lineText)
}
}
}
return wrapped
}