Compare commits

...

5 Commits

4 changed files with 141 additions and 3 deletions

View File

@ -36,12 +36,20 @@ var (
defaultFast bool = false defaultFast bool = false
defaultWhiteKelvin string = "" defaultWhiteKelvin string = ""
defaultWhiteName string = "" defaultWhiteName string = ""
defaultHue string = ""
defaultSaturation string = ""
defaultRGB string = ""
) )
func (f Flags) String(name string) string { func (f Flags) String(name string) string {
return f.FlagSet.Lookup(name).Value.String() return f.FlagSet.Lookup(name).Value.String()
} }
func (f Flags) Float32(name string) float32 {
val, _ := strconv.ParseFloat(f.String(name), 32)
return float32(val)
}
func (f Flags) Float64(name string) float64 { func (f Flags) Float64(name string) float64 {
val, _ := strconv.ParseFloat(f.String(name), 64) val, _ := strconv.ParseFloat(f.String(name), 64)
return val return val

105
cmd/setcolor.go Normal file
View File

@ -0,0 +1,105 @@
package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lume"
)
func init() {
var cmdName string = "set-color"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
hue := fs.String("hue", defaultHue, "hue level")
fs.StringVar(hue, "H", defaultHue, "hue level")
saturation := fs.String("saturation", defaultSaturation, "saturation level")
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
rgb := fs.String("rgb", defaultRGB, "RGB value")
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
RegisterCommand(cmdName, Command{
Func: SetColorCmd,
Flags: fs,
})
}
func SetColorCmd(args CmdArgs) int {
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
power := args.Flags.String("power")
if power != "" {
state.Power = power
}
hueFlag := args.Flags.String("hue")
saturationFlag := args.Flags.String("saturation")
rgbFlag := args.Flags.String("rgb")
if hueFlag != "" || saturationFlag != "" {
color := lifx.HSBKColor{}
if hueFlag != "" {
hue := args.Flags.Float32("hue")
color.H = lifx.Float32Ptr(hue)
}
if saturationFlag != "" {
saturation := args.Flags.Float32("saturation")
color.S = lifx.Float32Ptr(saturation)
}
state.Color = color
} else if rgbFlag != "" {
color, err := parseRGB(rgbFlag)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
}
state.Color = color
}
brightnessFlag := args.Flags.String("brightness")
if brightnessFlag != "" {
brightness := args.Flags.Float64("brightness")
state.Brightness = brightness
}
duration := args.Flags.Float64("duration")
state.Duration = duration
fast := args.Flags.Bool("fast")
state.Fast = fast
r, err := c.SetState(selector, state)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
}
if !fast {
PrintResults(r.Results)
}
return 0
}

View File

@ -2,6 +2,8 @@ package lumecmd
import ( import (
"fmt" "fmt"
"strconv"
"strings"
"time" "time"
"git.kill0.net/chill9/lume" "git.kill0.net/chill9/lume"
@ -101,3 +103,21 @@ func PrintLights(lights []lifx.Light) {
) )
} }
} }
func parseRGB(s string) (lifx.RGBColor, error) {
var c lifx.RGBColor
rgb := strings.SplitN(s, ",", 3)
r, err := strconv.ParseUint(rgb[0], 10, 8)
if err != nil {
return c, err
}
g, err := strconv.ParseUint(rgb[1], 10, 8)
if err != nil {
return c, err
}
b, err := strconv.ParseUint(rgb[2], 10, 8)
if err != nil {
return c, err
}
return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b))
}

View File

@ -77,12 +77,13 @@ var (
} }
) )
func NewRGBColor(r, g, b uint8) (*RGBColor, error) { func NewRGBColor(r, g, b uint8) (RGBColor, error) {
var c RGBColor
if (r < 0 || r > 255) && (g < 0 || r > 255) && (b < 0 || b > 255) { if (r < 0 || r > 255) && (g < 0 || r > 255) && (b < 0 || b > 255) {
return nil, errors.New("values must be between 0-255") return c, errors.New("values must be between 0-255")
} }
return &RGBColor{R: r, G: g, B: b}, nil return RGBColor{R: r, G: g, B: b}, nil
} }
func NewHSColor(h, s float32) (HSBKColor, error) { func NewHSColor(h, s float32) (HSBKColor, error) {
@ -188,6 +189,10 @@ func (c HSBKColor) MarshalText() ([]byte, error) {
return []byte(c.ColorString()), nil return []byte(c.ColorString()), nil
} }
func (c RGBColor) MarshalText() ([]byte, error) {
return []byte(c.ColorString()), nil
}
func (c NamedColor) ColorString() string { func (c NamedColor) ColorString() string {
return string(c) return string(c)
} }