Set the saturation for a given color The saturation of a color is determined by a combination of light intensity and how much it is distributed across the spectrum of different wavelengths. Scale of saturation (0% at bottom). - Android Graphics

Android examples for Graphics:Color

Description

Set the saturation for a given color The saturation of a color is determined by a combination of light intensity and how much it is distributed across the spectrum of different wavelengths. Scale of saturation (0% at bottom).

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*  w  w w. ja  v  a 2 s  .c o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**
     * Set the saturation for a given color
     * <p>
     * The saturation of a color is determined by a combination of light
     * intensity and how much it is distributed across the spectrum of different
     * wavelengths.<br/>
     * <img src="../../../resources/saturation.png"> <small>Scale of saturation
     * (0% at bottom).</small>
     * </p>
     * 
     * @param color
     *            argb value
     * @param percent
     *            percent always less than or equal to 100
     * @return color with the set saturation level
     */
    public static int setSaturation(int color, int percent) {
        int per = (128 * percent) / 100;
        int a = (color >> 24) & 0xff;
        int r = (color >> 16) & 0xff;
        int g = (color >> 8) & 0xff;
        int b = color & 0xff;
        r = (r + per) / 2;
        g = (g + per) / 2;
        b = (b + per) / 2;
        return toRGB(a, r, g, b);
    }

    /**
     * 
     * @param alpha
     * @param red
     * @param green
     * @param blue
     * @return
     */
    public static int toRGB(int alpha, int red, int green, int blue) {
        return (alpha & 0xff) << 24 | (red & 0xff) << 16
                | (green & 0xff) << 8 | (blue & 0xff) << 0;
    }
}

Related Tutorials