-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
77 lines (65 loc) · 1.17 KB
/
utils.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
package cmd
import (
"context"
"fmt"
"image"
"image/color"
"io"
"net/http"
"os"
"strings"
"github.com/fox-one/mixin-sdk-go"
)
const (
SIZE = 520
)
type (
Asset struct {
Asset *mixin.Asset
AssetID string
Icon string
ColorHex string
}
)
func (asset *Asset) Load(ctx context.Context) error {
if asset.Asset != nil {
return nil
}
if asset.AssetID == "" {
return fmt.Errorf("empty asset id")
}
ass, err := mixin.ReadNetworkAsset(ctx, asset.AssetID)
if err != nil {
return err
}
asset.Asset = ass
return nil
}
func (asset *Asset) Image(ctx context.Context) (image.Image, error) {
var (
err error
reader io.Reader
)
if asset.Icon != "" {
reader, err = os.Open(asset.Icon)
if err != nil {
return nil, err
}
} else {
resp, err := http.Get(strings.ReplaceAll(asset.Asset.IconURL, "=s128", ""))
if err != nil {
return nil, err
}
defer resp.Body.Close()
reader = resp.Body
}
img, _, err := image.Decode(reader)
if err != nil {
return nil, err
}
return img, nil
}
func hexColor(c color.Color) string {
rgba := color.RGBAModel.Convert(c).(color.RGBA)
return fmt.Sprintf("#%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
}