add new color structs

This commit is contained in:
Ryan Cavicchioni 2020-02-29 12:54:56 -06:00
parent ad8315668e
commit abc9e0a978
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 58 additions and 3 deletions

View File

@ -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) {

View File

@ -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
}