KSemenenko / ColorThief

Color Thief for .NET
MIT License
163 stars 28 forks source link

Error CS0012: The type 'QuantizedColor' is defined in an assembly that is not referenced. You must add a reference to assembly 'ColorThief.Forms, Version=1.1.0.1, Culture=neutral, PublicKeyToken=null'. #9

Open afbase opened 6 years ago

afbase commented 6 years ago

Hi @KSemenenko

I'm trying to follow your suggestion in the prior issue:

create proxy for ColorThief platform specific implementation using Xamarin Forms DependencyService.

In my PCL Xamarin project I have an interface and added reference to ksesmenenko.ColorThief.Forms v1.0.0.4:

using ColorThiefDotNet;
...
public interface IColorThiefHelper
{
    List<QuantizedColor> GetPalette(string sourceImage, int colorCount);
    QuantizedColor GetColor(string sourceImage);
}

and then also in my PCL Xamarin project call it using dependency service:

var colorTask = DependencyService.Get<IColorThiefHelper>().GetPalette(Logo, 8);
var backgroundColor = Color.FromHex(colorTask.FirstOrDefault().Color.ToHexString());
var foregroundColor = Color.FromHex(colorTask.LastOrDefault().Color.ToHexString());
BackgroundColor = backgroundColor;
TextInverseColor = foregroundColor;

Finally in my iOS project I have added reference to ksesmenenko.ColorThief.Forms v1.0.0.4 again and then made the dependency service implementation like so:

[assembly: Dependency(typeof(ColorThiefHelper))]
namespace IOSNameSpace
{
    public class ColorThiefHelper : IColorThiefHelper  {
        private ColorThief Thief { get; set; }
        public ColorThiefHelper()
        {
            Thief = new ColorThief();
        }    

        static UIImage FromUrl(string uri)
        {
            using (var url = new NSUrl(uri))
            using (var data = NSData.FromUrl(url))
                return UIImage.LoadFromData(data);
        }    

        public List<QuantizedColor> GetPalette(string sourceImage, int colorCount)
        {
            UIImage image = FromUrl(sourceImage);
            return Thief.GetPalette(image,colorCount,10,true);
        }
        public QuantizedColor GetColor(string sourceImage)
        {
            UIImage image = FromUrl(sourceImage);
            return Thief.GetColor(image, 10, true); 
        }
}

And here I get the error on the ColorThiefHelper class :

Error CS0012: The type 'QuantizedColor' is defined in an assembly that is not referenced. You must add a reference to assembly 'ColorThief.Forms, Version=1.1.0.1, Culture=neutral, PublicKeyToken=null'. (CS0012)

The bug is strange as there is no Version=1.1.0.1 of ColorThief.Forms in nuget.

Do you know what might be the problem?

KSemenenko commented 6 years ago

Hello @afbase, thanks for your detailed description. most likely I did not correctly make a nuget for PCL. I will fix it) You are already using .net standard?

afbase commented 6 years ago

@KSemenenko I am using .NET Portable PCL 4.5 - Profile111, not .net standard.

KSemenenko commented 6 years ago

@afbase, why do you need DI? For Xamarin.Forms you need only

await CrossColorThief.Current.GetPalette(sourceImage);

or you want to use new method like FromUrl?

afbase commented 6 years ago

@KSemenenko

For Xamarin.Forms you need only

await CrossColorThief.Current.GetPalette(sourceImage);

If I write this line of code in my PCL project, I get a System.NotImplementedException error. The error message for me is not clear. If I write this line of code in my PCL project, do I need to add a colorthief package to my iOS project and android projects as well?

or you want to use new method like FromUrl?

I'd rather try to write all of this in a PCL project but I'm beginning to think this is not possible.

afbase commented 6 years ago

I think where this issue is coming up is ColorThief reference/object confusion between PCL and platform projects.

I think what we want in the PCL project is using ColorThiefDotNet.Forms;:

using ColorThiefDotNet.Forms;
...
public interface IColorThiefHelper
{
    List<QuantizedColor> GetPalette(string sourceImage, int colorCount);
    QuantizedColor GetColor(string sourceImage);
}

instead of what I originally had here:

using ColorThiefDotNet;
...
public interface IColorThiefHelper
{
    List<QuantizedColor> GetPalette(string sourceImage, int colorCount);
    QuantizedColor GetColor(string sourceImage);
}

If I try using ColorThiefDotNet.Forms;, I get an error stating that QuantizedColor could not be found. I think this is where quantizedcolor should be instead of just the ColorThiefDotNet namespace. If that is the case I think that resolves the problem. I just forked over the repo, I may try to play with it a bit and see if it works this weekend.

KSemenenko commented 6 years ago

@afbase I think I found a problem, it's still in the structure of the project. apparently I need to do the refactoring

afbase commented 6 years ago

@ksemenenko

I may follow up on this later in my development near the end of March as I have reprioritized my development as this became a bit of a blocker. I am working on other features but what I am now thinking is to refactor the Shared library's implementation to a dependency service onto interfaces implemented in the various platform libraries.