locked
Store a color in SQL database RRS feed

  • Question

  • User383510 posted

    Hello,

    I have a problem with ToArgb() Method in Xamarin...

    I need to convert a color to int32 and store it in a SQL database, but the method ToArgb is not working...

    Any Sugestion??

    Monday, May 6, 2019 4:18 PM

Answers

  • User369979 posted

    ToArgb is a method of System.Drawing.Color. But if you are using Xamarin.Forms.Color, it can't be applied. You could convert the color to a string like:

    var color = Xamarin.Forms.Color.Red;
    int red = (int)(color.R * 255);
    int green = (int)(color.G * 255);
    int blue = (int)(color.B * 255);
    int alpha = (int)(color.A * 255);
    string hex = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", red, green, blue, alpha);
    

    Then store this string to your database. When you want to consume it, use the code below:

    public static Color FromHex(string hex);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 7, 2019 7:55 AM

All replies

  • User180523 posted

    What do you mean by "not working" ? What do you get as a returned result? A screen shot of Visual Studio showing the values/watch palette during the breakpoint would help a ton.

    Monday, May 6, 2019 4:25 PM
  • User369979 posted

    ToArgb is a method of System.Drawing.Color. But if you are using Xamarin.Forms.Color, it can't be applied. You could convert the color to a string like:

    var color = Xamarin.Forms.Color.Red;
    int red = (int)(color.R * 255);
    int green = (int)(color.G * 255);
    int blue = (int)(color.B * 255);
    int alpha = (int)(color.A * 255);
    string hex = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", red, green, blue, alpha);
    

    Then store this string to your database. When you want to consume it, use the code below:

    public static Color FromHex(string hex);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 7, 2019 7:55 AM
  • User89714 posted

    @Enrique4to said: I need to convert a color to int32 and store it in a SQL database, but the method ToArgb is not working...

    The answer from @LandLu is correct.

    However, the question is whether you should be storing color information in the database. In most situations the answer is no. For example:

    (1) if a particular color is used to represent a state (e.g. valid/invalid, love/like/dislike/hate etc), you should store the state in the database, not the color, and then have your ViewModel or a ValueConverter in your View, convert between the state and the color.

    (2) if you are storing colors configured by a particular user for their local device, you would normally store those colors in settings (e.g. using Xamarin.Essentials) rather than in a SQL database.

    Of course, there are situations where storing color information in a SQL database is appropriate, and for those situations the answer from @LandLu is appropriate.

    Tuesday, May 7, 2019 8:53 AM