Files
better-genshin-impact/BetterGenshinImpact/View/Converters/StringToColorConverter.cs
躁动的氨气 292dc8fb4f feat: 显示后台角色技能cd (#2754)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: 辉鸭蛋 <huiyadanli@gmail.com>
2026-02-13 23:19:32 +08:00

65 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace BetterGenshinImpact.View.Converters;
/// <summary>
/// 将颜色字符串(#RRGGBB 或 #RRGGBBAA转换为Color对象
/// </summary>
public class StringToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string colorStr)
{
return ParseColor(colorStr) ?? Colors.White;
}
return Colors.White;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
private static Color? ParseColor(string colorStr)
{
if (string.IsNullOrWhiteSpace(colorStr))
{
return null;
}
try
{
string hex = colorStr.Trim().TrimStart('#');
if (hex.Length == 6)
{
// RGB格式
byte r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
return Color.FromArgb(255, r, g, b);
}
else if (hex.Length == 8)
{
// RGBA格式
byte r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
byte a = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
}
catch (Exception)
{
// 解析失败返回null
}
return null;
}
}