commit62e7cb88f4Author: Gisle Aune <dev@gisle.me> Date: Mon Feb 28 22:52:06 2022 +0100 refactor color system to be separate from models, also move errors out of models. commitdf865574bfAuthor: Gisle Aune <dev@gisle.me> Date: Mon Feb 21 19:27:42 2022 +0100 rework color system and make it work with drivers.
This commit is contained in:
@@ -2,13 +2,14 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ColorPreset struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Value ColorValue `json:"value"`
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Value color.Color `json:"value"`
|
||||
}
|
||||
|
||||
type ColorPresetRepository interface {
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ColorValue struct {
|
||||
Hue float64 `json:"h,omitempty"` // 0..360
|
||||
Saturation float64 `json:"s,omitempty"` // 0..=1
|
||||
Kelvin int `json:"kelvin,omitempty"`
|
||||
XY *ColorXY `json:"xy,omitempty"`
|
||||
}
|
||||
|
||||
func (c *ColorValue) IsEmpty() bool {
|
||||
return c.XY == nil && c.Kelvin == 0 && c.Saturation == 0 && c.Hue == 0
|
||||
}
|
||||
|
||||
func (c *ColorValue) IsHueSat() bool {
|
||||
return !c.IsKelvin()
|
||||
}
|
||||
|
||||
func (c *ColorValue) IsKelvin() bool {
|
||||
return !c.IsXY() && c.Kelvin > 0
|
||||
}
|
||||
|
||||
func (c *ColorValue) IsXY() bool {
|
||||
return c.XY != nil
|
||||
}
|
||||
|
||||
// ToXY converts the color to XY if possible. If the color already is XY, it returns
|
||||
// a copy of its held value. There are no guarantees of conforming to a gamut, however.
|
||||
func (c *ColorValue) ToXY() (xy ColorXY, ok bool) {
|
||||
if c.XY != nil {
|
||||
xy = *c.XY
|
||||
ok = true
|
||||
} else if c.Kelvin > 0 && c.Hue < 0.001 && c.Saturation <= 0.001 {
|
||||
ok = false
|
||||
} else {
|
||||
xy = hsToXY(c.Hue, c.Saturation)
|
||||
ok = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *ColorValue) String() string {
|
||||
if c.Kelvin > 0 {
|
||||
return fmt.Sprintf("k:%d", c.Kelvin)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("hs:%.4g,%.3g", c.Hue, c.Saturation)
|
||||
}
|
||||
|
||||
func ParseColorValue(raw string) (ColorValue, error) {
|
||||
tokens := strings.SplitN(raw, ":", 2)
|
||||
if len(tokens) != 2 {
|
||||
return ColorValue{}, ErrBadInput
|
||||
}
|
||||
|
||||
switch tokens[0] {
|
||||
case "kelvin", "k":
|
||||
{
|
||||
parsedPart, err := strconv.Atoi(tokens[1])
|
||||
if err != nil {
|
||||
return ColorValue{}, ErrBadInput
|
||||
}
|
||||
|
||||
return ColorValue{Kelvin: parsedPart}, nil
|
||||
}
|
||||
|
||||
case "xy":
|
||||
{
|
||||
parts := strings.Split(tokens[1], ",")
|
||||
if len(parts) < 2 {
|
||||
return ColorValue{}, ErrUnknownColorFormat
|
||||
}
|
||||
|
||||
x, err1 := strconv.ParseFloat(parts[0], 64)
|
||||
y, err2 := strconv.ParseFloat(parts[1], 64)
|
||||
if err1 != nil || err2 != nil {
|
||||
return ColorValue{}, ErrBadInput
|
||||
}
|
||||
|
||||
return ColorValue{XY: &ColorXY{X: x, Y: y}}, nil
|
||||
}
|
||||
|
||||
case "hs":
|
||||
{
|
||||
parts := strings.Split(tokens[1], ",")
|
||||
if len(parts) < 2 {
|
||||
return ColorValue{}, ErrUnknownColorFormat
|
||||
}
|
||||
|
||||
part1, err1 := strconv.ParseFloat(parts[0], 64)
|
||||
part2, err2 := strconv.ParseFloat(parts[1], 64)
|
||||
if err1 != nil || err2 != nil {
|
||||
return ColorValue{}, ErrBadInput
|
||||
}
|
||||
|
||||
return ColorValue{Hue: math.Mod(part1, 360), Saturation: math.Min(math.Max(part2, 0), 1)}, nil
|
||||
}
|
||||
|
||||
case "hsk":
|
||||
{
|
||||
parts := strings.Split(tokens[1], ",")
|
||||
if len(parts) < 3 {
|
||||
return ColorValue{}, ErrUnknownColorFormat
|
||||
}
|
||||
|
||||
part1, err1 := strconv.ParseFloat(parts[0], 64)
|
||||
part2, err2 := strconv.ParseFloat(parts[1], 64)
|
||||
part3, err3 := strconv.Atoi(parts[2])
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
return ColorValue{}, ErrBadInput
|
||||
}
|
||||
|
||||
return ColorValue{
|
||||
Hue: math.Mod(part1, 360),
|
||||
Saturation: math.Min(math.Max(part2, 0), 1),
|
||||
Kelvin: part3,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return ColorValue{}, ErrUnknownColorFormat
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"math"
|
||||
)
|
||||
|
||||
const eps = 0.0001
|
||||
const epsSquare = eps * eps
|
||||
|
||||
type ColorGamut struct {
|
||||
Red ColorXY `json:"red"`
|
||||
Green ColorXY `json:"green"`
|
||||
Blue ColorXY `json:"blue"`
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) side(x1, y1, x2, y2, x, y float64) float64 {
|
||||
return (y2-y1)*(x-x1) + (-x2+x1)*(y-y1)
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) naiveContains(color ColorXY) bool {
|
||||
x, y := color.X, color.Y
|
||||
x1, y1 := cg.Red.X, cg.Red.Y
|
||||
x2, y2 := cg.Green.X, cg.Green.Y
|
||||
x3, y3 := cg.Blue.X, cg.Blue.Y
|
||||
|
||||
checkSide1 := cg.side(x1, y1, x2, y2, x, y) < 0
|
||||
checkSide2 := cg.side(x2, y2, x3, y3, x, y) < 0
|
||||
checkSide3 := cg.side(x3, y3, x1, y1, x, y) < 0
|
||||
|
||||
return checkSide1 && checkSide2 && checkSide3
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) getBounds() (xMin, xMax, yMin, yMax float64) {
|
||||
x1, y1 := cg.Red.X, cg.Red.Y
|
||||
x2, y2 := cg.Green.X, cg.Green.Y
|
||||
x3, y3 := cg.Blue.X, cg.Blue.Y
|
||||
|
||||
xMin = math.Min(x1, math.Min(x2, x3)) - eps
|
||||
xMax = math.Max(x1, math.Max(x2, x3)) + eps
|
||||
yMin = math.Min(y1, math.Min(y2, y3)) - eps
|
||||
yMax = math.Max(y1, math.Max(y2, y3)) + eps
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) isInBounds(color ColorXY) bool {
|
||||
x, y := color.X, color.Y
|
||||
xMin, xMax, yMin, yMax := cg.getBounds()
|
||||
|
||||
return !(x < xMin || xMax < x || y < yMin || yMax < y)
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) distanceSquarePointToSegment(x1, y1, x2, y2, x, y float64) float64 {
|
||||
sqLength1 := (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)
|
||||
dotProduct := ((x-x1)*(x2-x1) + (y-y1)*(y2-y1)) / sqLength1
|
||||
if dotProduct < 0 {
|
||||
return (x-x1)*(x-x1) + (y-y1)*(y-y1)
|
||||
} else if dotProduct <= 1 {
|
||||
sqLength2 := (x1-x)*(x1-x) + (y1-y)*(y1-y)
|
||||
return sqLength2 - dotProduct*dotProduct*sqLength1
|
||||
} else {
|
||||
return (x-x2)*(x-x2) + (y-y2)*(y-y2)
|
||||
}
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) atTheEdge(color ColorXY) bool {
|
||||
x, y := color.X, color.Y
|
||||
x1, y1 := cg.Red.X, cg.Red.Y
|
||||
x2, y2 := cg.Green.X, cg.Green.Y
|
||||
x3, y3 := cg.Blue.X, cg.Blue.Y
|
||||
|
||||
if cg.distanceSquarePointToSegment(x1, y1, x2, y2, x, y) <= epsSquare {
|
||||
return true
|
||||
}
|
||||
if cg.distanceSquarePointToSegment(x2, y2, x3, y3, x, y) <= epsSquare {
|
||||
return true
|
||||
}
|
||||
if cg.distanceSquarePointToSegment(x3, y3, x1, y1, x, y) <= epsSquare {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) Contains(color ColorXY) bool {
|
||||
if cg == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return cg.isInBounds(color) && (cg.naiveContains(color) || cg.atTheEdge(color))
|
||||
}
|
||||
|
||||
func (cg *ColorGamut) Conform(color ColorXY) ColorXY {
|
||||
if cg.Contains(color) {
|
||||
return color
|
||||
}
|
||||
|
||||
var best *ColorXY
|
||||
|
||||
xMin, xMax, yMin, yMax := cg.getBounds()
|
||||
|
||||
for x := xMin; x < xMax; x += 0.001 {
|
||||
for y := yMin; y < yMax; y += 0.001 {
|
||||
color2 := ColorXY{X: x, Y: y}
|
||||
|
||||
if cg.Contains(color2) {
|
||||
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
|
||||
best = &color2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if best == nil {
|
||||
centerX := (cg.Red.X + cg.Green.X + cg.Blue.X) / 3
|
||||
centerY := (cg.Red.Y + cg.Green.Y + cg.Blue.Y) / 3
|
||||
|
||||
stepX := (centerX - color.X) / 5000
|
||||
stepY := (centerY - color.Y) / 5000
|
||||
|
||||
for !cg.Contains(color) {
|
||||
color.X += stepX
|
||||
color.Y += stepY
|
||||
}
|
||||
|
||||
return color
|
||||
}
|
||||
|
||||
for x := best.X - 0.001; x < best.X+0.001; x += 0.0002 {
|
||||
for y := best.Y - 0.001; y < best.Y+0.001; y += 0.0002 {
|
||||
color2 := ColorXY{X: x, Y: y}
|
||||
|
||||
if cg.atTheEdge(color2) {
|
||||
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
|
||||
best = &color2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for x := best.X - 0.0001; x < best.X+0.0001; x += 0.00003 {
|
||||
for y := best.Y - 0.0001; y < best.Y+0.0001; y += 0.00003 {
|
||||
color2 := ColorXY{X: x, Y: y}
|
||||
|
||||
if cg.atTheEdge(color2) {
|
||||
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
|
||||
best = &color2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return *best
|
||||
}
|
||||
|
||||
type ColorXY struct {
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
}
|
||||
|
||||
func (xy ColorXY) DistanceTo(other ColorXY) float64 {
|
||||
return math.Sqrt(math.Pow(xy.X-other.X, 2) + math.Pow(xy.Y-other.Y, 2))
|
||||
}
|
||||
|
||||
func (xy ColorXY) Round() ColorXY {
|
||||
return ColorXY{
|
||||
X: math.Round(xy.X*10000) / 10000,
|
||||
Y: math.Round(xy.Y*10000) / 10000,
|
||||
}
|
||||
}
|
||||
|
||||
func hsToXY(hue, sat float64) ColorXY {
|
||||
c := colorful.Hsv(hue, sat, 1)
|
||||
red255, green255, blue255 := c.RGB255()
|
||||
red := float64(red255) / 255.0
|
||||
green := float64(green255) / 255.0
|
||||
blue := float64(blue255) / 255.0
|
||||
|
||||
return rgbToXY(red, green, blue)
|
||||
}
|
||||
|
||||
func rgbToXY(red float64, green float64, blue float64) ColorXY {
|
||||
x := red*0.649926 + green*0.103455 + blue*0.197109
|
||||
y := red*0.234327 + green*0.743075 + blue*0.022598
|
||||
z := green*0.053077 + blue*1.035763
|
||||
|
||||
return ColorXY{
|
||||
X: x / (x + y + z),
|
||||
Y: y / (x + y + z),
|
||||
}
|
||||
}
|
||||
|
||||
func screenRGBToXY(red, green, blue float64) ColorXY {
|
||||
for _, component := range []*float64{&red, &green, &blue} {
|
||||
if *component > 0.04045 {
|
||||
*component = math.Pow((*component+0.055)/(1.055), 2.4)
|
||||
} else {
|
||||
*component /= 12.92
|
||||
}
|
||||
}
|
||||
|
||||
return rgbToXY(red, green, blue)
|
||||
}
|
||||
+22
-16
@@ -2,6 +2,8 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -34,10 +36,10 @@ type DeviceUpdate struct {
|
||||
// - Intensity: e.g. brightness, range 0..=1
|
||||
// - Temperature: e.g. for thermostats
|
||||
type DeviceState struct {
|
||||
Power bool `json:"power"`
|
||||
Color ColorValue `json:"color,omitempty"`
|
||||
Intensity float64 `json:"intensity,omitempty"`
|
||||
Temperature int `json:"temperature"`
|
||||
Power bool `json:"power"`
|
||||
Color color.Color `json:"color,omitempty"`
|
||||
Intensity float64 `json:"intensity,omitempty"`
|
||||
Temperature int `json:"temperature"`
|
||||
}
|
||||
|
||||
type DeviceScene struct {
|
||||
@@ -83,10 +85,11 @@ func DeviceCapabilitiesToStrings(caps []DeviceCapability) []string {
|
||||
|
||||
var (
|
||||
DCPower DeviceCapability = "Power"
|
||||
DCColorHS DeviceCapability = "ColorHS"
|
||||
DCColorHS DeviceCapability = "HueSat"
|
||||
DCColorHSK DeviceCapability = "ColorHSK"
|
||||
DCColorKelvin DeviceCapability = "ColorKelvin"
|
||||
DCColorXY DeviceCapability = "ColorXY"
|
||||
DCColorXY DeviceCapability = "XY"
|
||||
DCColorRGB DeviceCapability = "RGB"
|
||||
DCButtons DeviceCapability = "Buttons"
|
||||
DCPresence DeviceCapability = "Presence"
|
||||
DCIntensity DeviceCapability = "Intensity"
|
||||
@@ -127,7 +130,7 @@ func (d *Device) ApplyUpdate(update DeviceUpdate) {
|
||||
func (d *Device) Validate() error {
|
||||
d.Name = strings.Trim(d.Name, " \t\n ")
|
||||
if d.Name == "" {
|
||||
return ErrInvalidName
|
||||
return lerrors.ErrInvalidName
|
||||
}
|
||||
|
||||
newCaps := make([]DeviceCapability, 0, len(d.Capabilities))
|
||||
@@ -185,12 +188,19 @@ func (d *Device) SetState(newState NewDeviceState) error {
|
||||
}
|
||||
|
||||
if newState.Color != nil {
|
||||
parsed, err := ParseColorValue(*newState.Color)
|
||||
parsed, err := color.Parse(*newState.Color)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if (parsed.IsKelvin() && d.HasCapability(DCColorKelvin, DCColorHSK)) || (parsed.IsHueSat() && d.HasCapability(DCColorHS)) {
|
||||
switch {
|
||||
case (parsed.RGB != nil || parsed.XY != nil || parsed.HS != nil) || d.HasCapability(DCColorHS, DCColorXY, DCColorRGB):
|
||||
d.State.Color = parsed
|
||||
case parsed.K != nil && d.HasCapability(DCColorKelvin, DCColorHSK):
|
||||
if !d.HasCapability(DCColorKelvin) {
|
||||
d.State.Color.HS = &color.HueSat{Hue: 0, Sat: 0}
|
||||
}
|
||||
|
||||
d.State.Color = parsed
|
||||
}
|
||||
}
|
||||
@@ -229,14 +239,10 @@ func (s *NewDeviceState) Interpolate(other NewDeviceState, fac float64) NewDevic
|
||||
}
|
||||
|
||||
if s.Color != nil && other.Color != nil {
|
||||
sc, err := ParseColorValue(*s.Color)
|
||||
oc, err2 := ParseColorValue(*other.Color)
|
||||
sc, err := color.Parse(*s.Color)
|
||||
oc, err2 := color.Parse(*other.Color)
|
||||
if err == nil && err2 == nil {
|
||||
rc := ColorValue{}
|
||||
rc.Hue = interpolateFloat(sc.Hue, oc.Hue, fac)
|
||||
rc.Saturation = interpolateFloat(sc.Saturation, oc.Saturation, fac)
|
||||
rc.Kelvin = interpolateInt(sc.Kelvin, oc.Kelvin, fac)
|
||||
|
||||
rc := sc.Interpolate(oc, fac)
|
||||
rcStr := rc.String()
|
||||
n.Color = &rcStr
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package models
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrNotFound = errors.New("not found")
|
||||
var ErrInvalidName = errors.New("invalid name")
|
||||
var ErrBadInput = errors.New("bad input")
|
||||
var ErrBadColor = errors.New("bad color")
|
||||
var ErrInternal = errors.New("internal")
|
||||
var ErrUnknownColorFormat = errors.New("unknown color format")
|
||||
var ErrUnknownDriver = errors.New("unknown driver")
|
||||
|
||||
var ErrMissingToken = errors.New("driver is missing authentication information")
|
||||
var ErrIncorrectToken = errors.New("driver is not accepting authentication information")
|
||||
var ErrUnexpectedResponse = errors.New("driver api returned unexpected response (wrong driver selected?)")
|
||||
var ErrBridgeSearchFailed = errors.New("bridge search failed")
|
||||
var ErrAddressOnlyDryRunnable = errors.New("this address may only be used for a dry run")
|
||||
var ErrCannotForwardRequest = errors.New("driver is not able to forward requests")
|
||||
var ErrCannotForget = errors.New("forget not supported on this bridge type")
|
||||
|
||||
var ErrInvalidAddress = errors.New("invalid mac address")
|
||||
var ErrPayloadTooShort = errors.New("payload too short")
|
||||
var ErrInvalidPacketSize = errors.New("invalid packet size")
|
||||
var ErrReadTimeout = errors.New("read timeout")
|
||||
var ErrUnrecognizedPacketType = errors.New("packet type not recognized")
|
||||
var ErrBridgeRunningRequired = errors.New("this operation cannot be performed when bridge is not running")
|
||||
|
||||
var ErrSceneInvalidInterval = errors.New("scene interval must be 0 (=disabled) or greater")
|
||||
var ErrSceneNoRoles = errors.New("scene cannot have zero rules")
|
||||
var ErrSceneRoleNoStates = errors.New("scene rule has no states")
|
||||
var ErrSceneRoleUnsupportedOrdering = errors.New("scene rule has an unsupported ordering")
|
||||
var ErrSceneRoleUnknownEffect = errors.New("scene rule has an unknown effect")
|
||||
var ErrSceneRoleUnknownPowerMode = errors.New("scene rule has an unknown power mode")
|
||||
+8
-7
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"math"
|
||||
"math/rand"
|
||||
"sort"
|
||||
@@ -18,10 +19,10 @@ type Scene struct {
|
||||
|
||||
func (s *Scene) Validate() error {
|
||||
if s.IntervalMS < 0 {
|
||||
return ErrSceneInvalidInterval
|
||||
return lerrors.ErrSceneInvalidInterval
|
||||
}
|
||||
if len(s.Roles) == 0 {
|
||||
return ErrSceneNoRoles
|
||||
return lerrors.ErrSceneNoRoles
|
||||
}
|
||||
|
||||
for _, role := range s.Roles {
|
||||
@@ -118,31 +119,31 @@ func (d *SceneRunContext) IntervalFac() float64 {
|
||||
|
||||
func (r *SceneRole) Validate() error {
|
||||
if len(r.States) == 0 {
|
||||
return ErrSceneRoleNoStates
|
||||
return lerrors.ErrSceneRoleNoStates
|
||||
}
|
||||
|
||||
switch r.TargetKind {
|
||||
case RKTag, RKBridgeID, RKDeviceID, RKName, RKAll:
|
||||
default:
|
||||
return ErrBadInput
|
||||
return lerrors.ErrBadInput
|
||||
}
|
||||
|
||||
switch r.PowerMode {
|
||||
case SPScene, SPDevice, SPBoth:
|
||||
default:
|
||||
return ErrSceneRoleUnknownPowerMode
|
||||
return lerrors.ErrSceneRoleUnknownPowerMode
|
||||
}
|
||||
|
||||
switch r.Effect {
|
||||
case SEStatic, SERandom, SEGradient, SEWalkingGradient, SETransition, SEMotion, SETemperature:
|
||||
default:
|
||||
return ErrSceneRoleUnknownEffect
|
||||
return lerrors.ErrSceneRoleUnknownEffect
|
||||
}
|
||||
|
||||
switch r.Order {
|
||||
case "", "-name", "name", "+name", "-id", "id", "+id":
|
||||
default:
|
||||
return ErrSceneRoleUnsupportedOrdering
|
||||
return lerrors.ErrSceneRoleUnsupportedOrdering
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user