Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Trying to create a custom image color filter using a filter image map.

$
0
0

I'm working in creating a composite image on the fly for iOS. I have two images, one is the base image (black and white), and the other is a alpha mask 'map' for a color filter. Then, at runtime, supply an RGB value so I can make the composite image be whatever color it should be.

I've found plenty of solutions for changing the entire image, but I'm having trouble finding a good solution for creating that color filter that only colors the area (with the alpha values) contained in the mask map:

Base Images:

Resulting image (if I tell it I want red):

In MonoDroid, I was able to create a custom ImageView and override the OnDraw method then used a ColorMatrixColorFilter which worked like a charm, but don't know how to accomplish the same thing in MonoTouch.

This is what I did for MonoDroid:

protected override void OnDraw(Canvas canvas)
    {
        if (_alphaMask == null)
            _alphaMask = BitmapFactory.DecodeResource(Resources, GetDrawable("star_mask"));
    if(_image == null)
        _image = BitmapFactory.DecodeResource(Resources, GetDrawable("star"));

    AdjustColor(_color, canvas, _alphaMask, _image);
}

private static void AdjustColor(Color color, Canvas c, Bitmap alphaMask, Bitmap image)
{
    float R = color.R;
    float G = color.G;
    float B = color.B;
    var maskPaint = new Paint();
    var mat = new[]
    {
        0, 0, 0, 0, R, //red
        0, 0, 0, 0, G, //green
        0, 0, 0, 0, B, //blue
        0, 0, 0, 1, 0  //alpha
    };
    ColorMatrix cm = new ColorMatrix(mat);
    maskPaint.SetColorFilter(new ColorMatrixColorFilter(cm));
    c.DrawBitmap(image, 0, 0, null);
    c.DrawBitmap(alphaMask, 0, 0, maskPaint);
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>