|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using UnityEngine.UI; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class for individual taxonomy colors |
| 10 | + */ |
| 11 | +public class MarketingColor { |
| 12 | + public string category { get; set; } |
| 13 | + public string colorStr { get; set; } |
| 14 | + public Color color { get; set; } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +public class ColorManager : Singleton<ColorManager> { |
| 19 | + // singleton |
| 20 | + protected ColorManager () { } |
| 21 | + //public static new ColorManager Instance; |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + [Space (10)] |
| 29 | + [Header ("COLORS")] |
| 30 | + |
| 31 | + |
| 32 | + static string [] colors = { |
| 33 | + "5f1475", "5f1475", "f4e713", "8820aa", "42da82", "f413bc", "3f0236", "42da82", "31a8cb", "42da82", "f413bc", "ec391a", "6261a8", "ec391a", "4d7bbd", "4d7bbd", "6261a8", "5f1475", "31a8cb", "ec391a", "6939ac", "6261a8", "6939ac", "48daa3", "ec391a", "5f40bd", "f413bc", "5f40bd", "6939ac", "074381", "5f40bd", "6939ac", "6939ac", "074381", "5f40bd", "de2319", "058eb8", "5f1475", "6939ac", "5f1475", "074381", "de2319", "463260", "ae1ff1", "ae1ff1", "ae1ff1", "69004b", "5300e3", "129740", "f413bc", "ae1ff1", "de2319", "418fb0", "418fb0", "5f1475", "074381", "ae1ff1", "463260", "a43a9f", "0356d8", "ae1ff1", "f3af1f", "5f1475", "a6134c", "02b65c", "ef4138", "de2dca", "63fbf0", "63fbf0", "63fbf0", "5300e3", "5f1475", "8336bd", "3957c9", "3957c9", "a6134c", "a6134c", "a6134c", "a6134c", "a6134c", "ce218d", "074381", "5f1475", "ef4138", "ef4138", "ef4138", "90c053", "ef4138", "5fadd1", "f41182", "5f1475", "ef4138", "5f1475", "42da82", "5300e3", "5300e3", "5300e3", "5bd6fa", "1f4fbc", "5300e3", "60139b", "f413bc", "5f1475" |
| 34 | + }; |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + // dictionary of all marketing colors, with mids as keys |
| 39 | + private static Dictionary<int, MarketingColor> MarketingColorDict = new Dictionary<int, MarketingColor> () { |
| 40 | + |
| 41 | + { 453, new MarketingColor{ category = "Spiritual", colorStr = "0078C2"} }, |
| 42 | + { 596, new MarketingColor{ category = "Tech", colorStr = "00A7E5"} }, |
| 43 | + { 391, new MarketingColor{ category = "Personal$", colorStr = "078591"} }, |
| 44 | + { 274, new MarketingColor{ category = "Garden", colorStr = "0c9a47"} }, |
| 45 | + { 52, new MarketingColor{ category = "Business", colorStr = "0E6897"} }, |
| 46 | + { 338, new MarketingColor{ category = "Music", colorStr = "1DB954"} }, |
| 47 | + { 677, new MarketingColor{ category = "Camping", colorStr = "21753E"} }, |
| 48 | + { 464, new MarketingColor{ category = "Science", colorStr = "95a43f"} }, |
| 49 | + { 552, new MarketingColor{ category = "Style", colorStr = "242960"} }, |
| 50 | + { 483, new MarketingColor{ category = "Sports", colorStr = "253785"} }, |
| 51 | + { 239, new MarketingColor{ category = "Hobbies", colorStr = "5b11db"} }, |
| 52 | + { 1, new MarketingColor{ category = "Auto", colorStr = "74037f"} }, |
| 53 | + { 640, new MarketingColor{ category = "TV", colorStr = "7ba2a3"} }, |
| 54 | + { 473, new MarketingColor{ category = "Shopping", colorStr = "800245"} }, |
| 55 | + { 42, new MarketingColor{ category = "Books", colorStr = "86037e"} }, |
| 56 | + { 680, new MarketingColor{ category = "Gaming", colorStr = "96023b"} }, |
| 57 | + { 123, new MarketingColor{ category = "Careers", colorStr = "9b0465"} }, |
| 58 | + { 422, new MarketingColor{ category = "Pets", colorStr = "a74762"} }, |
| 59 | + { 132, new MarketingColor{ category = "Education", colorStr = "be0441"} }, |
| 60 | + { 653, new MarketingColor{ category = "Travel", colorStr = "d43422"} }, |
| 61 | + { 201, new MarketingColor{ category = "Fine Art", colorStr = "d90623"} }, |
| 62 | + { 324, new MarketingColor{ category = "Movies", colorStr = "ed2820"} }, |
| 63 | + { 150, new MarketingColor{ category = "Events", colorStr = "f74229"} }, |
| 64 | + { 379, new MarketingColor{ category = "News", colorStr = "f8061a"} }, |
| 65 | + { 186, new MarketingColor{ category = "Relations", colorStr = "f9672c"} }, |
| 66 | + { 441, new MarketingColor{ category = "Housing", colorStr = "febd34"} }, |
| 67 | + { 286, new MarketingColor{ category = "Health", colorStr = "f9d24c"} }, |
| 68 | + { 223, new MarketingColor{ category = "Living", colorStr = "fa9a6d"} }, |
| 69 | + { 210, new MarketingColor{ category = "Food", colorStr = "fc7185"} }, |
| 70 | + { 432, new MarketingColor{ category = "Culture", colorStr = "fc9731"} }, |
| 71 | + { 110, new MarketingColor{ category = "Energy", colorStr = "fd27b0"} } |
| 72 | + }; |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + private void Awake () |
| 77 | + { |
| 78 | + // convert strings to colors |
| 79 | + foreach (KeyValuePair<int, MarketingColor> m in MarketingColorDict) { |
| 80 | + MarketingColorDict [m.Key].color = GetColorFromString (m.Value.colorStr); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Return Color from array, if no index received then choose random |
| 90 | + */ |
| 91 | + public static Color GetColorFromArray (int index = -1) |
| 92 | + { |
| 93 | + // if no index received then get random |
| 94 | + if (index == -1) { |
| 95 | + // get random index from array |
| 96 | + index = (int)Random.Range (0, colors.Length - 1); |
| 97 | + } |
| 98 | + // parse string as Color |
| 99 | + return GetColorFromString (colors [index]); |
| 100 | + } |
| 101 | + /** |
| 102 | + * Return Color from dict, if no index received then choose random |
| 103 | + */ |
| 104 | + public static Color GetColorFromDict (int index = -1) |
| 105 | + { |
| 106 | + // if no index received then get random |
| 107 | + if (index == -1) { |
| 108 | + // create list of keys |
| 109 | + List<int> keyList = new List<int> (MarketingColorDict.Keys); |
| 110 | + // select a random key from your list |
| 111 | + index = keyList [(int)Random.Range (0, keyList.Count - 1)]; |
| 112 | + } |
| 113 | + // return the element |
| 114 | + return MarketingColorDict [index].color; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + /** |
| 119 | + * Return a Color object from a string |
| 120 | + * ^ Documentation https://docs.unity3d.com/ScriptReference/ColorUtility.TryParseHtmlString.html |
| 121 | + * ^ Strings that begin with '#' will be parsed as hexadecimal |
| 122 | + * ^ Strings that do not begin with '#' will be parsed as literal colors (red,green,...) |
| 123 | + */ |
| 124 | + public static Color GetColorFromString (string colorStr) |
| 125 | + { |
| 126 | + Color c; |
| 127 | + |
| 128 | + // if string contains integers then a hex |
| 129 | + if (Regex.IsMatch (colorStr, @"^(?:[0-9a-fA-F]{6})$")) { |
| 130 | + // if no hash then add one |
| 131 | + if (!Regex.IsMatch (colorStr, @"^#")) |
| 132 | + colorStr = "#" + colorStr; |
| 133 | + // if hex is 7 chars then add alpha |
| 134 | + if (colorStr.Length == 7) |
| 135 | + colorStr += "FF"; |
| 136 | + } |
| 137 | + //Debug.Log ("GetColorFromString() colorStr = " + colorStr); |
| 138 | + |
| 139 | + // if hex parses |
| 140 | + if (ColorUtility.TryParseHtmlString (colorStr, out c)) { |
| 141 | + // return color |
| 142 | + return c; |
| 143 | + } else { |
| 144 | + // else something bright and obviously wrong |
| 145 | + return Color.magenta; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +} |
0 commit comments