From f8e9dbc7164def136d9b62132751d7afe0579610 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 4 Apr 2020 10:27:09 -0500 Subject: [PATCH] add named colors via the config file --- cmd/command.go | 3 ++- cmd/setcolor.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/command.go b/cmd/command.go index 0b4ce5a..6048afc 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -10,7 +10,7 @@ import ( type Config struct { AccessToken string - Colors map[string]string `toml:"colors"` + Colors map[string][]float32 `toml:"colors"` } type CmdArgs struct { @@ -44,6 +44,7 @@ var ( defaultHue string = "" defaultSaturation string = "" defaultRGB string = "" + defaultName string = "" ) func (f Flags) String(name string) string { diff --git a/cmd/setcolor.go b/cmd/setcolor.go index d20d15c..1384434 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -27,6 +27,9 @@ func init() { rgb := fs.String("rgb", defaultRGB, "RGB value") fs.StringVar(rgb, "r", defaultRGB, "RGB value") + name := fs.String("name", defaultName, "named color") + fs.StringVar(name, "n", defaultName, "named color") + brightness := fs.String("brightness", defaultBrightness, "brightness state") fs.StringVar(brightness, "b", defaultBrightness, "brightness state") @@ -57,6 +60,7 @@ func SetColorCmd(args CmdArgs) (int, error) { hueFlag := args.Flags.String("hue") saturationFlag := args.Flags.String("saturation") rgbFlag := args.Flags.String("rgb") + name := args.Flags.String("name") if hueFlag != "" || saturationFlag != "" { color := lifx.HSBKColor{} @@ -78,6 +82,16 @@ func SetColorCmd(args CmdArgs) (int, error) { return 1, err } state.Color = color + } else if name != "" { + hsb, ok := args.Config.Colors[name] + if !ok { + return 1, fmt.Errorf("%s is not a defined color", name) + } + color, err := lifx.NewHSBColor(hsb[0], hsb[1], hsb[2]) + if err != nil { + return 1, err + } + state.Color = color } brightnessFlag := args.Flags.String("brightness")