Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Written by Alexander Lyons 2013.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Monotouch.CoreGraphics;
- using Monotouch.UIKit;
- namespace YourNamespace.Graphics // We used the Graphics so that we could have 'using Lume.Graphics;' and have access to the color pallette
- {
- public enum ColorTheme
- {
- Light,
- Dark,
- Normal
- }
- public class MyColor
- {
- #region Variables
- protected static MyColor _me;
- protected static ColorTheme theme;
- protected static Dictionary<string, UIColor> normalColors;
- protected static Dictionary<string, UIColor> lightColors;
- protected static Dictionary<string, UIColor> darkColors;
- #endregion
- #region Properties
- public static ColorTheme Theme
- {
- get { return theme; }
- set
- {
- if (theme != value)
- {
- theme = value;
- SaveCurrentTheme();// We had logic here to save the theme choice to the database;
- }
- }
- }
- public static UIColor Background
- {
- get
- {
- switch(Theme)
- {
- case ColorTheme.Light: return lightColors["background"];
- case ColorTheme.Dark: return darkColors["background"];
- default: return normalColors["background"];
- }
- }
- }
- public static UIColor Text
- {
- get
- {
- switch(Theme)
- {
- case ColorTheme.Light: return lightColors["text"];
- case ColorTheme.Dark: return darkColors["text"];
- default: return normalColors["text"];
- }
- }
- }
- public static UIColor Confirm
- {
- get
- {
- switch(Theme)
- {
- case ColorTheme.Light: return lightColors["confirm"];
- case ColorTheme.Dark: return darkColors["confirm"];
- default: return normalColors["confirm"];
- }
- }
- }
- public static UIColor Decline
- {
- get
- {
- switch(Theme)
- {
- case ColorTheme.Light: return lightColors["decline"];
- case ColorTheme.Dark: return darkColors["decline"];
- default: return normalColors["decline"];
- }
- }
- }
- #endregion
- #region Constructors
- static MyColor()
- {
- _me = new MyColor();
- }
- protected MyColor()
- {
- var themeSetting = Database.UserSettings.GetUserSetting(typeof(ColorTheme).ToString());
- if (themeSettng == null)
- {
- theme = ColorTheme.Normal;
- }
- else
- {
- var success = Enum.TryParse<ColorTheme>(themeSetting.Value, out theme);
- if (!success)
- {
- theme = ColorTheme.Normal;
- }
- normalColors = new Dictionary<string, UIColor>();
- lightColors = new Dictionary<string, UIColor>();
- darkColors = new Dictionary<string, UIColor>();
- CreateColors();
- }
- }
- #endregion
- #region Methods
- static void CreateColors()
- {
- CreateNormalColors();
- CreateLightColors();
- CreateDarkColors();
- }
- static void CreateNormalColors()
- {
- normalColors.Add("background", ConvertHexToUIColor("#FAFAFA"));
- normalColors.Add("text", ConvertHexToUIColor("#2A2A2A"));
- normalColors.Add("confirm", ConvertHexToUIColor("#00EA1A"));
- normalColors.Add("decline", ConvertHexToUIColor("#E70000"));
- }
- static void CreateLightColors()
- {
- // Similar set up to normal colors.
- }
- static void CreateDarkColors()
- {
- // Similar set up to normal colors.
- }
- public static UIColor ConvertHexToUIColor(string hex)
- {
- if (hex.Contains('#')) hex = hex.Replace("#", "");
- float[] rgba = new float[] { 255.0f, 255.0f, 255.0f, 255.0f };
- if (hex.Length == 6)
- {
- rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
- rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
- rgba[2] = Convert.ToInt32(hex.Substring(4, 2), 16);
- rgba[3] = 255.0f;
- }
- else if (hex.Length == 8)
- {
- rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
- rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
- rgba[2] = Convert.ToInt32(hex.Substring(4, 2), 16);
- rgba[3] = Convert.ToInt32(hex.Substring(6, 2), 16);
- }
- else
- {
- throw new ArgumentException("Hash must be color code six or eight characters in length.");
- }
- return new UIColor(rgba[0] / 255.0f, rgba[1] / 255.0f, rgba[2] / 255.0f, rgba[3] / 255.0f);
- }
- static void SaveCurrentTheme()
- {
- Database.UserSettings.SaveUserSetting(new UserSetting(typeof(ColorTheme).ToString(), Theme.ToString()));
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement