Java Color Brighten brightnessToAlpha(Image image, float alpha)

Here you can find the source of brightnessToAlpha(Image image, float alpha)

Description

Takes the image brightness and turns it into an alpha-channel.

License

Apache License

Declaration

public static BufferedImage brightnessToAlpha(Image image, float alpha) 

Method Source Code


//package com.java2s;
/*/*from   ww  w  . j a  va2  s .c  o  m*/
 * Copyright 2010 Roger Kapsi
 *
 *   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
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   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.Color;
import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Takes the image brightness and turns it into an alpha-channel.
     */
    public static BufferedImage brightnessToAlpha(Image image, float alpha) {
        return brightnessToAlpha(image, (int) (255f * alpha));
    }

    /**
     * Takes the image brightness and turns it into an alpha-channel.
     */
    public static BufferedImage brightnessToAlpha(Image image, int alpha) {

        if (image == null) {
            throw new NullPointerException("image");
        }

        if (alpha < 0 || 255 < alpha) {
            throw new IllegalArgumentException("alpha=" + alpha);
        }

        BufferedImage src = toBufferedImage(image);

        int width = src.getWidth();
        int height = src.getHeight();

        int[] rgb = new int[width * height];
        src.getRGB(0, 0, width, height, rgb, 0, width);

        BufferedImage dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        int value, r, g, b, a;
        float[] hsb = new float[3];

        for (int i = 0; i < rgb.length; i++) {
            value = rgb[i];

            // Get the RGB components
            r = (value >> 16) & 0xFF;
            g = (value >> 8) & 0xFF;
            b = (value) & 0xFF;

            // Turn RGB into HSB
            Color.RGBtoHSB(r, g, b, hsb);

            // Use the Brightness to compute the alpha value
            a = (int) (hsb[2] * alpha);

            // Re-Create the pixel with the new alpha value
            rgb[i] = (value & 0x00FFFFFF) | (a << 24);
        }

        dst.setRGB(0, 0, width, height, rgb, 0, width);
        return dst;
    }

    /**
     * Takes an {@link Image} and turns it into a {@link BufferedImage}.
     */
    public static BufferedImage toBufferedImage(Image image) {
        if (image == null) {
            throw new NullPointerException("image");
        }

        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        int width = image.getWidth(null);
        int height = image.getHeight(null);

        BufferedImage dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D gfx = dst.createGraphics();
        gfx.drawImage(image, 0, 0, null);
        gfx.dispose();
        return dst;
    }
}

Related

  1. brighter(Color color, float factor)
  2. brighterColor(Color c)
  3. brighterColor(String hexValue)
  4. brightness(Color c, float scale)
  5. brightness(java.awt.Color c)
  6. changeBrightness(Color c, int percent)
  7. changeColorBrightness(final Color color, final double delta)
  8. deriveByBrightness(Color original, Color brightnessSource)
  9. deriveColorHSB(Color base, float hue, float saturation, float brightness)