UIColor from Hex in Monotouch

Luis picture Luis · Apr 25, 2012 · Viewed 17.4k times · Source

How to get a UIColor from an hex value in Monotouch?

Answer

Luis picture Luis · Apr 25, 2012

I found some solutions for Objective C and none specifically for Monotouch I ended up developing an extension method based on the most popular solution for IOS:

public static class UIColorExtensions
    {
        public static UIColor FromHex(this UIColor color,int hexValue)
        {
            return UIColor.FromRGB(
                (((float)((hexValue & 0xFF0000) >> 16))/255.0f),
                (((float)((hexValue & 0xFF00) >> 8))/255.0f),
                (((float)(hexValue & 0xFF))/255.0f)
            );
        }
    }

and use it like this:

new UIColor().FromHex(0x4F6176);

Update, it seems that as off Monotouch 5.4 UIColor does not have a parameterless constructor so use it like this:

 UIColor.Clear.FromHex(0xD12229);