Skip to content

Commit

Permalink
查询角色增加背景
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Jun 18, 2024
1 parent 50eff6f commit 61a8353
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions maplebot/find_role.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package maplebot

import (
"bytes"
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
. "github.com/CuteReimu/onebot"
. "github.com/vicanso/go-charts/v2"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"log/slog"
"math"
"slices"
Expand All @@ -14,6 +21,20 @@ import (
"time"
)

//go:embed lucid_bkg_mid.jpeg
var bkgFile []byte

var bkg *image.Image

func init() {
file, err := jpeg.Decode(bytes.NewReader(bkgFile))
if err != nil {
slog.Error("解析背景图片失败", "error", err)
} else {
bkg = &file
}
}

type findRoleReturnData struct {
CharacterData struct {
CharacterImageURL string `json:"CharacterImageURL"`
Expand Down Expand Up @@ -214,6 +235,23 @@ func findRole(name string) MessageChain {
} else if buf, err := p.Bytes(); err != nil {
slog.Error("render chart failed", "error", err)
} else {
if bkg != nil {
img, err := png.Decode(bytes.NewReader(buf))
if err != nil {
slog.Error("解析图片失败", "error", err)
} else {
mask := &image.Uniform{C: color.RGBA{R: 255, G: 255, B: 255, A: 56}}
newImg := image.NewRGBA(img.Bounds())
draw.Draw(newImg, newImg.Bounds(), img, image.Point{}, draw.Src)
draw.DrawMask(newImg, newImg.Bounds(), *bkg, image.Point{}, mask, image.Point{}, draw.Over)
buf2 := &bytes.Buffer{}
if err = png.Encode(buf2, newImg); err != nil {
slog.Error("生成图片失败", "error", err)
} else {
buf = buf2.Bytes()
}
}
}
messageChain = append(messageChain, &Text{Text: s}, &Image{File: "base64://" + base64.StdEncoding.EncodeToString(buf)})
}
} else {
Expand Down
Binary file added maplebot/lucid_bkg_mid.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 61a8353

Please sign in to comment.