get Lighter Colors - Java 2D Graphics

Java examples for 2D Graphics:Color Light

Description

get Lighter Colors

Demo Code

/*//from  w ww  .  j  av a  2 s . co m
Storybook: Scene-based software for novelists and authors.
Copyright (C) 2008 - 2011 Martin Mustun

This program 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.

This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.awt.Color;

public class Main {
    public static Color[] getLighterColors(Color[] colors, double fraction) {
        Color[] lightColors = new Color[colors.length];
        int i = 0;
        for (Color color : colors) {
            lightColors[i] = lighter(color, fraction);
            ++i;
        }
        return lightColors;
    }

    /**
     * Make a color lighter.
     *
     * @param color
     *            Color to make lighter.
     * @param fraction
     *            Darkness fraction.
     * @return Lighter color.
     */
    public static Color lighter(Color color, double fraction) {
        int red = (int) Math.round(color.getRed() * (1.0 + fraction));
        int green = (int) Math.round(color.getGreen() * (1.0 + fraction));
        int blue = (int) Math.round(color.getBlue() * (1.0 + fraction));

        if (red < 0)
            red = 0;
        else if (red > 255)
            red = 255;
        if (green < 0)
            green = 0;
        else if (green > 255)
            green = 255;
        if (blue < 0)
            blue = 0;
        else if (blue > 255)
            blue = 255;

        int alpha = color.getAlpha();
        return new Color(red, green, blue, alpha);
    }
}

Related Tutorials