Browse Source

add color conversion in parse and rgb to ui state.

beelzebub
Gisle Aune 2 years ago
parent
commit
cfb1727a89
  1. 40
      internal/color/color.go
  2. 13
      internal/color/rgb.go
  3. 4
      internal/color/xy.go
  4. 6
      services/uistate/data.go
  5. 3
      services/uistate/patch.go
  6. 12
      services/uistate/service.go

40
internal/color/color.go

@ -126,6 +126,14 @@ func (col *Color) ToRGB() (col2 Color, ok bool) {
ok = true ok = true
} }
if ok {
col2 = Color{RGB: &RGB{
Red: math.Max(col2.RGB.Red, 0.0),
Green: math.Max(col2.RGB.Green, 0.0),
Blue: math.Max(col2.RGB.Blue, 0.0),
}}
}
return return
} }
@ -309,7 +317,9 @@ func Parse(raw string) (col Color, err error) {
return return
} }
tokens := strings.SplitN(raw, ":", 2)
convertTokens := strings.SplitN(raw, "|", 2)
tokens := strings.SplitN(convertTokens[0], ":", 2)
if len(tokens) != 2 { if len(tokens) != 2 {
err = ErrBadColorInput err = ErrBadColorInput
return return
@ -434,6 +444,34 @@ func Parse(raw string) (col Color, err error) {
err = ErrUnknownColorFormat err = ErrUnknownColorFormat
} }
if err == nil && len(convertTokens) == 2 {
switch convertTokens[1] {
case "xy":
col2, ok := col.ToXY()
if !ok {
err = ErrUnknownColorFormat
} else {
col = col2
}
case "rgb":
col2, ok := col.ToRGB()
if !ok {
err = ErrUnknownColorFormat
} else {
col = col2
}
case "hs":
col2, ok := col.ToHS()
if !ok {
err = ErrUnknownColorFormat
} else {
col = col2
}
default:
err = ErrUnknownColorFormat
}
}
return return
} }

13
internal/color/rgb.go

