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 }