Compare commits

..

2 Commits

Author SHA1 Message Date
6a5e6d0753
forgot to add utils file 2020-03-08 13:47:21 -05:00
39a9d377d1
use pointers 2020-03-08 13:47:01 -05:00
2 changed files with 70 additions and 30 deletions

View File

@ -2,6 +2,7 @@ package lifx
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@ -19,41 +20,73 @@ type (
} }
HSBKColor struct { HSBKColor struct {
H float32 `json:"hue"` H *float32 `json:"hue"`
S float32 `json:"saturation"` S *float32 `json:"saturation"`
B float32 `json:"brightness"` B *float32 `json:"brightness"`
K int16 `json:"kelvin"` K *int16 `json:"kelvin"`
} }
NamedColor string NamedColor string
) )
var ( var (
Candlelight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 1500} } Candlelight = func() HSBKColor { c, _ := NewWhite(1500); return c }
Sunset = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2000} } Sunset = func() HSBKColor { c, _ := NewWhite(2000); return c }
UltraWarm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2500} } UltraWarm = func() HSBKColor { c, _ := NewWhite(2500); return c }
Incandescent = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2700} } Incandescent = func() HSBKColor { c, _ := NewWhite(2700); return c }
Warm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 3000} } Warm = func() HSBKColor { c, _ := NewWhite(3000); return c }
Cool = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4000} } Cool = func() HSBKColor { c, _ := NewWhite(4000); return c }
CoolDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4500} } CoolDaylight = func() HSBKColor { c, _ := NewWhite(4500); return c }
SoftDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5000} } SoftDaylight = func() HSBKColor { c, _ := NewWhite(5000); return c }
Daylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5600} } Daylight = func() HSBKColor { c, _ := NewWhite(5600); return c }
NoonDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6000} } NoonDaylight = func() HSBKColor { c, _ := NewWhite(6000); return c }
BrightDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6500} } BrightDaylight = func() HSBKColor { c, _ := NewWhite(6500); return c }
CloudDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7000} } CloudDaylight = func() HSBKColor { c, _ := NewWhite(7000); return c }
BlueDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7500} } BlueDaylight = func() HSBKColor { c, _ := NewWhite(7500); return c }
BlueOvercast = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 8000} } BlueOvercast = func() HSBKColor { c, _ := NewWhite(8000); return c }
BlueIce = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 9000} } BlueIce = func() HSBKColor { c, _ := NewWhite(9000); return c }
) )
func NewRGBColor(r, g, b uint8) (*RGBColor, error) { func NewRGBColor(r, g, b uint8) (*RGBColor, error) {
return &RGBColor{R: r, G: g, B: b}, nil return &RGBColor{R: r, G: g, B: b}, nil
} }
func NewHSBKColor() HSBKColor { func NewHSBColor(h, s, b float32) (HSBKColor, error) {
var c HSBKColor var c HSBKColor
c.H, c.S, c.B, c.K = -1, -1, -1, -1
return c if h < 0 || h > 360 {
return c, errors.New("hue must be between 0.0-360.0")
}
if s < 0 || s > 1 {
return c, errors.New("saturation must be between 0.0-1.0")
}
if b < 0 || b > 1 {
return c, errors.New("brightness must be between 0.0-1.0")
}
c = HSBKColor{
H: Float32Ptr(h),
S: Float32Ptr(s),
B: Float32Ptr(b),
}
return c, nil
}
func NewWhite(k int16) (HSBKColor, error) {
var c HSBKColor
if k < 1500 || k > 8000 {
return c, errors.New("kelvin must be between 1500-9000")
}
c = HSBKColor{
H: Float32Ptr(0.0),
S: Float32Ptr(0.0),
K: Int16Ptr(k),
}
return c, nil
} }
func (c RGBColor) ColorString() string { func (c RGBColor) ColorString() string {
@ -66,17 +99,17 @@ func (c RGBColor) Hex() string {
func (c HSBKColor) ColorString() string { func (c HSBKColor) ColorString() string {
var s []string var s []string
if c.H >= 0 { if c.H != nil {
s = append(s, fmt.Sprintf("hue:%g", c.H)) s = append(s, fmt.Sprintf("hue:%g", *c.H))
} }
if c.S >= 0 { if c.S != nil {
s = append(s, fmt.Sprintf("saturation:%g", c.S)) s = append(s, fmt.Sprintf("saturation:%g", *c.S))
} }
if c.B >= 0 { if c.B != nil {
s = append(s, fmt.Sprintf("brightness:%g", c.B)) s = append(s, fmt.Sprintf("brightness:%g", *c.B))
} }
if c.K >= 0 { if c.K != nil {
s = append(s, fmt.Sprintf("kelvin:%d", c.K)) s = append(s, fmt.Sprintf("kelvin:%d", *c.K))
} }
return strings.Join(s, " ") return strings.Join(s, " ")
} }

7
util.go Normal file
View File

@ -0,0 +1,7 @@
package lifx
func StringPtr(v string) *string { return &v }
func Float64Ptr(v float64) *float64 { return &v }
func Float32Ptr(v float32) *float32 { return &v }
func IntPtr(v int) *int { return &v }
func Int16Ptr(v int16) *int16 { return &v }