GibboK
GibboK

Reputation: 73918

How to fix Cannot implicitly convert type 'System.Drawing.Color' to 'Microsoft.Xna.Framework.Color'

I have this code

string hex = "#FFFFFF";
Color _color = System.Drawing.ColorTranslator.FromHtml(hex);

I need to use _color in Microsoft.Xna.Framework

using previous code I receive this error:

Cannot implicitly convert type 'System.Drawing.Color' to 'Microsoft.Xna.Framework.Color'

any idea how to solve this?

Upvotes: 2

Views: 2749

Answers (2)

GibboK
GibboK

Reputation: 73918

Solved with this

  private Microsoft.Xna.Framework.Color ConvertFromHex(string s)
    {
        if (s.Length != 7)
            return Color.Gray;

        int r = Convert.ToInt32(s.Substring(1, 2), 16);
        int g = Convert.ToInt32(s.Substring(3, 2), 16);
        int b = Convert.ToInt32(s.Substring(5, 2), 16);
        return new Color(r, g, b);
    }

Upvotes: 1

tweellt
tweellt

Reputation: 2161

Is this what you need?

public Microsoft.Xna.Framework.Graphics.Color XNAColor(System.Drawing.Color color)
{
    return new Microsoft.Xna.Framework.Graphics.Color(color.R, color.G, color.B, color.A) 
}

Upvotes: 3

Related Questions