How to darken a given color (int)

后端 未结 6 2094

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 colo

6条回答
  •  感情败类
    2020-12-29 21:56

    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.

提交回复
热议问题