Java BufferedImage Color Set changeRGBSaturation(final BufferedImage image, final double s)

Here you can find the source of changeRGBSaturation(final BufferedImage image, final double s)

Description

Changes the color saturation of the given RGB image.

License

Apache License

Parameter

Parameter Description
image image expected to contain a 4 band rgba color model.
s The factor with which the saturation value should be multiplied.

Declaration

public static void changeRGBSaturation(final BufferedImage image, final double s) 

Method Source Code

//package com.java2s;
/**// ww  w  .  j  av  a2  s .c  o m
 * This class provides a set of methods aroung image and graphics manipulation. Most of those manipulation are mere
 * gimmicks, but sometimes vanity is virtue.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.awt.image.*;

public class Main {
    /**
     * Changes the color saturation of the given RGB image.
     *
     * @param image image expected to contain a 4 band rgba color model.
     * @param s     The factor with which the saturation value should be multiplied.
     */
    public static void changeRGBSaturation(final BufferedImage image, final double s) {
        double RW = 0.3086;
        double RG = 0.6084;
        double RB = 0.0820;

        final double a = (1 - s) * RW + s;
        final double b = (1 - s) * RW;
        final double c = (1 - s) * RW;
        final double d = (1 - s) * RG;
        final double e = (1 - s) * RG + s;
        final double f = (1 - s) * RG;
        final double g = (1 - s) * RB;
        final double h = (1 - s) * RB;
        final double i = (1 - s) * RB + s;

        final int width = image.getWidth();
        final int height = image.getHeight();
        final double[] red = new double[width * height];
        final double[] green = new double[width * height];
        final double[] blue = new double[width * height];

        final WritableRaster raster = image.getRaster();
        raster.getSamples(0, 0, width, height, 0, red);
        raster.getSamples(0, 0, width, height, 1, green);
        raster.getSamples(0, 0, width, height, 2, blue);

        for (int x = 0; x < red.length; x++) {
            final double r0 = red[x];
            final double g0 = green[x];
            final double b0 = blue[x];
            red[x] = a * r0 + d * g0 + g * b0;
            green[x] = b * r0 + e * g0 + h * b0;
            blue[x] = c * r0 + f * g0 + i * b0;
        }

        raster.setSamples(0, 0, width, height, 0, red);
        raster.setSamples(0, 0, width, height, 1, green);
        raster.setSamples(0, 0, width, height, 2, blue);
    }
}

Related

  1. changeBrightness(BufferedImage image, int offset)
  2. changeColor(BufferedImage image, Color color, Color replacement_color)
  3. changeColor(BufferedImage image, Color replacement)
  4. changeColor(BufferedImage image, String hexval)
  5. changeToTypeIntRGB(BufferedImage image)
  6. changeTranslucentImage(BufferedImage img, float transperancy)