From abc9e0a9784c6136bf0bd0402d628cd82fed203f Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 29 Feb 2020 12:54:56 -0600 Subject: [PATCH] add new color structs --- client.go | 3 ++- structs.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index d492acb..0a85f12 100644 --- a/client.go +++ b/client.go @@ -86,6 +86,8 @@ func (s *Client) SetState(selector string, state State) ([]Result, error) { return nil, err } + fmt.Println(string(j)) + res, err := s.Request("PUT", EndpointState(selector), bytes.NewBuffer(j)) if err != nil { return nil, err @@ -124,7 +126,6 @@ func (s *Client) Toggle(selector string, duration float64) ([]Result, error) { func (s *Client) PowerOff(selector string) ([]Result, error) { return s.SetState(selector, State{Power: "off"}) - } func (s *Client) FastPowerOff(selector string) { diff --git a/structs.go b/structs.go index f821b1b..80a23bf 100644 --- a/structs.go +++ b/structs.go @@ -1,17 +1,23 @@ package lifx import ( + "fmt" "net/http" + "strings" ) -const API_BASE_URL = "https://api.lifx.com/v1" +type ( + Color interface { + ColorString() string + } +) type ( Status string State struct { Power string `json:"power,omitempty"` - Color string `json:"color,omitempty"` + Color Color `json:"color,omitempty"` Brightness float64 `json:"brightness,omitempty"` Duration float64 `json:"duration,omitempty"` Infrared float64 `json:"infrared,omitempty"` @@ -46,6 +52,15 @@ type ( Toggle struct { Duration float64 `json:"duration,omitempty"` } + + RGBColor struct { + R, G, B uint8 + } + + HSBKColor struct { + H, K int16 + S, B float32 + } ) const ( @@ -57,3 +72,42 @@ const ( func (s Status) Success() bool { return s == OK } + +func NewRGBColor(r, g, b uint8) (*RGBColor, error) { + return &RGBColor{R: r, G: g, B: b}, nil +} + +func NewHSBKColor() HSBKColor { + var c HSBKColor + c.H, c.S, c.B, c.K = -1, -1, -1, -1 + return c +} + +func (c RGBColor) ColorString() string { + return fmt.Sprintf("rgb:%d,%d,%d", c.R, c.G, c.B) +} + +func (c RGBColor) Hex() string { + return fmt.Sprintf("#%x%x%x", c.R, c.G, c.B) +} + +func (c HSBKColor) ColorString() string { + var s []string + if c.H >= 0 { + s = append(s, fmt.Sprintf("hue:%d", c.H)) + } + if c.S >= 0 { + s = append(s, fmt.Sprintf("saturation:%g", c.S)) + } + if c.B >= 0 { + s = append(s, fmt.Sprintf("brightness:%g", c.B)) + } + if c.K >= 0 { + s = append(s, fmt.Sprintf("kelvin:%d", c.K)) + } + return strings.Join(s, " ") +} + +func (c HSBKColor) MarshalText() ([]byte, error) { + return []byte(c.ColorString()), nil +}