Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change image hue of bitmap on android ImageView?

Here is the situation, I have a SeekBar and I want, when I slide it, to change the hue of an image that is in an ImageView. All of the things I've seen about changing hue require the use of a ColorMatrix, but I don't know how to relate a bitmap to a color matrix. Suggestions?

like image 830
Mastergeek Avatar asked Dec 03 '25 02:12

Mastergeek


2 Answers

    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));
    }

    private static float cleanValue(float p_val, float p_limit)
    {
        return Math.min(p_limit, Math.max(-p_limit, p_val));
    }


-------

while drawing from canvas ... create an object of paint and ... set... 
ColorMatrix mat = new ColorMatrix();`
adjustHue(mat, 110);
paint.setColorFilter(new ColorMatrixColorFilter(mat));


----
canvas.draw(bmp, 0,0,paint);
like image 149
jpm Avatar answered Dec 05 '25 16:12

jpm


There's an example here that draws a bitmap in greyscale.

http://www.mail-archive.com/[email protected]/msg38890.html

Just replace cm.setSaturation(0); with cm.setRotate(int axis, float degrees);.

like image 27
benjymous Avatar answered Dec 05 '25 16:12

benjymous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!