Adjusting Lightness using ColorMatrix
Asked Answered
D

2

3

I'm trying to adjust the lightness of an image (You can see this option in Photoshop when trying to adjust the Hue, also lightness and brightness are two different features) using ColorMatrix but am lost on which values to change to achive this.

Currently I'm able to change the Hue using this code

public static void adjustHue(ColorMatrix cm, float value)
    {
        value = cleanValue(value, 180f) / 180f * (float) Math.PI;
        if (value == 0)
        {
            return;
        }
        float cosVal = (float) Math.cos(value);
        float sinVal = (float) Math.sin(value);
        float lumR = 0.213f;
        float lumG = 0.715f;
        float lumB = 0.072f;
        float[] mat = new float[]
        { 
                lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0, 
                lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0, 
                0f, 0f, 0f, 1f, 0f, 
                0f, 0f, 0f, 0f, 1f };
        cm.postConcat(new ColorMatrix(mat));
    }

I want to understand on how to use the colormatrix to change the lightness in the same way. If there is any other way of achieving this, I'm open for solutions :)

Depalma answered 7/2, 2012 at 11:2 Comment(0)
D
4

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.

Depalma answered 10/2, 2012 at 11:5 Comment(1)
how to use this method ??Taurus
B
4

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.

Berseem answered 27/11, 2014 at 22:33 Comment(1)
You've got one too many rows in your input array here. ColorMatrix is a 4x5 matrixPastore

© 2022 - 2024 — McMap. All rights reserved.