Posted November 4, 20195 yr I've tried looking everywhere online with no luck, so I've come here. Since nothing I've tried works, I'm think that Minecraft .png images have an unusual way of reading transparency? I'm trying to find the average color in a texture (ignoring transparent pixels). It works for the most part, except it also ignores black/white pixels (pixels with the same rgb values, example r=222, g=222, b=222). The code I am using is in the spoiler. Any help would be appreciated! Spoiler //textureName is the file location of the texture (in the form of a String), which works fine. try { BufferedImage image = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation(textureName)).getInputStream()); int width = image.getWidth(); int height = image.getHeight(); float pixels = 0; float r = 0; float g = 0; float b = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color color = new Color(image.getRGB(x, y)); if(color.getRGB() != -1) { pixels++; r += color.getRed(); g += color.getGreen(); b += color.getBlue(); } } } if(pixels == width * height) { float avgR = r / (pixels * 255.0F); float avgG = g / (pixels * 255.0F); float avgB = b / (pixels * 255.0F); GlStateManager.color(avgR, avgG, avgB, 1.0F); image.getGraphics().dispose(); } } catch (IOException e) { //e.printStackTrace(); }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.