Code
/**
*Pixel-by-pixel Red,Green,Assert the element value of the Blue color.
*
*Difference in element values(±)Increases or decreases 5%Is allowed.
*/
static void assertImages(BufferedImage actual, BufferedImage expected) {
assertThat("The image size being compared is different (vertical)", actual.getHeight(), is(expected.getHeight()));
assertThat("The image size being compared is different (horizontal)", actual.getWidth(), is(expected.getWidth()));
//Percentage tolerate error
final int ACCEPTABLE_PERCENTAGE = 5;
//Numerical value tolerate error(±)
final int ACCEPTABLE_DIFFERENCE = 255 * ACCEPTABLE_PERCENTAGE / 100 / 2;
int width = actual.getWidth();
int height = actual.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Color actualColor = new Color(actual.getRGB(x, y));
Color expectedColor = new Color(expected.getRGB(x, y));
assertThat("Color element values are acceptable(±" + ACCEPTABLE_PERCENTAGE + "%)Beyond: Red",
(double) actualColor.getRed(),
is(closeTo((double) expectedColor.getRed(), ACCEPTABLE_DIFFERENCE)));
assertThat("Color element values are acceptable(±" + ACCEPTABLE_PERCENTAGE + "%)Beyond: Green",
(double) actualColor.getGreen(),
is(closeTo((double) expectedColor.getGreen(), ACCEPTABLE_DIFFERENCE)));
assertThat("Color element values are acceptable(±" + ACCEPTABLE_PERCENTAGE + "%)Beyond: Blue",
(double) actualColor.getBlue(),
is(closeTo((double) expectedColor.getBlue(), ACCEPTABLE_DIFFERENCE)));
}
}
}
Recommended Posts