Skip to content

Commit

Permalink
Add icon theme with configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zMoooooritz authored and muesli committed Jun 10, 2021
1 parent c3f9531 commit 5ad136a
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ A widget that displays the weather condition and teamperature.
unit = "celsius" # optional
color = "#fefefe" # optional
flatten = true # optional
theme = "openmoji" # optional
```

The supported location types can be found [here](http://wttr.in/:help).
Expand Down
Binary file modified assets/weather/cloudy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/weather/fog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/weather/heavy_snow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/weather/light_rain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/weather/light_snow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/weather/lightning.png
Binary file not shown.
Binary file modified assets/weather/moon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/weather/partly_cloudy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/weather/rain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/weather/snow.png
Binary file not shown.
Binary file modified assets/weather/sun.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/weather/thunder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/weather/thunder_rain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image/color"
"image/draw"
"os"
"path/filepath"
"time"

"github.com/golang/freetype"
Expand Down Expand Up @@ -162,6 +163,34 @@ func loadImage(path string) (image.Image, error) {
return icon, err
}

func loadThemeImage(theme string, img string) (image.Image, error) {
path := filepath.Join("~", ".local", "share", "deckmaster", "themes", theme, img+".png")
abs, err := expandPath("", path)
if err != nil {
return nil, err
}
return loadImage(abs)
}

func flattenImage(img image.Image, clr color.Color) image.Image {
bounds := img.Bounds()
flatten := image.NewRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
draw.Draw(flatten, flatten.Bounds(), img, image.Point{}, draw.Src)
alphaThreshold := uint32(20000)

for x := 0; x < bounds.Dx(); x++ {
for y := 0; y < bounds.Dy(); y++ {
_, _, _, alpha := flatten.At(x, y).RGBA()
if alpha > alphaThreshold {
flatten.Set(x, y, clr)
} else {
flatten.Set(x, y, color.RGBA{0, 0, 0, 0})
}
}
}
return flatten
}

func drawImage(img *image.RGBA, icon image.Image, size int, pt image.Point) error {
if pt.X < 0 {
xcenter := float64(img.Bounds().Dx()/2.0) - (float64(size) / 2.0)
Expand Down
26 changes: 3 additions & 23 deletions widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"image"
"image/color"
"image/draw"

"github.com/muesli/streamdeck"
)
Expand Down Expand Up @@ -54,6 +53,9 @@ func NewButtonWidget(bw BaseWidget, opts WidgetConfig) (*ButtonWidget, error) {
if err != nil {
return nil, err
}
if w.icon != nil && w.flatten {
w.icon = flattenImage(w.icon, w.color)
}
}

return w, nil
Expand All @@ -66,9 +68,6 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) error {
height := size - (margin * 2)
img := image.NewRGBA(image.Rect(0, 0, size, size))

if w.icon != nil && w.flatten {
w.icon = flattenImage(w.icon, w.color)
}
if w.label != "" {
iconsize := int((float64(height) / 3.0) * 2.0)
bounds := img.Bounds()
Expand Down Expand Up @@ -108,22 +107,3 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) error {

return w.render(dev, img)
}

func flattenImage(img image.Image, clr color.Color) image.Image {
bounds := img.Bounds()
flatten := image.NewRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
draw.Draw(flatten, flatten.Bounds(), img, image.Point{}, draw.Src)
alphaThreshold := uint32(20000)

for x := 0; x < bounds.Dx(); x++ {
for y := 0; y < bounds.Dy(); y++ {
_, _, _, alpha := flatten.At(x, y).RGBA()
if alpha > alphaThreshold {
flatten.Set(x, y, clr)
} else {
flatten.Set(x, y, color.RGBA{0, 0, 0, 0})
}
}
}
return flatten
}
46 changes: 34 additions & 12 deletions widget_weather.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"image"
"image/color"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -38,6 +40,7 @@ type WeatherWidget struct {

data WeatherData
color color.Color
theme string
flatten bool
}

Expand All @@ -58,7 +61,8 @@ func (w *WeatherData) Condition() (string, error) {
defer w.responseMutex.RUnlock()

if strings.Contains(w.response, "Unknown location") {
return "", fmt.Errorf("unknown location: %s", w.location)
fmt.Println("unknown location:", w.location)
return "", nil
}

wttr := strings.Split(w.response, " ")
Expand All @@ -75,7 +79,8 @@ func (w *WeatherData) Temperature() (string, error) {
defer w.responseMutex.RUnlock()

if strings.Contains(w.response, "Unknown location") {
return "", fmt.Errorf("unknown location: %s", w.location)
fmt.Println("unknown location:", w.location)
return "", nil
}

wttr := strings.Split(w.response, " ")
Expand Down Expand Up @@ -108,14 +113,14 @@ func (w *WeatherData) Fetch() {

resp, err := http.Get(url) //nolint:gosec
if err != nil {
fmt.Println("Can't fetch weather data:", err)
fmt.Println("can't fetch weather data:", err)
return
}
defer resp.Body.Close() //nolint:errcheck

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Can't read weather data:", err)
fmt.Println("can't read weather data:", err)
return
}

Expand All @@ -127,9 +132,10 @@ func (w *WeatherData) Fetch() {
func NewWeatherWidget(bw BaseWidget, opts WidgetConfig) *WeatherWidget {
bw.setInterval(opts.Interval, 60000)

var location, unit string
var location, unit, theme string
_ = ConfigValue(opts.Config["location"], &location)
_ = ConfigValue(opts.Config["unit"], &unit)
_ = ConfigValue(opts.Config["theme"], &theme)
var color color.Color
_ = ConfigValue(opts.Config["color"], &color)
var flatten bool
Expand All @@ -146,6 +152,7 @@ func NewWeatherWidget(bw BaseWidget, opts WidgetConfig) *WeatherWidget {
unit: unit,
},
color: color,
theme: theme,
flatten: flatten,
}
}
Expand Down Expand Up @@ -177,26 +184,41 @@ func (w *WeatherWidget) Update(dev *streamdeck.Device) error {
case "///", "//", "x", "x/": // rain
iconName = "rain"
case "/", ".": // light rain
iconName = "rain"
iconName = "light_rain"
case "**", "*/*": // heavy snow
iconName = "snow"
iconName = "heavy_snow"
case "*", "*/": // light snow
iconName = "snow"
iconName = "light_snow"
case "/!/": // thunder
iconName = "lightning"
iconName = "thunder"
case "!/", "*!*": // thunder rain
iconName = "thunder_rain"
// case "o": // sunny
default:
case "o": // sunny
if time.Now().Hour() < 7 || time.Now().Hour() > 21 {
iconName = "moon"
} else {
iconName = "sun"
}
default:
return w.render(dev, nil)
}

weatherIcon := weatherImage("assets/weather/" + iconName + ".png")
var weatherIcon image.Image
imagePath := filepath.Join("assets", "weather", iconName+".png")
if w.theme != "" {
var err error
weatherIcon, err = loadThemeImage(w.theme, iconName)
if err != nil {
log.Println("using fallback icons")
weatherIcon = weatherImage(imagePath)
}
} else {
weatherIcon = weatherImage(imagePath)
}

if w.flatten {
weatherIcon = flattenImage(weatherIcon, w.color)
}
bw := ButtonWidget{
BaseWidget: w.BaseWidget,
color: w.color,
Expand Down

0 comments on commit 5ad136a

Please sign in to comment.