Reputation:
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
Upvotes: 8
Views: 3805
Reputation: 3817
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;
Upvotes: 4