@@ -39,7 +39,7 @@ func twoColorImage(w, h int, c1, c2 color.RGBA) image.Image {
3939// colorInSlice 判断给定颜色是否在切片中(允许 tolerance 误差)
4040func colorInSlice (c color.RGBA , slice []color.RGBA , tolerance float64 ) bool {
4141 for _ , s := range slice {
42- if distance (c , s ) <= tolerance {
42+ if distanceRGBAsq (c , s ) <= tolerance {
4343 return true
4444 }
4545 }
@@ -100,15 +100,15 @@ func TestSq_LargeValue(t *testing.T) {
100100
101101func TestDistance_SameColor (t * testing.T ) {
102102 c := color.RGBA {100 , 150 , 200 , 255 }
103- if got := distance (c , c ); got != 0 {
103+ if got := distanceRGBAsq (c , c ); got != 0 {
104104 t .Errorf ("distance(same, same) = %v, want 0" , got )
105105 }
106106}
107107
108108func TestDistance_BlackAndWhite (t * testing.T ) {
109109 // sqrt(255^2 * 3) = 255 * sqrt(3)
110110 want := 255 * math .Sqrt (3 )
111- got := distance ( Black , White )
111+ got := math . Sqrt ( distanceRGBAsq ( Black , White ) )
112112 if math .Abs (got - want ) > 1e-9 {
113113 t .Errorf ("distance(black, white) = %v, want %v" , got , want )
114114 }
@@ -119,7 +119,7 @@ func TestDistance_SingleChannel(t *testing.T) {
119119 b := color.RGBA {3 , 4 , 0 , 255 }
120120 // sqrt(9 + 16) = 5
121121 want := 5.0
122- got := distance ( a , b )
122+ got := math . Sqrt ( distanceRGBAsq ( a , b ) )
123123 if math .Abs (got - want ) > 1e-9 {
124124 t .Errorf ("distance(%v, %v) = %v, want %v" , a , b , got , want )
125125 }
@@ -128,7 +128,7 @@ func TestDistance_SingleChannel(t *testing.T) {
128128func TestDistance_Symmetry (t * testing.T ) {
129129 a := color.RGBA {10 , 20 , 30 , 255 }
130130 b := color.RGBA {50 , 80 , 110 , 255 }
131- if d1 , d2 := distance (a , b ), distance (b , a ); math .Abs (d1 - d2 ) > 1e-9 {
131+ if d1 , d2 := distanceRGBAsq (a , b ), distanceRGBAsq (b , a ); math .Abs (d1 - d2 ) > 1e-9 {
132132 t .Errorf ("distance not symmetric: d(a,b)=%v, d(b,a)=%v" , d1 , d2 )
133133 }
134134}
@@ -138,7 +138,7 @@ func TestDistance_NonNegative(t *testing.T) {
138138 for range 100 {
139139 a := color.RGBA {uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), 255 }
140140 b := color.RGBA {uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), 255 }
141- got := distance (a , b )
141+ got := distanceRGBAsq (a , b )
142142 if got < 0 {
143143 t .Errorf ("distance returned negative value %v for %v, %v" , got , a , b )
144144 }
@@ -150,7 +150,7 @@ func TestDistance_IgnoresAlpha(t *testing.T) {
150150 a2 := color.RGBA {100 , 150 , 200 , 255 }
151151 b := color.RGBA {50 , 50 , 50 , 128 }
152152 // Alpha 不参与计算,结果应相同
153- if d1 , d2 := distance (a1 , b ), distance (a2 , b ); math .Abs (d1 - d2 ) > 1e-9 {
153+ if d1 , d2 := distanceRGBAsq (a1 , b ), distanceRGBAsq (a2 , b ); math .Abs (d1 - d2 ) > 1e-9 {
154154 t .Errorf ("distance should ignore alpha: d(a1,b)=%v, d(a2,b)=%v" , d1 , d2 )
155155 }
156156}
@@ -320,7 +320,7 @@ func BenchmarkDistance(b *testing.B) {
320320 c := color.RGBA {50 , 80 , 30 , 255 }
321321 b .ResetTimer ()
322322 for range b .N {
323- distance (a , c )
323+ distanceRGBAsq (a , c )
324324 }
325325}
326326
0 commit comments