@@ -106,18 +106,16 @@ func TestDistance_SameColor(t *testing.T) {
106106}
107107
108108func TestDistance_BlackAndWhite (t * testing.T ) {
109- black := color.RGBA {0 , 0 , 0 , 255 }
110- white := color.RGBA {255 , 255 , 255 , 255 }
111109 // sqrt(255^2 * 3) = 255 * sqrt(3)
112110 want := 255 * math .Sqrt (3 )
113- got := distance (black , white )
111+ got := distance (Black , White )
114112 if math .Abs (got - want ) > 1e-9 {
115113 t .Errorf ("distance(black, white) = %v, want %v" , got , want )
116114 }
117115}
118116
119117func TestDistance_SingleChannel (t * testing.T ) {
120- a := color. RGBA { 0 , 0 , 0 , 255 }
118+ a := Black
121119 b := color.RGBA {3 , 4 , 0 , 255 }
122120 // sqrt(9 + 16) = 5
123121 want := 5.0
@@ -263,13 +261,11 @@ func TestTakecolor_SolidColorKGreaterThan1(t *testing.T) {
263261func TestTakecolor_TwoDistinctColors (t * testing.T ) {
264262 // k-means 的初始中心随机选取,可能两次都落到同一颜色区域导致不收敛。
265263 // 多次运行,验证算法至少能在 30 次尝试中有一次正确分离两种颜色。
266- red := color.RGBA {255 , 0 , 0 , 255 }
267- blue := color.RGBA {0 , 0 , 255 , 255 }
268- img := twoColorImage (20 , 20 , red , blue )
264+ img := twoColorImage (20 , 20 , Red , Blue )
269265 const maxAttempts = 30
270266 for attempt := 0 ; attempt < maxAttempts ; attempt ++ {
271267 result := takecolor (img , 2 )
272- if len (result ) == 2 && colorInSlice (red , result , 5 ) && colorInSlice (blue , result , 5 ) {
268+ if len (result ) == 2 && colorInSlice (Red , result , 5 ) && colorInSlice (Blue , result , 5 ) {
273269 return // 成功分离,测试通过
274270 }
275271 }
0 commit comments