baskren / Forms9Patch

Simplify image management and text formatting in your Xamarin.Forms apps

Home Page:http://Forms9Patch.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug] Image TintColor is always at maximum brightness

grigionicarlo opened this issue · comments

Description

Since version 2.4.0 I found a problem applying the TintColor on an Image (the images are always at maximum brightness, eg: orange shows yellow, green you see light green, etc.).

Steps to Reproduce

var image = new Forms9Patch.Image("Forms9Patch_TEST.Resources.Images.account_circle@3x.png")
{
TintColor = Color.Green,
HorizontalOptions = LayoutOptions.Center
};

Expected Behavior

Image colored with Color.Green (#008000)

Actual Behavior

Image colored with #00FF00

Basic Information

  • Version with issue: from 2.4.0
  • Last known good version: 2.3.9
  • IDE: VisualStudio 2019
  • Platform Target Frameworks: Xamarin 4.8.0.1821
    • iOS: 12
    • Android: 29

Screenshots

image

Reproduction Link - a link to a small demo project that reproduces this issue

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        var layout = new Xamarin.Forms.StackLayout
        {
            VerticalOptions = LayoutOptions.Center,
            Spacing = 16.0
        };
        this.Content = layout; 

        var image = new Forms9Patch.Image("Forms9Patch_TEST.Resources.Images.account_circle@3x.png")
        {
            TintColor = Color.Green,
            HorizontalOptions = LayoutOptions.Center
        }; 
        layout.Children.Add(image);

        Task.Run(async () =>
        {
            var image_source = Forms9Patch.ImageSource.FromResource("Forms9Patch_TEST.Resources.Images.account_circle@3x.png");

            var stream = await GetStreamFromImageSourceAsync(image_source as StreamImageSource);

            using (var managedStream = new SKManagedStream(stream))
            {
                bitmap = SKBitmap.Decode(managedStream);
            }

            SKCanvasView canvasView = new SKCanvasView()
            {
                HorizontalOptions = LayoutOptions.Center
            };
            canvasView.PaintSurface += OnCanvasViewPaintSurface;

            layout.Children.Add(canvasView);
        });
    }

    private async Task<Stream> GetStreamFromImageSourceAsync(StreamImageSource imageSource, CancellationToken cancellationToken = default(CancellationToken))
    {
        if (imageSource.Stream != null)
        {
            return await imageSource.Stream(cancellationToken);
        }
        return null;
    }

    SKBitmap bitmap;

    void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
    {
        SKImageInfo info = args.Info;
        SKSurface surface = args.Surface;
        SKCanvas canvas = surface.Canvas;

        canvas.Clear();


        Color TintColor = Color.Green;
        SKPaint paint = null;
        var mx = new[]
                {
                    0, 0, 0, 0, (float)TintColor.R,
                    0, 0, 0, 0, (float)TintColor.G,
                    0, 0, 0, 0, (float)TintColor.B,
                    0, 0, 0, (float)(TintColor.A * Opacity), 0
                };

       
        var cf = SKColorFilter.CreateColorMatrix(mx);

        paint = new SKPaint
        {
            ColorFilter = cf,
            IsAntialias = true
        };

        canvas.DrawBitmap(bitmap, 0, 0, paint: paint);
        canvas.Restore();
    }
}

Workaround

In the GenerateImageLayout function in Image.cs the matrix for the TintColor must be changed to
var mx = new [] { 0, 0, 0, 0, (float) TintColor.R,
0, 0, 0, 0, (float) TintColor.G,
0, 0, 0, 0, (float) TintColor.B,
0, 0, 0, (float) (TintColor.A * Opacity), 0 };

THANK YOU!

When will this fix be released as it is still an issue in the current version 2.4.9?