mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-05-25 10:05:49 +08:00
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: 辉鸭蛋 <huiyadanli@gmail.com>
65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
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;
|
||
}
|
||
}
|