using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Text; using System.Windows.Forms; namespace UltraParadise { internal static class Theme { public static Color Back, Side, Panel, Hover, Line, Edge, Text, Dim, Orange, On, Bad, Warn; public static Color Bright, Soft, Faint, Thumb, Scroll, Sunk, Grid, OnFill, OnEdge, AccentHover, OnAccent; public static bool Light; public static float Scale = 1f, UiScale = 1f; public static string FontName; public static Image BgImage; public static string BgFit = "cover", OverlayCorner = "top-left"; public static int BgOpacity = 30, OverlayOpacity = 93, OverlaySize = 100; public static bool BannerArt = true; public static event Action Changed; public static readonly Dictionary Values = new Dictionary(StringComparer.OrdinalIgnoreCase); public static readonly string[] ColorKeys = { "accent", "background", "sidebar", "panel", "line", "text", "dim", "good", "warn", "bad" }; public static readonly string[] ColorNames = { "Accent", "Background", "Sidebar and header", "Panels and boxes", "Lines and borders", "Text", "Dim text", "Good / On", "Warning", "Bad / Error" }; public static readonly string[] PresetIds = { "dark", "light", "midnight", "contrast", "paradise", "system" }; public static readonly string[] PresetNames = { "Dark", "Light", "Midnight", "High Contrast", "Paradise", "Follow Windows" }; public static readonly string[] Fits = { "cover", "fit", "stretch", "center", "tile" }; public static readonly string[] Corners = { "top-left", "top-right", "bottom-left", "bottom-right" }; private static readonly string[] OtherKeys = { "base", "name", "font", "scale", "image", "image.opacity", "image.fit", "banner", "overlay.corner", "overlay.opacity" }; private static Bitmap cache; private static Size cacheSize; static Theme() { Apply(); } public static string Folder { get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "UltraParadise"); } } public static string FilePath { get { return Path.Combine(Folder, "theme.ini"); } } private static Dictionary Preset(string id) { string[] v; switch (id) { case "light": v = new[] { "e8650a", "f3f3f1", "e6e6e3", "ffffff", "d4d4cf", "1e1e20", "6b6b70", "2f9e44", "b86e00", "d63b2f" }; break; case "midnight": v = new[] { "ff7a18", "000000", "000000", "0c0c0e", "1e1e23", "e6e6ea", "6c6c75", "84d65c", "ffb03c", "e85448" }; break; case "contrast": v = new[] { "ffd400", "000000", "000000", "000000", "ffffff", "ffffff", "d0d0d0", "00ff66", "ffd400", "ff4040" }; break; case "paradise": v = new[] { "ff5fa2", "1a1320", "140f19", "241a2c", "3a2c46", "f0e6f5", "9a88a8", "7fe0a0", "ffc060", "ff5a6a" }; break; default: v = new[] { "ff7a18", "141415", "0f0f10", "1c1c1e", "2e2e32", "e2e2de", "7a7a7e", "84d65c", "ffb03c", "e85448" }; break; } Dictionary d = new Dictionary(); for (int i = 0; i < ColorKeys.Length; i++) d[ColorKeys[i]] = "#" + v[i]; return d; } public static bool SystemLight() { try { using (Microsoft.Win32.RegistryKey k = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize")) { object o = k != null ? k.GetValue("AppsUseLightTheme") : null; return o is int && (int)o == 1; } } catch (Exception) { return false; } } public static string Base { get { string b; if (!Values.TryGetValue("base", out b)) return "dark"; b = b.Trim().ToLowerInvariant(); return Array.IndexOf(PresetIds, b) >= 0 ? b : "dark"; } } private static string Resolved { get { string b = Base; return b == "system" ? (SystemLight() ? "light" : "dark") : b; } } public static string PresetName { get { int i = Array.IndexOf(PresetIds, Base); bool custom = false; foreach (string k in ColorKeys) if (Values.ContainsKey(k)) custom = true; return (i >= 0 ? PresetNames[i] : "Dark") + (custom ? " (customised)" : ""); } } public static bool TryHex(string s, out Color c) { c = Color.Empty; if (s == null) return false; s = s.Trim().TrimStart('#'); if (s.Length == 3) s = new string(new[] { s[0], s[0], s[1], s[1], s[2], s[2] }); int v; if (s.Length != 6 || !int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out v)) return false; c = Color.FromArgb((v >> 16) & 255, (v >> 8) & 255, v & 255); return true; } public static string Hex(Color c) { return "#" + c.R.ToString("x2") + c.G.ToString("x2") + c.B.ToString("x2"); } public static Color ColorOf(string key) { string v; Color c; if (Values.TryGetValue(key, out v) && TryHex(v, out c)) return c; TryHex(Preset(Resolved)[key], out c); return c; } public static Color Mix(Color a, Color b, double t) { return Color.FromArgb((int)(a.R + (b.R - a.R) * t), (int)(a.G + (b.G - a.G) * t), (int)(a.B + (b.B - a.B) * t)); } public static double Lum(Color c) { return (0.2126 * c.R + 0.7152 * c.G + 0.0722 * c.B) / 255.0; } private static int Int(string k, int def, int min, int max) { string v; int i; if (!Values.TryGetValue(k, out v) || !int.TryParse(v.Trim().TrimEnd('%'), NumberStyles.Integer, CultureInfo.InvariantCulture, out i)) return def; return Math.Max(min, Math.Min(max, i)); } private static string Pick(string k, string[] allowed, string def) { string v; if (!Values.TryGetValue(k, out v)) return def; v = v.Trim().ToLowerInvariant(); return Array.IndexOf(allowed, v) >= 0 ? v : def; } public static void Apply() { Orange = ColorOf("accent"); Back = ColorOf("background"); Side = ColorOf("sidebar"); Panel = ColorOf("panel"); Line = ColorOf("line"); Text = ColorOf("text"); Dim = ColorOf("dim"); On = ColorOf("good"); Warn = ColorOf("warn"); Bad = ColorOf("bad"); Light = Lum(Back) > 0.5; Hover = Mix(Panel, Text, Light ? 0.07 : 0.05); Edge = Mix(Line, Text, 0.18); Bright = Light ? Mix(Text, Color.Black, 0.5) : Mix(Text, Color.White, 0.6); Soft = Mix(Text, Back, 0.22); Faint = Mix(Dim, Back, 0.35); Thumb = Mix(Panel, Text, 0.05); Scroll = Mix(Back, Text, 0.22); Sunk = Mix(Back, Panel, 0.45); Grid = Mix(Back, Line, 0.5); OnFill = Mix(Panel, On, 0.13); OnEdge = Mix(Panel, On, 0.4); AccentHover = Mix(Orange, Light ? Color.Black : Color.White, 0.15); OnAccent = Lum(Orange) > 0.45 ? Color.FromArgb(20, 20, 21) : Color.White; string f; FontName = Values.TryGetValue("font", out f) && f.Trim().Length > 0 ? f.Trim() : null; if (FontName != null) { try { using (FontFamily ff = new FontFamily(FontName)) { } } catch (Exception) { FontName = null; } } UiScale = Int("scale", 100, 80, 160) / 100f; BgOpacity = Int("image.opacity", 30, 0, 100); BgFit = Pick("image.fit", Fits, "cover"); BannerArt = Pick("banner", new[] { "art", "plain" }, "art") == "art"; OverlayCorner = Pick("overlay.corner", Corners, "top-left"); OverlayOpacity = Int("overlay.opacity", 93, 30, 100); OverlaySize = Int("overlay.size", 100, 60, 160); string img; Image old = BgImage; BgImage = Values.TryGetValue("image", out img) ? LoadImage(img.Trim()) : null; if (old != null && old != BgImage) old.Dispose(); if (cache != null) { cache.Dispose(); cache = null; } } private static Image LoadImage(string path) { try { if (path.Length == 0 || !File.Exists(path) || new FileInfo(path).Length > 40L * 1024 * 1024) return null; byte[] b = File.ReadAllBytes(path); using (MemoryStream ms = new MemoryStream(b)) using (Image im = Image.FromStream(ms)) return new Bitmap(im); } catch (Exception) { return null; } } public static void Load() { Values.Clear(); try { if (File.Exists(FilePath)) Parse(File.ReadAllLines(FilePath), Values); } catch (Exception) { } Apply(); } public static void Parse(string[] lines, Dictionary into) { foreach (string raw in lines) { string l = raw; int hash = l.IndexOf(" #", StringComparison.Ordinal); if (hash >= 0) l = l.Substring(0, hash); l = l.Trim(); if (l.Length == 0 || l[0] == '#' || l[0] == ';' || l[0] == '[') continue; int eq = l.IndexOf('='); if (eq <= 0) continue; string k = l.Substring(0, eq).Trim().ToLowerInvariant(), v = l.Substring(eq + 1).Trim(); if (Array.IndexOf(ColorKeys, k) < 0 && Array.IndexOf(OtherKeys, k) < 0) continue; if (v.Length > 400) continue; into[k] = v; } } public static string Serialize() { StringBuilder sb = new StringBuilder(); sb.AppendLine("# Ultra Paradise CU:Trainer theme"); sb.AppendLine("# Edit this file and click Reload Theme File in the Appearance tab. The same file works in the Windows, Linux and Mac trainers."); sb.AppendLine("#"); sb.AppendLine("# base dark | light | midnight | contrast | paradise | system (the preset everything starts from)"); sb.AppendLine("# accent, background, sidebar, panel, line, text, dim, good, warn, bad colours as #rrggbb (leave one out to use the preset's)"); sb.AppendLine("# font any installed font name scale 80 to 160 (percent, needs a restart)"); sb.AppendLine("# image path to a background picture image.opacity 0 to 100 image.fit cover | fit | stretch | center | tile"); sb.AppendLine("# banner art | plain overlay.corner top-left | top-right | bottom-left | bottom-right overlay.opacity 30 to 100"); sb.AppendLine(); sb.AppendLine("base = " + Base); string n; if (Values.TryGetValue("name", out n)) sb.AppendLine("name = " + n); foreach (string k in ColorKeys) { string v; if (Values.TryGetValue(k, out v)) sb.AppendLine(k + " = " + v); } foreach (string k in OtherKeys) { string v; if (k != "base" && k != "name" && Values.TryGetValue(k, out v)) sb.AppendLine(k + " = " + v); } return sb.ToString(); } public static void Save() { try { Directory.CreateDirectory(Folder); File.WriteAllText(FilePath, Serialize(), new UTF8Encoding(false)); } catch (Exception) { } } public static void Set(string k, string v) { Color[] before = Tokens(); if (v == null) Values.Remove(k); else Values[k] = v; Save(); Apply(); Fire(before); } public static void Fire(Color[] before) { if (Changed != null) Changed(before); } public static void ResetAll() { Color[] before = Tokens(); Values.Clear(); Save(); Apply(); Fire(before); } public static void UsePreset(string id) { Color[] before = Tokens(); foreach (string k in ColorKeys) Values.Remove(k); Values["base"] = id; Save(); Apply(); Fire(before); } public static void Reload() { Color[] before = Tokens(); Load(); Fire(before); } public static string Import(string path) { try { Dictionary d = new Dictionary(StringComparer.OrdinalIgnoreCase); if (new FileInfo(path).Length > 64 * 1024) return "That file is too big to be a theme."; Parse(File.ReadAllLines(path), d); if (d.Count == 0) return "That file has no theme settings in it."; Color[] before = Tokens(); Values.Clear(); foreach (KeyValuePair kv in d) Values[kv.Key] = kv.Value; Save(); Apply(); Fire(before); return null; } catch (Exception e) { return "Couldn't read that file: " + e.Message; } } public static string Export(string path) { try { File.WriteAllText(path, Serialize(), new UTF8Encoding(false)); return null; } catch (Exception e) { return "Couldn't save it: " + e.Message; } } public static Color[] Tokens() { return new[] { Back, Side, Panel, Hover, Line, Edge, Text, Dim, Orange, On, Bad, Warn, Bright, Soft, Faint, Thumb, Scroll, Sunk, Grid, OnFill, OnEdge, AccentHover, OnAccent }; } public static void Recolor(Control c, Color[] before, Color[] after) { c.BackColor = Map(c.BackColor, before, after); c.ForeColor = Map(c.ForeColor, before, after); Button b = c as Button; if (b != null) { b.FlatAppearance.MouseOverBackColor = Map(b.FlatAppearance.MouseOverBackColor, before, after); b.FlatAppearance.MouseDownBackColor = Map(b.FlatAppearance.MouseDownBackColor, before, after); if (!b.FlatAppearance.BorderColor.IsEmpty) b.FlatAppearance.BorderColor = Map(b.FlatAppearance.BorderColor, before, after); } foreach (Control k in c.Controls) Recolor(k, before, after); c.Invalidate(); } private static Color Map(Color c, Color[] before, Color[] after) { if (c.IsEmpty || c.A == 0) return c; for (int i = 0; i < before.Length; i++) if (before[i].ToArgb() == c.ToArgb()) return after[i]; return c; } public static Font F(string family, float size) { return F(family, size, FontStyle.Regular); } public static Font F(string family, float size, FontStyle style) { float s = size * UiScale; if (FontName != null && family != "Consolas") { FontStyle st = style; if (family.EndsWith("Semibold", StringComparison.Ordinal) || family.EndsWith("Black", StringComparison.Ordinal)) st |= FontStyle.Bold; try { return new Font(FontName, s, st); } catch (Exception) { } } return new Font(family, s, style); } public static void PaintBack(Graphics g, Control c) { g.Clear(Back); if (BgImage == null || BgOpacity <= 0) return; Form f = c.FindForm(); if (f == null || f.ClientSize.Width <= 0 || f.ClientSize.Height <= 0) return; if (cache == null || cacheSize != f.ClientSize) Build(f.ClientSize); if (cache == null) return; Point off = f.PointToClient(c.PointToScreen(Point.Empty)); g.DrawImage(cache, new Rectangle(0, 0, c.Width, c.Height), new Rectangle(off.X, off.Y, c.Width, c.Height), GraphicsUnit.Pixel); } private static void Build(Size area) { if (cache != null) { cache.Dispose(); cache = null; } try { Bitmap b = new Bitmap(area.Width, area.Height); using (Graphics g = Graphics.FromImage(b)) using (ImageAttributes ia = new ImageAttributes()) { g.Clear(Back); g.InterpolationMode = InterpolationMode.HighQualityBicubic; ColorMatrix cm = new ColorMatrix(); cm.Matrix33 = BgOpacity / 100f; ia.SetColorMatrix(cm); int iw = BgImage.Width, ih = BgImage.Height; if (BgFit == "tile") { for (int y = 0; y < area.Height; y += ih) for (int x = 0; x < area.Width; x += iw) g.DrawImage(BgImage, new Rectangle(x, y, iw, ih), 0, 0, iw, ih, GraphicsUnit.Pixel, ia); } else { Rectangle dest; if (BgFit == "stretch") dest = new Rectangle(Point.Empty, area); else if (BgFit == "center") dest = new Rectangle((area.Width - iw) / 2, (area.Height - ih) / 2, iw, ih); else { float s = BgFit == "fit" ? Math.Min(area.Width / (float)iw, area.Height / (float)ih) : Math.Max(area.Width / (float)iw, area.Height / (float)ih); int w = (int)(iw * s), h = (int)(ih * s); dest = new Rectangle((area.Width - w) / 2, (area.Height - h) / 2, w, h); } g.DrawImage(BgImage, dest, 0, 0, iw, ih, GraphicsUnit.Pixel, ia); } } cache = b; cacheSize = area; } catch (Exception) { cache = null; } } public static GraphicsPath Round(Rectangle r, int rad) { GraphicsPath p = new GraphicsPath(); int d = Math.Max(1, Math.Min(rad * 2, Math.Min(r.Width, r.Height))); p.AddArc(r.X, r.Y, d, d, 180, 90); p.AddArc(r.Right - d, r.Y, d, d, 270, 90); p.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90); p.AddArc(r.X, r.Bottom - d, d, d, 90, 90); p.CloseFigure(); return p; } public static void FillRound(Graphics g, Color c, Rectangle r, int rad) { SmoothingMode m = g.SmoothingMode; g.SmoothingMode = SmoothingMode.AntiAlias; using (GraphicsPath p = Round(r, rad)) using (SolidBrush b = new SolidBrush(c)) g.FillPath(b, p); g.SmoothingMode = m; } public static void DrawRound(Graphics g, Color c, Rectangle r, int rad) { SmoothingMode m = g.SmoothingMode; g.SmoothingMode = SmoothingMode.AntiAlias; using (GraphicsPath p = Round(r, rad)) using (Pen pen = new Pen(c)) g.DrawPath(pen, p); g.SmoothingMode = m; } public static string Spaced(string s) { StringBuilder sb = new StringBuilder(); foreach (char c in s) { if (sb.Length > 0) sb.Append(' '); sb.Append(c); } return sb.ToString(); } public static int S(float v) { return (int)Math.Round(v * Scale); } } }