Method returning given color modified to lighter color (positive translation) or darker (negative translation). - Android Graphics

Android examples for Graphics:Color Darken

Description

Method returning given color modified to lighter color (positive translation) or darker (negative translation).

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {
    /**//from   www. j a v  a 2  s.co  m
     * Method returning given color modified to lighter color (positive translation) or darker
     * (negative translation).
     *
     * @param primaryColor basic color
     * @param translation  positive or negative value of color translation
     * @return lighter/darker color
     */
    public static int getColorWithTranslateBrightness(int primaryColor,
            int translation) {
        if (translation >= 0) {
            return Color.argb(Color.alpha(primaryColor),
                    Math.min(Color.red(primaryColor) + translation, 255),
                    Math.min(Color.green(primaryColor) + translation, 255),
                    Math.min(Color.blue(primaryColor) + translation, 255));
        } else {
            return Color.argb(Color.alpha(primaryColor),
                    Math.max(Color.red(primaryColor) + translation, 0),
                    Math.max(Color.green(primaryColor) + translation, 0),
                    Math.max(Color.blue(primaryColor) + translation, 0));
        }
    }
}

Related Tutorials