예제 #1
0
func TestHexConversion(t *testing.T) {
	goodColors := []string{
		"FF0000",
		"#0000FF",
		"00FF00",
	}

	badColors := []string{
		"000FF00",
		"0000FF#",
		"F0000",
		"1234f5F",
	}

	for _, color := range goodColors {
		_, err := imgutil.HexToRBGA(color)
		if err != nil {
			t.Fatalf("color %s failed when it was supposed to pass", color)
		}
	}

	for _, badColor := range badColors {
		_, err := imgutil.HexToRBGA(badColor)
		if err == nil {
			t.Fatalf("color %s passed when it was supposed to fail", badColor)
		}
	}
}
예제 #2
0
func TestConversion(t *testing.T) {
	// colors to test
	baseImgs := []image.Image{
		makeImage(255, 0, 0), // red
		makeImage(0, 255, 0), // green
		makeImage(0, 0, 255), // blue
		// test to see if greyscale works
		makeImage(0, 0, 255), // blue
	}

	// colors that should be after conversion
	expectedImgs := []image.Image{
		makeImage(0, 0, 255), // to blue
		makeImage(255, 0, 0), // to red
		makeImage(0, 255, 0), // to green
		// test to see if greyscale works
		makeGray(0, 0, 255), // gray
	}

	red, err := imgutil.HexToRBGA("FF0000")
	if err != nil {
		t.Fatal(err)
	}
	blue, err := imgutil.HexToRBGA("0000Ff")
	if err != nil {
		t.Fatal(err)
	}
	green, err := imgutil.HexToRBGA("00FF00")
	if err != nil {
		t.Fatal(err)
	}

	goodConvertedImgs := []image.Image{
		imgutil.ConvertImageColor(baseImgs[0], imgutil.ColorConverter(red, blue)),
		imgutil.ConvertImageColor(baseImgs[1], imgutil.ColorConverter(green, red)),
		imgutil.ConvertImageColor(baseImgs[2], imgutil.ColorConverter(blue, green)),
		imgutil.ConvertImageColor(baseImgs[2], image.NewGray(image.Rect(0, 0, 1920, 1200)).ColorModel()),
	}

	badConvertedImgs := []image.Image{
		imgutil.ConvertImageColor(baseImgs[0], imgutil.ColorConverter(blue, green)),
		imgutil.ConvertImageColor(baseImgs[1], imgutil.ColorConverter(red, blue)),
		imgutil.ConvertImageColor(baseImgs[2], imgutil.ColorConverter(green, red)),
		imgutil.ConvertImageColor(baseImgs[2], imgutil.ColorConverter(green, red)),
	}

	for i, img := range expectedImgs {
		if !compare(img, goodConvertedImgs[i]) {
			t.Fatalf("was supposed to pass: image %d did not match", i)
		}
		if compare(img, badConvertedImgs[i]) {
			t.Fatalf("was supposed to fail: image %d did match", i)
		}
	}
}