2020-02-29 22:00:21 +00:00
|
|
|
package lifx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Color interface {
|
|
|
|
ColorString() string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
RGBColor struct {
|
|
|
|
R, G, B uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
HSBKColor struct {
|
2020-03-06 02:20:16 +00:00
|
|
|
H float32 `json:"hue"`
|
|
|
|
S float32 `json:"saturation"`
|
|
|
|
B float32 `json:"brightness"`
|
|
|
|
K int16 `json:"kelvin"`
|
2020-02-29 22:00:21 +00:00
|
|
|
}
|
2020-02-29 22:09:05 +00:00
|
|
|
|
|
|
|
NamedColor string
|
2020-02-29 22:00:21 +00:00
|
|
|
)
|
|
|
|
|
2020-03-03 05:52:41 +00:00
|
|
|
var (
|
2020-03-06 02:20:54 +00:00
|
|
|
Candlelight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 1500} }
|
|
|
|
Sunset = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2000} }
|
|
|
|
UltraWarm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2500} }
|
|
|
|
Incandescent = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2700} }
|
|
|
|
Warm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 3000} }
|
|
|
|
Cool = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4000} }
|
|
|
|
CoolDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4500} }
|
|
|
|
SoftDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5000} }
|
|
|
|
Daylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5600} }
|
|
|
|
NoonDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6000} }
|
|
|
|
BrightDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6500} }
|
|
|
|
CloudDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7000} }
|
|
|
|
BlueDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7500} }
|
|
|
|
BlueOvercast = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 8000} }
|
|
|
|
BlueIce = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 9000} }
|
2020-03-03 05:52:41 +00:00
|
|
|
)
|
|
|
|
|
2020-02-29 22:00:21 +00:00
|
|
|
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 {
|
2020-03-01 05:15:39 +00:00
|
|
|
s = append(s, fmt.Sprintf("hue:%f", c.H))
|
2020-02-29 22:00:21 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2020-02-29 22:09:05 +00:00
|
|
|
|
|
|
|
func (c NamedColor) ColorString() string {
|
|
|
|
return string(c)
|
|
|
|
}
|
2020-03-01 05:15:39 +00:00
|
|
|
|
|
|
|
func (c *Client) ValidateColor(color Color) (Color, error) {
|
|
|
|
resp, err := c.Request("GET", EndpointColor(color.ColorString()), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &HSBKColor{}
|
|
|
|
err = c.UnmarshalResponse(resp, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|