@ -1,6 +1,9 @@
package color package color
import "github.com/lucasb-eyer/go-colorful"
import (
"github.com/lucasb-eyer/go-colorful"
"math"
)
type RGB struct { type RGB struct {
Red float64 `json:"red"` Red float64 `json:"red"`
@ -36,6 +39,14 @@ func (rgb RGB) ToXY() XY {
} }
} }
func (rgb RGB) Round() RGB {
return RGB{
Red: math.Max(math.Round(rgb.Red*1000)/1000, 0),
Green: math.Max(math.Round(rgb.Green*1000)/1000, 0),
Blue: math.Max(math.Round(rgb.Blue*1000)/1000, 0),
}
}
func (rgb RGB) Normalize() (RGB, float64) { func (rgb RGB) Normalize() (RGB, float64) {
hue, sat, value := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv() hue, sat, value := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv()
hs := HueSat{Hue: hue, Sat: sat} hs := HueSat{Hue: hue, Sat: sat}

4
internal/color/xy.go

@ -162,13 +162,13 @@ type XY struct {
func (xy XY) ToRGB() RGB { func (xy XY) ToRGB() RGB {
h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv() h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv()
c := colorful.Hsv(h, s, 1)
c := colorful.Hsv(math.Mod(h, 360), s, 1)
return RGB{Red: c.R, Green: c.G, Blue: c.B} return RGB{Red: c.R, Green: c.G, Blue: c.B}
} }
func (xy XY) ToHS() HueSat { func (xy XY) ToHS() HueSat {
h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv() h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv()
return HueSat{Hue: h, Sat: s}
return HueSat{Hue: math.Mod(h, 360), Sat: s}
} }
func (xy XY) DistanceTo(other XY) float64 { func (xy XY) DistanceTo(other XY) float64 {

6
services/uistate/data.go

@ -4,6 +4,7 @@ import (
"git.aiterp.net/lucifer3/server/device" "git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/effects" "git.aiterp.net/lucifer3/server/effects"
"git.aiterp.net/lucifer3/server/events" "git.aiterp.net/lucifer3/server/events"
"git.aiterp.net/lucifer3/server/internal/color"
"git.aiterp.net/lucifer3/server/internal/gentools" "git.aiterp.net/lucifer3/server/internal/gentools"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -25,6 +26,7 @@ func (d *Data) WithPatch(patches ...Patch) Data {
gentools.ApplyUpdatePtr(&pd.HWMetadata, patch.Device.HWMetadata) gentools.ApplyUpdatePtr(&pd.HWMetadata, patch.Device.HWMetadata)
gentools.ApplyUpdatePtr(&pd.DesiredState, patch.Device.DesiredState) gentools.ApplyUpdatePtr(&pd.DesiredState, patch.Device.DesiredState)
gentools.ApplyUpdatePtr(&pd.Assignment, patch.Device.Assignment) gentools.ApplyUpdatePtr(&pd.Assignment, patch.Device.Assignment)
gentools.ApplyUpdatePtr(&pd.ActiveColorRGB, patch.Device.ActiveColorRGB)
if patch.Device.AddAlias != nil { if patch.Device.AddAlias != nil {
pd.Aliases = append(pd.Aliases[:0:0], pd.Aliases...) pd.Aliases = append(pd.Aliases[:0:0], pd.Aliases...)
@ -42,6 +44,9 @@ func (d *Data) WithPatch(patches ...Patch) Data {
if patch.Device.ClearAssignment { if patch.Device.ClearAssignment {
pd.Assignment = nil pd.Assignment = nil
} }
if patch.Device.ClearActiveColorRGB {
pd.ActiveColorRGB = nil
}
if patch.Device.Delete { if patch.Device.Delete {
delete(newData.Devices, pd.ID) delete(newData.Devices, pd.ID)
@ -106,6 +111,7 @@ type Device struct {
Name string `json:"name"` Name string `json:"name"`
HWMetadata *events.HardwareMetadata `json:"hwMetadata"` HWMetadata *events.HardwareMetadata `json:"hwMetadata"`
HWState *events.HardwareState `json:"hwState"` HWState *events.HardwareState `json:"hwState"`
ActiveColorRGB *color.RGB `json:"activeColorRgb"`
DesiredState *device.State `json:"desiredState"` DesiredState *device.State `json:"desiredState"`
Aliases []string `json:"aliases"` Aliases []string `json:"aliases"`
Assignment *uuid.UUID `json:"assignment"` Assignment *uuid.UUID `json:"assignment"`

3
services/uistate/patch.go

@ -5,6 +5,7 @@ import (
"git.aiterp.net/lucifer3/server/device" "git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/effects" "git.aiterp.net/lucifer3/server/effects"
"git.aiterp.net/lucifer3/server/events" "git.aiterp.net/lucifer3/server/events"
"git.aiterp.net/lucifer3/server/internal/color"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -39,6 +40,8 @@ type DevicePatch struct {
RemoveAlias *string `json:"removeAlias,omitempty"` RemoveAlias *string `json:"removeAlias,omitempty"`
Assignment *uuid.UUID `json:"assignment,omitempty"` Assignment *uuid.UUID `json:"assignment,omitempty"`
ClearAssignment bool `json:"clearAssignment,omitempty"` ClearAssignment bool `json:"clearAssignment,omitempty"`
ActiveColorRGB *color.RGB `json:"activeColorRgb"`
ClearActiveColorRGB bool `json:"clearActiveColorRGB"`
Delete bool `json:"delete,omitempty"` Delete bool `json:"delete,omitempty"`
} }

12
services/uistate/service.go

@ -56,6 +56,18 @@ func (s *service) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
patches = []Patch{{Device: &DevicePatch{ID: event.ID, RemoveAlias: &event.Alias}}} patches = []Patch{{Device: &DevicePatch{ID: event.ID, RemoveAlias: &event.Alias}}}
case events.HardwareState: case events.HardwareState:
patches = []Patch{{Device: &DevicePatch{ID: event.ID, HWState: &event}}} patches = []Patch{{Device: &DevicePatch{ID: event.ID, HWState: &event}}}
if event.State.Color != nil {
rgb, _ := event.State.Color.ToRGB()
patches = append(patches, Patch{Device: &DevicePatch{
ID: event.ID,
ActiveColorRGB: gentools.Ptr(rgb.RGB.Round()),
}})
} else {
patches = append(patches, Patch{Device: &DevicePatch{
ID: event.ID,
ClearActiveColorRGB: false,
}})
}
case events.HardwareMetadata: case events.HardwareMetadata:
patches = []Patch{{Device: &DevicePatch{ID: event.ID, HWMetadata: &event}}} patches = []Patch{{Device: &DevicePatch{ID: event.ID, HWMetadata: &event}}}
case events.AssignmentCreated: case events.AssignmentCreated:

Loading…
Cancel
Save