mirror of
https://github.com/nxshock/colorcrop.git
synced 2025-07-02 00:23:44 +05:00
32 lines
1 KiB
Go
32 lines
1 KiB
Go
package colorcrop
|
|
|
|
import (
|
|
"image/color"
|
|
"math"
|
|
)
|
|
|
|
// comparator is a function that returns a difference between two colors in
|
|
// range 0.0..1.0.
|
|
type comparator func(color.Color, color.Color) float64
|
|
|
|
// CmpSquareRGBComponentsDiff returns difference of two colors
|
|
func CmpSquareRGBComponentsDiff(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
|
|
}
|
|
|
|
// CmpRGBComponentsDiff returns difference of two colors.
|
|
func CmpRGBComponentsDiff(color1 color.Color, color2 color.Color) float64 {
|
|
const maxDiff = 765 // 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))+
|
|
math.Abs(float64(b2)-float64(b1))) / maxDiff
|
|
}
|