2022-11-10 07:04:51 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/png"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const width, height = 288, 288
|
|
|
|
|
|
|
|
func parseHexColor(s string) (*color.RGBA, error) {
|
|
|
|
c := &color.RGBA{
|
|
|
|
A: 0xff,
|
|
|
|
}
|
|
|
|
_, err := fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEdge(x, y int) bool {
|
2022-11-21 19:54:29 -07:00
|
|
|
if x == 0 || x == width-1 {
|
2022-11-10 07:04:51 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-11-21 19:54:29 -07:00
|
|
|
if y == 0 || y == height-1 {
|
2022-11-10 07:04:51 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type myIcon struct {
|
|
|
|
data *image.RGBA
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *myIcon) Name() string {
|
|
|
|
return "Hi.png"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *myIcon) Content() []byte {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_ = png.Encode(buf, m.data)
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildImage(xin *xinStatus) *myIcon {
|
|
|
|
i := &myIcon{}
|
|
|
|
|
2022-11-22 07:27:19 -07:00
|
|
|
on, err := parseHexColor("#92CAFF")
|
2022-12-07 07:44:37 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-11-10 07:04:51 -07:00
|
|
|
off, err := parseHexColor("#c1c1c1")
|
2022-12-07 07:44:37 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-11-22 07:27:19 -07:00
|
|
|
border, err := parseHexColor("#000000")
|
2023-03-03 06:14:05 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
rebootColor, err := parseHexColor("#DE3163")
|
2022-11-10 07:04:51 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.data = image.NewRGBA(image.Rect(0, 0, width, height))
|
|
|
|
|
2022-11-21 19:54:29 -07:00
|
|
|
aliveCount := int(xin.aliveCount())
|
|
|
|
utdCount := int(xin.uptodateCount())
|
|
|
|
gridMark := 1
|
|
|
|
if aliveCount > 0 {
|
|
|
|
gridMark = int(height / aliveCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
for y := 0; y < height; y++ {
|
2022-11-10 07:04:51 -07:00
|
|
|
if isEdge(x, y) {
|
|
|
|
i.data.Set(x, y, border)
|
|
|
|
} else {
|
2022-11-21 19:54:29 -07:00
|
|
|
if aliveCount > 0 && y < gridMark*utdCount {
|
2023-03-03 06:17:44 -07:00
|
|
|
if xin.hasReboot {
|
2023-03-03 06:14:05 -07:00
|
|
|
i.data.Set(x, y, rebootColor)
|
|
|
|
} else {
|
|
|
|
i.data.Set(x, y, on)
|
|
|
|
}
|
2022-11-10 07:04:51 -07:00
|
|
|
} else {
|
|
|
|
i.data.Set(x, y, off)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|