Skip to content

Commit

Permalink
Change HighlightCallsign message to use strings for colors
Browse files Browse the repository at this point in the history
  • Loading branch information
xylo04 committed Nov 21, 2021
1 parent 5b888a1 commit 8197a0e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Build-Depends: debhelper-compat (= 13),
dh-golang,
golang-any (>= 2:1.15~1),
golang-github-leemcloughlin-jdn-dev,
golang-github-mazznoer-csscolorparser-dev,
golang-github-stretchr-testify-dev
Standards-Version: 4.5.0
Vcs-Browser: https://salsa.debian.org/go-team/packages/golang-github-k0swe-wsjtx-go
Expand Down
20 changes: 15 additions & 5 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package wsjtx
import (
"bytes"
"encoding/binary"
"image/color"
"math"

"github.com/mazznoer/csscolorparser"
)

func encodeHeartbeat(msg HeartbeatMessage) ([]byte, error) {
Expand Down Expand Up @@ -84,8 +85,12 @@ func encodeHighlightCallsign(msg HighlightCallsignMessage) ([]byte, error) {
e.encodeUint32(highlightCallsignNum)
e.encodeUtf8(msg.Id)
e.encodeUtf8(msg.Callsign)
e.encodeColor(msg.BackgroundColor, msg.Reset)
e.encodeColor(msg.ForegroundColor, msg.Reset)
if err := e.encodeColor(msg.BackgroundColor, msg.Reset); err != nil {
return []byte{}, err
}
if err := e.encodeColor(msg.ForegroundColor, msg.Reset); err != nil {
return []byte{}, err
}
e.encodeBool(msg.HighlightLast)
return e.finish()
}
Expand Down Expand Up @@ -175,7 +180,7 @@ func (e encoder) encodeUtf8(str string) {
e.buf.WriteString(str)
}

func (e encoder) encodeColor(color color.Color, invalid bool) {
func (e encoder) encodeColor(color string, invalid bool) error {
// Spec enum: https://github.com/radekp/qt/blob/b881d8fb/src/gui/painting/qcolor.h#L70
const invalidSpec = uint8(0)
const rgbSpec = uint8(1)
Expand All @@ -187,7 +192,11 @@ func (e encoder) encodeColor(color color.Color, invalid bool) {
}

// pre-multiplied to range 0x0 to 0xffff
r, g, b, a := color.RGBA()
c, err := csscolorparser.Parse(color)
if err != nil {
return err
}
r, g, b, a := c.RGBA()

// Field type and order: https://github.com/radekp/qt/blob/b881d8fb/src/gui/painting/qcolor.cpp#L2506
e.encodeUint8(spec)
Expand All @@ -196,6 +205,7 @@ func (e encoder) encodeColor(color color.Color, invalid bool) {
e.encodeUint16(uint16(g))
e.encodeUint16(uint16(b))
e.encodeUint16(pad)
return nil
}

func (e encoder) finish() ([]byte, error) {
Expand Down
9 changes: 4 additions & 5 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package wsjtx

import (
"encoding/hex"
"image/color"
"reflect"
"testing"
)
Expand Down Expand Up @@ -292,8 +291,8 @@ func Test_encodeHighlightCallsign(t *testing.T) {
args: args{msg: HighlightCallsignMessage{
Id: "WSJT-X",
Callsign: "KM4ACK",
BackgroundColor: color.RGBA{R: 0xeb, G: 0x40, B: 0x34, A: 0xff},
ForegroundColor: color.RGBA{R: 0x25, G: 0x27, B: 0x2e, A: 0xff},
BackgroundColor: "#eb4034",
ForegroundColor: "#25272e",
HighlightLast: true,
Reset: false,
}},
Expand All @@ -305,8 +304,8 @@ func Test_encodeHighlightCallsign(t *testing.T) {
args: args{msg: HighlightCallsignMessage{
Id: "WSJT-X",
Callsign: "KM4ACK",
BackgroundColor: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
ForegroundColor: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
BackgroundColor: "white",
ForegroundColor: "white",
HighlightLast: false,
Reset: true,
}},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.15

require (
github.com/leemcloughlin/jdn v0.0.0-20201102080031-6f88db6a6bf2
github.com/mazznoer/csscolorparser v0.1.1
github.com/stretchr/testify v1.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/leemcloughlin/jdn v0.0.0-20201102080031-6f88db6a6bf2 h1:CdyD5OzAIzNFzpJ9WQRjJWj4pVRxZ9v15xdHnhvUPdw=
github.com/leemcloughlin/jdn v0.0.0-20201102080031-6f88db6a6bf2/go.mod h1:LAowglanJPLb6WYSx3D1Ht/XE54OGIr0i4mz9kdbXrs=
github.com/mazznoer/csscolorparser v0.1.1 h1:+cEJ/okx0DXYTzFmvZpXUYhNnD14YOG9U3NkfEupqnw=
github.com/mazznoer/csscolorparser v0.1.1/go.mod h1:Aj22+L/rYN/Y6bj3bYqO3N6g1dtdHtGfQ32xZ5PJQic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
7 changes: 3 additions & 4 deletions integration/send_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package integration

import (
"image/color"
"math"
"time"

Expand Down Expand Up @@ -128,12 +127,12 @@ func (s *integrationTestSuite) TestSendHighlightCallsign() {
msg := wsjtx.HighlightCallsignMessage{
Id: "WSJT-X",
Callsign: "KM4ACK",
BackgroundColor: color.RGBA{R: 255},
ForegroundColor: color.Black,
BackgroundColor: "red",
ForegroundColor: "black",
HighlightLast: true,
Reset: false,
}
want := decode(`adbccbda000000020000000d0000000657534a542d58000000064b4d3441434b010000ffff00000000000001ffff000000000000000001`)
want := decode(`adbccbda000000020000000d0000000657534a542d58000000064b4d3441434b01ffffffff00000000000001ffff000000000000000001`)

s.T().Log("sending highlightCallsign struct")
err := s.server.HighlightCallsign(msg)
Expand Down
11 changes: 5 additions & 6 deletions messages.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wsjtx

import (
"image/color"
"time"
)

Expand Down Expand Up @@ -308,11 +307,11 @@ In only.
https://sourceforge.net/p/wsjt/wsjtx/ci/wsjtx-2.5.2/tree/Network/NetworkMessage.hpp#l444
*/
type HighlightCallsignMessage struct {
Id string `json:"id"`
Callsign string `json:"callsign"`
BackgroundColor color.Color `json:"backgroundColor"`
ForegroundColor color.Color `json:"foregroundColor"`
HighlightLast bool `json:"highlightLast"`
Id string `json:"id"`
Callsign string `json:"callsign"`
BackgroundColor string `json:"backgroundColor"`
ForegroundColor string `json:"foregroundColor"`
HighlightLast bool `json:"highlightLast"`
// This field is not part of the WSJT-X message and is specific to the golang library. It is a
// necessary addition to be able to reset the highlighting. QT's color has a sentinel value in
// QColor to signal an "invalid" color; golang image/color doesn't have that, so we add this
Expand Down

0 comments on commit 8197a0e

Please sign in to comment.