Returns a colour with new opacity. - Java 2D Graphics

Java examples for 2D Graphics:Graphics

Description

Returns a colour with new opacity.

Demo Code

/*//  www . j  av  a2 s  .  c o  m
This file is part of leafdigital util.

util is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

util is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with util.  If not, see <http://www.gnu.org/licenses/>.

Copyright 2011 Samuel Marshall.
 */
//package com.java2s;
import java.awt.*;

public class Main {
    /**
     * Returns a colour with new opacity. If the original colour is solid, the
     * result will have alpha=opacity. Otherwise it'll be the combination.
     * @param c Original colour
     * @param opacity Opacity 0-255
     * @return New colour
     */
    public static Color combineOpacity(Color c, int opacity) {
        if (opacity == 255)
            return c;
        else
            return new Color(c.getRed(), c.getGreen(), c.getBlue(),
                    (opacity * c.getAlpha()) / 255);
    }
}

Related Tutorials