Using color and color.darker in Android?

后端 未结 4 1763
傲寒
傲寒 2020-12-02 09:07

Okay, so I have an integer variable in my application. It\'s the value of a color, being set by a color picker in my preferences. Now, I need to use both that color and a da

4条回答
  •  一个人的身影
    2020-12-02 09:47

    Ted's answer to lighten a color wasn't working for me so here is a solution that might help someone else:

    /**
     * Lightens a color by a given factor.
     * 
     * @param color
     *            The color to lighten
     * @param factor
     *            The factor to lighten the color. 0 will make the color unchanged. 1 will make the
     *            color white.
     * @return lighter version of the specified color.
     */
    public static int lighter(int color, float factor) {
        int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255);
        int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255);
        int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255);
        return Color.argb(Color.alpha(color), red, green, blue);
    }
    

提交回复
热议问题