How to darken a given color (int)

后端 未结 6 2107

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 22:05

    If you want more simple and not accurately, below might help you.

    public static int returnDarkerColor(int color){
        float ratio = 1.0f - 0.2f;
        int a = (color >> 24) & 0xFF;
        int r = (int) (((color >> 16) & 0xFF) * ratio);
        int g = (int) (((color >> 8) & 0xFF) * ratio);
        int b = (int) ((color & 0xFF) * ratio);
    
        return (a << 24) | (r << 16) | (g << 8) | b;
    }
    

提交回复
热议问题