androidandroid-color

How to darken a given color (int)


I have a function that takes a given color and I would like it to darken the color (reduce its brightness by 20% or so). I can't figure out how to do this given just a color (int). What is the proper approach?

public static int returnDarkerColor(int color){
    int darkerColor = .... 
    return darkerColor;
}

Solution

  • A more Android way of doing it:

        public static int manipulateColor(int color, float factor) {
            int a = Color.alpha(color);
            int r = Math.round(Color.red(color) * factor);
            int g = Math.round(Color.green(color) * factor);
            int b = Math.round(Color.blue(color) * factor);
            return Color.argb(a,
                    Math.min(r,255),
                    Math.min(g,255),
                    Math.min(b,255));
        }
    

    You will want to use a factor less than 1.0f to darken. try 0.8f.