/**
* ピクセルごとのRed,Green,Blueの色の要素値をAssertします。
*
* 要素値の差(±)は 増減 5% まで許容してます。
*/
static void assertImages(BufferedImage actual, BufferedImage expected) {
assertThat("比較している画像サイズが異なります(縦)", actual.getHeight(), is(expected.getHeight()));
assertThat("比較している画像サイズが異なります(横)", actual.getWidth(), is(expected.getWidth()));
// 誤差を許容するパーセント
final int ACCEPTABLE_PERCENTAGE = 5;
// 誤差を許容する数値(±)
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("色の要素値が許容範囲(±" + ACCEPTABLE_PERCENTAGE + "%)を超えてます:Red",
(double) actualColor.getRed(),
is(closeTo((double) expectedColor.getRed(), ACCEPTABLE_DIFFERENCE)));
assertThat("色の要素値が許容範囲(±" + ACCEPTABLE_PERCENTAGE + "%)を超えてます:Green",
(double) actualColor.getGreen(),
is(closeTo((double) expectedColor.getGreen(), ACCEPTABLE_DIFFERENCE)));
assertThat("色の要素値が許容範囲(±" + ACCEPTABLE_PERCENTAGE + "%)を超えてます:Blue",
(double) actualColor.getBlue(),
is(closeTo((double) expectedColor.getBlue(), ACCEPTABLE_DIFFERENCE)));
}
}
}