59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package xep0392
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/binary"
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
)
|
|
|
|
// TODO: import "github.com/hsluv/go-hsluv"
|
|
func HsluvToRGB(h, s, l float64) (r, g, b float64) {
|
|
return 0, 0, 0
|
|
}
|
|
|
|
func ColorFromString(id string) color.RGBA {
|
|
hue := hueFromString(id)
|
|
s := 100.0
|
|
l := 50.0
|
|
r, g, b := HsluvToRGB(hue, s, l)
|
|
return color.RGBA{
|
|
R: uint8(clamp01(r) * 255),
|
|
G: uint8(clamp01(g) * 255),
|
|
B: uint8(clamp01(b) * 255),
|
|
A: 255,
|
|
}
|
|
}
|
|
|
|
func ImageWithColor(id string, width, height int) *image.RGBA {
|
|
c := ColorFromString(id)
|
|
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
for y := 0; y < height; y++ {
|
|
for x := 0; x < width; x++ {
|
|
img.SetRGBA(x, y, c)
|
|
}
|
|
}
|
|
return img
|
|
}
|
|
|
|
func hueFromString(s string) float64 {
|
|
h := sha1.Sum([]byte(s))
|
|
v := binary.LittleEndian.Uint16(h[18:20])
|
|
hue := float64(v) * 360.0 / 65536.0
|
|
if hue >= 360.0 {
|
|
hue = math.Mod(hue, 360.0)
|
|
}
|
|
return hue
|
|
}
|
|
|
|
func clamp01(f float64) float64 {
|
|
if f < 0 {
|
|
return 0
|
|
}
|
|
if f > 1 {
|
|
return 1
|
|
}
|
|
return f
|
|
}
|