Java examples for 2D Graphics:Color
Returns a new color equal to the old one, except the saturation is set to the new value.
/*/* www . j a v a 2 s .co 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 saturation 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 saturation must be a * float between 0 and 1. If 0 the resulting color will be gray. If 1 the * resulting color will be the most saturated possible form of the passed in * color. * * @param color * the color to modify * @param saturation * the saturation to use in the new color * @return a new saturation-applied {@code Color} * @throws IllegalArgumentException * if {@code saturation} is not between 0 and 1 inclusive * @throws NullPointerException * if {@code color} is {@code null} */ public static Color setSaturation(Color color, float saturation) { if (saturation < 0f || saturation > 1f) { throw new IllegalArgumentException("invalid saturation value"); } int alpha = color.getAlpha(); float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null); Color c = Color.getHSBColor(hsb[0], saturation, hsb[2]); 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); } }