Files
2026-11-02 22:12:30 +03:00

30 lines
578 B
Go

package util
type Set map[color.Color]struct{}
func PaletteFromImage(img image.Image) color.Palette {
m := Set{}
for y := 0; y < img.Bounds().Dy(); y++ {
for x := 0; x < img.Bounds().Dx(); x++ {
m[img.At] = struct{}{}
}
}
return func(m Set)(p color.Palette){
for k := range m {
p = append(p, k)
}
}(m)
}
func ImageToPaletted(img image.Image) (image.Paletted) {
p := image.NewPaletted(img.Bounds(), PaletteFromImage(img))
for y := 0; y < img.Bounds().Dy(); y++ {
for x := 0; x < img.Bounds().Dx(); x++ {
p.Set(x, y, img.At(x,y))
}
}
return p
}