简体   繁体   中英

Java Change Saturation Of Image

I'm trying to change the saturation of a particular image, in Java. I already know how to edit the hue and brightness of a pixel, but I'm stumped how to do saturation. Here's the loop I use to cycle through each of the pixels, if you need to know it. I know it isn't good for performance, but it's temporary. Loop:

for(int y = 0; y < height; y++) {
    for(int x = 0; x < width; x++) {
        int pixel = image.getRGB(x, y);
        int r = (pixel >> 16) & 0xFF;
        int g = (pixel >> 8) & 0xFF;
        int b = (pixel) & 0xFF;

        //Adjust saturation:
        //?????????????????????
    }
}

In short, I'm not sure how to change the saturation of a pixel, but I want to know how. The loop I'm using above is working perfectly, so no problems there. Thanks! :D

You can use:

int red = ...;
int green = ...;
int blue = ...;

float[] hsb = Color.RGBtoHSB(red, green, blue, null);

float hue = hsb[0];
float saturation = hsb[1];
float brightness = hsb[2];

/* then change the saturation... */

int rgb = Color.HSBtoRGB(hue, saturation, brightness);

red = (rgb>>16)&0xFF;
green = (rgb>>8)&0xFF;
blue = rgb&0xFF;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM