From b1894b38de386d5f9569c92c27a7e2019f8ee8cd Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 24 Jan 2021 19:02:48 -0600 Subject: [PATCH] Validate colors from configuration file --- cmd/command.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cmd/command.go b/cmd/command.go index ab0e0fa..c8b0c6b 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -100,5 +100,31 @@ func (c *Config) Validate() error { if c.AccessToken == "" { err = errors.New("access_token is not set") } + + if err = c.validateColors(); err != nil { + return err + } + + return err +} + +func (c *Config) validateColors() (err error) { + if len(c.Colors) > 0 { + for name, hsb := range c.Colors { + if len(hsb) != 3 { + return fmt.Errorf("color '%s' needs three values", name) + } + h, s, b := hsb[0], hsb[1], hsb[2] + if h < 0 || h > 360 { + return fmt.Errorf("color '%s' hue value must be between 0.0-360.0", name) + } + if s < 0 || b > 1 { + return fmt.Errorf("color '%s' saturation value must be between 0.0-1.0", name) + } + if b < 0 || b > 1 { + return fmt.Errorf("color '%s' brightness value must be between 0.0-1.0", name) + } + } + } return err }