using System; using System.Globalization; using System.Windows.Data; using System.Windows.Media; namespace XP.Hardware.RaySource.Converters { /// /// 灯丝寿命百分比转颜色转换器 | Filament lifetime percentage to color converter /// 根据寿命使用百分比返回对应的颜色画刷: /// 低于 80% → 绿色 (#FF4CAF50) /// 80%-89% → 黄色 (#FFFFC107) /// 90% 及以上 → 红色 (#FFE53935) /// public class FilamentLifetimeColorConverter : IValueConverter { // 预定义颜色画刷(避免重复创建)| Pre-defined color brushes (avoid repeated creation) private static readonly SolidColorBrush GreenBrush = new(Color.FromArgb(0xFF, 0x4C, 0xAF, 0x50)); private static readonly SolidColorBrush YellowBrush = new(Color.FromArgb(0xFF, 0xFF, 0xC1, 0x07)); private static readonly SolidColorBrush RedBrush = new(Color.FromArgb(0xFF, 0xE5, 0x39, 0x35)); static FilamentLifetimeColorConverter() { // 冻结画刷以提升性能 | Freeze brushes for better performance GreenBrush.Freeze(); YellowBrush.Freeze(); RedBrush.Freeze(); } /// /// 将寿命百分比(0-100)转换为对应颜色画刷 | Convert lifetime percentage (0-100) to corresponding color brush /// /// 寿命百分比值(double,0-100)| Lifetime percentage value (double, 0-100) /// 目标类型 | Target type /// 转换参数(未使用)| Converter parameter (unused) /// 区域信息 | Culture info /// 对应颜色的 SolidColorBrush | SolidColorBrush of corresponding color public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is not double percentage) { return GreenBrush; } if (percentage >= 90) { return RedBrush; } if (percentage >= 80) { return YellowBrush; } return GreenBrush; } /// /// 反向转换(不支持,单向绑定)| Reverse conversion (not supported, one-way binding) /// public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }