2020-03-09 20:47:43 -06:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Palette responds to color messages
|
|
|
|
type Palette struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Descr describes this plugin
|
|
|
|
func (h *Palette) Descr() string {
|
2020-03-10 08:03:03 -06:00
|
|
|
return "Creates an solid 56x56 image of the color specified."
|
2020-03-09 20:47:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re is the regex for matching color messages.
|
|
|
|
func (h *Palette) Re() string {
|
|
|
|
return `(?i)^#[a-f0-9]{6}$`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match determines if we are asking for a color
|
2020-05-13 16:53:31 -06:00
|
|
|
func (h *Palette) Match(_, msg string) bool {
|
2020-03-09 20:47:43 -06:00
|
|
|
re := regexp.MustCompile(h.Re())
|
|
|
|
return re.MatchString(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStore we don't need a store here
|
2020-05-13 16:53:31 -06:00
|
|
|
func (h *Palette) SetStore(_ PluginStore) {}
|
2020-03-09 20:47:43 -06:00
|
|
|
|
|
|
|
func (h *Palette) 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
|
|
|
|
}
|
|
|
|
|
2020-03-10 08:03:03 -06:00
|
|
|
func isEdge(x, y int) bool {
|
|
|
|
if x == 0 || x == 55 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if y == 0 || y == 55 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:47:43 -06:00
|
|
|
// RespondText to color request events
|
2020-05-13 16:53:31 -06:00
|
|
|
func (h *Palette) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
|
2020-03-10 08:03:03 -06:00
|
|
|
const width, height = 56, 56
|
2020-03-09 20:47:43 -06:00
|
|
|
|
2020-03-10 08:03:03 -06:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 56, 56))
|
|
|
|
border := &color.RGBA{
|
|
|
|
R: 0x00,
|
|
|
|
G: 0x00,
|
|
|
|
B: 0x00,
|
|
|
|
A: 0xff,
|
|
|
|
}
|
2020-05-13 16:53:31 -06:00
|
|
|
clr, err := h.parseHexColor(post)
|
2020-03-09 20:47:43 -06:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2020-05-13 16:53:31 -06:00
|
|
|
return SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
|
2020-03-09 20:47:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for x := 0; x < width; x++ {
|
2020-03-10 08:03:03 -06:00
|
|
|
if isEdge(x, y) {
|
|
|
|
img.Set(x, y, border)
|
|
|
|
} else {
|
2020-05-13 16:53:31 -06:00
|
|
|
img.Set(x, y, clr)
|
2020-03-10 08:03:03 -06:00
|
|
|
}
|
2020-03-09 20:47:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = SendImage(c, ev.RoomID, img)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2020-05-13 16:53:31 -06:00
|
|
|
return err
|
2020-03-09 20:47:43 -06:00
|
|
|
}
|
2020-05-13 16:53:31 -06:00
|
|
|
return nil
|
2020-03-09 20:47:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name color
|
|
|
|
func (h *Palette) Name() string {
|
|
|
|
return "Palette"
|
|
|
|
}
|