github.com/Kintar/etxt@v0.0.0-20221224033739-2fc69f000137/emask/shape_test.go (about) 1 package emask 2 3 import "image/color" 4 import "testing" 5 6 import "golang.org/x/image/math/fixed" 7 8 func TestShape(t *testing.T) { 9 shape := NewShape(5) 10 11 shape.MoveTo(0, 50) 12 shape.CubeTo(-25, -25, -25, 25, 0, -50) 13 shape.QuadTo(-50, 0, 0, 50) 14 15 shape.MoveTo(0, 50) 16 shape.CubeTo(25, -25, 25, 25, 0, -50) 17 shape.QuadTo(50, 0, 0, 50) 18 19 shape.MoveTo(0, -50) 20 shape.LineTo(0, 50) 21 shape.LineTo(50, 50) 22 shape.LineTo(50, -50) 23 shape.LineTo(0, -50) 24 25 mask, err := Rasterize(shape.Segments(), &DefaultRasterizer{}, fixed.P(0, 0)) 26 if err != nil { 27 panic(err) 28 } 29 30 gotSize := mask.Rect.Dx() 31 if gotSize != 100 { 32 t.Fatalf("expected mask rect.Dx == 100, got %d", gotSize) 33 } 34 gotSize = mask.Rect.Dy() 35 if gotSize != 100 { 36 t.Fatalf("expected mask rect.Dx == 100, got %d", gotSize) 37 } 38 39 posSum := 0 40 negSum := 0 41 for _, value := range mask.Pix { 42 intValue := int(value) 43 posSum += intValue 44 negSum += 255 - intValue 45 } 46 halfSum := 255 * 100 * 50 47 48 posSumDist := halfSum - posSum 49 if posSumDist < -halfSum/1000 || posSumDist > halfSum/1000 { 50 t.Fatalf("expected posSum (%d) around %d", posSum, halfSum) 51 } 52 53 negSumDist := halfSum - negSum 54 if negSumDist < -halfSum/1000 || negSumDist > halfSum/1000 { 55 t.Fatalf("expected negSum (%d) around %d", negSum, halfSum) 56 } 57 58 rgbSum := 0 59 img := shape.Paint(color.White, color.Black) 60 for i := 0; i < len(img.Pix); i += 4 { 61 rgbSum += int(img.Pix[i]) + int(img.Pix[i+1]) + int(img.Pix[i+2]) 62 } 63 avgColorChanValue := rgbSum / (100 * 100 * 3) 64 if avgColorChanValue < 127 || avgColorChanValue > 129 { 65 t.Fatalf("expected avg color (%d) around 128", avgColorChanValue) 66 } 67 68 shape.Reset() 69 if len(shape.Segments()) != 0 { 70 t.Fatal("expected zero segments after reset") 71 } 72 }