Java examples for 2D Graphics:Color Light
Returns a new color equal to the old one, except the brightness is set to the new value.
/*//from w w w. ja v a2s. c o m * $Id: ColorUtil.java 3742 2010-08-05 03:25:09Z kschaefe $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ //package com.java2s; import java.awt.Color; public class Main { /** * Returns a new color equal to the old one, except the brightness is set to * the new value. The new color will have the same alpha (transparency) as * the original color. * <p> * The color is modified using HSB calculations. The brightness must be a * float between 0 and 1. If 0 the resulting color will be black. If 1 the * resulting color will be the brightest possible form of the passed in * color. * * @param color * the color to modify * @param brightness * the brightness to use in the new color * @return a new brightness-applied {@code Color} * @throws IllegalArgumentException * if {@code brightness} is not between 0 and 1 inclusive * @throws NullPointerException * if {@code color} is {@code null} */ public static Color setBrightness(Color color, float brightness) { if (brightness < 0f || brightness > 1f) { throw new IllegalArgumentException("invalid brightness value"); } int alpha = color.getAlpha(); float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null); Color c = Color.getHSBColor(hsb[0], hsb[1], brightness); return setAlpha(c, alpha); } /** * Returns a new color equal to the old one, except alpha (transparency) * channel is set to the new value. * * @param color * the color to modify * @param alpha * the new alpha (transparency) level. Must be an int between 0 * and 255 * @return a new alpha-applied {@code Color} * @throws IllegalArgumentException * if {@code alpha} is not between 0 and 255 inclusive * @throws NullPointerException * if {@code color} is {@code null} */ public static Color setAlpha(Color color, int alpha) { if (alpha < 0 || alpha > 255) { throw new IllegalArgumentException("invalid alpha value"); } return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); } }