using System.Windows; using System.Windows.Media; namespace ytLive.Helpers; /// Resolves a design token for code that cannot bind — view-model state colours (the /// top bar, the health banner, the meters, toast tints). The values live once, in /// Themes/Tokens.xaml; this reads them from the application resources. /// /// Why not a Brush field per colour: a second list in C# would drift from the theme. Why not /// OutputPalette: that one exists for the frame-pump thread, which must not touch resources; these /// properties are read by bindings on the UI thread. /// /// Without an Application (unit tests, design-time) a neutral grey comes back: visibly wrong /// rather than silently plausible, and never a crash. internal static class ThemeBrush { private static readonly SolidColorBrush Missing = Frozen(Color.FromRgb(0x80, 0x80, 0x80)); /// The brush for a token key, e.g. ThemeBrush.Get("Brush.Primary"). public static SolidColorBrush Get(string key) => Application.Current?.TryFindResource(key) as SolidColorBrush ?? Missing; /// The token's colour as a browser-style hex string, for APIs that take one /// (Notification.Wpf's NotificationColor.FromHex, for instance). public static string Hex(string key) { var c = Get(key).Color; return c.A == 255 ? $"#{c.R:x2}{c.G:x2}{c.B:x2}" : $"#{c.A:x2}{c.R:x2}{c.G:x2}{c.B:x2}"; } private static SolidColorBrush Frozen(Color colour) { var brush = new SolidColorBrush(colour); brush.Freeze(); return brush; } }