Skip to content

Instantly share code, notes, and snippets.

@NickSablukov
Created August 1, 2022 05:32
Show Gist options
  • Save NickSablukov/e190c95f0a7c7b2ec82e068732e7edba to your computer and use it in GitHub Desktop.
Save NickSablukov/e190c95f0a7c7b2ec82e068732e7edba to your computer and use it in GitHub Desktop.
func getClosestPart(mp *map[PixelColor]image.Image, pix color.Color) image.Image {
c := color.RGBAModel.Convert(pix).(color.RGBA)
key := [3]uint8{c.R, c.G, c.B}
if part, ok := (*mp)[key]; ok {
return part
}
var minD *float64
var prt *image.Image
for m, i := range *mp {
o := int64(m[0])
a := int64(c.R)
rr := float64(o - a)
rd := math.Pow(rr, 2)
gd := math.Pow(float64(int64(m[1])-int64(c.G)), 2)
bd := math.Pow(float64(int64(m[2])-int64(c.B)), 2)
d := math.Sqrt(rd + gd + bd)
if minD == nil || *minD > d {
minD = &d
prt = &i
}
}
(*mp)[key] = *prt
return *prt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment