2025-08-20 12:10:13 +08:00
|
|
|
|
using System.Globalization;
|
2025-08-12 23:08:54 +08:00
|
|
|
|
using System.Windows.Data;
|
2026-01-02 17:30:41 +08:00
|
|
|
|
using Melskin.Controls;
|
2025-08-12 23:08:54 +08:00
|
|
|
|
|
2026-01-02 17:30:41 +08:00
|
|
|
|
namespace Melskin.Converters.Internal;
|
2025-08-12 23:08:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 表示一个值转换器,用于将色调(Hue)值转换为对应的RGB颜色。该转换器实现了IValueConverter接口,
|
|
|
|
|
|
/// 通过Convert方法接收一个表示色调的int类型的值,并将其转换成System.Windows.Media.Color结构体中的颜色。
|
|
|
|
|
|
/// 转换过程中,饱和度和亮度均设为最大值(1.0),透明度设为完全不透明(255)。如果输入不是有效的int类型或者转换失败,则返回透明色。
|
|
|
|
|
|
/// ConvertBack方法目前未实现,尝试调用它会抛出NotImplementedException异常。
|
|
|
|
|
|
/// 此转换器适用于需要根据给定的色调快速生成对应RGB颜色的应用场景。
|
|
|
|
|
|
/// </summary>
|
2025-08-20 12:10:35 +08:00
|
|
|
|
internal class HueToColorConverter : IValueConverter
|
2025-08-12 23:08:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
public object Convert(object? value,
|
2025-09-04 22:39:00 +08:00
|
|
|
|
Type t,
|
2025-08-12 23:08:54 +08:00
|
|
|
|
object? p,
|
|
|
|
|
|
CultureInfo c) =>
|
|
|
|
|
|
value is int hue
|
|
|
|
|
|
? ColorPanel.HsvToRgb(hue,
|
|
|
|
|
|
1.0,
|
|
|
|
|
|
1.0,
|
|
|
|
|
|
255)
|
|
|
|
|
|
: Colors.Transparent;
|
|
|
|
|
|
|
2025-09-04 22:39:00 +08:00
|
|
|
|
public object ConvertBack(object? value, Type t, object? p, CultureInfo c)
|
2025-08-12 23:08:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (value is Color color)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 将RGB颜色转换为HSV颜色
|
|
|
|
|
|
var r = color.R / 255.0;
|
|
|
|
|
|
var g = color.G / 255.0;
|
|
|
|
|
|
var b = color.B / 255.0;
|
|
|
|
|
|
|
|
|
|
|
|
var max = Math.Max(r, Math.Max(g, b));
|
|
|
|
|
|
var min = Math.Min(r, Math.Min(g, b));
|
|
|
|
|
|
var delta = max - min;
|
|
|
|
|
|
|
|
|
|
|
|
double h = 0, s = 0, v = max;
|
|
|
|
|
|
|
|
|
|
|
|
if (delta != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (r == max)
|
|
|
|
|
|
{
|
|
|
|
|
|
h = (g - b) / delta;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (g == max)
|
|
|
|
|
|
{
|
|
|
|
|
|
h = 2 + (b - r) / delta;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (b == max)
|
|
|
|
|
|
{
|
|
|
|
|
|
h = 4 + (r - g) / delta;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
h *= 60;
|
|
|
|
|
|
if (h < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
h += 360;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s = delta / max;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (int)Math.Round(h); // 返回整数色调值
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Invalid input type for conversion.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|