Fix comparators and simplify calculations

This commit is contained in:
nxshock 2017-06-25 17:48:40 +05:00
parent 2390cdd35d
commit 1cf1415b29

View file

@ -24,30 +24,47 @@ func CmpEuclidean(color1 color.Color, color2 color.Color) float64 {
// CmpRGBComponents returns RGB components difference of two colors. // CmpRGBComponents returns RGB components difference of two colors.
func CmpRGBComponents(color1 color.Color, color2 color.Color) float64 { func CmpRGBComponents(color1 color.Color, color2 color.Color) float64 {
const maxDiff = 195075.0 // Difference between black and white colors const maxDiff = 765.0 // Difference between black and white colors
r1, g1, b1, _ := color1.RGBA() r1, g1, b1, _ := color1.RGBA()
r2, g2, b2, _ := color2.RGBA() r2, g2, b2, _ := color2.RGBA()
return (math.Abs(float64(r2)-float64(r1)) + r1, g1, b1 = r1/256, g1/256, b1/256
math.Abs(float64(g2)-float64(g1)) + r2, g2, b2 = r2/256, g2/256, b2/256
math.Abs(float64(b2)-float64(b1))) / maxDiff
return float64((max(r1, r2)-min(r1, r2))+
(max(g1, g2)-min(g1, g2))+
(max(b1, b2)-min(b1, b2))) / maxDiff
} }
// CmpCIE76 returns difference of two colors defined in CIE76 standart. // CmpCIE76 returns difference of two colors defined in CIE76 standart.
// https://en.wikipedia.org/wiki/Color_difference#CIE76 // https://en.wikipedia.org/wiki/Color_difference#CIE76
func CmpCIE76(color1 color.Color, color2 color.Color) float64 { func CmpCIE76(color1 color.Color, color2 color.Color) float64 {
const maxDiff = 150.8463301377893 // Difference between blue and white colors const maxDiff = 149.95514755026548 // Difference between blue and white colors
r1, g1, b1, _ := color1.RGBA() r1, g1, b1, _ := color1.RGBA()
r2, g2, b2, _ := color2.RGBA() r2, g2, b2, _ := color2.RGBA()
cl1, ca1, cb1 := xyztoLAB(rgbtoXYZ(r1/255, g1/255, b1/255)) cl1, ca1, cb1 := xyztoLAB(rgbtoXYZ(r1/256, g1/256, b1/256))
cl2, ca2, cb2 := xyztoLAB(rgbtoXYZ(r2/255, g2/255, b2/255)) cl2, ca2, cb2 := xyztoLAB(rgbtoXYZ(r2/256, g2/256, b2/256))
return math.Sqrt(distance(cl2, cl1) + distance(ca2, ca1) + distance(cb2, cb1)) return math.Sqrt(distance(cl2, cl1)+distance(ca2, ca1)+distance(cb2, cb1)) / maxDiff
} }
func distance(x, y float64) float64 { func distance(x, y float64) float64 {
return (x - y) * (x - y) return (x - y) * (x - y)
} }
func min(a, b uint32) uint32 {
if a < b {
return a
}
return b
}
func max(a, b uint32) uint32 {
if a > b {
return a
}
return b
}