32

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

Luis
  • 5,979
  • 2
  • 31
  • 51

4 Answers4

47

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);
Luis
  • 5,979
  • 2
  • 31
  • 51
42

Maybe this helps you, if you are using Xamarin.Forms:

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

...
Color.FromHex("#00FF00").ToUIColor();
alexcons
  • 651
  • 6
  • 5
  • 1
    This should be the accepted answer, no need to reinvent the wheel. – MonkeyCoder Jan 18 '17 at 10:32
  • Second that, this is the answer. Creating a class to do this is overkill. But then that was from 2012. – Adam Jun 29 '17 at 12:50
  • 7
    This is useful on xamarin.forms, but if you're working in xamarin.ios then pulling in the platform libraries from xamarin.forms is not something I'd do. – Steven Evers Dec 13 '17 at 17:54
33

Here one that lets you use a string like in css:

UIColor textColorNormal = UIColor.Clear.FromHexString("#f4f28d", 1.0f);

And here is the class:

using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Globalization;

namespace YourApp
{
    public static class UIColorExtensions
    {
        public static UIColor FromHexString (this UIColor color, string hexValue, float alpha = 1.0f)
        {
            var colorString = hexValue.Replace ("#", "");
            if (alpha > 1.0f) {
                alpha = 1.0f;
            } else if (alpha < 0.0f) {
                alpha = 0.0f;
            }

            float red, green, blue;

            switch (colorString.Length) 
            {
                case 3 : // #RGB
                {
                    red = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(0, 1)), 16) / 255f;
                    green = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(1, 1)), 16) / 255f;
                    blue = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(2, 1)), 16) / 255f;
                    return UIColor.FromRGBA(red, green, blue, alpha);
                }
                case 6 : // #RRGGBB
                {
                    red = Convert.ToInt32(colorString.Substring(0, 2), 16) / 255f;
                    green = Convert.ToInt32(colorString.Substring(2, 2), 16) / 255f;
                    blue = Convert.ToInt32(colorString.Substring(4, 2), 16) / 255f;
                    return UIColor.FromRGBA(red, green, blue, alpha);
                }   

                default :
                        throw new ArgumentOutOfRangeException(string.Format("Invalid color value {0} is invalid. It should be a hex value of the form #RBG, #RRGGBB", hexValue));

            }
        }
    }   
}
superlogical
  • 14,332
  • 9
  • 66
  • 76
  • 1
    Super useful! I'm communicating with a web service that stores everything exactly like css. Thank you for posting! – BRogers Feb 14 '13 at 19:25
  • 1
    alexcons answer: https://stackoverflow.com/a/41046485/2539616 ... below is the one... Color.FromHex("#00FF00").ToUIColor(); – Adam Jun 29 '17 at 12:51
4

As an option, for example

public static UIColor FromHEX(string hex)
    {
        int r = 0, g = 0, b = 0, a = 0;

        if (hex.Contains("#"))
            hex = hex.Replace("#", "");

        switch (hex.Length)
        {
            case 2:
                r = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
                g = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
                b = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
                a = 255;
                break;
            case 3:
                r = int.Parse(hex.Substring(0, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                g = int.Parse(hex.Substring(1, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                b = int.Parse(hex.Substring(2, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                a = 255;
                break;
            case 4:
                r = int.Parse(hex.Substring(0, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                g = int.Parse(hex.Substring(1, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                b = int.Parse(hex.Substring(2, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                a = int.Parse(hex.Substring(3, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                break;
            case 6:
                r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                a = 255;
                break;
            case 8:
                r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                a = int.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                break;
        }

        return UIColor.FromRGBA(r, g, b, a);
    }

KIzotov
  • 41
  • 1
  • 3
  • If you use HEX with an alpha channel, note that it is indicated at the end Or correct in your how the code works for you – KIzotov May 04 '18 at 09:28