-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.go
226 lines (191 loc) · 4.55 KB
/
cards.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package tarot
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"io/fs"
"math"
"path/filepath"
"sync"
"github.com/disintegration/imaging"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)
const (
defaultImageWidth = 1200
defaultImageHeight = 720
)
type Position string
const (
PositionUpright = "Upright"
PositionReversed = "Reversed"
)
func (p *Position) ZhName() string {
if *p == PositionUpright {
return "正位"
}
return "逆位"
}
type Card struct {
Name string `json:"name"`
ZhName string `json:"zh_name"`
Position Position `json:"-"`
Pic image.Image `json:"-"`
}
func (c *Card) ZhString() string {
return fmt.Sprintf("%s(%s)", c.ZhName, c.Position.ZhName())
}
type Assets struct {
Cards []Card
BackgroundImg image.Image
Font *truetype.Font
FontFace font.Face
AskerImg image.Image
ReaderImg image.Image
}
var (
//go:embed assets/*
assetsFS embed.FS
initAssetsOnce sync.Once
assets Assets
)
func mustReadImg(p string) image.Image {
data, err := fs.ReadFile(assetsFS, p)
if err != nil {
panic(err)
}
var img image.Image
if filepath.Ext(p) == ".png" {
img, err = png.Decode(bytes.NewReader(data))
} else {
img, err = jpeg.Decode(bytes.NewReader(data))
}
if err != nil {
panic(err)
}
return img
}
func imageTypeToRGBA64(m image.Image) *image.RGBA64 {
bounds := m.Bounds()
dx := bounds.Dx()
dy := bounds.Dy()
img := image.NewRGBA64(bounds)
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
colorRgb := m.At(x, y)
r, g, b, a := colorRgb.RGBA()
rr := uint16(r)
gg := uint16(g)
bb := uint16(b)
aa := uint16(a)
img.SetRGBA64(x, y, color.RGBA64{
R: rr,
G: gg,
B: bb,
A: aa,
})
}
}
return img
}
func initCards() {
data, err := fs.ReadFile(assetsFS, "assets/cards.json")
if err != nil {
panic(err)
}
var cards []Card
if err := json.Unmarshal(data, &cards); err != nil {
panic(err)
}
for idx, card := range cards {
// card.Pic = imaging.Resize(mustReadImg(fmt.Sprintf("assets/%d.jpg", idx)), 217, 385, imaging.Lanczos)
card.Pic = imaging.Resize(mustReadImg(fmt.Sprintf("assets/%d.jpg", idx)), 120, 212, imaging.Lanczos)
assets.Cards = append(assets.Cards, card)
}
}
func processReaderImg(pic image.Image) image.Image {
pic = imaging.Resize(pic, 0, defaultImageHeight, imaging.Lanczos)
rgb64 := imageTypeToRGBA64(pic)
bounds := rgb64.Bounds()
dx := bounds.Dx()
dy := bounds.Dy()
img := image.NewNRGBA64(bounds)
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
colorRgb := rgb64.At(x, y)
r, g, b, a := colorRgb.RGBA()
percent := 1.0 - math.Abs(float64(x)-float64(dx)/2.0)/(float64(dx)/2.0)
percent = percent * percent
rr, gg, bb, aa := img.ColorModel().Convert(color.NRGBA64{
R: uint16(r),
G: uint16(g),
B: uint16(b),
A: uint16(float64(a) * percent),
}).RGBA()
img.SetRGBA64(x, y, color.RGBA64{
R: uint16(rr),
G: uint16(gg),
B: uint16(bb),
A: uint16(aa),
})
}
}
return img
}
func initBgImg() {
img := image.NewNRGBA(image.Rect(0, 0, 1200, 720))
draw.Draw(img, img.Bounds(), image.Black, image.Point{}, draw.Src)
readerImg := mustReadImg("assets/reader.jpg")
readerImg = processReaderImg(readerImg)
draw.Draw(img, readerImg.Bounds().Add(image.Pt((defaultImageWidth-readerImg.Bounds().Dx())/2, 0)), readerImg, image.Point{}, draw.Over)
assets.BackgroundImg = img
}
func initFonts() {
data, err := fs.ReadFile(assetsFS, "assets/LXGWWenKaiMono-Light.ttf")
if err != nil {
panic(err)
}
font, err := truetype.Parse(data)
if err != nil {
panic(err)
}
assets.Font = font
}
const (
defaultIconSize = 30
)
func processIcon(pic image.Image) image.Image {
if b := pic.Bounds(); b.Dx() != defaultIconSize || b.Dy() != defaultIconSize {
pic = imaging.Resize(pic, defaultIconSize, defaultIconSize, imaging.Lanczos)
}
dc := gg.NewContextForImage(pic)
dc.DrawCircle(float64(defaultIconSize/2), float64(defaultIconSize/2), float64(defaultIconSize/2))
dc.Clip()
dc.InvertMask()
dc.DrawRectangle(0, 0, float64(defaultIconSize), float64(defaultIconSize))
dc.SetColor(color.Black)
dc.Fill()
return dc.Image()
}
func initIcon() {
askerImg := processIcon(mustReadImg("assets/moon.jpg"))
readerImg := processIcon(mustReadImg("assets/crystay_ball_1f52e.png"))
assets.AskerImg = askerImg
assets.ReaderImg = readerImg
}
func GetDefaultAssets() Assets {
initAssetsOnce.Do(func() {
initCards()
initBgImg()
initFonts()
initIcon()
})
return assets
}