Skip to content

Instantly share code, notes, and snippets.

@betandr
Created March 5, 2014 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betandr/9377142 to your computer and use it in GitHub Desktop.
Save betandr/9377142 to your computer and use it in GitHub Desktop.
Java: Get color from a solid image
Color getImageColor(File imagePath) {
BufferedImage image = ImageIO.read(imagePath);
int color = image.getRGB(0, 0);
for (int r = 0; r < image.getHeight(); r += 1) {
for (int c = 0; c < image.getWidth(); c += 1) {
if (image.getRGB(c, r) != color) {
throw new IllegalArgumentException("Image: " + imagePath + " is not a solid color.");
}
}
}
return new Color(color);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment