Darkens a color by reducing its RGB channel values. - Android Graphics

Android examples for Graphics:Color RGB Value

Description

Darkens a color by reducing its RGB channel values.

Demo Code


import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;

public class Main{
    /**/*from  w w  w. j a  va  2  s.  com*/
     * Darkens a color by reducing its RGB channel values.
     *
     * @param context the current context
     * @param res     the color resource
     * @param percent the percent to decrease
     * @return a color int
     */
    @ColorInt
    public static int decreaseRgbChannels(Context context,
            @ColorRes int res, float percent) {
        int c = resolveColor(res, context);

        // reduce rgb channel values to produce box shadow effect
        int red = (Color.red(c));
        red -= (red * percent);
        red = red > 0 ? red : 0;

        int green = (Color.green(c));
        green -= (green * percent);
        green = green > 0 ? green : 0;

        int blue = (Color.blue(c));
        blue -= (blue * percent);
        blue = blue > 0 ? blue : 0;

        return Color.argb(Color.alpha(c), red, green, blue);
    }
    /**
     * Resolves a color resource.
     *
     * @param color   the color resource
     * @param context the current context
     * @return a color int
     */
    public static @ColorInt int resolveColor(@ColorRes int color,
            Context context) {
        if (Build.VERSION.SDK_INT >= 23) {
            return context.getResources().getColor(color,
                    context.getTheme());
        } else {
            return context.getResources().getColor(color);
        }
    }
}

Related Tutorials