diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a80e533 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module forge.sunglocto.net/sunglocto/xep0392 + +go 1.25.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..03dd63f --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package xep0392 + +import ( + "crypto/sha1" + "encoding/binary" + "fmt" + "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 +}