Search code examples
javaimagecolorsbufferedimagemask

BufferedImage Color Channel Mask


I have found this code on JavaDoc, but I can't seem to understand it.

output.setRGB(x, y, (image.getRGB(x, y) & 0xff00ff00)
                    | ((image.getRGB(x, y) & 0xff0000) >> 16)
                    | ((image.getRGB(x, y) & 0xff) << 16));

All I know that this code turns blue color to red in a BufferedImage. but what if I want to replace blue with white or some other color and vice-versa?

I would appreciate any help.


Solution

  • When I work with color I use different idea:

        BufferedImage image = //create Buffered image
        int rgb = image.getRGB(x,y);   //get Rgb color value
        Color color = new Color(rgb);  // create color with this value
        Color resultColor = new Color(color.getRed(), color.getBlue(), color.getGreen()); //create new color change blue and green colors values
        image.setRGB(x,y,resultColor.getRGB());   //set color
    

    I think this idea is easier to understand.

    if you want to get white color use this :

        BufferedImage image = new BufferedImage();
        Color color = new Color(255,255,255);
        image.setRGB(x,y,color.getRGB());