Reputation: 128
I am trying to change the saturation of an image by a certain amount but I am getting some strange results. I am using the following code
// shiftAmount should be a float value between -1 and 1
public static int[] saturation( int[] pixels, float shiftAmount )
{
int[] newPixels = new int[ pixels.length ];
for( int i = 0; i < pixels.length; i++ )
{
// get HSB color values
Color rgb = new Color( pixels[ i ] );
float[] hsb = Color.RGBtoHSB( rgb.getRed(), rgb.getGreen(), rgb.getBlue(), null );
float hue = hsb[ 0 ];
float saturation = hsb[ 1 ];
float brightness = hsb[ 2 ];
// shift
saturation += shiftAmount;
if( saturation > 1f )
saturation = 1f;
else if( saturation < 0f )
saturation = 0f;
// convert HSB color back
newPixels[ i ] = Color.HSBtoRGB( hue, saturation, brightness );
}
return newPixels;
}
Below is an example of 80% saturation shift in another image editing software (Aseprite) and one where I did it with my own code (using 0.8f).
What it looks like when I use Aseprite
What it looks like using my own code
As you can see, in the last image the colors are very distorted. Does anyone know how to solve this?
Upvotes: 0
Views: 248