XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Windows.Media;

namespace XFEToolBox.Client.ViewModel.Pages;

/// <summary>
/// 工具目录中的分类标题及其稳定配色。
/// </summary>
public sealed class ToolCategoryGroupViewModel
{
    private static readonly (Color Accent, Color Tint)[] Palettes =
    [
        (Color.FromRgb(123, 112, 222), Color.FromRgb(237, 235, 253)),
        (Color.FromRgb(67, 151, 213), Color.FromRgb(232, 245, 253)),
        (Color.FromRgb(65, 168, 137), Color.FromRgb(231, 248, 242)),
        (Color.FromRgb(224, 139, 75), Color.FromRgb(253, 241, 230)),
        (Color.FromRgb(205, 100, 151), Color.FromRgb(252, 235, 244))
    ];

    public ToolCategoryGroupViewModel(string name, int itemCount)
    {
        Name = string.IsNullOrWhiteSpace(name) ? "其他工具" : name.Trim();
        CountText = $"{itemCount} 个";
        var palette = Palettes[GetStablePaletteIndex(Name)];
        AccentBrush = CreateBrush(palette.Accent);
        TintBrush = CreateBrush(palette.Tint);
    }

    public string Name { get; }

    public Brush AccentBrush { get; }

    public Brush TintBrush { get; }

    public string CountText { get; }

    private static int GetStablePaletteIndex(string value)
    {
        uint hash = 2166136261;
        foreach (var character in value)
            hash = (hash ^ character) * 16777619;
        return (int)(hash % Palettes.Length);
    }

    private static SolidColorBrush CreateBrush(Color color)
    {
        var brush = new SolidColorBrush(color);
        brush.Freeze();
        return brush;
    }
}