BufferedImage pageImage = pageValidation.getPage().getScreenshotImage();
Rect area = mainObject.getArea();
if (pageImage.getWidth() < area.getLeft() + area.getWidth() || pageImage.getHeight() < area.getTop() + area.getHeight()) {
throw new ValidationErrorException()
.withErrorArea(new ErrorArea(area, objectName))
.withMessage("Can't fetch image for \"object\" as it is outside of screenshot");
}
Spectrum spectrum;
try {
spectrum = Rainbow4J.readSpectrum(pageImage, new Rectangle(area.getLeft(), area.getTop(), area.getWidth(), area.getHeight()), PRECISION);
} catch (Exception e) {
throw new ValidationErrorException(String.format("Couldn't fetch spectrum for \"%s\"", objectName));
}
List<String> messages = new LinkedList<String>();
for (ColorRange colorRange : spec.getColorRanges()) {
Color color = colorRange.getColor();
int percentage = (int)spectrum.getPercentage(color.getRed(), color.getGreen(), color.getBlue(), TEST_RANGE);
if (!colorRange.getRange().holds(percentage)) {
messages.add(String.format("color %s on \"%s\" is %d%% %s", toHexColor(color), objectName, (int)percentage, colorRange.getRange().getErrorMessageSuffix("%")));
}
}
if (messages.size() > 0) {
throw new ValidationErrorException()
.withErrorArea(new ErrorArea(area, objectName))
.withMessages(messages);
}
}