Compare commits

..

No commits in common. "6a5e6d07531f75ec500dc18b05ea98d602aaee9e" and "73a666a2163081b65b9c614ff42a33bbd9cce64c" have entirely different histories.

2 changed files with 30 additions and 70 deletions

View File

@ -2,7 +2,6 @@ package lifx
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@ -20,73 +19,41 @@ 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 { c, _ := NewWhite(1500); return c } Candlelight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 1500} }
Sunset = func() HSBKColor { c, _ := NewWhite(2000); return c } Sunset = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2000} }
UltraWarm = func() HSBKColor { c, _ := NewWhite(2500); return c } UltraWarm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2500} }
Incandescent = func() HSBKColor { c, _ := NewWhite(2700); return c } Incandescent = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2700} }
Warm = func() HSBKColor { c, _ := NewWhite(3000); return c } Warm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 3000} }
Cool = func() HSBKColor { c, _ := NewWhite(4000); return c } Cool = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4000} }
CoolDaylight = func() HSBKColor { c, _ := NewWhite(4500); return c } CoolDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4500} }
SoftDaylight = func() HSBKColor { c, _ := NewWhite(5000); return c } SoftDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5000} }
Daylight = func() HSBKColor { c, _ := NewWhite(5600); return c } Daylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5600} }
NoonDaylight = func() HSBKColor { c, _ := NewWhite(6000); return c } NoonDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6000} }
BrightDaylight = func() HSBKColor { c, _ := NewWhite(6500); return c } BrightDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6500} }
CloudDaylight = func() HSBKColor { c, _ := NewWhite(7000); return c } CloudDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7000} }
BlueDaylight = func() HSBKColor { c, _ := NewWhite(7500); return c } BlueDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7500} }
BlueOvercast = func() HSBKColor { c, _ := NewWhite(8000); return c } BlueOvercast = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 8000} }
BlueIce = func() HSBKColor { c, _ := NewWhite(9000); return c } BlueIce = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 9000} }
) )
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 NewHSBColor(h, s, b float32) (HSBKColor, error) { func NewHSBKColor() HSBKColor {
var c HSBKColor var c HSBKColor
c.H, c.S, c.B, c.K = -1, -1, -1, -1
if h < 0 || h > 360 { return c
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 {
@ -99,17 +66,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 != nil { if c.H >= 0 {
s = append(s, fmt.Sprintf("hue:%g", *c.H)) s = append(s, fmt.Sprintf("hue:%g", c.H))
} }
if c.S != nil { if c.S >= 0 {
s = append(s, fmt.Sprintf("saturation:%g", *c.S)) s = append(s, fmt.Sprintf("saturation:%g", c.S))
} }
if c.B != nil { if c.B >= 0 {
s = append(s, fmt.Sprintf("brightness:%g", *c.B)) s = append(s, fmt.Sprintf("brightness:%g", c.B))
} }
if c.K != nil { if c.K >= 0 {
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, " ")
} }

View File

@ -1,7 +0,0 @@
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 }