Created
August 1, 2022 05:32
-
-
Save NickSablukov/e190c95f0a7c7b2ec82e068732e7edba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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