shortcuts/plot/src/utils.go

39 lines
684 B
Go
Raw Normal View History

2024-09-29 18:27:50 +00:00
//go:build js && wasm
// +build js,wasm
package src
import (
"bytes"
"encoding/base64"
2024-09-30 09:55:13 +00:00
"fmt"
"image/color"
2024-09-29 18:27:50 +00:00
"io"
)
func WriterToBase64String(writer io.WriterTo) string {
var buf bytes.Buffer
writer.WriteTo(&buf)
return base64.StdEncoding.EncodeToString(buf.Bytes())
}
2024-09-30 09:55:13 +00:00
func HexToRGBA(hex string) color.RGBA {
var c color.RGBA
switch len(hex) {
case 4:
fmt.Sscanf(hex, "#%1x%1x%1x%1x", &c.R, &c.G, &c.B, &c.A)
c.R *= 17
c.G *= 17
c.B *= 17
c.A = 255
case 7:
fmt.Sscanf(hex, "#%02x%02x%02x", &c.R, &c.G, &c.B)
c.A = 255
case 9:
fmt.Sscanf(hex, "#%02x%02x%02x%02x", &c.R, &c.G, &c.B, &c.A)
default:
fmt.Println("Invalid hex color:", hex)
}
return c
}