using System.Globalization; using Microsoft.Maui.Controls; using Microsoft.Maui.Graphics; namespace cfs_tracker.Converters; public class HexToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string hex && !string.IsNullOrWhiteSpace(hex)) { try { return Color.FromArgb(hex); } catch { } } return Color.FromArgb("#203041"); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value is Color color) return color.ToArgbHex(); return "#203041"; } } public static class ColorExtensions { public static string ToArgbHex(this Color color) { byte a = (byte)(color.Alpha * 255); byte r = (byte)(color.Red * 255); byte g = (byte)(color.Green * 255); byte b = (byte)(color.Blue * 255); if (a < 255) return $"#{a:X2}{r:X2}{g:X2}{b:X2}"; return $"#{r:X2}{g:X2}{b:X2}"; } }