mirror of
https://github.com/nxshock/colorcrop.git
synced 2025-07-02 00:23:44 +05:00
Comparator rework and fixes
This commit is contained in:
parent
ab1c13e694
commit
eff44ff1cc
5 changed files with 117 additions and 16 deletions
|
@ -6,27 +6,48 @@ import (
|
|||
)
|
||||
|
||||
// comparator is a function that returns a difference between two colors in
|
||||
// range 0.0..1.0.
|
||||
// range 0.0..1.0 (0.0 - same colors, 1.0 - totally different colors).
|
||||
type comparator func(color.Color, color.Color) float64
|
||||
|
||||
// CmpSquareRGBComponentsDiff returns difference of two colors
|
||||
func CmpSquareRGBComponentsDiff(color1 color.Color, color2 color.Color) float64 {
|
||||
// CmpEuclidean returns Euclidean difference of two colors.
|
||||
// https://en.wikipedia.org/wiki/Color_difference#Euclidean
|
||||
func CmpEuclidean(color1 color.Color, color2 color.Color) float64 {
|
||||
const maxDiff = 113509.94967402637 // Difference between black and white colors
|
||||
|
||||
r1, g1, b1, _ := color1.RGBA()
|
||||
r2, g2, b2, _ := color2.RGBA()
|
||||
return math.Sqrt(math.Pow(float64(r2)-float64(r1), 2.0)+
|
||||
math.Pow(float64(g2)-float64(g1), 2.0)+
|
||||
math.Pow(float64(b2)-float64(b1), 2.0)) / maxDiff
|
||||
|
||||
return math.Sqrt(distance(float64(r2), float64(r1))+
|
||||
distance(float64(g2), float64(g1))+
|
||||
distance(float64(b2), float64(b1))) / maxDiff
|
||||
}
|
||||
|
||||
// CmpRGBComponentsDiff returns difference of two colors.
|
||||
func CmpRGBComponentsDiff(color1 color.Color, color2 color.Color) float64 {
|
||||
const maxDiff = 765 // Difference between black and white colors
|
||||
// CmpRGBComponents returns RGB components difference of two colors.
|
||||
func CmpRGBComponents(color1 color.Color, color2 color.Color) float64 {
|
||||
const maxDiff = 195075.0 // Difference between black and white colors
|
||||
|
||||
r1, g1, b1, _ := color1.RGBA()
|
||||
r2, g2, b2, _ := color2.RGBA()
|
||||
return math.Sqrt(math.Abs(float64(r2)-float64(r1))+
|
||||
math.Abs(float64(g2)-float64(g1))+
|
||||
|
||||
return (math.Abs(float64(r2)-float64(r1)) +
|
||||
math.Abs(float64(g2)-float64(g1)) +
|
||||
math.Abs(float64(b2)-float64(b1))) / maxDiff
|
||||
}
|
||||
|
||||
// CmpCIE76 returns difference of two colors defined in CIE76 standart.
|
||||
// https://en.wikipedia.org/wiki/Color_difference#CIE76
|
||||
func CmpCIE76(color1 color.Color, color2 color.Color) float64 {
|
||||
const maxDiff = 150.8463301377893 // Difference between blue and white colors
|
||||
|
||||
r1, g1, b1, _ := color1.RGBA()
|
||||
r2, g2, b2, _ := color2.RGBA()
|
||||
|
||||
cl1, ca1, cb1 := xyztoLAB(rgbtoXYZ(r1/255, g1/255, b1/255))
|
||||
cl2, ca2, cb2 := xyztoLAB(rgbtoXYZ(r2/255, g2/255, b2/255))
|
||||
|
||||
return math.Sqrt(distance(cl2, cl1) + distance(ca2, ca1) + distance(cb2, cb1))
|
||||
}
|
||||
|
||||
func distance(x, y float64) float64 {
|
||||
return (x - y) * (x - y)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue