Question

I need a function that will take a bitmap and return the bitmap with a changed colour. It needs to be fast and simple.

It's purpose is to change the colour, also it's a png with alphas.

I've had a look online but I can't use canvas or anything external. The function resides in an external object (Don't ask..)

Here is what I've had a go at so far (not working). I know I'm really close, just a matter fo sorting out the color matrix and getting alpha to work.

public Bitmap changeBitmapColor(Bitmap sourceBitmap, int deg)
{   
    int width, height;
    height = sourceBitmap.getHeight();
    width = sourceBitmap.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();

    //figure out color matricies.
    ColorMatrix cm = new ColorMatrix();

    //cm.setSaturation(0);

    cm.set(new float[] 
    {
            0, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, 255, 0, 0,
            0, 0, 0, 1, 0,
            0, 0, 0, 0, 1
         }); 


    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);

    c.drawBitmap(sourceBitmap, 0, 0, paint);

    return bmpGrayscale;
}

Any help would be great!

------- FIXED --------

I've fixed this by changing the colour matrix, now the bitmap will change colour & not show the alpha values.

First thing is the matrix:

    cm.set(new float[] 
    {
            0, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, 140, 0, 0,
            0, 0, 0, 1, 0,
            0, 0, 0, 0, 1
    }); 

Second thing I had to change was this line of code:

Bitmap newBM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Good luck readers!

Was it helpful?

Solution 3

------- FIXED --------

I've fixed this by changing the colour matrix, now the bitmap will change colour & not show the alpha values.

First thing is the matrix:

cm.set(new float[] 
{
        0, 0, 0, 0, 0,
        0, 1, 0, 0, 0,
        0, 0, 140, 0, 0,
        0, 0, 0, 1, 0,
        0, 0, 0, 0, 1
}); 

Second thing I had to change was this line of code:

Bitmap newBM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Good luck readers!

OTHER TIPS

I don't understand why being stand-alone precludes the use of a Canvas. You can create a new Bitmap, create a Canvas to draw into it, and then paint the original Bitmap using a Paint that has a color filter set (using setColorFilter). The PorterDuffColorFilter class may be of help with this.

I did not get what your aim is with changing the colors, and what means the parameter int deg?? For the alpha thing: it might help to change the color model in your Bitmap bmpGrayscale from RGB_565to ARGB_8888 since that one has an alpha channel. Look at Bitmap.Config

You haven't said exactly what change you're trying to make in the colors. The cm matrix you've defined looks like it has an extra row, and it removes the red, leaves the green and alpha alone, and multiplies the blue by 255. I'm guessing that's not what you want.

I'm going by the documenation at http://developer.android.com/reference/android/graphics/ColorMatrix.html

If you're trying to rotate the hue, you might get some good information from this answer: https://stackoverflow.com/a/8510751/5987

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top