Advertisement
BlazePhoenix

MyColorIOS.cs

Dec 31st, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. // Written by Alexander Lyons 2013.
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using Monotouch.CoreGraphics;
  8. using Monotouch.UIKit;
  9.  
  10. namespace YourNamespace.Graphics // We used the Graphics so that we could have 'using Lume.Graphics;' and have access to the color pallette
  11. {
  12.     public enum ColorTheme
  13.     {
  14.         Light,
  15.         Dark,
  16.         Normal
  17.     }
  18.    
  19.     public class MyColor
  20.     {
  21.         #region Variables
  22.         protected static MyColor _me;
  23.         protected static ColorTheme theme;
  24.         protected static Dictionary<string, UIColor> normalColors;
  25.         protected static Dictionary<string, UIColor> lightColors;
  26.         protected static Dictionary<string, UIColor> darkColors;
  27.         #endregion
  28.        
  29.         #region Properties
  30.         public static ColorTheme Theme
  31.         {
  32.             get { return theme; }
  33.             set
  34.             {
  35.                 if (theme != value)
  36.                 {
  37.                     theme = value;
  38.                     SaveCurrentTheme();// We had logic here to save the theme choice to the database;
  39.                 }
  40.             }
  41.         }
  42.         public static UIColor Background
  43.         {
  44.             get
  45.             {
  46.                 switch(Theme)
  47.                 {
  48.                     case ColorTheme.Light: return lightColors["background"];
  49.                     case ColorTheme.Dark: return darkColors["background"];
  50.                     default: return normalColors["background"];
  51.                 }
  52.             }
  53.         }
  54.         public static UIColor Text
  55.         {
  56.             get
  57.             {
  58.                 switch(Theme)
  59.                 {
  60.                     case ColorTheme.Light: return lightColors["text"];
  61.                     case ColorTheme.Dark: return darkColors["text"];
  62.                     default: return normalColors["text"];
  63.                 }
  64.             }
  65.         }
  66.         public static UIColor Confirm
  67.         {
  68.             get
  69.             {
  70.                 switch(Theme)
  71.                 {
  72.                     case ColorTheme.Light: return lightColors["confirm"];
  73.                     case ColorTheme.Dark: return darkColors["confirm"];
  74.                     default: return normalColors["confirm"];
  75.                 }
  76.             }
  77.         }
  78.         public static UIColor Decline
  79.         {
  80.             get
  81.             {
  82.                 switch(Theme)
  83.                 {
  84.                     case ColorTheme.Light: return lightColors["decline"];
  85.                     case ColorTheme.Dark: return darkColors["decline"];
  86.                     default: return normalColors["decline"];
  87.                 }
  88.             }
  89.         }
  90.         #endregion
  91.        
  92.         #region Constructors
  93.         static MyColor()
  94.         {
  95.             _me = new MyColor();
  96.         }
  97.         protected MyColor()
  98.         {
  99.             var themeSetting = Database.UserSettings.GetUserSetting(typeof(ColorTheme).ToString());
  100.             if (themeSettng == null)
  101.             {
  102.                 theme = ColorTheme.Normal;
  103.             }
  104.             else
  105.             {
  106.                 var success = Enum.TryParse<ColorTheme>(themeSetting.Value, out theme);
  107.                 if (!success)
  108.                 {
  109.                     theme = ColorTheme.Normal;
  110.                 }
  111.                 normalColors = new Dictionary<string, UIColor>();
  112.                 lightColors = new Dictionary<string, UIColor>();
  113.                 darkColors = new Dictionary<string, UIColor>();
  114.                 CreateColors();
  115.             }
  116.         }
  117.         #endregion
  118.        
  119.         #region Methods
  120.         static void CreateColors()
  121.         {
  122.             CreateNormalColors();
  123.             CreateLightColors();
  124.             CreateDarkColors();
  125.         }
  126.         static void CreateNormalColors()
  127.         {
  128.             normalColors.Add("background", ConvertHexToUIColor("#FAFAFA"));
  129.             normalColors.Add("text", ConvertHexToUIColor("#2A2A2A"));
  130.             normalColors.Add("confirm", ConvertHexToUIColor("#00EA1A"));
  131.             normalColors.Add("decline", ConvertHexToUIColor("#E70000"));
  132.         }
  133.         static void CreateLightColors()
  134.         {
  135.             // Similar set up to normal colors.
  136.         }
  137.         static void CreateDarkColors()
  138.         {
  139.             // Similar set up to normal colors.
  140.         }
  141.         public static UIColor ConvertHexToUIColor(string hex)
  142.         {
  143.             if (hex.Contains('#')) hex = hex.Replace("#", "");
  144.             float[] rgba = new float[] { 255.0f, 255.0f, 255.0f, 255.0f };
  145.  
  146.             if (hex.Length == 6)
  147.             {
  148.                 rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
  149.                 rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
  150.                 rgba[2] = Convert.ToInt32(hex.Substring(4, 2), 16);
  151.                 rgba[3] = 255.0f;
  152.             }
  153.             else if (hex.Length == 8)
  154.             {
  155.                 rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
  156.                 rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
  157.                 rgba[2] = Convert.ToInt32(hex.Substring(4, 2), 16);
  158.                 rgba[3] = Convert.ToInt32(hex.Substring(6, 2), 16);
  159.             }
  160.             else
  161.             {
  162.                 throw new ArgumentException("Hash must be color code six or eight characters in length.");
  163.             }
  164.  
  165.             return new UIColor(rgba[0] / 255.0f, rgba[1] / 255.0f, rgba[2] / 255.0f, rgba[3] / 255.0f);
  166.         }
  167.         static void SaveCurrentTheme()
  168.         {
  169.             Database.UserSettings.SaveUserSetting(new UserSetting(typeof(ColorTheme).ToString(), Theme.ToString()));
  170.         }
  171.         #endregion
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement