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
|
package descriptors
import (
"fmt"
"strings"
"github.com/gookit/color"
ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI"
rl "github.com/gen2brain/raylib-go/raylib"
)
// Using this method instead of the base style structs allows us to use
// hex values as colours since golang's implimetation of JSON deserializer
// doesn't really like color.RGBA
type StyleDescriptor struct {
BackgroundColor string
FontColor string
Padding *float32
Roundness *float32
}
func (base *StyleDescriptor) Parse() ui.Style {
var outp = ui.Style {
BacgroundColor: base.getBackgroundColor(),
FontColor: base.getFontColor(),
Roundness: base.Roundness,
Padding: base.Padding,
}
return outp
}
func (base *StyleDescriptor) getBackgroundColor() *rl.Color {
if len(base.BackgroundColor) == 0 {
return nil
}
var outp, err = parseColor(base.BackgroundColor)
if err != nil {
rl.TraceLog(rl.LogWarning, "Failed to parse the background color %s in style: %s", base.BackgroundColor, err.Error())
return nil
}
return outp
}
func (base *StyleDescriptor) getFontColor() *rl.Color {
if len(base.FontColor) == 0 {
return nil
}
var outp, err = parseColor(base.FontColor)
if err != nil {
rl.TraceLog(rl.LogWarning, "Failed to parse the font color %s in style: %s", base.FontColor, err.Error())
return nil
}
return outp
}
func parseColor(val string) (*rl.Color, error) {
val = strings.TrimPrefix(val, "#")
if !isColorValid(val) {
return nil, fmt.Errorf("Invalid color notation")
}
var parsed = color.HEX(val)
var outp = rl.NewColor(parsed[0], parsed[1], parsed[2], parsed[3])
// If there is no alpha cahnnel
if len(val) % 2 == 0 {
outp.A = 0xFF
}
return &outp, nil
}
func isColorValid(val string) bool {
switch len(val) {
// #RGB, #RGBA, #RRGGBB, #RRGGBBAA
case 3, 4, 6, 8:
return true
}
return false
}
|