Skip to content

Commit

Permalink
fix(dynamicicon): Improve hex color conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 14, 2024
1 parent c907d0b commit b9d8319
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 156 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.3

require (
fyne.io/systray v1.11.0
gabe565.com/utils v0.0.0-20241114041836-facb2857c9b1
github.com/Masterminds/sprig/v3 v3.3.0
github.com/dmarkham/enumer v1.5.10
github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a
Expand Down Expand Up @@ -50,7 +51,7 @@ require (
github.com/tj/assert v0.0.3 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
fyne.io/systray v1.11.0 h1:D9HISlxSkx+jHSniMBR6fCFOUjk1x/OOOJLa9lJYAKg=
fyne.io/systray v1.11.0/go.mod h1:RVwqP9nYMo7h5zViCBHri2FgjXF7H2cub7MAq4NSoLs=
gabe565.com/utils v0.0.0-20241114041836-facb2857c9b1 h1:Y7L3PkUDFFhEOxaN+vhqstaPNqlE8K55wdHonon2Ux4=
gabe565.com/utils v0.0.0-20241114041836-facb2857c9b1/go.mod h1:1WioSVukwGZYG4Q0LJBnRhgYyVljmW2Izl+RW36ALUc=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
Expand Down Expand Up @@ -107,8 +109,8 @@ golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
68 changes: 0 additions & 68 deletions internal/config/color.go

This file was deleted.

69 changes: 0 additions & 69 deletions internal/config/color_test.go

This file was deleted.

9 changes: 5 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"runtime"

"gabe565.com/utils/colorx"
"github.com/spf13/pflag"
)

Expand All @@ -26,10 +27,10 @@ type Config struct {
}

type DynamicIcon struct {
Enabled bool `toml:"enabled"`
FontColor HexColor `toml:"font-color" comment:"Hex code used to render text."`
FontFile string `toml:"font-file" comment:"Font path or filename of a system font. If left blank, an embedded font will be used."`
MaxFontSize float64 `toml:"max-font-size" comment:"Maximum font size in points."`
Enabled bool `toml:"enabled"`
FontColor colorx.Hex `toml:"font-color" comment:"Hex code used to render text."`
FontFile string `toml:"font-file" comment:"Font path or filename of a system font. If left blank, an embedded font will be used."`
MaxFontSize float64 `toml:"max-font-size" comment:"Maximum font size in points."`
}

type Arrows struct {
Expand Down
6 changes: 4 additions & 2 deletions internal/config/default.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package config

import (
"image/color"
"log/slog"
"path/filepath"
"runtime"
"strings"
"time"

"gabe565.com/utils/colorx"
flag "github.com/spf13/pflag"
)

Expand All @@ -18,7 +20,7 @@ func New(opts ...Option) *Config {
Units: UnitMgdl,
DynamicIcon: DynamicIcon{
Enabled: true,
FontColor: White(),
FontColor: colorx.Hex{Color: color.White},
MaxFontSize: 40,
},
Arrows: Arrows{
Expand Down Expand Up @@ -50,7 +52,7 @@ func New(opts ...Option) *Config {
case "darwin":
conf.DynamicIcon.Enabled = false
case "windows":
conf.DynamicIcon.FontColor = Black()
conf.DynamicIcon.FontColor = colorx.Hex{Color: color.Black}
}

conf.Flags = flag.NewFlagSet("", flag.ContinueOnError)
Expand Down
2 changes: 1 addition & 1 deletion internal/dynamicicon/dynamicicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (d *DynamicIcon) Generate(p *nightscout.Properties) ([]byte, error) {

drawer := &font.Drawer{
Dst: d.img,
Src: image.NewUniform(d.config.DynamicIcon.FontColor.RGBA()),
Src: image.NewUniform(d.config.DynamicIcon.FontColor),
}

fontSize := d.config.DynamicIcon.MaxFontSize * 2
Expand Down
9 changes: 2 additions & 7 deletions internal/tray/items/preferences/dynamic_icon_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ type DynamicIconColor struct {
*systray.MenuItem
}

//nolint:gosec
func (l DynamicIconColor) Choose() error {
c, err := zenity.SelectColor(
zenity.Title("Dynamic Icon Color"),
zenity.Color(l.config.DynamicIcon.FontColor.RGBA()),
zenity.Color(l.config.DynamicIcon.FontColor),
)
if err != nil {
if errors.Is(err, zenity.ErrCanceled) {
Expand All @@ -32,11 +31,7 @@ func (l DynamicIconColor) Choose() error {
return err
}

r, g, b, a := c.RGBA()
l.config.DynamicIcon.FontColor.R = uint8(r)
l.config.DynamicIcon.FontColor.G = uint8(g)
l.config.DynamicIcon.FontColor.B = uint8(b)
l.config.DynamicIcon.FontColor.A = uint8(a)
l.config.DynamicIcon.FontColor.Color = c
if err := l.config.Write(); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/tray/systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tray

import (
"context"
"image/color"
"io"
"log/slog"
"os"
Expand Down Expand Up @@ -154,8 +155,8 @@ func (t *Tray) onReady(ctx context.Context) func() { //nolint:gocyclo
} else {
if icon, err := t.dynamicIcon.Generate(msg.Properties); err == nil {
systray.SetTitle("")
switch t.config.DynamicIcon.FontColor {
case config.White():
switch t.config.DynamicIcon.FontColor.Color {
case color.White:
systray.SetTemplateIcon(icon, icon)
default:
systray.SetIcon(icon)
Expand Down

0 comments on commit b9d8319

Please sign in to comment.