Adjusting Lightness using ColorMatrix

牧云@^-^@ 提交于 2019-12-04 12:21:55

Ok..So I finally figured this out.

Lightness can be adjusted using the PorterDuffColorFilter Class

Here's the method that I'm using

public static PorterDuffColorFilter applyLightness(int progress) {

    if(progress>0)
    {
        int value = (int) progress*255/100;
        return new PorterDuffColorFilter(Color.argb(value, 255, 255, 255), Mode.SRC_OVER);
    } else {
        int value = (int) (progress*-1)*255/100;
        return new PorterDuffColorFilter(Color.argb(value, 0, 0, 0), Mode.SRC_ATOP);
    }

}

The value of progress is from -100 (dark) to 100(bright)

Just pass the values as in photoshop to this method. The filter that you get can be used with paint and canvas.

Hope this helps someone.

You can use this to change lightness

public static void adjustLightness(ColorMatrix cm, float value)
{
    value = cleanValue(value, 100);
    if (value == 0)
    {
        return;
    }

    float[] mat = new float[]
            {
                    1,0,0,0,value,
                    0,1,0,0,value,
                    0,0,1,0,value,
                    0,0,0,1,0,
                    0,0,0,0,1
            };
    cm.postConcat(new ColorMatrix(mat));
}

//Helper method
protected static float cleanValue(float p_val, float p_limit)
{
    return Math.min(p_limit, Math.max(-p_limit, p_val));
}

You can read more here, but I'd recommend you to use RenderScript because of it's speed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!